lib.sh: Extend wait_for function to catch no resp
[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 # 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
23   # wait_for has 10sec timeout * 96 = 16 min > 15min for Failed state
24   wait_for 96 "${statuscmd} | tee /dev/stderr | " \
25     "grep -Eq '((Deployed|Ready): ${ncount}|status: (Failed|Allocated))'"
26   local statusout=$(eval "${statuscmd}")
27
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}'}"
33     sleep 10
34   done
35   if [ -n "${fcnodes}" ]; then
36     salt -C 'mas01*' state.apply maas.machines
37     return 1
38   fi
39
40   local fdnodes=$(echo "${statusout}" | \
41     grep -Pzo 'status: (Failed deployment|Allocated)\n\s+system_id: \K.+\n')
42   local rnodes=$(echo "${statusout}" | \
43     grep -Pzo 'status: Ready\n\s+system_id: \K.+\n')
44   for node_system_id in ${fdnodes}; do
45     salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
46       pillar="{'system_id': '${node_system_id}'}"
47     sleep 10
48   done
49   if [ -n "${fdnodes}" ] || [ -n "${rnodes}" ]; then
50     salt -C 'mas01*' state.apply maas.machines.deploy
51     return 1
52   fi
53
54   return 0
55 }
56
57 # Optionally destroy MaaS machines from a previous run
58 if [ "${ERASE_ENV}" -gt 1 ]; then
59   set +e; dnodes=$(salt 'mas01*' --out yaml state.apply maas.machines.status | \
60     grep -Pzo '\s+system_id: \K.+\n'); set -e
61   cleanup_uefi
62   for node_system_id in ${dnodes}; do
63     salt -C 'mas01*' state.apply maas.machines.delete \
64       pillar="{'system_id': '${node_system_id}'}"
65     sleep 10
66   done
67 fi
68
69 # MaaS rack/region controller, node commissioning
70 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
71 salt -C 'mas01*' state.apply maas.cluster
72
73 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
74
75 salt -C 'mas01*' state.apply maas.machines
76
77 # cleanup outdated salt keys
78 sleep 30
79 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
80   xargs -I{} salt-key -yd {}
81
82 # MaaS node deployment
83 wait_for 10 maas_fixup
84
85 salt -C 'mas01*' pillar.item\
86   maas:region:admin:username \
87   maas:region:admin:password
88
89 # Check all baremetal nodes are available
90 rc=1
91 attempt=0
92 total_attempts=10
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}')
96   rc=0
97   for node in $bm_nodes; do
98     salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
99   done
100   sleep 5
101   ((attempt+=1))
102 done
103
104 wait_for 10.0 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"