-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprovision.sh
executable file
·68 lines (57 loc) · 1.76 KB
/
provision.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
ROLE=base
LOG=warning
TARGET=target
MASTER=192.168.99.1
while getopts 'r:l:t:m:' flag; do
case "${flag}" in
r) ROLE="${OPTARG}" ;;
l) LOG="${OPTARG}" ;;
t) TARGET="${OPTARG}" ;;
m) MASTER="${OPTARG}" ;;
*) error "Unexpected option ${flag}" ;;
esac
done
echo "role: $ROLE, log level: $LOG, target: $TARGET, master: $MASTER"
ARGS="-l $LOG"
echo "Looking for connected salt minion on $TARGET"
# Note this will spew an error if the master has zero known minions
salt-run manage.up | tee .out
MINIONS_FOUND="`grep -e '^- ' .out | wc -l`"
if [ "$MINIONS_FOUND" -ge 1 ]
then
echo "Found $MINIONS_FOUND active salt minion(s)"
else
rm -f $HOME/.ssh/known_hosts
echo "Installing salt-minion on $TARGET"
./salt-ssh $ARGS -i $TARGET state.apply salt.provision_minion pillar="{\"salt_minion\": {\"master_host\": \"$MASTER\"}, \"roles\": [\"$ROLE\"]}" | tee .out
if grep 'Failed: *0' .out >/dev/null
then
echo "INFO: $TARGET was updated with salt-minion"
else
echo "ERROR: could not install salt-minion"
exit 1
fi
while :
do
echo "Waiting for new minion to connect"
salt-run manage.up | tee .out
MINIONS_FOUND="`grep -e '^- ' .out | wc -l`"
if [ "$MINIONS_FOUND" -ge 1 ]
then
break
fi
sleep 5
done
fi
echo "Applying states to new minion"
salt $ARGS -G 'roles:provision_minion' state.apply | tee .out
if grep '# of minions with errors:.* 0' .out >/dev/null && grep '# of minions that did not return:.* 0' .out >/dev/null
then
echo "INFO: $TARGET was updated with desired states"
else
echo "ERROR: could not apply desired states"
exit 1
fi
echo "Erasing provisioning diversions on minion"
salt $ARGS -G 'roles:provision_minion' state.apply salt.provision_reset