-
Notifications
You must be signed in to change notification settings - Fork 16
/
cluster_destroy_local.sh
executable file
·104 lines (85 loc) · 3.15 KB
/
cluster_destroy_local.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
#!/bin/bash
# Uncomment below to help with debugging
# set -x
#This script makes several assumptions:
# 1. Running on a host with openstack client tools installed
# 2. Using a default ssh key in ~/.ssh/
# 3. The user knows what they're doing.
# 4. Take some options:
# openrc file
# cluster name
# volume size
show_help() {
echo "Options:
-n: HEADNODE_NAME: required, name of the cluster to delete
-o: OPENRC_PATH: optional, path to a valid openrc file, defaults to ~/openrc.sh
-v: VOLUME_DELETE: optional flag, set to delete storage volumes, default false
-d: HEADNODE_DELETE: delete the headnode once the cluster is deleted
Usage: $0 -n <HEADNODE_NAME> -o [OPENRC_PATH] [-v] [-d]"
}
OPTIND=1
openrc_path="${HOME}/openrc.sh"
headnode_name="$(hostname --short)"
volume_delete="0"
headnode_delete="0"
while getopts ":hhelp:o:n:v:d" opt; do
case ${opt} in
h|help|\?) show_help
exit 0
;;
o) openrc_path=${OPTARG}
;;
n) headnode_name=${OPTARG}
;;
v) volume_delete=1
;;
d) headnode_delete=1
;;
:) echo "Option -$OPTARG requires an argument."
exit 1
;;
esac
done
if [[ ! -f ${openrc_path} ]]; then
echo "openrc path: ${openrc_path} \n does not point to a file!"
exit 1
elif [[ "${volume_delete}" != "0" && "${volume_delete}" != "1" ]]; then
echo "Volume_delete parameter must be 0 or 1 instead of ${volume_delete}"
exit 1
elif [[ "${headnode_delete}" != "0" && "${headnode_delete}" != "1" ]]; then
echo "Headnode_delete parameter must be 0 or 1 instead of ${headnode_delete}"
exit 1
fi
source ${openrc_path}
#There's only one of each thing floating around, potentially some compute instances that weren't cleaned up and some images... SO!
OS_PREFIX=${headnode_name}
OS_SSH_SECGROUP_NAME=${OS_PREFIX}-ssh-global
OS_INTERNAL_SECGROUP_NAME=${OS_PREFIX}-internal
OS_SLURM_KEYPAIR=${OS_PREFIX}-slurm-key
OS_KEYPAIR_NAME=${OS_PREFIX}-elastic-key
compute_nodes=$(openstack server list -f value -c Name | grep -E "compute-${headnode_name}-base-instance|${headnode_name}-compute" )
if [[ -n "${compute_nodes}" ]]; then
for node in "${compute_nodes}"
do
echo "Deleting compute node: ${node}"
openstack server delete ${node}
done
fi
sleep 5 # seems like there are issues with the network deleting correctly
SERVER_UUID=$(curl http://169.254.169.254/openstack/latest/meta_data.json | jq '.uuid' | sed -e 's#"##g')
openstack server remove security group ${SERVER_UUID} ${OS_SSH_SECGROUP_NAME} || true
openstack server remove security group ${SERVER_UUID} ${OS_INTERNAL_SECGROUP_NAME} || true
openstack security group delete ${OS_SSH_SECGROUP_NAME}
openstack security group delete ${OS_INTERNAL_SECGROUP_NAME}
openstack keypair delete ${OS_SLURM_KEYPAIR}
# We DO delete the elastic-key, since we created it from scratch before
openstack keypair delete ${OS_KEYPAIR_NAME}
headnode_images=$(openstack image list --private -f value -c Name | grep ${headnode_name}-compute-image- )
for image in "${headnode_images}"
do
openstack image delete ${image}
done
if [[ "${headnode_delete}" == "1" ]]; then
echo "DELETING HEADNODE: ${headnode_name}"
openstack server delete ${headnode_name}
fi