-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdestroy_cluster.sh
executable file
·118 lines (96 loc) · 4.03 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# Ideally, we never have any bugs in cluster delete.
# However, even in this ideal scenario, we need
# protection from a patch under review breaking cluster delete
# and filling up our tenant with undeletable resources.
# In this case call the destroy script with `-f|--force`
CONFIG=${CONFIG:-cluster_config.sh}
if [ -r "$CONFIG" ]; then
# shellcheck disable=SC1090
source "${CONFIG}"
fi
ARTIFACT_DIR=clusters/${CLUSTER_NAME}
opts=$(getopt -n "$0" -o "fi:" --long "force,infra-id:" -- "$@")
eval set --"$opts"
while [[ $# -gt 0 ]]; do
case "$1" in
-f|--force)
FORCE=true
shift
;;
-i|--infra-id)
INFRA_ID=$2
shift 2
;;
*)
break
;;
esac
done
declare -r installer="${OPENSHIFT_INSTALLER:-openshift-install}"
# Remove entries from /etc/hosts and ssh config
if sudo -l sed /etc/hosts &>/dev/null; then
sudo sed -i "/# Generated by shiftstack for $CLUSTER_NAME - Do not edit/,/# End of $CLUSTER_NAME nodes/d" /etc/hosts
fi
if [[ -w "${HOME}/.ssh/config" ]]; then
sed -i "/# Generated by shiftstack for $CLUSTER_NAME - Do not edit/,/# End of $CLUSTER_NAME nodes/d" "$HOME"/.ssh/config
fi
if [ -n "$INFRA_ID" ]; then
TMP_DIR=$(mktemp -d -t shiftstack-XXXXXXXXXX)
echo "{\"clusterName\":\"$CLUSTER_NAME\",\"infraID\":\"$INFRA_ID\",\"openstack\":{\"cloud\":\"$OS_CLOUD\",\"identifier\":{\"openshiftClusterID\":\"$INFRA_ID\"}}}" > "$TMP_DIR"/metadata.json
fi
if [[ $FORCE == true ]]; then
echo Destroying cluster using openstack cli
if [ -z "$INFRA_ID" ] && [ -f "$ARTIFACT_DIR"/metadata.json ]; then
# elements created by the cluster are named $CLUSTER_NAME-hash by the installer
INFRA_ID=$(jq .infraID "$ARTIFACT_DIR"/metadata.json | sed "s/\"//g")
fi
if [ -z "$INFRA_ID" ]; then
echo "Could not find infrastructure id."
echo "You may specify it with -i|--infra-id option to the script."
exit 1
fi
openstack server list -c ID -f value --name "$INFRA_ID" | xargs --no-run-if-empty openstack server delete
openstack router remove subnet "$INFRA_ID"-external-router "$INFRA_ID"-service
openstack router remove subnet "$INFRA_ID"-external-router "$INFRA_ID"-nodes
# delete interfaces from the router
openstack network trunk list -c Name -f value | grep "$INFRA_ID" | xargs --no-run-if-empty openstack network trunk delete
openstack port list --network "$INFRA_ID"-openshift -c ID -f value | xargs --no-run-if-empty openstack port delete
# delete interfaces from the router
PORT=$(openstack router show "$INFRA_ID"-external-router -c interfaces_info -f value | cut -d '"' -f 12)
if [ -n "$PORT" ]; then
openstack router remove port "$INFRA_ID"-external-router "$PORT"
fi
openstack router unset --external-gateway "$INFRA_ID"-external-router
openstack router delete "$INFRA_ID"-external-router
# IPI network
openstack network delete "$INFRA_ID"-openshift
# UPI network
openstack network delete "$INFRA_ID"-network
openstack security group delete "$INFRA_ID"-api
openstack security group delete "$INFRA_ID"-master
openstack security group delete "$INFRA_ID"-worker
openstack server group delete "$INFRA_ID"-master
openstack server group delete "$INFRA_ID"-worker
for c in $(openstack container list -f value); do
echo "$c"
openstack container show "$c" | grep "$INFRA_ID"
# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
CONTAINER=$c
fi
done
if [ -n "$CONTAINER" ]; then
openstack object list -f value "$CONTAINER" | xargs --no-run-if-empty openstack object delete "$CONTAINER"
openstack container delete "$CONTAINER"
fi
else
echo Destroying cluster using openshift-install
timeout 900 "$installer" --log-level=debug destroy cluster --dir "${TMP_DIR:-$ARTIFACT_DIR}"
if [ $? -eq "124" ]; then
echo "Timeout to destroy cluster after 15 min"
fi
fi
if [ -n "$TMP_DIR" ]; then
rm -rf "$TMP_DIR"
fi