forked from HSBawa/icp-ce-on-linux-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestroy-cluster.sh
executable file
·97 lines (84 loc) · 3.05 KB
/
destroy-cluster.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
#!/bin/bash
function read_properties() {
while IFS== read -r KEY VALUE
do
if [[ "${KEY:0:1}" =~ ^[A-Z]$ ]]; then
export "$KEY=$VALUE"
fi
done < ./install.properties
}
function clear_haproxy_cfg(){
## Clear existing configuration
if grep -Fxq "${HAPROXY_ICP_MARKER_START}" ${HAPROXY_CFG} && grep -Fxq "${HAPROXY_ICP_MARKER_END}" ${HAPROXY_CFG}
then
echo "Configuration exists. Overwriting ..."
lineNumStart="$(grep -n "${HAPROXY_ICP_MARKER_START}" ${HAPROXY_CFG} | head -n 1 | cut -d: -f1)"
lineNumEnd="$(grep -n "${HAPROXY_ICP_MARKER_END}" ${HAPROXY_CFG} | head -n 1 | cut -d: -f1)"
sudo sed -i "${lineNumStart},${lineNumEnd}d" ${HAPROXY_CFG}
sudo systemctl restart haproxy.service
if [[ $? -ne 0 ]]; then
echo "ERROR!!! HAProxy service restart failed."
fi
fi
}
function destory_cluster(){
if [[ ! -z ${ICP_ENV_NAME_SHORT} ]]; then
env=${ICP_ENV_NAME_SHORT}
vms="$(lxc list ${env}- -c n --format=csv)"
profiles="$(lxc profile list | grep ${env} | awk '{print $2}')"
networks="$(lxc network list | grep ${env} | awk '{print $2}')"
lineNumStart="$(grep -n "${HAPROXY_ICP_MARKER_START}" ${HAPROXY_CFG} | head -n 1 | cut -d: -f1)"
if [[ $? -ne 0 ]] || [[ -z ${lineNumStart} ]]; then
lineNumStart=0
fi
lineNumEnd="$(grep -n "${HAPROXY_ICP_MARKER_END}" ${HAPROXY_CFG} | head -n 1 | cut -d: -f1)"
if [[ $? -ne 0 ]] || [[ -z ${lineNumEnd} ]]; then
lineNumEnd=0
fi
if [[ ! -z ${vms} ]] || [[ ! -z ${profiles} ]] || [[ ! -z ${networks} ]] || [[ ${lineNumStart} -gt 0 ]] || [[ ${lineNumEnd} -gt 0 ]]; then
echo "This program will delete following IBM Cloud Private [$env] cluster LXD components."
if [[ ! -z "${vms}" ]]; then
echo "Containers: $vms"
fi
if [[ ! -z ${profiles} ]]; then
echo "Profile: $profiles"
fi
if [[ ! -z ${networks} ]]; then
echo "Networks: $networks"
fi
if [[ ${lineNumStart} -gt 0 ]] || [[ ${lineNumEnd} -gt 0 ]]; then
echo "ICP HAPRoxy configuration."
fi
echo "Press Ctrl-C to cancel now OR in next 10 seconds they are gone ...."
sleep 10
for vm in ${vms[*]}
do
# echo "Deleting container: ${vm}"
lxc stop -f $vm ; lxc delete -f $vm
done
echo ""
for profile in ${profiles[*]}
do
# echo "Deleting profile: ${profile}"
lxc profile delete $profile
done
echo ""
for network in ${networks[*]}
do
# echo "Deleting network: ${network}"
lxc network delete $network
done
echo ""
echo "Cleaning ICP HAProxy configuration:"
clear_haproxy_cfg
echo ""
echo "Done"
else
echo "There are no LXD components to delete at this time. Bye."
fi
else
echo "Invalid environment name: ${ICP_ENV_NAME_SHORT}. Please try again with valid environment name."
fi
}
read_properties
destory_cluster