Drop infinite loops in favor of finite wait_for
[fuel.git] / mcp / config / states / maas
1 #!/bin/bash
2 set -x
3
4 # shellcheck disable=SC1090
5 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
6
7 # Wait for MaaS commissioning/deploy to finish, retry on failure
8 function maas_fixup() {
9   local statuscmd="salt 'mas01*' --out yaml state.apply maas.machines.status"
10   # shellcheck disable=SC2155
11   local ncount=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
12     grep -cE '^\s{2}\w+:$')
13   wait_for 180 "${statuscmd} | tee /dev/stderr | " \
14     "grep -Eq '((Deployed|Ready): ${ncount}|status:Failed|status:Allocated)'"
15   # shellcheck disable=SC2155
16   local statusout=$(eval "${statuscmd}")
17
18   # shellcheck disable=SC2155
19   local fcnodes=$(echo "${statusout}" | \
20     grep -Po '(?<=system_id:)(.*)(?=,status:Failed commissioning)')
21   for node_system_id in ${fcnodes}; do
22     salt -C 'mas01*' state.apply maas.machines.delete \
23       pillar="{'system_id': '${node_system_id}'}"
24     sleep 30
25   done
26   if [ -n "${fcnodes}" ]; then
27     salt -C 'mas01*' state.apply maas.machines
28     return 1
29   fi
30
31   # shellcheck disable=SC2155
32   local fdnodes=$(echo "${statusout}" | \
33     grep -Po '(?<=system_id:)(.*)(?=,status:(Failed deployment|Allocated))')
34   for node_system_id in ${fdnodes}; do
35     salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
36       pillar="{'system_id': '${node_system_id}'}"
37     sleep 30
38   done
39   if [ -n "${fdnodes}" ]; then
40     salt -C 'mas01*' state.apply maas.machines.deploy
41     return 1
42   fi
43
44   return 0
45 }
46
47 # MaaS rack/region controller, node commissioning
48 salt -C 'mas01*' cmd.run "add-apt-repository ppa:maas/stable"
49
50 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
51 salt -C 'mas01*' state.apply linux.network.interface
52 salt -C 'mas01*' state.apply maas.pxe_nat
53 salt -C 'mas01*' state.apply maas.cluster
54 salt -C 'cfg01*' state.apply maas.pxe_route
55
56 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
57
58 salt -C 'mas01*' state.apply maas.machines
59 wait_for 10 maas_fixup
60
61 # cleanup outdated salt keys
62 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
63   xargs -I{} salt-key -yd {}
64
65 # MaaS node deployment
66 salt -C 'mas01*' state.apply maas.machines.deploy
67 wait_for 10 maas_fixup
68
69 salt -C 'mas01*' pillar.item\
70   maas:region:admin:username \
71   maas:region:admin:password
72
73 salt -C '* and not cfg01* and not mas01*' saltutil.sync_all