Merge "Open ports 443 and 80 on haproxy's firewall when horizon is standalone"
[apex-tripleo-heat-templates.git] / validation-scripts / all-nodes.sh
1 #!/bin/bash
2 set -e
3
4 function ping_retry() {
5   local IP_ADDR=$1
6   local TIMES=${2:-'10'}
7   local COUNT=0
8   local PING_CMD=ping
9   if [[ $IP_ADDR =~ ":" ]]; then
10     PING_CMD=ping6
11   fi
12   until [ $COUNT -ge $TIMES ]; do
13     if $PING_CMD -w 300 -c 1 $IP_ADDR &> /dev/null; then
14       echo "Ping to $IP_ADDR succeeded."
15       return 0
16     fi
17     echo "Ping to $IP_ADDR failed. Retrying..."
18     COUNT=$(($COUNT + 1))
19   done
20   return 1
21 }
22
23 # For each unique remote IP (specified via Heat) we check to
24 # see if one of the locally configured networks matches and if so we
25 # attempt a ping test the remote network IP.
26 function ping_controller_ips() {
27   local REMOTE_IPS=$1
28   for REMOTE_IP in $(echo $REMOTE_IPS | sed -e "s| |\n|g" | sort -u); do
29     if [[ $REMOTE_IP =~ ":" ]]; then
30       networks=$(ip -6 r | grep -v default | cut -d " " -f 1 | grep -v "unreachable")
31     else
32       networks=$(ip r | grep -v default | cut -d " " -f 1)
33     fi
34     for LOCAL_NETWORK in $networks; do
35       in_network=$(python -c "import ipaddr; net=ipaddr.IPNetwork('$LOCAL_NETWORK'); addr=ipaddr.IPAddress('$REMOTE_IP'); print(addr in net)")
36       if [[ $in_network == "True" ]]; then
37         echo "Trying to ping $REMOTE_IP for local network ${LOCAL_NETWORK}."
38         set +e
39         if ! ping_retry $REMOTE_IP; then
40           echo "FAILURE"
41           echo "$REMOTE_IP is not pingable. Local Network: $LOCAL_NETWORK" >&2
42           exit 1
43         fi
44         set -e
45         echo "SUCCESS"
46       fi
47     done
48   done
49 }
50
51 # Ping all default gateways. There should only be one
52 # if using upstream t-h-t network templates but we test
53 # all of them should some manual network config have
54 # multiple gateways.
55 function ping_default_gateways() {
56   DEFAULT_GW=$(ip r | grep ^default | cut -d " " -f 3)
57   set +e
58   for GW in $DEFAULT_GW; do
59     echo -n "Trying to ping default gateway ${GW}..."
60     if ! ping_retry $GW; then
61       echo "FAILURE"
62       echo "$GW is not pingable."
63       exit 1
64     fi
65   done
66   set -e
67   echo "SUCCESS"
68 }
69
70 # Verify the FQDN from the nova/ironic deployment matches
71 # FQDN in the heat templates.
72 function fqdn_check() {
73   HOSTNAME=$(hostname)
74   SHORT_NAME=$(hostname -s)
75   FQDN_FROM_HOSTS=$(awk '$3 == "'${SHORT_NAME}'"{print $2}' /etc/hosts)
76   echo -n "Checking hostname vs /etc/hosts entry..."
77   if [[ $HOSTNAME != $FQDN_FROM_HOSTS ]]; then
78     echo "FAILURE"
79     echo -e "System hostname: ${HOSTNAME}\nEntry from /etc/hosts: ${FQDN_FROM_HOSTS}\n"
80     exit 1
81   fi
82   echo "SUCCESS"
83 }
84
85 # Verify at least one time source is available.
86 function ntp_check() {
87   NTP_SERVERS=$(hiera ntp::servers nil |tr -d '[],"')
88   if [[ "$NTP_SERVERS" != "nil" ]];then
89     echo -n "Testing NTP..."
90     NTP_SUCCESS=0
91     for NTP_SERVER in $NTP_SERVERS; do
92       set +e
93       NTPDATE_OUT=$(ntpdate -qud $NTP_SERVER 2>&1)
94       NTPDATE_EXIT=$?
95       set -e
96       if [[ "$NTPDATE_EXIT" == "0" ]];then
97         NTP_SUCCESS=1
98         break
99       else
100         NTPDATE_OUT_FULL="$NTPDATE_OUT_FULL $NTPDATE_OUT"
101       fi
102     done
103     if  [[ "$NTP_SUCCESS" == "0" ]];then
104       echo "FAILURE"
105       echo "$NTPDATE_OUT_FULL"
106       exit 1
107     fi
108     echo "SUCCESS"
109   fi
110 }
111
112 ping_controller_ips "$ping_test_ips"
113 ping_default_gateways
114 if [[ $validate_fqdn == "True" ]];then
115   fqdn_check
116 fi
117 if [[ $validate_ntp == "True" ]];then
118   ntp_check
119 fi