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