[states] maas, vcp: Use `wait_for` in online check
[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(/:$/, "*"); print $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   for node_system_id in ${fcnodes}; do
34     salt -C 'mas01*' state.apply maas.machines.delete \
35       pillar="{'system_id': '${node_system_id}'}"
36     sleep 10
37   done
38   if [ -n "${fcnodes}" ]; then
39     salt -C 'mas01*' state.apply maas.machines
40     return 1
41   fi
42
43   local fdnodes=$(echo "${statusout}" | \
44     grep -Pzo 'status: (Failed deployment|Allocated)\n\s+system_id: \K.+\n')
45   local rnodes=$(echo "${statusout}" | \
46     grep -Pzo 'status: Ready\n\s+system_id: \K.+\n')
47   for node_system_id in ${fdnodes}; do
48     salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
49       pillar="{'system_id': '${node_system_id}'}"
50     sleep 10
51   done
52   if [ -n "${fdnodes}" ] || [ -n "${rnodes}" ]; then
53     salt -C 'mas01*' state.apply maas.machines.deploy
54     return 1
55   fi
56
57   return 0
58 }
59
60 # Optionally destroy MaaS machines from a previous run
61 if [ "${ERASE_ENV}" -gt 1 ]; then
62   set +e; dnodes=$(salt 'mas01*' --out yaml state.apply maas.machines.status | \
63     grep -Pzo '\s+system_id: \K.+\n'); set -e
64   cleanup_uefi
65   for node_system_id in ${dnodes}; do
66     salt -C 'mas01*' state.apply maas.machines.delete \
67       pillar="{'system_id': '${node_system_id}'}"
68     sleep 10
69   done
70 fi
71
72 # MaaS rack/region controller, node commissioning
73 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
74 salt -C 'mas01*' state.apply maas.cluster
75
76 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
77
78 if [ -n "${bm_nodes[*]}" ]; then
79   salt -C 'mas01*' state.apply maas.machines
80 fi
81
82 # cleanup outdated salt keys
83 sleep 30
84 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
85   xargs --no-run-if-empty -I{} salt-key -yd {}
86
87 # MaaS node deployment
88 wait_for 10 maas_fixup
89
90 salt -C 'mas01*' pillar.item\
91   maas:region:admin:username \
92   maas:region:admin:password
93
94 # Check all baremetal nodes are available
95 wait_for 5.0 "for n in ${bm_nodes}; do salt '${n}' test.ping 2>/dev/null; done"
96
97 wait_for 10.0 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"