forked from Elastifile/gcp-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy_google_ilb.sh
executable file
·80 lines (71 loc) · 2.87 KB
/
destroy_google_ilb.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
#!/bin/bash
set -ux
usage() {
cat << E_O_F
Usage:
-z zone
-n network
-s subnet
-c cluster name
-a availability zones
-e service email
-p project
E_O_F
exit 1
}
#variables
LOG="destroy_google_ilb.log"
while getopts "h?:z:n:s:c:a:e:p:" opt; do
case "$opt" in
h|\?)
usage
exit 0
;;
z) ZONE=${OPTARG}
;;
n) NETWORK=${OPTARG}
;;
s) SUBNETWORK=${OPTARG}
;;
c) CLUSTER_NAME=${OPTARG}
;;
a) AVAILABILITY_ZONES=${OPTARG}
;;
e) SERVICE_EMAIL=${OPTARG}
;;
p) PROJECT=${OPTARG}
;;
esac
done
#capture computed variables
EMS_HOSTNAME="${CLUSTER_NAME}.local"
REGION=`echo $ZONE | awk -F- '{print $1"-"$2 }'`
echo "REGION: $REGION" | tee ${LOG}
echo "ZONE: $ZONE" | tee -a ${LOG}
echo "NETWORK: $NETWORK" | tee -a ${LOG}
echo "SUBNETWORK: $SUBNETWORK" | tee -a ${LOG}
echo "CLUSTER_NAME: $CLUSTER_NAME" | tee -a ${LOG}
#set -x
# Destroy Google Internal Load Balancer
function destroy_google_ilb {
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
gcloud compute firewall-rules delete $CLUSTER_NAME-allow-health-check --quiet --account=$SERVICE_EMAIL --project=$PROJECT
gcloud compute firewall-rules delete $CLUSTER_NAME-allow-internal-lb --quiet --account=$SERVICE_EMAIL --project=$PROJECT
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
gcloud compute forwarding-rules delete $CLUSTER_NAME-int-lb-forwarding-rule --region $REGION --quiet --account=$SERVICE_EMAIL --project=$PROJECT
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
for zone in ${AVAILABILITY_ZONES//,/ }; do
instances=`gcloud compute instances list --filter="zone:($zone)" --account=$SERVICE_EMAIL --project=$PROJECT | grep $CLUSTER_NAME- | awk '{ print $1"," }'`
instances=`echo $instances| tr -d ' '|sed s'/[,]$//'`
instance_group="$CLUSTER_NAME-$zone"
gcloud compute backend-services remove-backend $CLUSTER_NAME-int-lb --instance-group $instance_group --instance-group-zone $zone --region $REGION --quiet --account=$SERVICE_EMAIL --project=$PROJECT
gcloud compute instance-groups unmanaged remove-instances $instance_group --instances ${instances} --zone $zone --quiet --account=$SERVICE_EMAIL --project=$PROJECT
gcloud compute instance-groups unmanaged delete $instance_group --zone $zone --quiet --account=$SERVICE_EMAIL --project=$PROJECT
done
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
gcloud compute backend-services delete $CLUSTER_NAME-int-lb --region $REGION --quiet --account=$SERVICE_EMAIL --project=$PROJECT
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
gcloud compute health-checks delete $CLUSTER_NAME-tcp-health-check --quiet --account=$SERVICE_EMAIL --project=$PROJECT
}
# Main
destroy_google_ilb