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+:$')
22 wait_for 180 "${statuscmd} | tee /dev/stderr | " \
23 "grep -Eq '((Deployed|Ready): ${ncount}|status: (Failed|Allocated))'"
24 local statusout=$(eval "${statuscmd}")
26 local fcnodes=$(echo "${statusout}" | \
27 grep -Pzo 'status: Failed commissioning\n\s+system_id: \K.+\n')
28 for node_system_id in ${fcnodes}; do
29 salt -C 'mas01*' state.apply maas.machines.delete \
30 pillar="{'system_id': '${node_system_id}'}"
33 if [ -n "${fcnodes}" ]; then
34 salt -C 'mas01*' state.apply maas.machines
38 local fdnodes=$(echo "${statusout}" | \
39 grep -Pzo 'status: (Failed deployment|Allocated)\n\s+system_id: \K.+\n')
40 for node_system_id in ${fdnodes}; do
41 salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
42 pillar="{'system_id': '${node_system_id}'}"
45 if [ -n "${fdnodes}" ]; then
46 salt -C 'mas01*' state.apply maas.machines.deploy
53 # Optionally destroy MaaS machines from a previous run
54 if [ "${ERASE_ENV}" -gt 1 ]; then
55 set +e; dnodes=$(salt 'mas01*' --out yaml state.apply maas.machines.status | \
56 grep -Pzo '\s+system_id: \K.+\n'); set -e
58 for node_system_id in ${dnodes}; do
59 salt -C 'mas01*' state.apply maas.machines.delete \
60 pillar="{'system_id': '${node_system_id}'}"
65 # MaaS rack/region controller, node commissioning
66 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
67 salt -C 'mas01*' state.apply linux.network.interface
68 salt -C 'mas01*' state.apply maas.pxe_nat
69 salt -C 'mas01*' state.apply maas.cluster
70 salt -C 'cfg01*' state.apply maas.pxe_route
72 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
74 salt -C 'mas01*' state.apply maas.machines
75 wait_for 10 maas_fixup
77 # cleanup outdated salt keys
78 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
79 xargs -I{} salt-key -yd {}
81 # MaaS node deployment
82 salt -C 'mas01*' state.apply maas.machines.deploy
83 wait_for 10 maas_fixup
85 salt -C 'mas01*' pillar.item\
86 maas:region:admin:username \
87 maas:region:admin:password
89 # Check all baremetal nodes are available
93 while [ $rc -ne 0 ] && [ ${attempt} -lt ${total_attempts} ]; do
94 bm_nodes=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
95 awk '/^\s+\w+[[:digit:]]+:$/ {gsub(/:$/, "*"); print $1}')
97 for node in $bm_nodes; do
98 salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
104 wait_for 10 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"