7ccf0188ef241cbd7c07ac51a0061ba6694f5221
[fuel.git] / mcp / config / states / maas
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2017 Mirantis Inc., Enea AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
11 ERASE_ENV=${ERASE_ENV:-0}
12
13 # shellcheck disable=SC1090
14 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
15
16 # Wait for MaaS commissioning/deploy to finish, retry on failure
17 function maas_fixup() {
18   local statuscmd="salt 'mas01*' --out yaml state.apply maas.machines.status"
19   # shellcheck disable=SC2155
20   local ncount=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
21     grep -cE '^\s{2}\w+:$')
22   wait_for 180 "${statuscmd} | tee /dev/stderr | " \
23     "grep -Eq '((Deployed|Ready): ${ncount}|status: (Failed|Allocated))'"
24   # shellcheck disable=SC2155
25   local statusout=$(eval "${statuscmd}")
26
27   # shellcheck disable=SC2155
28   local fcnodes=$(echo "${statusout}" | \
29     grep -Pzo 'status: Failed commissioning\n\s+system_id: \K.+\n')
30   for node_system_id in ${fcnodes}; do
31     salt -C 'mas01*' state.apply maas.machines.delete \
32       pillar="{'system_id': '${node_system_id}'}"
33     sleep 30
34   done
35   if [ -n "${fcnodes}" ]; then
36     salt -C 'mas01*' state.apply maas.machines
37     return 1
38   fi
39
40   # shellcheck disable=SC2155
41   local fdnodes=$(echo "${statusout}" | \
42     grep -Pzo 'status: (Failed deployment|Allocated)\n\s+system_id: \K.+\n')
43   for node_system_id in ${fdnodes}; do
44     salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
45       pillar="{'system_id': '${node_system_id}'}"
46     sleep 30
47   done
48   if [ -n "${fdnodes}" ]; then
49     salt -C 'mas01*' state.apply maas.machines.deploy
50     return 1
51   fi
52
53   return 0
54 }
55
56 # Optionally destroy MaaS machines from a previous run
57 if [ "${ERASE_ENV}" -gt 1 ]; then
58   dnodes=$(salt 'mas01*' --out yaml state.apply maas.machines.status | \
59     grep -Pzo '\s+system_id: \K.+\n')
60   for node_system_id in ${dnodes}; do
61     salt -C 'mas01*' state.apply maas.machines.delete \
62       pillar="{'system_id': '${node_system_id}'}"
63     sleep 30
64   done
65 fi
66
67 # MaaS rack/region controller, node commissioning
68 salt -C 'mas01*' cmd.run "add-apt-repository ppa:maas/stable"
69
70 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
71 salt -C 'mas01*' state.apply linux.network.interface
72 salt -C 'mas01*' state.apply maas.pxe_nat
73 salt -C 'mas01*' state.apply maas.cluster
74 salt -C 'cfg01*' state.apply maas.pxe_route
75
76 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
77
78 salt -C 'mas01*' state.apply maas.machines
79 wait_for 10 maas_fixup
80
81 # cleanup outdated salt keys
82 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
83   xargs -I{} salt-key -yd {}
84
85 # MaaS node deployment
86 salt -C 'mas01*' state.apply maas.machines.deploy
87 wait_for 10 maas_fixup
88
89 salt -C 'mas01*' pillar.item\
90   maas:region:admin:username \
91   maas:region:admin:password
92
93 # Check all baremetal nodes are available
94 rc=1
95 attempt=0
96 total_attempts=10
97 while [ $rc -ne 0 ] && [ ${attempt} -lt ${total_attempts} ]; do
98   bm_nodes=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
99              awk '/^\s+\w+[[:digit:]]+:$/ {gsub(/:$/, "*"); print $1}')
100   rc=0
101   for node in $bm_nodes; do
102     salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
103   done
104   sleep 5
105   ((attempt+=1))
106 done
107
108 wait_for 10 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"