-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.sh
executable file
·307 lines (260 loc) · 8.57 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
# This script is **destructive** and will delete a lot of IBM
# Cloud resources. Use with caution!
# In order to use this script you must have installed the IBM
# Cloud CLI. If you need to do that run the following:
# curl -fsSL https://clis.cloud.ibm.com/install/linux | sh
# Once installed, this script uses several plugins, to
# install them run the following commands:
# ibmcloud plugin install -f code-engine
# ibmcloud plugin install -f container-registry
# ibmcloud plugin install -f container-service
# ibmcloud plugin install -f cloud-functions
# ibmcloud plugin install -f infrastructure-service
# ibmcloud plugin install -f schematics
# ibmcloud plugin install -f sdk-gen
# Lastly, you must be authenticated and have targetted
# a resource group. To do so, run the following:
# ibmcloud login
# ibmcloud target -g <resource-group>
usage() { echo "Usage: $0 [-n] [-c <config-file>]" 1>&2; exit 1; }
NO_DRY_RUN=''
CONFIG_FILE=''
while getopts "nc:" o; do
case "${o}" in
n)
n=${OPTARG}
NO_DRY_RUN=1
;;
c)
c=${OPTARG}
CONFIG_FILE=$c
;;
*)
usage
;;
esac
done
if [ "$CONFIG_FILE" ]; then
echo "Attempting to use config file at location: $CONFIG_FILE"
# Ensure config file exists
if ! test -f ${CONFIG_FILE};
then
echo "specified config file ${CONFIG_FILE} not found"
exit 1
fi
else
CONFIG_FILE='.ibmcloud-nuke'
echo "Attempting to use config file at default location: $CONFIG_FILE"
fi
if [ "${NO_DRY_RUN}" ]; then
echo "The (-n) flag was found. Will begin to delete all resources."
NO_DRY_RUN=1
else
echo "No (-n) flag found. Will NOT delete any resources."
NO_DRY_RUN=0
fi
echo "================================="
echo "NO_DRY_RUN = ${NO_DRY_RUN}"
echo "CONFIG_FILE = ${CONFIG_FILE}"
echo "================================="
# Usage: get_or_create_user <name_or_id>
function check_config_file() {
if test -f ${CONFIG_FILE} && grep -Fxq "${1}" ${CONFIG_FILE}
then
echo "skipping ${name} as it exists in ${CONFIG_FILE}"
continue
fi
}
# clusters
echo "================================="
echo "Clusters: "
echo "================================="
ibmcloud ks clusters -q | tail -n+2 | while read -r name rest_of_cmd ; do
echo "${name}"
check_config_file "${name}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud ks cluster rm -f -c ${name}
fi
done
# namespaces
echo "================================="
echo "Container Registry (namespaces): "
echo "================================="
ibmcloud cr namespaces | tail -n+4 | while read -r name rest_of_cmd ; do
echo "${name}"
check_config_file "${name}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud cr namespace-rm -f ${name}
fi
done
## IBM Cloud CLI Developer tools are deprecated and end-of-support 31st March, 2023
# # apps
# echo "================================="
# echo "Applications: "
# echo "================================="
# ibmcloud dev list | tail -n+8 | while read -r name rest_of_cmd ; do
# echo "${name}"
# check_config_file "${name}"
# if [ "${NO_DRY_RUN}" == 1 ]; then
# ibmcloud dev delete -f ${name}
# fi
# done
# services - use awk here as the service name can include spaces
echo "================================="
echo "Services: "
echo "================================="
ibmcloud resource service-instances | tail -n+4 | awk -F ' +' '{print $1}' | while read -r name; do
echo "${name}"
check_config_file "${name}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud resource service-instance-delete -f --recursive "${name}"
fi
done
# baremetal
echo "================================="
echo "Classic Baremetal: "
echo "================================="
ibmcloud sl hardware list | grep -v 'kube-' | tail -n+2 | while read -r id rest_of_cmd ; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud sl hardware cancel -f ${id}
fi
done
# VMs
echo "================================="
echo "Classic VMs: "
echo "================================="
ibmcloud sl vs list | grep -v 'kube-' | tail -n+2 | while read -r id rest_of_cmd ; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud sl vs cancel -f ${id}
fi
done
# File volumes
echo "================================="
echo "Classic File Volumes: "
echo "================================="
ibmcloud sl file volume-list | tail -n+2 | while read -r id rest_of_cmd ; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud sl file volume-cancel --immediate -f ${id}
fi
done
# Block volumes
echo "================================="
echo "Classic Block Volumes: "
echo "================================="
ibmcloud sl block volume-list | tail -n+2 | while read -r id rest_of_cmd ; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud sl block volume-cancel --immediate -f ${id}
fi
done
# code engine
echo "================================="
echo "Code Engine (Projects):"
echo "================================="
ibmcloud ce project list | tail -n+5 | while read -r name rest_of_cmd ; do
echo "${name}"
check_config_file "${name}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud ce project delete -f --name ${name}
fi
done
# functions - use awk because namespaces can have spaces
echo "================================="
echo "Cloud Functions (namespaces): "
echo "================================="
ibmcloud fn namespace list | tail -n+3 | awk -F ' +' '{print $1}' | while read -r name; do
echo "${name}"
check_config_file "${name}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud fn namespace delete "${name}"
fi
done
# satellite locations (cannot contain a space)
echo "================================="
echo "Satellite locations: "
echo "================================="
ibmcloud sat location ls | tail -n+4 | while read -r name rest_of_cmd; do
echo "${name}"
check_config_file "${name}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud sat location rm --location "${name}" -f
fi
done
# Virtual Private Cloud VM (VSI) (cannot contain a space)
echo "================================="
echo "VPC VSI: "
echo "================================="
ibmcloud is ins | tail -n+3 | while read -r id rest_of_cmd; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud is ind "${id}" -f
fi
done
# Virtual Private Cloud subnets (cannot contain a space)
echo "================================="
echo "VPC Subnets "
echo "================================="
ibmcloud is subnets | tail -n+3 | while read -r id rest_of_cmd; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
pgwid=$(ibmcloud is subnet-public-gateway "${id}" --output json | jq -r ".id")
if [ "${pgwid}" ]; then
ibmcloud is subnet-pubgwd "${id}" -f
fi
ibmcloud is subnetd "${id}" -f
fi
done
# Virtual Private Cloud public gateway (cannot contain a space)
echo "================================="
echo "VPC Public Gateways "
echo "================================="
ibmcloud is pubgws | tail -n+3 | while read -r id rest_of_cmd; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud is pubgwd "${id}" -f
fi
done
# Virtual Private Clouds (VPCs) (cannot contain a space)
echo "================================="
echo "Virtual Private Clouds: "
echo "================================="
ibmcloud is vpcs | tail -n+3 | while read -r id rest_of_cmd; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud is vpcd "${id}" -f
fi
done
# Virtual Private Cloud Block Storage (cannot contain a space)
echo "================================="
echo "VPC Block Storage "
echo "================================="
ibmcloud is vols | tail -n+3 | while read -r id rest_of_cmd; do
echo "${id}"
check_config_file "${id}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud is vold "${id}" -f
fi
done
# api keys - skip the keys that say "Do not delete"
echo "================================="
echo "API Keys: "
echo "================================="
ibmcloud iam api-keys | tail -n+4 | grep -v 'Do not delete' | while read -r name rest_of_cmd; do
echo "${name}"
check_config_file "${name}"
if [ "${NO_DRY_RUN}" == 1 ]; then
ibmcloud iam api-key-delete ${name}
fi
done