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