forked from officialmofabs/modern-data-warehouse-dataops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.sh
executable file
·333 lines (312 loc) · 12 KB
/
destroy.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env bash
DEPLOYMENT_PREFIX=${DEPLOYMENT_PREFIX:-}
AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID:-}
AZURE_RESOURCE_GROUP_NAME=${AZURE_RESOURCE_GROUP_NAME:-}
AZURE_RESOURCE_GROUP_LOCATION=${AZURE_RESOURCE_GROUP_LOCATION:-}
DELETE_RESOURCE_GROUP=${DELETE_RESOURCE_GROUP:-}
if [[ -z "$DEPLOYMENT_PREFIX" ]]; then
echo "No deployment prefix [DEPLOYMENT_PREFIX] specified."
exit 1
fi
if [[ -z "$AZURE_SUBSCRIPTION_ID" ]]; then
echo "No Azure subscription id [AZURE_SUBSCRIPTION_ID] specified."
exit 1
fi
if [[ -z "$AZURE_RESOURCE_GROUP_NAME" ]]; then
echo "No Azure resource group [AZURE_RESOURCE_GROUP_NAME] specified."
exit 1
fi
if [[ -z "$AZURE_RESOURCE_GROUP_LOCATION" ]]; then
echo "No Azure resource group [AZURE_RESOURCE_GROUP_LOCATION] specified."
echo "Default location will be set to -> westus"
AZURE_RESOURCE_GROUP_LOCATION="westus"
fi
# Login to Azure and select the subscription
if ! AZURE_USERNAME=$(az account show --query user.name --output tsv); then
echo "No Azure account logged in, now trying to log in."
az login --output none
az account set --subscription "$AZURE_SUBSCRIPTION_ID"
else
echo "Logged in as $AZURE_USERNAME, set the active subscription to \"$AZURE_SUBSCRIPTION_ID\""
az account set --subscription "$AZURE_SUBSCRIPTION_ID"
fi
# Check the resource group and region
RG_EXISTS=$(az group exists --resource-group "$AZURE_RESOURCE_GROUP_NAME" --output tsv)
if [[ $RG_EXISTS == "false" ]]; then
echo "Error: Resource group $AZURE_RESOURCE_GROUP_NAME in $AZURE_RESOURCE_GROUP_LOCATION does not exist."
else
echo "Resource group $AZURE_RESOURCE_GROUP_NAME exists in $AZURE_RESOURCE_GROUP_LOCATION. Removing created resources"
fi
# Name references
adbWorkspaceName="${DEPLOYMENT_PREFIX}adb01"
keyVaultName="${DEPLOYMENT_PREFIX}akv01"
storageAccountName="${DEPLOYMENT_PREFIX}asa01"
spokeVnetName="${DEPLOYMENT_PREFIX}spokeVnet01"
hubVnetName="${DEPLOYMENT_PREFIX}hubVnet01"
securityGroupName="${DEPLOYMENT_PREFIX}nsg01"
routeTableName="${DEPLOYMENT_PREFIX}FWRT01"
firewallName="${DEPLOYMENT_PREFIX}HubFW01"
ipAddressName="${DEPLOYMENT_PREFIX}FWIP01"
keyVaultPrivateEndpoint="${DEPLOYMENT_PREFIX}akv01privateendpoint"
storageAccountPrivateEndpoint="${DEPLOYMENT_PREFIX}asa01privateendpoint"
echo "Delete Resouce Group? $DELETE_RESOURCE_GROUP"
if [[ $DELETE_RESOURCE_GROUP == true ]]; then
echo "Deleting resource group: $AZURE_RESOURCE_GROUP_NAME with all the resources. In 5 seconds..."
sleep 5s
az group delete --resource-group "$AZURE_RESOURCE_GROUP_NAME" --output none --yes
echo "Purging key vault..."
az keyvault purge --subscription "$AZURE_SUBSCRIPTION_ID" --name "$keyVaultName" --output none
else
echo "The following resources will be deleted:"
echo "ADB Workspace: $adbWorkspaceName"
echo "Key Vault: $keyVaultName"
echo "Storage Account: $storageAccountName"
echo "Spoke Virtual Network: $spokeVnetName"
echo "Hub Virtual Network: $hubVnetName"
echo "Network Security Group: $securityGroupName"
echo "Routing Table: $routeTableName"
echo "Firewall: $firewallName"
echo "Public IP Address: $ipAddressName"
echo "Validating ADB workspace..."
if az databricks workspace show \
--name "$adbWorkspaceName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting ADB workspace..."
{ az databricks workspace delete \
--name "$adbWorkspaceName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--yes &&
echo "Successfully deleted ADB workspace."; } ||
{
echo "Failed to delete ADB workspace."
exit 1
}
else
echo "$adbWorkspaceName was not found."
fi
echo "Validating Key Vault..."
if az keyvault show \
--name "$keyVaultName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Key Vault..."
{ az keyvault delete \
--name "$keyVaultName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
az keyvault purge \
--subscription "$AZURE_SUBSCRIPTION_ID" \
--name "$keyVaultName" &&
echo "Successfully deleted and purged Key Vault."; } ||
{
echo "Failed to delete and purge Key Vault."
exit 1
}
else
echo "$keyVaultName was not found."
fi
echo "Validating Storage Account..."
if az storage account show \
--name "$storageAccountName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Storage Account..."
{ az storage account delete \
--name "$storageAccountName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--yes &&
echo "Successfully deleted Storage Account."; } ||
{
echo "Failed to delete Storage Account."
exit 1
}
else
echo "$storageAccountName was not found."
fi
echo "Validating Private Endpoint for Key Vault"
if az network private-endpoint show \
--name "$keyVaultPrivateEndpoint" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Private Endpoint for Key Vault"
{
az network private-endpoint delete \
--name "$keyVaultPrivateEndpoint" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
echo "Successfully deleted Private Endpoint for Key Vault."
} ||
{
echo "Failed to delete Private Endpoint for Key Vault."
exit 1
}
else
echo "$keyVaultPrivateEndpoint was not found."
fi
echo "Validating Private Endpoint for Key Vault"
if az network private-endpoint show \
--name "$storageAccountPrivateEndpoint" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Private Endpoint for Key Vault"
{
az network private-endpoint delete \
--name "$storageAccountPrivateEndpoint" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
echo "Successfully deleted Private Endpoint for Key Vault."
} ||
{
echo "Failed to delete Private Endpoint for Key Vault."
exit 1
}
else
echo "$storageAccountPrivateEndpoint was not found."
fi
echo "Validating Firewall..."
if az network firewall show \
--name "$firewallName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Firewall..."
{ az network firewall delete \
--name "$firewallName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
echo "Successfully deleted Firewall."; } ||
{
echo "Failed to delete Firewall."
exit 1
}
else
echo "$firewallName was not found."
fi
echo "Validating Public-IP..."
if az network public-ip show \
--name "$ipAddressName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Public-IP..."
{ az network public-ip delete \
--name "$ipAddressName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
echo "Successfully deleted Public-IP."; } ||
{
echo "Failed to delete Public-IP."
exit 1
}
else
echo "$ipAddressName was not found."
fi
echo "Validating Spoke Virtual Network..."
if az network vnet show \
--name "$spokeVnetName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Spoke Virtual Network..."
{ az network vnet delete \
--name "$spokeVnetName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
echo "Successfully deleted Spoke Virtual Network."; } ||
{
echo "Failed to delete Spoke Virtual Network."
exit 1
}
else
echo "$spokeVnetName was not found."
fi
echo "Validating Hub Virtual Network..."
if az network vnet show \
--name "$hubVnetName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Hub Virtual Network..."
{ az network vnet delete \
--name "$hubVnetName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
echo "Successfully deleted Hub Virtual Network."; } ||
{
echo "Failed to delete Hub Virtual Network."
exit 1
}
else
echo "$hubVnetName was not found."
fi
echo "Validating Network Security Group..."
if az network nsg show \
--name "$securityGroupName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Network Security Group..."
{ az network nsg delete \
--name "$securityGroupName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
echo "Successfully deleted Network Security Group."; } ||
{
echo "Failed to delete Network Security Group."
exit 1
}
else
echo "$securityGroupName was not found."
fi
echo "Validating Route table..."
if az network route-table show \
--name "$routeTableName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Route table..."
{ az network route-table delete \
--name "$routeTableName" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" &&
echo "Successfully deleted Route table."; } ||
{
echo "Failed to delete Route table."
exit 1
}
else
echo "$routeTableName was not found."
fi
echo "Validating Private DNS Zone for Key Vault..."
if az network private-dns zone show \
--name "privatelink.vaultcore.azure.net" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Private DNS Zone for Key Vault..."
counter=0
while :; do
az network private-dns zone delete \
--name "privatelink.vaultcore.azure.net" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--yes \
--output none &&
echo "Successfully deleted Private DNS Zone for Key Vault" && break ||
echo "Delete failed retrying ..." && ((counter++)) && sleep 10
if [[ "$counter" == '3' ]]; then
echo "Failed to delete Private DNS Zone for Key Vault"
exit 1
fi
done
else
echo "privatelink.vaultcore.azure.net was not found."
fi
echo "Validating Private DNS Zone for Storage Account..."
if az network private-dns zone show \
--name "privatelink.dfs.core.windows.net" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--output none; then
echo "Deleting Private DNS Zone for Storage Account..."
counter=0
while :; do
az network private-dns zone delete \
--name "privatelink.dfs.core.windows.net" \
--resource-group "$AZURE_RESOURCE_GROUP_NAME" \
--yes \
--output none &&
echo "Successfully deleted Private DNS Zone for Storage Account" && break ||
echo "Delete failed retrying ..." && ((counter++)) && sleep 10
if [[ "$counter" == '3' ]]; then
echo "Failed to delete Private DNS Zone for Storage Account"
exit 1
fi
done
else
echo "privatelink.dfs.core.windows.net was not found."
fi
fi