2 ##############################################################################
3 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
11 ERASE_ENV=${ERASE_ENV:-0}
13 # shellcheck disable=SC1090
14 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
16 # Wait for MaaS commissioning/deploy to finish, retry on failure
17 function maas_fixup() {
18 local statuscmd="salt 'mas01*' --out yaml state.apply maas.machines.status"
19 # shellcheck disable=SC2155
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 # shellcheck disable=SC2155
25 local statusout=$(eval "${statuscmd}")
27 # shellcheck disable=SC2155
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 # shellcheck disable=SC2155
41 local fdnodes=$(echo "${statusout}" | \
42 grep -Pzo 'status: (Failed deployment|Allocated)\n\s+system_id: \K.+\n')
43 for node_system_id in ${fdnodes}; do
44 salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
45 pillar="{'system_id': '${node_system_id}'}"
48 if [ -n "${fdnodes}" ]; then
49 salt -C 'mas01*' state.apply maas.machines.deploy
56 # Optionally destroy MaaS machines from a previous run
57 if [ "${ERASE_ENV}" -gt 1 ]; then
58 dnodes=$(salt 'mas01*' --out yaml state.apply maas.machines.status | \
59 grep -Pzo '\s+system_id: \K.+\n')
60 for node_system_id in ${dnodes}; do
61 salt -C 'mas01*' state.apply maas.machines.delete \
62 pillar="{'system_id': '${node_system_id}'}"
67 # MaaS rack/region controller, node commissioning
68 salt -C 'mas01*' cmd.run "add-apt-repository ppa:maas/stable"
70 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
71 salt -C 'mas01*' state.apply linux.network.interface
72 salt -C 'mas01*' state.apply maas.pxe_nat
73 salt -C 'mas01*' state.apply maas.cluster
74 salt -C 'cfg01*' state.apply maas.pxe_route
76 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
78 salt -C 'mas01*' state.apply maas.machines
79 wait_for 10 maas_fixup
81 # cleanup outdated salt keys
82 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
83 xargs -I{} salt-key -yd {}
85 # MaaS node deployment
86 salt -C 'mas01*' state.apply maas.machines.deploy
87 wait_for 10 maas_fixup
89 salt -C 'mas01*' pillar.item\
90 maas:region:admin:username \
91 maas:region:admin:password
93 # Check all baremetal nodes are available
97 while [ $rc -ne 0 ] && [ ${attempt} -lt ${total_attempts} ]; do
98 bm_nodes=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
99 awk '/^\s+\w+[[:digit:]]+:$/ {gsub(/:$/, "*"); print $1}')
101 for node in $bm_nodes; do
102 salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
108 wait_for 10 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"