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