Use mapfile for array
[releng/builder.git] / jjb / integration / integration-get-slave-addresses.sh
1 #!/bin/bash
2 # Get the Controller and Tools VM slave addresses
3
4 set -x
5
6 ODL_SYSTEM=()
7 TOOLS_SYSTEM=()
8 OPENSTACK_SYSTEM=()
9 OPENSTACK_CONTROLLERS=()
10 [ "$NUM_OPENSTACK_SITES" ] || NUM_OPENSTACK_SITES=1
11
12 OPENSTACK_VENV="/tmp/v/openstack"
13 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
14 source $OPENSTACK_VENV/bin/activate
15 mapfile -t ADDR <<< "$(openstack stack show -f json -c outputs "$STACK_NAME" | jq -r '.outputs[] | select(.output_key | match("^vm_[0-9]+_ips$")) | .output_value | .[]')"
16
17 # The next two blocks of code will parse the list of vm IP's hostnames to determine which type of node
18 # the vm is: odl, devstack controller or compute, ha_proxy or tools. For the odl node's the hsotname will contain
19 # java in the name. The tools nodes are anything left after odl and devstack nodes have been found.
20 #
21 # The devstack nodes are identified with devstack in the hostname but require more checks to determine the controller
22 # node. Heat names the vms as devstack-<index> with index starting at 0. The vms are created in groups of which
23 # the openstack jobs create a vm_1_group for the controller and vm_2_group for the other vms such as the compute
24 # and ha_proxy nodes. The list of IP addresses for all the created vms is returned in reverse order of the group
25 # creation, but ordered within the group. I.E vm_1_group will be created first and named devstack-0, followed by
26 # vm_2_group and named devstack-0, devstack-1 and devstack-2. The list of IPs will be: devstack-0 (vm_2_group, compute),
27 # devstack-1, devstack-2, devstack-0 (vm_1_group, controller). Notice both the compute and first control node are both
28 # named devstack-0. We know the controller because it would be last in the list of IPs. This first block of code will
29 # produce two lists: one for the list of potential controllers and the second is a list of all devstack nodes.
30
31 for i in "${ADDR[@]}"
32 do
33     REMHOST=$(ssh "${i}" hostname -s)
34     case ${REMHOST} in
35     *builder*)
36        ODL_SYSTEM=( "${ODL_SYSTEM[@]}" "${i}" )
37        ;;
38     *devstack*)
39        # track potential controllers which would have -0 at the end of the hostname
40        if [[ ${REMHOST: -2} == "-0" ]]; then
41           OPENSTACK_CONTROLLERS=( "${OPENSTACK_CONTROLLERS[@]}" "${i}" )
42        fi
43
44        OPENSTACK_SYSTEM=( "${OPENSTACK_SYSTEM[@]}" "${i}" )
45        ;;
46     *)
47        TOOLS_SYSTEM=( "${TOOLS_SYSTEM[@]}" "${i}" )
48        ;;
49     esac
50 done
51
52 echo "NUM_ODL_SYSTEM=${#ODL_SYSTEM[@]}" >> slave_addresses.txt
53 echo "NUM_TOOLS_SYSTEM=${#TOOLS_SYSTEM[@]}" >> slave_addresses.txt
54 #if HA Proxy is requested the last devstack node will be configured as haproxy
55 if [ "${ENABLE_HAPROXY_FOR_NEUTRON}" == "yes" ]; then
56    # HA Proxy is installed on one OPENSTACK_SYSTEM VM on each site
57    NUM_OPENSTACK_SYSTEM=$(( ${#OPENSTACK_SYSTEM[@]} - NUM_OPENSTACK_SITES ))
58 else
59    NUM_OPENSTACK_SYSTEM=${#OPENSTACK_SYSTEM[@]}
60 fi
61 echo "NUM_OPENSTACK_SYSTEM=${NUM_OPENSTACK_SYSTEM}" >> slave_addresses.txt
62
63 # Rearrange the devstack node list to place the controller at the beginning of the list. The later code expects
64 # the list to be ordered with controller first followed by other types.
65 #
66 # At this point there is a list of potential devstack controllers as: devstack-0 (vm_2_group, compute),
67 # devstack-0 (vm_1_group, controller) and there is the full list with other compute or ha_proxy nodes in the middle.
68 # We know the controller is at the end of the devstack nodes list based on the ordering described above. Swap that entry
69 # with the first entry.
70 if [ ${#OPENSTACK_CONTROLLERS[@]} -eq 2 ]; then
71     ctrl_index=${#OPENSTACK_SYSTEM[@]}
72     ctrl_index=$((ctrl_index -1))
73     tmp_addr=${OPENSTACK_SYSTEM[0]}
74     OPENSTACK_SYSTEM[0]=${OPENSTACK_SYSTEM[$ctrl_index]}
75     OPENSTACK_SYSTEM[$ctrl_index]=$tmp_addr
76 fi
77
78 # Add alias for ODL_SYSTEM_1_IP as ODL_SYSTEM_IP
79 echo "ODL_SYSTEM_IP=${ODL_SYSTEM[0]}" >> slave_addresses.txt
80 for i in $(seq 0 $(( ${#ODL_SYSTEM[@]} - 1 )))
81 do
82     echo "ODL_SYSTEM_$((i+1))_IP=${ODL_SYSTEM[${i}]}" >> slave_addresses.txt
83 done
84
85 # Add alias for TOOLS_SYSTEM_1_IP as TOOLS_SYSTEM_IP
86 echo "TOOLS_SYSTEM_IP=${TOOLS_SYSTEM[0]}" >> slave_addresses.txt
87 for i in $(seq 0 $(( ${#TOOLS_SYSTEM[@]} - 1 )))
88 do
89     echo "TOOLS_SYSTEM_$((i+1))_IP=${TOOLS_SYSTEM[${i}]}" >> slave_addresses.txt
90 done
91
92 openstack_index=0
93 # Assuming number of openstack control nodes equals number of openstack sites
94 NUM_OPENSTACK_CONTROL_NODES=$(( NUM_OPENSTACK_SITES ))
95 echo "NUM_OPENSTACK_CONTROL_NODES=${NUM_OPENSTACK_CONTROL_NODES}" >> slave_addresses.txt
96 for i in $(seq 0 $((NUM_OPENSTACK_CONTROL_NODES - 1)))
97 do
98     echo "OPENSTACK_CONTROL_NODE_$((i+1))_IP=${OPENSTACK_SYSTEM[$((openstack_index++))]}" >> slave_addresses.txt
99 done
100
101 # The rest of the openstack nodes until NUM_OPENSTACK_SYSTEM are computes
102 NUM_OPENSTACK_COMPUTE_NODES=$(( NUM_OPENSTACK_SYSTEM - NUM_OPENSTACK_CONTROL_NODES ))
103 echo "NUM_OPENSTACK_COMPUTE_NODES=${NUM_OPENSTACK_COMPUTE_NODES}" >> slave_addresses.txt
104
105 # Order the computes in the list so that the devstack-0 is index 1 and devstack-1 is index 2. Currently they are
106 # backwards because of the controller swap earlier.
107 if [ ${NUM_OPENSTACK_COMPUTE_NODES} -ge 2 ]; then
108     tmp_addr=${OPENSTACK_SYSTEM[1]}
109     OPENSTACK_SYSTEM[1]=${OPENSTACK_SYSTEM[2]}
110     OPENSTACK_SYSTEM[2]=${tmp_addr}
111 fi
112
113 for i in $(seq 0 $((NUM_OPENSTACK_COMPUTE_NODES - 1)))
114 do
115     echo "OPENSTACK_COMPUTE_NODE_$((i+1))_IP=${OPENSTACK_SYSTEM[$((openstack_index++))]}" >> slave_addresses.txt
116 done
117
118 # The remaining openstack nodes are haproxy nodes (for ODL cluster)
119 NUM_OPENSTACK_HAPROXY_NODES=$(( ${#OPENSTACK_SYSTEM[@]} - NUM_OPENSTACK_SYSTEM ))
120 echo "NUM_OPENSTACK_HAPROXY_NODES=${NUM_OPENSTACK_HAPROXY_NODES}" >> slave_addresses.txt
121 for i in $(seq 0 $((NUM_OPENSTACK_HAPROXY_NODES - 1)))
122 do
123     echo "OPENSTACK_HAPROXY_$((i+1))_IP=${OPENSTACK_SYSTEM[$((openstack_index++))]}" >> slave_addresses.txt
124 done
125 echo "Contents of slave_addresses.txt:"
126 cat slave_addresses.txt
127 # vim: sw=4 ts=4 sts=4 et ft=sh :