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