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