Browse code

Merge "refactor ping_check"

Jenkins authored on 2015/04/20 22:39:33
Showing 8 changed files
... ...
@@ -182,7 +182,7 @@ IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
182 182
 die_if_not_set $LINENO IP "Failure retrieving IP address"
183 183
 
184 184
 # Private IPs can be pinged in single node deployments
185
-ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
185
+ping_check $IP $BOOT_TIMEOUT "$PRIVATE_NETWORK_NAME"
186 186
 
187 187
 # Clean up
188 188
 # --------
... ...
@@ -142,7 +142,7 @@ else
142 142
         die $LINENO "Failure authorizing rule in $SECGROUP"
143 143
 
144 144
     # Test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
145
-    ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
145
+    ping_check $FLOATING_IP $ASSOCIATE_TIMEOUT "$PUBLIC_NETWORK_NAME"
146 146
 
147 147
     # Revoke pinging
148 148
     euca-revoke -P icmp -s 0.0.0.0/0 -t -1:-1 $SECGROUP || \
... ...
@@ -139,7 +139,7 @@ IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
139 139
 die_if_not_set $LINENO IP "Failure retrieving IP address"
140 140
 
141 141
 # Private IPs can be pinged in single node deployments
142
-ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
142
+ping_check $IP $BOOT_TIMEOUT "$PRIVATE_NETWORK_NAME"
143 143
 
144 144
 # Floating IPs
145 145
 # ------------
... ...
@@ -158,7 +158,7 @@ nova add-floating-ip $VM_UUID $FLOATING_IP || \
158 158
     die $LINENO "Failure adding floating IP $FLOATING_IP to $VM_NAME"
159 159
 
160 160
 # Test we can ping our floating IP within ASSOCIATE_TIMEOUT seconds
161
-ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
161
+ping_check $FLOATING_IP $ASSOCIATE_TIMEOUT "$PUBLIC_NETWORK_NAME"
162 162
 
163 163
 if ! is_service_enabled neutron; then
164 164
     # Allocate an IP from second floating pool
... ...
@@ -182,7 +182,7 @@ fi
182 182
 # FIXME (anthony): make xs support security groups
