Use set -e for validation-scripts/all-nodes.sh
[apex-tripleo-heat-templates.git] / validation-scripts / all-nodes.sh
1 #!/bin/bash
2 set -e
3
4 # For each unique remote IP (specified via Heat) we check to
5 # see if one of the locally configured networks matches and if so we
6 # attempt a ping test the remote network IP.
7 function ping_controller_ips() {
8   local REMOTE_IPS=$1
9   for REMOTE_IP in $(echo $REMOTE_IPS | sed -e "s| |\n|g" | sort -u); do
10     if [[ $REMOTE_IP =~ ":" ]]; then
11       networks=$(ip -6 r | grep -v default | cut -d " " -f 1 | grep -v "unreachable")
12       ping=ping6
13     else
14       networks=$(ip r | grep -v default | cut -d " " -f 1)
15       ping=ping
16     fi
17     for LOCAL_NETWORK in $networks; do
18       in_network=$(python -c "import ipaddr; net=ipaddr.IPNetwork('$LOCAL_NETWORK'); addr=ipaddr.IPAddress('$REMOTE_IP'); print(addr in net)")
19       if [[ $in_network == "True" ]]; then
20         echo -n "Trying to ping $REMOTE_IP for local network $LOCAL_NETWORK..."
21         set +e
22         if ! $ping -W 300 -c 1 $REMOTE_IP &> /dev/null; then
23           echo "FAILURE"
24           echo "$REMOTE_IP is not pingable. Local Network: $LOCAL_NETWORK" >&2
25           exit 1
26         fi
27         set -e
28         echo "SUCCESS"
29       fi
30     done
31   done
32 }
33
34 # Ping all default gateways. There should only be one
35 # if using upstream t-h-t network templates but we test
36 # all of them should some manual network config have
37 # multiple gateways.
38 function ping_default_gateways() {
39   DEFAULT_GW=$(ip r | grep ^default | cut -d " " -f 3)
40   set +e
41   for GW in $DEFAULT_GW; do
42     echo -n "Trying to ping default gateway ${GW}..."
43     if ! ping -c 1 $GW &> /dev/null; then
44       echo "FAILURE"
45       echo "$GW is not pingable."
46       exit 1
47     fi
48   done
49   set -e
50   echo "SUCCESS"
51 }
52
53 ping_controller_ips "$ping_test_ips"
54 ping_default_gateways