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