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