-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcleanup.sh
executable file
·115 lines (101 loc) · 3.13 KB
/
cleanup.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
#!/bin/bash
# Exit on any error
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# Configuration variables (matching deploy.sh)
PROJECT_ID=""
REGION="us-central1"
ZONE="${REGION}-a"
INSTANCE_NAME="ads-monitoring-instance"
DISK_NAME="${INSTANCE_NAME}-data"
# Function to print step description
print_step() {
echo -e "\n${GREEN}=== $1 ===${NC}\n"
}
# Function to print error and exit
error_exit() {
echo -e "${RED}Error: $1${NC}" >&2
exit 1
}
# Split each cleanup task into separate functions
cleanup_mig() {
print_step "Deleting Managed Instance Group"
if gcloud compute instance-groups managed describe "$INSTANCE_NAME-group" --zone="$ZONE" --project="$PROJECT_ID" &>/dev/null; then
gcloud compute instance-groups managed delete "$INSTANCE_NAME-group" \
--zone="$ZONE" \
--project="$PROJECT_ID" \
--quiet
echo "Managed Instance Group deleted"
else
echo "Managed Instance Group not found, skipping"
fi
}
cleanup_template() {
print_step "Deleting Instance Template"
if gcloud compute instance-templates describe "$INSTANCE_NAME-template" --project="$PROJECT_ID" &>/dev/null; then
gcloud compute instance-templates delete "$INSTANCE_NAME-template" \
--project="$PROJECT_ID" \
--quiet
echo "Instance Template deleted"
else
echo "Instance Template not found, skipping"
fi
}
cleanup_firewall() {
print_step "Deleting Firewall Rules"
if gcloud compute firewall-rules describe "allow-monitoring-ports" --project="$PROJECT_ID" &>/dev/null; then
gcloud compute firewall-rules delete "allow-monitoring-ports" \
--project="$PROJECT_ID" \
--quiet
echo "Firewall rules deleted"
else
echo "Firewall rules not found, skipping"
fi
}
cleanup_disk() {
print_step "Deleting Persistent Disk"
if gcloud compute disks describe "$DISK_NAME" --zone="$ZONE" --project="$PROJECT_ID" &>/dev/null; then
gcloud compute disks delete "$DISK_NAME" \
--zone="$ZONE" \
--project="$PROJECT_ID" \
--quiet
echo "Persistent disk deleted"
else
echo "Persistent disk not found, skipping"
fi
}
cleanup() {
print_step "Starting cleanup of Ads Monitor resources"
# Get project ID from gcloud config
PROJECT_ID=$(gcloud config get-value project 2> /dev/null)
echo "Using project: $PROJECT_ID"
case "${1:-all}" in # Use first argument, default to 'all' if none provided
"mig")
cleanup_mig
;;
"template")
cleanup_template
;;
"firewall")
cleanup_firewall
;;
"disk")
cleanup_disk
;;
"all" | "")
cleanup_mig
cleanup_template
cleanup_firewall
cleanup_disk
;;
*)
error_exit "Invalid component. Available components: mig, template, firewall, disk, all"
;;
esac
print_step "Cleanup complete!"
}
# Run the cleanup function with first argument
cleanup "$1"