forked from microsoft/BotFramework-FunctionalTests
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcleanupResources.yml
365 lines (346 loc) · 14.7 KB
/
cleanupResources.yml
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#
# Deletes resources from Deploy & Shared Pipelines for the Skills Functional Tests.
#
name: $(BUILD.BUILDID)
trigger: none
pr: none
variables:
## Azure Resources (Define these variables in Azure)
# AzureSubscription: Service Connection Name to Manage Azure resources.
# AppServicePlanDotNetName: Name of the App Service Plan for DotNet bots.
# AppServicePlanJSName: Name of the App Service Plan for JavaScript bots.
# AppServicePlanPythonName: Name of the App Service Plan for Python bots.
# DeployResourceGroup: (optional) Name of the Resource Group containing the bots.
# ResourceSuffix: (optional) Alphanumeric suffix to add to the resources' name to avoid collisions.
# SharedResourceGroup: (optional) Name of the Resource Group containing the shared resources.
## Internal variables
InternalAppInsightsName: "bffnappinsights$($env:RESOURCESUFFIX)"
InternalAppServicePlanDotNetName: $(APPSERVICEPLANDOTNETNAME)$($env:RESOURCESUFFIX)
InternalAppServicePlanJSName: $(APPSERVICEPLANJSNAME)$($env:RESOURCESUFFIX)
InternalAppServicePlanPythonName: $(APPSERVICEPLANPYTHONNAME)$($env:RESOURCESUFFIX)
InternalCosmosDBName: "bffnbotstatedb$($env:RESOURCESUFFIX)"
InternalContainerRegistryName: "bffncontainerregistry$($env:RESOURCESUFFIX)"
InternalKeyVaultName: "bffnbotkeyvault$($env:RESOURCESUFFIX)"
InternalBotResourceGroupName: $[coalesce(variables['DEPLOYRESOURCEGROUP'], 'BFFN')]
InternalSharedResourceGroupName: $[coalesce(variables['SHAREDRESOURCEGROUP'], 'BFFN-Shared')]
InternalVirtualNetworkName: "bffnvirtualnetwork$($env:RESOURCESUFFIX)"
InternalStorageAccountName: "bffnstorageaccount$($env:RESOURCESUFFIX)"
pool:
vmImage: "windows-2022"
stages:
- stage: "Delete_DotNet_Resource_Group"
displayName: "Delete DotNet's Resource Group"
dependsOn: []
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- template: ../common/deleteResourceGroup.yml
parameters:
azureSubscription: $(AZURESUBSCRIPTION)
name: "$(INTERNALBOTRESOURCEGROUPNAME)-DotNet"
- stage: "Delete_JS_Resource_Group"
displayName: "Delete JS's Resource Group"
dependsOn: []
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- template: ../common/deleteResourceGroup.yml
parameters:
azureSubscription: $(AZURESUBSCRIPTION)
name: "$(INTERNALBOTRESOURCEGROUPNAME)-JS"
- stage: "Delete_Python_Resource_Group"
displayName: "Delete Python's Resource Group"
dependsOn: []
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- template: ../common/deleteResourceGroup.yml
parameters:
azureSubscription: $(AZURESUBSCRIPTION)
name: "$(INTERNALBOTRESOURCEGROUPNAME)-Python"
- stage: "Delete_App_Service_Plan_DotNet"
displayName: "Delete App Service Plan (DotNet)"
dependsOn:
- Delete_DotNet_Resource_Group
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete App Service Plan (DotNet)"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Write-Host "Looking for $(INTERNALAPPSERVICEPLANDOTNETNAME)..."
$exists = az appservice plan show --name "$(INTERNALAPPSERVICEPLANDOTNETNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)"
if ($exists) {
Write-Host "Deleting $(INTERNALAPPSERVICEPLANDOTNETNAME)..."
az appservice plan delete --name "$(INTERNALAPPSERVICEPLANDOTNETNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)" --yes
Start-Sleep -Milliseconds 3000
} else {
Write-Host "No pre-existing $(INTERNALAPPSERVICEPLANDOTNETNAME) resource found."
}
- stage: "Delete_App_Service_Plan_JS"
displayName: "Delete App Service Plan (JS)"
dependsOn:
- Delete_JS_Resource_Group
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete App Service Plan (JS)"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Write-Host "Looking for $(INTERNALAPPSERVICEPLANJSNAME)..."
$exists = az appservice plan show --name "$(INTERNALAPPSERVICEPLANJSNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)"
if ($exists) {
Write-Host "Deleting $(INTERNALAPPSERVICEPLANJSNAME)..."
az appservice plan delete --name "$(INTERNALAPPSERVICEPLANJSNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)" --yes
Start-Sleep -Milliseconds 3000
} else {
Write-Host "No pre-existing $(INTERNALAPPSERVICEPLANJSNAME) resource found."
}
- stage: "Delete_App_Service_Plan_Python"
displayName: "Delete App Service Plan (Python)"
dependsOn:
- Delete_Python_Resource_Group
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete App Service Plan (Python)"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Write-Host "Looking for $(INTERNALAPPSERVICEPLANPYTHONNAME)..."
$exists = az appservice plan show --name "$(INTERNALAPPSERVICEPLANPYTHONNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)"
if ($exists) {
Write-Host "Deleting $(INTERNALAPPSERVICEPLANPYTHONNAME)..."
az appservice plan delete --name "$(INTERNALAPPSERVICEPLANPYTHONNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)" --yes
Start-Sleep -Milliseconds 3000
} else {
Write-Host "No pre-existing $(INTERNALAPPSERVICEPLANPYTHONNAME) resource found."
}
- stage: "Delete_Virtual_Network"
displayName: "Delete Virtual Network"
dependsOn:
- Delete_App_Service_Plan_DotNet
- Delete_App_Service_Plan_JS
- Delete_App_Service_Plan_Python
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete Virtual Network"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Write-Host "Looking for $(INTERNALVIRTUALNETWORKNAME)..."
$exists = az network vnet show --name "$(INTERNALVIRTUALNETWORKNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)"
if ($exists) {
Write-Host "Deleting $(INTERNALVIRTUALNETWORKNAME)..."
az network vnet delete --name "$(INTERNALVIRTUALNETWORKNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)"
Start-Sleep -Milliseconds 3000
} else {
Write-Host "No pre-existing $(INTERNALVIRTUALNETWORKNAME) resource found."
}
- stage: "Delete_App_Registrations"
displayName: "Delete App Registrations"
dependsOn: []
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- template: deleteAppRegistrations.yml
parameters:
azureSubscription: "$(AZURESUBSCRIPTION)"
keyVault: "$(INTERNALKEYVAULTNAME)"
resourceGroup: "$(INTERNALSHAREDRESOURCEGROUPNAME)"
- stage: "Delete_Key_Vault"
displayName: "Delete Key Vault"
dependsOn:
- Delete_App_Registrations
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete Key Vault"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Write-Host "Looking for $(INTERNALKEYVAULTNAME)..."
$exists = az keyvault list --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)" --query "[?name == '$(INTERNALKEYVAULTNAME)']" | ConvertFrom-Json
if ($exists) {
Write-Host "Deleting $(INTERNALKEYVAULTNAME)..."
az keyvault delete --name "$(INTERNALKEYVAULTNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)"
az keyvault purge --name "$(INTERNALKEYVAULTNAME)"
} else {
Write-Host "No pre-existing $(INTERNALKEYVAULTNAME) resource found."
}
- stage: "Delete_App_Insights"
displayName: "Delete App Insights"
dependsOn: []
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete App Insights"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
az config set extension.use_dynamic_install=yes_without_prompt
az feature register --name AIWorkspacePreview --namespace microsoft.insights
Write-Host "Looking for $(INTERNALAPPINSIGHTSNAME)..."
if ((az group exists -n "$(INTERNALSHAREDRESOURCEGROUPNAME)") -eq "true") {
$exists = az monitor app-insights component show --app "$(INTERNALAPPINSIGHTSNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)"
if ($exists) {
Write-Host "Deleting $(INTERNALAPPINSIGHTSNAME)..."
az monitor app-insights component delete --app "$(INTERNALAPPINSIGHTSNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)"
} else {
Write-Host "No pre-existing $(INTERNALAPPINSIGHTSNAME) resource found."
}
} else {
Write-Host "No pre-existing $(INTERNALSHAREDRESOURCEGROUPNAME) group found."
}
- stage: "Delete_CosmosDB"
displayName: "Delete CosmosDB"
dependsOn: []
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete CosmosDB"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Write-Host "Looking for $(INTERNALCOSMOSDBNAME)..."
$exists = az cosmosdb check-name-exists --name "$(INTERNALCOSMOSDBNAME)"
if ($exists -eq $true) {
Write-Host "Deleting $(INTERNALCOSMOSDBNAME)..."
az cosmosdb delete --name "$(INTERNALCOSMOSDBNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)" --yes
} else {
Write-Host "No pre-existing $(INTERNALCOSMOSDBNAME) resource found."
}
- stage: "Delete_Container_Registry"
displayName: "Delete Container Registry"
dependsOn: []
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete Container Registry"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Write-Host "Looking for $(INTERNALCONTAINERREGISTRYNAME)..."
$resource = az acr check-name --name "$(INTERNALCONTAINERREGISTRYNAME)" | ConvertFrom-Json
if ($resource.nameAvailable) {
Write-Host "No pre-existing $(INTERNALCONTAINERREGISTRYNAME) resource found."
} else {
Write-Host "Deleting $(INTERNALCONTAINERREGISTRYNAME)..."
az acr delete --name "$(INTERNALCONTAINERREGISTRYNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)" --yes
}
- stage: "Delete_Storage_Account"
displayName: "Delete Storage Account"
dependsOn: []
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete Storage Account"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Write-Host "Looking for $(INTERNALSTORAGEACCOUNTNAME)..."
$check = az storage account check-name --name "$(INTERNALSTORAGEACCOUNTNAME)" | ConvertFrom-Json
if ($check.nameAvailable -eq $false) {
Write-Host "Deleting $(INTERNALSTORAGEACCOUNTNAME)..."
az storage account delete --name "$(INTERNALSTORAGEACCOUNTNAME)" --resource-group "$(INTERNALSHAREDRESOURCEGROUPNAME)" --yes
} else {
Write-Host "No pre-existing $(INTERNALSTORAGEACCOUNTNAME) resource found."
}
- stage: "Delete_User_Identities"
displayName: "Delete User Assigned Managed Identities"
dependsOn:
- Delete_App_Service_Plan_DotNet
- Delete_App_Service_Plan_JS
- Delete_App_Service_Plan_Python
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- task: AzureCLI@2
displayName: "Delete User Assigned Managed Identities"
inputs:
azureSubscription: $(AZURESUBSCRIPTION)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
az identity delete -n "bffnsimplehostbotdotnetmsi$($env:RESOURCESUFFIX)" -g "$(INTERNALSHAREDRESOURCEGROUPNAME)"
az identity delete -n "bffnsimplehostbotjsmsi$($env:RESOURCESUFFIX)" -g "$(INTERNALSHAREDRESOURCEGROUPNAME)"
az identity delete -n "bffnechoskillbotdotnetmsi$($env:RESOURCESUFFIX)" -g "$(INTERNALSHAREDRESOURCEGROUPNAME)"
az identity delete -n "bffnechoskillbotjsmsi$($env:RESOURCESUFFIX)" -g "$(INTERNALSHAREDRESOURCEGROUPNAME)"
- stage: "Delete_Shared_Resource_Group"
displayName: "Delete Shared Resource Group"
dependsOn:
- Delete_Virtual_Network
- Delete_App_Registrations
- Delete_Key_Vault
- Delete_App_Insights
- Delete_CosmosDB
- Delete_Container_Registry
- Delete_Storage_Account
- Delete_User_Identities
jobs:
- job: "Delete"
displayName: "Delete steps"
steps:
- checkout: none
- template: ../common/deleteResourceGroup.yml
parameters:
azureSubscription: $(AZURESUBSCRIPTION)
name: "$(INTERNALSHAREDRESOURCEGROUPNAME)"