-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·134 lines (110 loc) · 3.3 KB
/
setup.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
#============================================================================
#
# USAGE: ./setup.sh
# DESCRIPTION: Automatically setup the cluster and tear it down.
#
#============================================================================
set -o nounset # Treat unset variables as an error.
trap 'kill -- -$$' SIGINT # Kill entire process group.
#============================================================================
VIRSHCMD="virsh -c qemu:///system"
FORCE=0
function start_cluster {
terraform plan
terraform apply
if [ "$FORCE" == "1" ]; then
start_inactive_vms
fi
}
function destroy_cluster {
terraform refresh
terraform destroy
echo "Warning: Destroy may not function as expected."
echo "Note: You may have to do some manual cleanups using virsh."
if [ "$FORCE" == "1" ]; then
destroy_remaining_vms
fi
}
function destroy_remaining_vms {
# XXX dbite: Fix it in the right place.
# Temporary hack!
active_vms=$($VIRSHCMD list |\
tail -n +3 |\
grep -E "running|dsaltmaster|dsaltminion-[0-9]" |\
awk '{ print$2 }')
printf "Found the following VM's to be active:\n$active_vms\n"
echo "Manually stopping VM's."
for vm in $active_vms ; do
echo "Stopping and destroying $vm."
$VIRSHCMD destroy $vm
sleep 2
$VIRSHCMD undefine $vm
sleep 3
done
}
function start_inactive_vms {
# XXX dbite: Fix this issue in the right place.
# Quick hack for this issue:
# https://github.com/dmacvicar/terraform-provider-libvirt/issues/65
# Search for inactive VM's which are created by this program.
# Really really make sure that the VM's belong to this cluster.
# Logic is flawed since it is very specific to the VM naming scheme
# of `libvirt.tf` configuration file.
inactive_vms=$($VIRSHCMD list --all |\
tail -n +3 |\
grep -v "running" |\
awk '{ print $2 }' |\
grep -E "dsaltmaster|dsaltminion-[0-9]")
printf "Found the following VM's to be inactive:\n$inactive_vms\n"
echo "Manually Starting inactive VMs."
for vm in $inactive_vms ; do
echo "Starting $vm."
$VIRSHCMD start $vm
sleep 5
done
}
function usage {
echo "Usage: $0 [-f] [h|help|provision|teardown]"
echo
echo "-f Force, manually trigger libvirt after terraform"
echo " has run. WARNING! This will break terraform."
echo
echo "h|help Help."
echo "provision Provision the salt cluster using terraform."
echo "teardown Destroy the cluster, additionally attempt cleanups."
echo
echo "Please retry with the above mentioned options. Have fun hacking!"
exit
}
function cli {
case $OPTIONS in
h|help)
usage
;;
provision)
start_cluster
exit
;;
teardown)
# XXX terraform libvirt is not a good citizen for destroy.
destroy_cluster
exit
;;
*)
echo "Invalid Option(s) $OPTIONS."
usage
;;
esac
}
OPTIONS=$@
if [ $# -eq 0 ]; then
usage
else
if [ "$1" == "-f" ]; then
shift
FORCE=1
fi
OPTIONS=$1
cli $OPTIONS
fi