[MaaS] Override failed testing by default
[fuel.git] / mcp / config / states / maas
1 #!/bin/bash -e
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 ##############################################################################
10
11 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
12 ERASE_ENV=${ERASE_ENV:-0}
13
14 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/globals.sh"
15 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
16
17 bm_nodes=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
18            awk '/^\s+\w+[[:digit:]]+:$/ {gsub(/:$/, "*"); printf "%s ", $1}')
19
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+:$')
25
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}")
30
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}'}"
38     sleep 10
39   done
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}'}"
43     sleep 10
44   done
45   if [ -n "${fcnodes}" ] || [ -n "${ftnodes}" ]; then
46     salt -C 'mas01*' state.apply maas.machines
47     return 1
48   fi
49
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}'}"
57     sleep 10
58   done
59   if [ -n "${fdnodes}" ] || [ -n "${rnodes}" ]; then
60     salt -C 'mas01*' state.apply maas.machines.deploy
61     return 1
62   fi
63
64   return 0
65 }
66
67 # Optionally destroy MaaS machines from a previous run
68 if [ "${ERASE_ENV}" -gt 1 ]; then
69   set +e; dnodes=$(salt 'mas01*' --out yaml state.apply maas.machines.status | \
70     grep -Pzo '\s+system_id: \K.+\n'); set -e
71   cleanup_uefi
72   for node_system_id in ${dnodes}; do
73     salt -C 'mas01*' state.apply maas.machines.delete \
74       pillar="{'system_id': '${node_system_id}'}"
75     sleep 10
76   done
77 fi
78
79 # MaaS rack/region controller, node commissioning
80 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
81 salt -C 'mas01*' state.apply maas.cluster
82
83 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
84
85 if [ -n "${bm_nodes}" ]; then
86   salt -C 'mas01*' state.apply maas.machines
87 fi
88
89 # cleanup outdated salt keys
90 sleep 30
91 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
92   xargs --no-run-if-empty -I{} salt-key -yd {}
93
94 # MaaS node deployment
95 wait_for 10 maas_fixup
96
97 salt -C 'mas01*' pillar.item\
98   maas:region:admin:username \
99   maas:region:admin:password
100
101 # Check all baremetal nodes are available
102 wait_for 5.0 "(for n in ${bm_nodes}; do salt \${n} test.ping 2>/dev/null || exit; done)"
103
104 wait_for 10.0 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"