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