2 # shellcheck disable=SC1090,SC2155
3 ##############################################################################
4 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
11 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
12 ERASE_ENV=${ERASE_ENV:-0}
14 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/globals.sh"
15 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
17 # Wait for MaaS commissioning/deploy to finish, retry on failure
18 function maas_fixup() {
19 local statuscmd="salt 'mas01*' --out yaml state.apply maas.machines.status"
20 local ncount=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
21 grep -cE '^\s{2}\w+:$')
23 # wait_for has 10sec timeout * 96 = 16 min > 15min for Failed state
24 wait_for 96 "${statuscmd} | tee /dev/stderr | " \
25 "grep -Eq '((Deployed|Ready): ${ncount}|status: (Failed|Allocated))'"
26 local statusout=$(eval "${statuscmd}")
28 local fcnodes=$(echo "${statusout}" | \
29 grep -Pzo 'status: Failed commissioning\n\s+system_id: \K.+\n')
30 for node_system_id in ${fcnodes}; do
31 salt -C 'mas01*' state.apply maas.machines.delete \
32 pillar="{'system_id': '${node_system_id}'}"
35 if [ -n "${fcnodes}" ]; then
36 salt -C 'mas01*' state.apply maas.machines
40 local fdnodes=$(echo "${statusout}" | \
41 grep -Pzo 'status: (Failed deployment|Allocated)\n\s+system_id: \K.+\n')
42 local rnodes=$(echo "${statusout}" | \
43 grep -Pzo 'status: Ready\n\s+system_id: \K.+\n')
44 for node_system_id in ${fdnodes}; do
45 salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
46 pillar="{'system_id': '${node_system_id}'}"
49 if [ -n "${fdnodes}" ] || [ -n "${rnodes}" ]; then
50 salt -C 'mas01*' state.apply maas.machines.deploy
57 # Optionally destroy MaaS machines from a previous run
58 if [ "${ERASE_ENV}" -gt 1 ]; then
59 set +e; dnodes=$(salt 'mas01*' --out yaml state.apply maas.machines.status | \
60 grep -Pzo '\s+system_id: \K.+\n'); set -e
62 for node_system_id in ${dnodes}; do
63 salt -C 'mas01*' state.apply maas.machines.delete \
64 pillar="{'system_id': '${node_system_id}'}"
69 # MaaS rack/region controller, node commissioning
70 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
71 salt -C 'mas01*' state.apply maas.pxe_nat
72 salt -C 'mas01*' state.apply maas.cluster
74 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
76 salt -C 'mas01*' state.apply maas.machines
78 # cleanup outdated salt keys
80 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
81 xargs -I{} salt-key -yd {}
83 # MaaS node deployment
84 wait_for 10 maas_fixup
86 salt -C 'mas01*' pillar.item\
87 maas:region:admin:username \
88 maas:region:admin:password
90 # Check all baremetal nodes are available
94 while [ $rc -ne 0 ] && [ ${attempt} -lt ${total_attempts} ]; do
95 bm_nodes=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
96 awk '/^\s+\w+[[:digit:]]+:$/ {gsub(/:$/, "*"); print $1}')
98 for node in $bm_nodes; do
99 salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
105 wait_for 10 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"