183 183
 if [ "$VIRT_DRIVER" != "ironic" -a "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then
184 184
     # Test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
185
-    ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT Fail
185
+    ping_check $FLOATING_IP $ASSOCIATE_TIMEOUT "$PUBLIC_NETWORK_NAME" Fail
186 186
 fi
187 187
 
188 188
 # Clean up
... ...
@@ -281,7 +281,7 @@ function ping_ip {
281 281
     local VM_NAME=$1
282 282
     local NET_NAME=$2
283 283
     IP=$(get_instance_ip $VM_NAME $NET_NAME)
284
-    ping_check $NET_NAME $IP $BOOT_TIMEOUT
284
+    ping_check $IP $BOOT_TIMEOUT $NET_NAME
285 285
 }
286 286
 
287 287
 function check_vm {
... ...
@@ -143,7 +143,7 @@ IP=$(get_instance_ip $VM_UUID $PRIVATE_NETWORK_NAME)
143 143
 die_if_not_set $LINENO IP "Failure retrieving IP address"
144 144
 
145 145
 # Private IPs can be pinged in single node deployments
146
-ping_check "$PRIVATE_NETWORK_NAME" $IP $BOOT_TIMEOUT
146
+ping_check $IP $BOOT_TIMEOUT "$PRIVATE_NETWORK_NAME"
147 147
 
148 148
 # Volumes
149 149
 # -------
... ...
@@ -344,39 +344,42 @@ function wait_for_service {
344 344
 
345 345
 
346 346
 # ping check
347
-# Uses globals ``ENABLED_SERVICES``
348
-# ping_check from-net ip boot-timeout expected
347
+# Uses globals ``ENABLED_SERVICES``, ``TOP_DIR``, ``MULTI_HOST``, ``PRIVATE_NETWORK``
348
+# ping_check <ip> [boot-timeout] [from_net] [expected]
349 349
 function ping_check {
350
-    if is_service_enabled neutron; then
351
-        _ping_check_neutron  "$1" $2 $3 $4
352
-        return
350
+    local ip=$1
351
+    local timeout=${2:-30}
352
+    local from_net=${3:-""}
353
+    local expected=${4:-True}
354
+    local op="!"
355
+    local failmsg="[Fail] Couldn't ping server"
356
+    local ping_cmd="ping"
357
+
358
+    # if we don't specify a from_net we're expecting things to work
359
+    # fine from our local box.
360
+    if [[ -n "$from_net" ]]; then
361
+        if is_service_enabled neutron; then
362
+            ping_cmd="$TOP_DIR/tools/ping_neutron.sh $from_net"
363
+        elif [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
364
+            # there is no way to address the multihost / private case, bail here for compatibility.
365
+            # TODO: remove this cruft and redo code to handle this at the caller level.
366
+            return
367
+        fi
353 368
     fi
354
-    _ping_check_novanet "$1" $2 $3 $4
355
-}
356 369
 
357
-# ping check for nova
358
-# Uses globals ``MULTI_HOST``, ``PRIVATE_NETWORK``
359
-function _ping_check_novanet {
360
-    local from_net=$1
361
-    local ip=$2
362
-    local boot_timeout=$3
363
-    local expected=${4:-"True"}
364
-    local check_command=""
365
-    MULTI_HOST=$(trueorfalse False MULTI_HOST)
366
-    if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
367
-        return
370
+    # inverse the logic if we're testing no connectivity
371
+    if [[ "$expected" != "True" ]]; then
372
+        op=""
373
+        failmsg="[Fail] Could ping server"
368 374
     fi
369
-    if [[ "$expected" = "True" ]]; then
370
-        check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
371
-    else
372
-        check_command="while ping -c1 -w1 $ip; do sleep 1; done"
373
-    fi
374
-    if ! timeout $boot_timeout sh -c "$check_command"; then
375
-        if [[ "$expected" = "True" ]]; then
376
-            die $LINENO "[Fail] Couldn't ping server"
377
-        else
378
-            die $LINENO "[Fail] Could ping server"
379
-        fi
375
+
376
+    # Because we've transformed this command so many times, print it
377
+    # out at the end.
378
+    local check_command="while $op $ping_cmd -c1 -w1 $ip; do sleep 1; done"
379
+    echo "Checking connectivity with $check_command"
380
+
381
+    if ! timeout $timeout sh -c "$check_command"; then
382
+        die $LINENO $failmsg
380 383
     fi
381 384
 }
382 385
 
... ...
@@ -1370,27 +1370,6 @@ function _get_probe_cmd_prefix {
1370 1370
     echo "$Q_RR_COMMAND ip netns exec qprobe-$probe_id"
1371 1371
 }
1372 1372
 
1373
-function _ping_check_neutron {
1374
-    local from_net=$1
1375
-    local ip=$2
1376
-    local timeout_sec=$3
1377
-    local expected=${4:-"True"}
1378
-    local check_command=""
1379
-    probe_cmd=`_get_probe_cmd_prefix $from_net`
1380
-    if [[ "$expected" = "True" ]]; then
1381
-        check_command="while ! $probe_cmd ping -w 1 -c 1 $ip; do sleep 1; done"
1382
-    else
1383
-        check_command="while $probe_cmd ping -w 1 -c 1 $ip; do sleep 1; done"
1384
-    fi
1385
-    if ! timeout $timeout_sec sh -c "$check_command"; then
1386
-        if [[ "$expected" = "True" ]]; then
1387
-            die $LINENO "[Fail] Couldn't ping server"
1388
-        else
1389
-            die $LINENO "[Fail] Could ping server"
1390
-        fi
1391
-    fi
1392
-}
1393
-
1394 1373
 # ssh check
1395 1374
 function _ssh_check_neutron {
1396 1375
     local from_net=$1
1397 1376
new file mode 100755
... ...
@@ -0,0 +1,65 @@
0
+#!/bin/bash
1
+#
2
+# Copyright 2015 Hewlett-Packard Development Company, L.P.
3
+#
4
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
+# not use this file except in compliance with the License. You may obtain
6
+# a copy of the License at
7
+#
8
+#    http://www.apache.org/licenses/LICENSE-2.0
9
+#
10
+# Unless required by applicable law or agreed to in writing, software
11
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+# License for the specific language governing permissions and limitations
14
+# under the License.
15
+
16
+# Ping a neutron guest using a network namespace probe
17
+
18
+set -o errexit
19
+set -o pipefail
20
+
21
+TOP_DIR=$(cd $(dirname "$0")/.. && pwd)
22
+
23
+# This *must* be run as the admin tenant
24
+source $TOP_DIR/openrc admin admin
25
+
26
+function usage {
27
+    cat - <<EOF
28
+ping_neutron.sh <net_name> [ping args]
29
+
30
+This provides a wrapper to ping neutron guests that are on isolated
31
+tenant networks that the caller can't normally reach. It does so by
32
+creating a network namespace probe.
33
+
34
+It takes arguments like ping, except the first arg must be the network
35
+name.
36
+
37
+Note: in environments with duplicate network names, the results are
38
+non deterministic.
39
+
40
+This should *really* be in the neutron cli.
41
+
42
+EOF
43
+    exit 1
44
+}
45
+
46
+NET_NAME=$1
47
+
48
+if [[ -z "$NET_NAME" ]]; then
49
+    echo "Error: net_name is required"
50
+    usage
51
+fi
52
+
53
+REMANING_ARGS="${@:2}"
54
+
55
+# BUG: with duplicate network names, this fails pretty hard.
56
+NET_ID=$(neutron net-list $NET_NAME | grep "$NET_NAME" | awk '{print $2}')
57
+PROBE_ID=$(neutron-debug probe-list -c id -c network_id | grep "$NET_ID" | awk '{print $2}' | head -n 1)
58
+
59
+# This runs a command inside the specific netns
60
+NET_NS_CMD="ip netns exec qprobe-$PROBE_ID"
61
+
62
+PING_CMD="sudo $NET_NS_CMD ping $REMAING_ARGS"
63
+echo "Running $PING_CMD"
64
+$PING_CMD