-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathade.ps1
executable file
·183 lines (153 loc) · 8.21 KB
/
ade.ps1
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
#!/usr/bin/env pwsh
###################################################################################################
# Parameters and PowerShell Config
###################################################################################################
[CmdletBinding(DefaultParameterSetName = 'deploy-interactive')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", '', Justification = 'Allowing plaintext for CLI purposes')]
param (
###################################################################################################
# Shared Command Set Parameters
###################################################################################################
[Parameter(Position = 1, mandatory = $true)]
[ValidateLength(1, 8)]
[string]$alias,
[Parameter(Position = 4, mandatory = $true, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 4, mandatory = $true, ParameterSetName = 'deploy-cli')]
[Parameter(Position = 3, mandatory = $true, ParameterSetName = 'remove')]
[string]$rootDomainName,
[Parameter(Position = 8, mandatory = $false, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 8, mandatory = $false, ParameterSetName = 'deploy-cli')]
[Parameter(Position = 4, mandatory = $false, ParameterSetName = 'remove')]
[switch]$skipConfirmation,
###################################################################################################
# Deploy Command Sets
###################################################################################################
[Parameter(Position = 0, mandatory = $true, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 0, mandatory = $true, ParameterSetName = 'deploy-cli')]
[switch]$deploy,
[Parameter(Position = 2, mandatory = $true, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 2, mandatory = $true, ParameterSetName = 'deploy-cli')]
[string]$email,
[Parameter(Position = 3, mandatory = $true, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 3, mandatory = $true, ParameterSetName = 'deploy-cli')]
[string]$resourceUserName,
# Required Only for Deploy Interactive
[Parameter(Position = 5, mandatory = $true, ParameterSetName = 'deploy-interactive')]
[SecureString]$secureResourcePassword,
[Parameter(Position = 6, mandatory = $true, ParameterSetName = 'deploy-interactive')]
[SecureString]$secureCertificatePassword,
# Required Only for Deploy CLI
[Parameter(Position = 5, mandatory = $true, ParameterSetName = 'deploy-cli')]
[string]$resourcePassword,
[Parameter(Position = 6, mandatory = $true, ParameterSetName = 'deploy-cli')]
[string]$certificatePassword,
[Parameter(Position = 7, mandatory = $true, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 7, mandatory = $true, ParameterSetName = 'deploy-cli')]
[string]$localNetworkRange,
[Parameter(Position = 3, mandatory = $false, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 3, mandatory = $false, ParameterSetName = 'deploy-cli')]
[string]$scriptsBaseUri,
[Parameter(Position = 9, mandatory = $false, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 9, mandatory = $false, ParameterSetName = 'deploy-cli')]
[string]$module,
[Parameter(Position = 10, mandatory = $false, ParameterSetName = 'deploy-interactive')]
[Parameter(Position = 10, mandatory = $false, ParameterSetName = 'deploy-cli')]
[switch]$overwriteParameterFiles,
###################################################################################################
# Remove Command Sets
###################################################################################################
[Parameter(Position = 0, mandatory = $true, ParameterSetName = 'remove')]
[switch]$remove,
[Parameter(Position = 2, mandatory = $false, ParameterSetName = 'remove')]
[switch]$includeKeyVault,
###################################################################################################
# Deallocate Command Sets
###################################################################################################
[Parameter(Position = 0, mandatory = $true, ParameterSetName = 'deallocate')]
[switch]$deallocate,
###################################################################################################
# Allocate Command Sets
###################################################################################################
[Parameter(Position = 0, mandatory = $true, ParameterSetName = 'allocate')]
[switch]$allocate
)
# We want to stop if *any* error occurs
Set-StrictMode -Version Latest
# set this to 0 to disable tracing, set to 1 or 2 to show lines being executed (useful for debugging)
Set-PSDebug -Trace 0 -Strict
$DebugPreference = "Continue"
$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
# Import Modules
Import-Module "$PSScriptRoot/src/ps/ADE.psm1" -Force -Verbose
try {
###################################################################################################
# Cleanup Parameters
###################################################################################################
if ($null -eq $module -or $module -eq '') {
$module = 'all'
}
$moduleIsValid = Confirm-ModuleIsValid $module
if (-not $moduleIsValid) {
throw "You specified an invalid module of $module"
}
$wildcardCertificatePath = "$PSScriptRoot/data/wildcard.pfx"
###################################################################################################
# Configuring ARM Parameters Parameters
###################################################################################################
Write-Status 'Configuring Parameters'
$defaultPrimaryRegion = $(az configure -l --query "[?name == 'location'].value | [0]" --output tsv)
$defaultSecondaryRegion = $(az configure -l --query "[?name == 'locationpair'].value | [0]" --output tsv)
if ($secureCertificatePassword -eq $null -and $certificatePassword) {
$secureCertificatePassword = ConvertTo-SecureString $certificatePassword -AsPlainText -Force
$certificatePassword = $null
}
if ($secureResourcePassword -eq $null -and $resourcePassword) {
$secureResourcePassword = ConvertTo-SecureString $resourcePassword -AsPlainText -Force
$resourcePassword = $null
}
if ([string]::IsNullOrWhiteSpace($scriptsBaseUri)) {
$scriptsBaseUri = "https://raw.githubusercontent.com/azuredemoenvironment/ade/main/scripts"
}
$armParameters = Set-InitialArmParameters -alias $alias `
-email $email `
-resourceUserName $resourceUserName `
-rootDomainName $rootDomainName `
-localNetworkRange $localNetworkRange `
-secureResourcePassword $secureResourcePassword `
-secureCertificatePassword $secureCertificatePassword `
-wildcardCertificatePath $wildcardCertificatePath `
-azureRegion $defaultPrimaryRegion `
-azurePairedRegion $defaultSecondaryRegion `
-module $module `
-scriptsBaseUri $scriptsBaseUri `
-isDeploying $deploy `
-overwriteParameterFiles $overwriteParameterFiles `
-skipConfirmation $skipConfirmation
###################################################################################################
# Start the Requested Action
###################################################################################################
# TODO: only one of these steps should be allowed
if ($deploy) {
Deploy-AzureDemoEnvironment $armParameters
}
if ($deallocate) {
Disable-HighCostAzureServices $armParameters
}
if ($allocate) {
Enable-HighCostAzureServices $armParameters
}
if ($remove) {
Remove-AzureDemoEnvironment $armParameters $includeKeyVault
}
}
catch {
$ErrorMessage = $_.Exception.Message
Write-Log "An error occurred: $ErrorMessage"
Write-Debug ($ErrorMessage | Format-Table | Out-String)
}
finally {
# Always set our location back to our script root to make it easier to re-execute
Set-Location -Path $PSScriptRoot
Set-PSDebug -Off
}