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 bm_nodes=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
18 awk '/^\s+\w+[[:digit:]]+:$/ {gsub(/:$/, "*"); printf "%s ", $1}')
20 # Wait for MaaS commissioning/deploy to finish, retry on failure
21 function maas_fixup() {
22 local statuscmd="salt 'mas01*' --out yaml state.apply maas.machines.status"
23 local ncount=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
24 grep -cE '^\s{2}\w+:$')
26 # wait_for has 10sec timeout * 96 = 16 min > 15min for Failed state
27 wait_for 96 "${statuscmd} | tee /dev/stderr | " \
28 "grep -Eq '((Deployed|Ready): ${ncount}|status: (Failed|Allocated))'"
29 local statusout=$(eval "${statuscmd}")
31 local fcnodes=$(echo "${statusout}" | \
32 grep -Pzo 'status: Failed commissioning\n\s+system_id: \K.+\n')
33 local ftnodes=$(echo "${statusout}" | \
34 grep -Pzo 'status: Failed testing\n\s+system_id: \K.+\n')
35 for node_system_id in ${fcnodes}; do
36 salt -C 'mas01*' state.apply maas.machines.delete \
37 pillar="{'system_id': '${node_system_id}'}"
40 for node_system_id in ${ftnodes}; do
41 salt -C 'mas01*' state.apply maas.machines.override_failed_testing \
42 pillar="{'system_id': '${node_system_id}'}"
45 if [ -n "${fcnodes}" ] || [ -n "${ftnodes}" ]; then
46 salt -C 'mas01*' state.apply maas.machines
50 local fdnodes=$(echo "${statusout}" | \
51 grep -Pzo 'status: (Failed deployment|Allocated)\n\s+system_id: \K.+\n')
52 local rnodes=$(echo "${statusout}" | \
53 grep -Pzo 'status: Ready\n\s+system_id: \K.+\n')
54 for node_system_id in ${fdnodes}; do
55 salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
56 pillar="{'system_id': '${node_system_id}'}"
59 if [ -n "${fdnodes}" ] || [ -n "${rnodes}" ]; then
60 for node_system_id in ${fdnodes} ${rnodes}; do
61 # For now, we allocate 30GB (fixed) for / on cmp nodes
62 local node_hostname=$(echo "${statusout}" | \
63 grep -Pzo 'hostname: \K.+(?=\n.+\n\s+system_id: '"${node_system_id}"')')
64 if [[ "${node_hostname}" =~ ^cmp ]]; then
65 salt -C 'mas01*' state.apply maas.machines.set_storage_layout \
66 pillar="{'system_id': '${node_system_id}', 'lv_size': '32212254720'}"
70 salt -C 'mas01*' state.apply maas.machines.deploy
77 # Optionally destroy MaaS machines from a previous run
78 if [ "${ERASE_ENV}" -gt 1 ]; then
79 set +e; dnodes=$(salt 'mas01*' --out yaml state.apply maas.machines.status | \
80 grep -Pzo '\s+system_id: \K.+\n'); set -e
82 for node_system_id in ${dnodes}; do
83 salt -C 'mas01*' state.apply maas.machines.delete \
84 pillar="{'system_id': '${node_system_id}'}"
89 # MaaS rack/region controller, node commissioning
90 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
91 salt -C 'mas01*' state.apply maas.pxe_nat
92 salt -C 'mas01*' state.apply maas.cluster
94 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
96 if [ -n "${bm_nodes}" ]; then
97 salt -C 'mas01*' state.apply maas.machines
100 # cleanup outdated salt keys
102 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
103 xargs --no-run-if-empty -I{} salt-key -yd {}
105 # MaaS node deployment
106 wait_for 10 maas_fixup
108 salt -C 'mas01*' pillar.item\
109 maas:region:admin:username \
110 maas:region:admin:password
112 # Check all baremetal nodes are available
113 wait_for 5.0 "(for n in ${bm_nodes}; do salt \${n} test.ping 2>/dev/null || exit; done)"
115 wait_for 10.0 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"