Merge "ci/deploy.sh, states: bash debug, continue on err"
[fuel.git] / mcp / config / states / maas
1 #!/bin/bash
2 set -x
3
4 function wait_for() {
5   local cmdstr=$@
6   local total_attempts=360
7   local sleep_time=10
8   local attempt=1
9   echo "[NOTE] Waiting for cmd to return success: ${cmdstr}"
10   while ((attempt <= total_attempts)); do
11     eval "${cmdstr}" && break || true
12     echo -n '.'; sleep "${sleep_time}"
13     ((attempt+=1))
14   done
15 }
16
17 # MaaS rack/region controller, node commissioning
18 salt -C 'mas01*' cmd.run "add-apt-repository ppa:maas/stable"
19
20 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
21 salt -C 'mas01*' state.apply linux.network.interface
22 salt -C 'mas01*' state.apply maas.pxe_nat
23 salt -C 'mas01*' state.apply maas.cluster
24 salt -C 'cfg01*' cmd.run \
25   "route add -net 192.168.11.0/24 gw ${MAAS_IP:-192.168.10.3}"
26
27 wait_for "salt -C 'mas01*' state.apply maas.region"
28
29 salt -C 'mas01*' state.apply maas.machines
30 # TODO: relax cond, as this is not re-entrant (e.g. nodes already deployed)
31 wait_for "salt 'mas01*' --out yaml state.apply maas.machines.status | " \
32          "tee /dev/stderr | fgrep -q 'Ready: 5'"
33
34 # cleanup outdated salt keys
35 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | xargs -I{} salt-key -yd {}
36
37 # MaaS node deployment
38 salt -C 'mas01*' state.apply maas.machines.deploy
39 wait_for "salt 'mas01*' --out yaml state.apply maas.machines.status | " \
40          "tee /dev/stderr | fgrep -q 'Deployed: 5'"
41
42 salt -C 'mas01*' pillar.item\
43   maas:region:admin:username \
44   maas:region:admin:password
45
46 # KVM, compute node prereqs (libvirt first), VCP deployment
47 salt -C '* and not cfg01* and not mas01*' saltutil.sync_all
48
49 salt -C 'kvm*' pkg.install bridge-utils
50 salt -C 'kvm*' state.apply linux.network
51 salt -C 'kvm*' system.reboot
52 wait_for "! salt '*' test.ping | tee /dev/stderr | fgrep -q 'Not connected'"
53
54 salt -C '* and not cfg01* and not mas01*' state.apply linux,ntp
55
56 salt -C 'kvm*' state.sls libvirt
57
58 salt -C '* and not cfg01* and not mas01*' state.apply salt
59 salt -C 'kvm*' saltutil.sync_all
60 salt -C 'kvm*' state.sls salt.control
61
62 vcp_nodes=$(salt --out yaml 'kvm01*' pillar.get salt:control:cluster:internal:node | awk '/\s+\w+:$/ {gsub(/:$/, "*"); print $1}')
63
64 # Check all vcp nodes are available
65 rc=1
66 while [ $rc -ne 0 ]; do
67   rc=0
68   for node in $vcp_nodes; do
69     salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
70   done
71   sleep 5
72 done
73
74 wait_for "salt -C '* and not cfg01* and not mas01*' ssh.set_auth_key ${SUDO_USER} \
75   $(awk 'NR==1{print $2}' $(eval echo ~${SUDO_USER}/.ssh/authorized_keys))"
76
77 wait_for "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"
78 salt -C '* and not cfg01* and not mas01*' state.apply salt
79 wait_for "salt -C '* and not cfg01* and not mas01*' state.apply linux,ntp"