[baremetal] Upgrade packages on kvm, cmp nodes too
[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*' state.apply linux,salt,openssh,ntp
69 salt -C 'mas01*' state.apply linux.network.interface
70 salt -C 'mas01*' state.apply maas.pxe_nat
71 salt -C 'mas01*' state.apply maas.cluster
72 salt -C 'cfg01*' state.apply maas.pxe_route
73
74 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
75
76 salt -C 'mas01*' state.apply maas.machines
77 wait_for 10 maas_fixup
78
79 # cleanup outdated salt keys
80 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
81   xargs -I{} salt-key -yd {}
82
83 # MaaS node deployment
84 salt -C 'mas01*' state.apply maas.machines.deploy
85 wait_for 10 maas_fixup
86
87 salt -C 'mas01*' pillar.item\
88   maas:region:admin:username \
89   maas:region:admin:password
90
91 # Check all baremetal nodes are available
92 rc=1
93 attempt=0
94 total_attempts=10
95 while [ $rc -ne 0 ] && [ ${attempt} -lt ${total_attempts} ]; do
96   bm_nodes=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
97              awk '/^\s+\w+[[:digit:]]+:$/ {gsub(/:$/, "*"); print $1}')
98   rc=0
99   for node in $bm_nodes; do
100     salt "$node" test.ping 2>/dev/null || { rc=$?; break; };
101   done
102   sleep 5
103   ((attempt+=1))
104 done
105
106 wait_for 10 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"