-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy-cluster.sh
executable file
·127 lines (112 loc) · 3.28 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
119
120
121
122
123
124
125
126
127
#!/bin/bash
#############
# Variables #
#############
# Get directory of script for locating templates and config
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Source application variables
source $DIR/settings.sh
# Source variables
if [ -z "${CONFIG}" ] ; then
source $DIR/configs/default.sh
else
config_path=$DIR/configs/$CONFIG.sh
if [ -f $config_path ] ; then
source $config_path
else
echo "Error loading $CONFIG"
echo "Could not load $config_path"
exit 1
fi
fi
CLUSTERS="$(ls /opt/flight/clusters/)"
CLUSTERNAME="$1"
CLUSTERNAMEMATCHES="$(echo "$CLUSTERS" |grep $CLUSTERNAME)"
CLUSTERNAMECOUNT="$(echo "$CLUSTERS" |grep $CLUSTERNAME -c)"
#############
# Functions #
#############
function destroy_cluster {
cluster=$1
case $PLATFORM in
"azure")
check_cluster_azure $cluster
destroy_cluster_azure $cluster
;;
"aws")
check_cluster_aws $cluster
destroy_cluster_aws $cluster
;;
*)
echo "Unrecognised platform, check config.sh"
exit 1
;;
esac
rm -f /opt/flight/clusters/$cluster
}
function check_cluster_azure {
cluster=$1
if ! az group show --name $cluster &>/dev/null ; then
cluster_error
fi
}
function destroy_cluster_azure {
cluster=$1
az group delete -y --name $cluster
az network dns record-set a delete --resource-group $AZURE_DOMAIN_RG --zone-name $AZURE_DOMAIN --name "chead1.$cluster" -y
}
function check_cluster_aws {
cluster=$1
if ! aws cloudformation describe-stacks --stack-name $cluster --region $AWS_LOCATION &>/dev/null ; then
cluster_error
fi
}
function destroy_cluster_aws {
cluster=$1
aws cloudformation delete-stack --stack-name $cluster --region $AWS_LOCATION
cat << EOF > /tmp/$cluster-dns-remove.json
{
"Changes": [
{
"Action": "DELETE",
"ResourceRecordSet": {
"Name": "chead1.${cluster}.${AWS_DOMAIN}",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "$(aws cloudformation describe-stack-resources --region "$AWS_LOCATION" --stack-name $cluster --logical-resource-id chead1pubIP |grep PhysicalResourceId |awk '{print $2}' |tr -d , | tr -d \")"
}
]
}
}
]
}
EOF
aws route53 change-resource-record-sets --hosted-zone-id $AWS_DOMAIN_ID --change-batch file:///tmp/$cluster-dns-remove.json
rm -f /tmp/$cluster-dns-remove.json
}
function cluster_error {
echo "Cannot find $CLUSTERNAME in specified config."
echo "Perhaps the wrong config has been used or the cluster has been deleted outside of the builder?"
echo
echo "Exiting without action"
exit 1
}
#################
# Run Functions #
#################
case $CLUSTERNAMECOUNT in
0)
echo "Cluster $CLUSTERNAME doesn't exist"
;;
1)
destroy_cluster $CLUSTERNAMEMATCHES
;;
*)
echo "More than one match for clustername:"
echo "$CLUSTERNAMEMATCHES"
echo
echo "Identify the seed for the cluster (check the logs) to narrow down which cluster to delete"
;;
esac