states: maas: Dynamic node count in stop condition
[fuel.git] / mcp / config / states / maas
1 #!/bin/bash
2 set -x
3
4 function wait_for() {
5   local total_attempts=$1; shift
6   local cmdstr=$*
7   local sleep_time=10
8   echo "[NOTE] Waiting for cmd to return success: ${cmdstr}"
9   # shellcheck disable=SC2034
10   for attempt in $(seq "${total_attempts}"); do
11     # shellcheck disable=SC2015
12     eval "${cmdstr}" && break || true
13     echo -n '.'; sleep "${sleep_time}"
14   done
15 }
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   # shellcheck disable=SC2155
21   local ncount=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
22     grep -cE '^\s{2}\w+:$')
23   wait_for 180 "${statuscmd} | tee /dev/stderr | " \
24     "grep -Eq '((Deployed|Ready): ${ncount}|status:Failed|status:Allocated)'"
25   # shellcheck disable=SC2155
26   local statusout=$(eval "${statuscmd}")
27
28   # shellcheck disable=SC2155
29   local fcnodes=$(echo "${statusout}" | \
30     grep -Po '(?<=system_id:)(.*)(?=,status:Failed commissioning)')
31   for node_system_id in ${fcnodes}; do
32     salt -C 'mas01*' state.apply maas.machines.delete \
33       pillar="{'system_id': '${node_system_id}'}"
34     sleep 30
35   done
36   if [ -n "${fcnodes}" ]; then
37     salt -C 'mas01*' state.apply maas.machines
38     return 1
39   fi
40
41   # shellcheck disable=SC2155
42   local fdnodes=$(echo "${statusout}" | \
43     grep -Po '(?<=system_id:)(.*)(?=,status:(Failed deployment|Allocated))')
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 30
48   done
49   if [ -n "${fdnodes}" ]; then
50     salt -C 'mas01*' state.apply maas.machines.deploy
51     return 1
52   fi
53
54   return 0
55 }
56
57 # MaaS rack/region controller, node commissioning
58 salt -C 'mas01*' cmd.run "add-apt-repository ppa:maas/stable"
59
60 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
61 salt -C 'mas01*' state.apply linux.network.interface
62 salt -C 'mas01*' state.apply maas.pxe_nat
63 salt -C 'mas01*' state.apply maas.cluster
64 salt -C 'cfg01*' state.apply maas.pxe_route
65
66 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
67
68 salt -C 'mas01*' state.apply maas.machines
69 wait_for 10 maas_fixup
70
71 # cleanup outdated salt keys
72 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
73   xargs -I{} salt-key -yd {}
74
75 # MaaS node deployment
76 salt -C 'mas01*' state.apply maas.machines.deploy
77 wait_for 10 maas_fixup
78
79 salt -C 'mas01*' pillar.item\
80   maas:region:admin:username \
81   maas:region:admin:password
82
83 # KVM, compute node prereqs (libvirt first), VCP deployment
84 salt -C '* and not cfg01* and not mas01*' saltutil.sync_all
85
86 salt -C 'kvm*' pkg.install bridge-utils
87 salt -C 'kvm*' state.apply linux.network
88 salt -C 'kvm*' system.reboot
89 wait_for 90 "! salt 'kvm*' test.ping | tee /dev/stderr | fgrep -q 'Not connected'"
90
91 salt -C '* and not cfg01* and not mas01*' state.apply linux,ntp
92
93 salt -C 'kvm*' state.sls libvirt
94
95 salt -C '* and not cfg01* and not mas01*' state.apply salt
96 salt -C 'kvm*' saltutil.sync_all
97 wait_for 10 "! salt -C 'kvm*' state.sls salt.control | " \
98   "tee /dev/stderr | fgrep -q 'Not connected'"
99
100 vcp_nodes=$(salt --out yaml 'kvm01*' pillar.get salt:control:cluster:internal:node | \
101             awk '/\s+\w+:$/ {gsub(/:$/, "*"); print $1}')
102
103 # Check all vcp nodes are available
104 rc=1
105 while [ $rc -ne 0 ]; do
106   rc=0
107   for node in $vcp_nodes; do
108     salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
109   done
110   sleep 5
111 done
112
113 wait_for 10 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"
114 wait_for 10 "salt -C 'E@^(?!cfg01|mas01|kvm|cmp00).*' state.apply salt"
115 wait_for 10 "! salt -C 'E@^(?!cfg01|mas01|kvm|cmp00).*' state.apply linux,ntp | " \
116   "tee /dev/stderr | fgrep -q 'Not connected'"
117
118 wait_for 10 "salt -C 'E@^(?!cfg01|mas01|kvm|cmp00).*' ssh.set_auth_key ${SUDO_USER} \
119   $(awk 'NR==1{print $2}' "$(eval echo "~${SUDO_USER}/.ssh/authorized_keys")")"
120
121 # Get the latest packages
122 wait_for 10 "! salt '*' pkg.upgrade refresh=False | " \
123   "tee /dev/stderr | fgrep -q 'Not connected'"