-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcleanSetUp.ps1
More file actions
317 lines (251 loc) · 9.12 KB
/
Copy pathcleanSetUp.ps1
File metadata and controls
317 lines (251 loc) · 9.12 KB
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
#Requires -Version 5.1
<#
.SYNOPSIS
Cleans up the DevExp-DevBox setup including users, credentials, and GitHub secrets.
.DESCRIPTION
This script orchestrates the complete cleanup of DevExp-DevBox infrastructure:
- Deletes Azure subscription deployments
- Removes user role assignments
- Deletes deployment credentials (service principals and app registrations)
- Removes GitHub secrets for Azure credentials
- Cleans up Azure resource groups
.PARAMETER EnvName
The environment name used in resource naming. Defaults to 'gitHub'.
.PARAMETER Location
The Azure region where resources are deployed. Defaults to 'eastus2'.
Valid values: eastus, eastus2, westus, westus2, westus3, northeurope, westeurope
.PARAMETER AppDisplayName
The display name of the Azure AD application to delete.
Defaults to 'ContosoDevEx GitHub Actions Enterprise App'.
.PARAMETER GhSecretName
The name of the GitHub secret to delete. Defaults to 'AZURE_CREDENTIALS'.
.EXAMPLE
.\cleanSetUp.ps1
Runs cleanup with default parameters.
.EXAMPLE
.\cleanSetUp.ps1 -EnvName "prod" -Location "westus2"
Runs cleanup for the 'prod' environment in 'westus2'.
.NOTES
Author: DevExp Team
Requires: Azure CLI (az), GitHub CLI (gh), and appropriate permissions
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$EnvName = "gitHub",
[Parameter(Mandatory = $false)]
[ValidateSet("eastus", "eastus2", "westus", "westus2", "westus3", "northeurope", "westeurope")]
[string]$Location = "eastus2",
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$AppDisplayName = "ContosoDevEx GitHub Actions Enterprise App",
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$GhSecretName = "AZURE_CREDENTIALS"
)
# Script Configuration
$ErrorActionPreference = 'Stop'
$WarningPreference = 'Stop'
# Script directory for relative path resolution
$Script:ScriptDirectory = $PSScriptRoot
function Remove-SubscriptionDeployments {
<#
.SYNOPSIS
Deletes all subscription-level deployments.
.DESCRIPTION
Lists and deletes all Azure Resource Manager deployments at the
subscription level.
.OUTPUTS
System.Boolean - True if successful, False otherwise.
.EXAMPLE
Remove-SubscriptionDeployments
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType([bool])]
param()
try {
Write-Output "Retrieving subscription deployments..."
$deployments = az deployment sub list --query "[].name" --output tsv
if ($LASTEXITCODE -ne 0) {
throw "Failed to list subscription deployments."
}
if ([string]::IsNullOrWhiteSpace($deployments)) {
Write-Output "No subscription deployments found."
return $true
}
foreach ($deployment in ($deployments -split "`n")) {
if (-not [string]::IsNullOrWhiteSpace($deployment)) {
if ($PSCmdlet.ShouldProcess($deployment, "Delete subscription deployment")) {
Write-Output "Deleting deployment: $deployment"
$null = az deployment sub delete --name $deployment 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Warning "Failed to delete deployment: $deployment"
}
else {
Write-Output "Deployment '$deployment' deleted."
}
}
}
}
return $true
}
catch {
Write-Error "Error deleting subscription deployments: $_"
return $false
}
}
function Invoke-CleanupScript {
<#
.SYNOPSIS
Invokes a cleanup script with proper error handling.
.DESCRIPTION
Executes a PowerShell script with specified parameters, handling
path resolution and error checking.
.PARAMETER ScriptPath
The relative path to the script from the script directory.
.PARAMETER Parameters
Hashtable of parameters to pass to the script.
.PARAMETER Description
Description of what the script does (for logging).
.OUTPUTS
System.Boolean - True if successful, False otherwise.
.EXAMPLE
Invoke-CleanupScript -ScriptPath "Azure\deleteUsers.ps1" -Parameters @{Name="test"} -Description "Delete users"
#>
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory = $true)]
[string]$ScriptPath,
[Parameter(Mandatory = $false)]
[hashtable]$Parameters = @{},
[Parameter(Mandatory = $true)]
[string]$Description
)
try {
$fullPath = Join-Path -Path $Script:ScriptDirectory -ChildPath $ScriptPath
$fullPath = [System.IO.Path]::GetFullPath($fullPath)
if (-not (Test-Path -Path $fullPath)) {
Write-Warning "Script not found: $fullPath. Skipping $Description."
return $true
}
Write-Output "$Description..."
if ($Parameters.Count -gt 0) {
& $fullPath @Parameters
}
else {
& $fullPath
}
if ($LASTEXITCODE -ne 0) {
throw "Script failed with exit code: $LASTEXITCODE"
}
Write-Output "$Description completed."
return $true
}
catch {
Write-Error "Error during ${Description}: $_"
return $false
}
}
function Start-FullCleanup {
<#
.SYNOPSIS
Orchestrates the complete cleanup process.
.DESCRIPTION
Runs all cleanup operations in sequence: deployments, users,
credentials, GitHub secrets, and resource groups.
.PARAMETER AppDisplayName
The Azure AD application display name.
.PARAMETER GhSecretName
The GitHub secret name.
.PARAMETER EnvName
The environment name.
.PARAMETER Location
The Azure region.
.OUTPUTS
System.Boolean - True if all cleanup succeeded, False otherwise.
.EXAMPLE
Start-FullCleanup -AppDisplayName "MyApp" -GhSecretName "SECRET" -EnvName "dev" -Location "eastus"
#>
[CmdletBinding(SupportsShouldProcess)]
[OutputType([bool])]
param(
[Parameter(Mandatory = $true)]
[string]$AppDisplayName,
[Parameter(Mandatory = $true)]
[string]$GhSecretName,
[Parameter(Mandatory = $true)]
[string]$EnvName,
[Parameter(Mandatory = $true)]
[string]$Location
)
try {
Write-Output "Starting full cleanup process..."
Write-Output "App Display Name: $AppDisplayName"
Write-Output "GitHub Secret: $GhSecretName"
Write-Output "Environment: $EnvName"
Write-Output "Location: $Location"
Write-Output ""
$allSucceeded = $true
# Delete subscription deployments
if (-not (Remove-SubscriptionDeployments)) {
Write-Warning "Subscription deployment cleanup had issues."
$allSucceeded = $false
}
# Delete users and assigned roles
$success = Invoke-CleanupScript `
-ScriptPath ".configuration\setup\powershell\Azure\deleteUsersAndAssignedRoles.ps1" `
-Parameters @{ AppDisplayName = $AppDisplayName } `
-Description "Deleting users and assigned roles"
if (-not $success) { $allSucceeded = $false }
# Delete deployment credentials
$success = Invoke-CleanupScript `
-ScriptPath ".configuration\setup\powershell\Azure\deleteDeploymentCredentials.ps1" `
-Parameters @{ AppDisplayName = $AppDisplayName } `
-Description "Deleting deployment credentials"
if (-not $success) { $allSucceeded = $false }
# Delete GitHub secret
$success = Invoke-CleanupScript `
-ScriptPath ".configuration\setup\powershell\GitHub\deleteGitHubSecretAzureCredentials.ps1" `
-Parameters @{ GhSecretName = $GhSecretName } `
-Description "Deleting GitHub secret for Azure credentials"
if (-not $success) { $allSucceeded = $false }
# Clean up resource groups
$cleanupScriptPath = ".configuration\powershell\cleanUp.ps1"
$success = Invoke-CleanupScript `
-ScriptPath $cleanupScriptPath `
-Parameters @{ EnvName = $EnvName; Location = $Location } `
-Description "Cleaning up resource groups"
if (-not $success) { $allSucceeded = $false }
return $allSucceeded
}
catch {
Write-Error "Error during full cleanup: $_"
return $false
}
}
# Main script execution
try {
Clear-Host
Write-Output "DevExp-DevBox Full Cleanup"
Write-Output "=========================="
Write-Output ""
$success = Start-FullCleanup `
-AppDisplayName $AppDisplayName `
-GhSecretName $GhSecretName `
-EnvName $EnvName `
-Location $Location
if ($success) {
Write-Output ""
Write-Output "All cleanup operations completed successfully."
}
else {
Write-Warning "Some cleanup operations failed. Check errors above."
exit 1
}
}
catch {
Write-Error "Script execution failed: $_"
exit 1
}