[baremetal] maas state: Retry sync_all on failure
[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
12 # shellcheck disable=SC1090
13 source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
14
15 # Wait for MaaS commissioning/deploy to finish, retry on failure
16 function maas_fixup() {
17   local statuscmd="salt 'mas01*' --out yaml state.apply maas.machines.status"
18   # shellcheck disable=SC2155
19   local ncount=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
20     grep -cE '^\s{2}\w+:$')
21   wait_for 180 "${statuscmd} | tee /dev/stderr | " \
22     "grep -Eq '((Deployed|Ready): ${ncount}|status:Failed|status:Allocated)'"
23   # shellcheck disable=SC2155
24   local statusout=$(eval "${statuscmd}")
25
26   # shellcheck disable=SC2155
27   local fcnodes=$(echo "${statusout}" | \
28     grep -Po '(?<=system_id:)(.*)(?=,status:Failed commissioning)')
29   for node_system_id in ${fcnodes}; do
30     salt -C 'mas01*' state.apply maas.machines.delete \
31       pillar="{'system_id': '${node_system_id}'}"
32     sleep 30
33   done
34   if [ -n "${fcnodes}" ]; then
35     salt -C 'mas01*' state.apply maas.machines
36     return 1
37   fi
38
39   # shellcheck disable=SC2155
40   local fdnodes=$(echo "${statusout}" | \
41     grep -Po '(?<=system_id:)(.*)(?=,status:(Failed deployment|Allocated))')
42   for node_system_id in ${fdnodes}; do
43     salt -C 'mas01*' state.apply maas.machines.mark_broken_fixed \
44       pillar="{'system_id': '${node_system_id}'}"
45     sleep 30
46   done
47   if [ -n "${fdnodes}" ]; then
48     salt -C 'mas01*' state.apply maas.machines.deploy
49     return 1
50   fi
51
52   return 0
53 }
54
55 # MaaS rack/region controller, node commissioning
56 salt -C 'mas01*' cmd.run "add-apt-repository ppa:maas/stable"
57
58 salt -C 'mas01*' state.apply linux,salt,openssh,ntp
59 salt -C 'mas01*' state.apply linux.network.interface
60 salt -C 'mas01*' state.apply maas.pxe_nat
61 salt -C 'mas01*' state.apply maas.cluster
62 salt -C 'cfg01*' state.apply maas.pxe_route
63
64 wait_for 10 "salt -C 'mas01*' state.apply maas.region"
65
66 salt -C 'mas01*' state.apply maas.machines
67 wait_for 10 maas_fixup
68
69 # cleanup outdated salt keys
70 salt-key --out yaml | awk '!/^(minions|- cfg01|- mas01)/ {print $2}' | \
71   xargs -I{} salt-key -yd {}
72
73 # MaaS node deployment
74 salt -C 'mas01*' state.apply maas.machines.deploy
75 wait_for 10 maas_fixup
76
77 salt -C 'mas01*' pillar.item\
78   maas:region:admin:username \
79   maas:region:admin:password
80
81 wait_for 10 "salt -C '* and not cfg01* and not mas01*' saltutil.sync_all"