-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCVADDeploymentCommon.psm1
496 lines (431 loc) · 21 KB
/
CVADDeploymentCommon.psm1
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# Copyright © 2023. Citrix Systems, Inc. All Rights Reserved.
function Add-DDCManagementInfoToGeneratedTfVarsFile {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $GeneratedTfVarsFilePath,
[Parameter(Mandatory = $true)]
[string] $SiteProvisionTerraformLocation
)
Set-Location $SiteProvisionTerraformLocation
terraform init -upgrade -var-file="$GeneratedTfVarsFilePath"
$siteProvisioningOutput = terraform output -json | ConvertFrom-Json
$tfvarContentJson = Get-Content $GeneratedTfVarsFilePath -Raw | ConvertFrom-Json
$tfvarContentJson | Add-Member -Name "setup_storage_account_name" -Value $siteProvisioningOutput.resource_group_information.value.cvad_setup_storage_account_name -MemberType NoteProperty -Force
$tfvarContentJson | Add-Member -Name "active_directory_domain_name" -Value $siteProvisioningOutput.active_directory_information.value.domain_name -MemberType NoteProperty -Force
$tfvarContentJson | Add-Member -Name "advm_machine_name" -Value $siteProvisioningOutput.domain_controller_details.value.vm_machine_name -MemberType NoteProperty -Force
$tfvarContentJson | Add-Member -Name "advm_admin_username" -Value $siteProvisioningOutput.active_directory_information.value.domain_admin_username -MemberType NoteProperty -Force
$tfvarContentJson | Add-Member -Name "advm_admin_password" -Value $siteProvisioningOutput.active_directory_information.value.domain_admin_password -MemberType NoteProperty -Force
$tfvarContentJson | Add-Member -Name "cvad_component_resource_group_name" -Value $siteProvisioningOutput.resource_group_information.value.cvad_components_resource_group_name -MemberType NoteProperty -Force
$tfvarContentJson | Add-Member -Name "cvad_vnet_name" -Value $siteProvisioningOutput.resource_group_information.value.cvad_vnet_name -MemberType NoteProperty -Force
$tfvarContentJson | Add-Member -Name "local_temp_file_dir" -Value $siteProvisioningOutput.local_temp_file_information.value.local_temp_file_dir -MemberType NoteProperty -Force
$genTfVarsJson = $tfvarContentJson | ConvertTo-Json
Set-Content -Path $GeneratedTfVarsFilePath -Value $genTfVarsJson -Force
}
function Clear-TerraformResourcesWithPattern {
param (
[Parameter(Mandatory = $true)]
[string]$GeneratedTfVarsFilePath,
[Parameter(Mandatory = $true)]
[string]$Pattern,
[Parameter(Mandatory = $false)]
[switch]$Destroy
)
$targetResources = terraform state list | Select-String -Pattern $Pattern
foreach ($targetResource in $targetResources) {
if ($Destroy) {
terraform destroy -refresh=false -var-file="$GeneratedTfVarsFilePath" -target="$($targetResource)" -auto-approve
}
else {
$parsedTarget = $targetResource.ToString() -replace '(.*)\["(.*)"\]', '$1[\"$2\"]'
terraform state rm $parsedTarget
}
}
}
function Clear-TerraformTempFiles {
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[string] $TerraformConfigurationLocation
)
Remove-Item "$($TerraformConfigurationLocation)\*.lock.hcl" -Force -ErrorAction SilentlyContinue
Remove-Item "$($TerraformConfigurationLocation)\*.lock.info" -Force -ErrorAction SilentlyContinue
Remove-Item "$($TerraformConfigurationLocation)\*.tfstate*backup" -Force -ErrorAction SilentlyContinue
Remove-Item "$($TerraformConfigurationLocation)\.terraform\providers\" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$($TerraformConfigurationLocation)\terraform.log" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$($TerraformConfigurationLocation)\.terraform" -Recurse -Force -ErrorAction SilentlyContinue
}
function New-DeploymentSummary {
param (
[Parameter(Mandatory = $true)]
[string]$WorkingDirectory,
[Parameter(Mandatory = $true)]
[string] $GeneratedTfVarsFilePath,
[Parameter(Mandatory = $true)]
[string] $SiteProvisionTerraformLocation,
[Parameter(Mandatory = $true)]
[string] $SiteManagementTerraformLocation,
[Parameter(Mandatory = $true)]
[int] $ShowSessionLaunchPassword
)
$scriptFilePath = "$($WorkingDirectory)\Setup\CustomScripts\Write-DeploymentSummary.ps1"
$provisionOutput = Invoke-TerraformOutputJson -TerraformConfigLocation $SiteProvisionTerraformLocation
$managementOutput = Invoke-TerraformOutputJson -TerraformConfigLocation $SiteManagementTerraformLocation
. $scriptFilePath -ShowSessionLaunchPassword $ShowSessionLaunchPassword `
-CoreResourceGroupName $provisionOutput.resource_group_information.value.cvad_components_resource_group_name `
-AdDomainName $provisionOutput.active_directory_information.value.domain_name `
-AdPublicIP $provisionOutput.domain_controller_details.value.public_ip `
-AdComputerName $provisionOutput.domain_controller_details.value.vm_machine_name `
-AdAdminUsername $provisionOutput.active_directory_information.value.domain_admin_username `
-AdAdminPassword $provisionOutput.active_directory_information.value.domain_admin_password `
-DdcComputerName $provisionOutput.ddc_details.value.vm_machine_name `
-DdcPublicIp $provisionOutput.ddc_details.value.public_ip `
-DdcPrivateIp $provisionOutput.ddc_details.value.private_ip `
-StoreVirtualPath $provisionOutput.site_information.value.storefront_virtual_path `
-StoreUserPassword $provisionOutput.active_directory_information.value.domain_user_default_password `
-MachineCatalogName $managementOutput.machine_catalog_information.value.machine_catalog_name`
-SessionSupport $managementOutput.machine_catalog_information.value.machine_catalog_session_support `
-VdaCount $managementOutput.machine_catalog_information.value.vda_count_in_catalog `
-WebStudioMachine $provisionOutput.site_information.value.default_webstudio_machine_name `
-VdaResourceGroupName $managementOutput.machine_catalog_information.value.vda_resource_group_name
}
function New-GeneratedTfVarFile {
param (
[Parameter(Mandatory = $true)]
[string]$WorkingDirectory,
[Parameter(Mandatory = $true)]
[string]$TempDirectoryPath,
[Parameter(Mandatory = $true)]
[string]$GeneratedTfVarsFilePath,
[Parameter(Mandatory = $true)]
[bool]$ShowSessionLaunchPassword,
[Parameter(Mandatory = $true)]
[bool]$Destroy
)
Remove-Item -Path $TempDirectoryPath -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path $TempDirectoryPath -Force | Out-Null
$TempDirectoryPath = Convert-Path -Path $TempDirectoryPath
$jsonTfVarPaths = Get-ChildItem -Path $WorkingDirectory -Filter "**.tfvars.json" -Recurse | Convert-Path
New-Item -ItemType File -Path $GeneratedTfVarsFilePath | Out-Null
$ShowSessionLaunchPassword = $ShowSessionLaunchPassword -and (-not $Destroy)
$genTfVarsJson = "{
'hide_session_launch_password': $("$(-not $ShowSessionLaunchPassword)".ToLower()),
'local_temp_file_dir':'$($TempDirectoryPath.Replace("\", "/"))'
}" | ConvertFrom-Json
foreach ($jsonTfvarPath in $jsonTfVarPaths) {
$tfvarContentJson = Get-Content $jsonTfvarPath -Raw | ConvertFrom-Json
$tfvarContentJson.PSObject.Properties | ForEach-Object {
$genTfVarsJson | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value -Force
}
}
$genTfVarsJson = $genTfVarsJson | ConvertTo-Json
Set-Content -Path $GeneratedTfVarsFilePath -Value $genTfVarsJson -Force
}
function Invoke-TerraformAction {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[ValidateSet("plan", "apply", "destroy")]
[string] $Action,
[Parameter(Mandatory = $true)]
[string] $TerraformConfigLocation,
[Parameter(Mandatory = $true)]
[string] $WorkingDirectory,
[Parameter(Mandatory = $true)]
[string] $GeneratedTfVarsFilePath,
[Parameter(Mandatory = $true)]
[AllowNull()]
[AllowEmptyString()]
[string] $TerraformAutoApproveArg,
[Parameter(Mandatory = $true)]
[bool] $OutputJsonDeploymentSummary,
[Parameter(Mandatory = $true)]
[int] $Parallelism
)
Set-Location $TerraformConfigLocation
# Backup Original Terraform Environment Variables
$Original_TF_LOG = $env:TF_LOG
$Original_TF_LOG_PATH = $env:TF_LOG_PATH
$Original_TF_DATA_DIR = $env:TF_DATA_DIR
try {
# Setup Terraform Environment Variables for Deployment
$env:TF_LOG = "TRACE"
$env:TF_LOG_PATH = "$($TerraformConfigLocation)\terraform.log"
$env:TF_DATA_DIR = "$($TerraformConfigLocation)\.terraform\"
# Initiate Terraform Environment
terraform init -upgrade -var-file="$GeneratedTfVarsFilePath"
Write-Warning "Please don't login to VM when the setup is still running"
# Perform Terraform Action
terraform $Action -var-file="$GeneratedTfVarsFilePath" -var-file="$($WorkingDirectory)\inputs.tfvars.json" $TerraformAutoApproveArg -parallelism="$($Parallelism)" -compact-warnings
if ($OutputJsonDeploymentSummary) {
$tfJsonOutput = terraform output -json
Set-Content -Path "$($TerraformConfigLocation)\DeploymentSummary.json" -Value $tfJsonOutput -Force
}
}
finally {
# Restore Terraform Environment Variables
$env:TF_LOG = $Original_TF_LOG
$env:TF_LOG_PATH = $Original_TF_LOG_PATH
$env:TF_DATA_DIR = $Original_TF_DATA_DIR
}
}
function Invoke-TerraformOutputJson {
param(
[Parameter(Mandatory = $true)]
[string] $TerraformConfigLocation
)
Set-Location $TerraformConfigLocation
# Backup Original Terraform Environment Variables
$Original_TF_LOG = $env:TF_LOG
$Original_TF_LOG_PATH = $env:TF_LOG_PATH
$Original_TF_DATA_DIR = $env:TF_DATA_DIR
$tfJsonOutput = $null
try {
# Setup Terraform Environment Variables for Deployment
$env:TF_LOG = "TRACE"
$env:TF_LOG_PATH = "$($TerraformConfigLocation)\terraform.log"
$env:TF_DATA_DIR = "$($TerraformConfigLocation)\.terraform\"
# Initiate Terraform Environment
terraform init -upgrade
$tfJsonOutput = terraform output -json
}
finally {
# Restore Terraform Environment Variables
$env:TF_LOG = $Original_TF_LOG
$env:TF_LOG_PATH = $Original_TF_LOG_PATH
$env:TF_DATA_DIR = $Original_TF_DATA_DIR
}
return $tfJsonOutput | ConvertFrom-Json
}
function Install-AzModule {
$isAzModuleInstalled = Get-InstalledModule -Name Az -ErrorAction SilentlyContinue
if (-not $isAzModuleInstalled) {
$retryCount = 0
while ($retryCount -lt 3) {
try {
Write-Output "Attempting to install Az PowerShell module, retry count: $retryCount"
Remove-Module Az*
Install-Module -Name Az -Repository PSGallery -Force -ErrorAction Stop
Uninstall-AzureRM -ErrorAction Stop
}
catch {
Write-Output "Failed to install Az module, increasing retry count" -ForegroundColor Yellow
$retryCount++
}
if ($retryCount -eq 3) {
Write-Output "Failed to install Az PowerShell module after 3 retries" -ForegroundColor Red
exit 1
}
}
}
}
function Install-Terraform {
# Chocolatey Setup
$isTerraformInstalled = Get-Command -Name terraform -ErrorAction SilentlyContinue
$isChocoInstalled = Get-Command -Name choco -ErrorAction SilentlyContinue
if ((-not $isTerraformInstalled) -and (-not $isChocoInstalled)) {
Write-Output "Installing Chocolatey"
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Write-Output "Chocolatey installed"
}
else {
Write-Output "Chocolatey already installed"
}
# Install Terraform with Chocolatey
if (-not $isTerraformInstalled) {
Write-Output "Installing Terraform"
choco install terraform -y
Write-Output "Terraform installed"
}
else {
Write-Output "Terraform already installed"
}
Write-Output "Upgrading Chocolatey to latest version"
choco upgrade chocolatey -fy
Write-Output "Upgraded Chocolatey to latest version"
Write-Output "Upgrading Terraform to latest version"
choco upgrade terraform -fy
Write-Output "Upgraded Terraform to latest version"
}
function Read-AzureConfigStringValue {
param (
[Parameter(Mandatory = $true)]
[AllowNull()]
[AllowEmptyString()]
[string]$ConfigStringValue,
[Parameter(Mandatory = $true)]
[string]$ConfigStringPropertyName
)
if ([string]::IsNullOrEmpty($ConfigStringValue)) {
Write-Output "Variable $($ConfigStringPropertyName) in azure.tfvars.json cannot be null or empty" -ForegroundColor Red
return $false
}
return $true
}
function Remove-DeploymentSummary {
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[string[]]$TerraformConfigLocations
)
foreach ($TerraformLocation in $TerraformConfigLocations) {
Remove-Item "$($TerraformLocation)\DeploymentSummary.md" -Force -ErrorAction SilentlyContinue
Remove-Item "$($TerraformLocation)\DeploymentSummary.json" -Force -ErrorAction SilentlyContinue
}
}
function Remove-TerraformResourcesForDestroyAction {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $TerraformConfigLocation,
[parameter(Mandatory = $true)]
[string] $GeneratedTfVarsFilePath
)
Set-Location $TerraformConfigLocation
# Backup Original Terraform Environment Variables
$Original_TF_LOG = $env:TF_LOG
$Original_TF_LOG_PATH = $env:TF_LOG_PATH
$Original_TF_DATA_DIR = $env:TF_DATA_DIR
try {
# Setup Terraform Environment Variables for Deployment
$env:TF_LOG = "TRACE"
$env:TF_LOG_PATH = "$($TerraformConfigLocation)\terraform.log"
$env:TF_DATA_DIR = "$($TerraformConfigLocation)\.terraform\"
# Initiate Terraform Environment
terraform init -upgrade -var-file="$GeneratedTfVarsFilePath"
# Perform Terraform Action
Clear-TerraformResourcesWithPattern -GeneratedTfVarsFilePath $GeneratedTfVarsFilePath -Pattern "azurerm_virtual_machine_extension.*"
Clear-TerraformResourcesWithPattern -GeneratedTfVarsFilePath $GeneratedTfVarsFilePath -Pattern "azurerm_network_interface_security_group_association.*" -Destroy
Clear-TerraformResourcesWithPattern -GeneratedTfVarsFilePath $GeneratedTfVarsFilePath -Pattern "azurerm_subnet_network_security_group_association.*" -Destroy
}
finally {
# Restore Terraform Environment Variables
$env:TF_LOG = $Original_TF_LOG
$env:TF_LOG_PATH = $Original_TF_LOG_PATH
$env:TF_DATA_DIR = $Original_TF_DATA_DIR
}
}
function Remove-TerraformTempFiles {
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[string[]] $TerraformConfigLocations
)
foreach ($TerraformConfigLocation in $TerraformConfigLocations) {
Clear-TerraformTempFiles -TerraformConfigurationLocation $TerraformConfigLocation
}
}
function Show-DeploymentSummary {
param (
[Parameter(Mandatory = $true)]
[string]$WorkingDirectory
)
$deploymentSummary = Get-Content "$($WorkingDirectory)\DeploymentSummary.md" -Raw -ErrorAction SilentlyContinue
Write-Warning "Please record all the information below before exiting this window`n"
Write-Output $($deploymentSummary.Replace("``````", ""))
}
function Test-AzModuleEnvironment {
$installedAzModule = Get-InstalledModule -Name Az -ErrorAction SilentlyContinue
if (-not $installedAzModule) {
Write-Output "Az module not found, please install Az Powershell Module v9.0.0 or higher." -ForegroundColor Red
exit 1
}
elseif (([Version]$installedAzModule.Version).Major -lt 9) {
Write-Output "Local Az PowerShell module has version $($installedAzModule.Version), please upgrade it to v9.0.0 or higher." -ForegroundColor Red
exit 1
}
}
function Test-AzureAppRegistrationCreation {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String] $azure_client_id,
[Parameter(Mandatory = $true)]
[String] $azure_client_secret,
[Parameter(Mandatory = $true)]
[String] $azure_tenant_id,
[Parameter(Mandatory = $true)]
[String] $azure_subscription_id
)
Write-Output "Waiting for App Registration creation to be finalized on Azure..."
$retryCount = 0
$maxRetryCount = 5
$verificationInterval = 15
$secret = ConvertTo-SecureString "$azure_client_secret" -AsPlainText -Force
$credential = New-Object -TypeName pscredential -ArgumentList $azure_client_id, $secret
while ($retryCount -lt $maxRetryCount) {
try {
Start-Sleep -Seconds $verificationInterval
Connect-AzAccount -Credential $credential -TenantId $azure_tenant_id -ServicePrincipal -SubscriptionId $azure_subscription_id -force -ErrorAction Stop | Out-Null
Disconnect-AzAccount -ErrorAction SilentlyContinue | Out-Null
break
}
catch {
Write-Output "App Registration creation is not finalized yet on Azure..."
$retryCount++
if ($retryCount -eq $maxRetryCount) {
Write-Output "Failed to create App Registration in Azure subscription $($azure_subscription_id) with tenant $($azure_tenant_id): $($_)`n" -ForegroundColor Red
exit 1
}
}
}
Write-Output "`nApp Registration creation is finalized on Azure...`n" -ForegroundColor Green
}
function Test-AzureConfig {
param (
[Parameter(Mandatory = $true)]
[string]$WorkingDirectory
)
$azureConfig = Get-Content "$($WorkingDirectory)\azure.tfvars.json" -raw | ConvertFrom-Json
$azureSubscriptionId = $azureConfig.azure_subscription_id
$isSubscriptionIdValid = Read-AzureConfigStringValue -ConfigStringValue $azureSubscriptionId -ConfigStringPropertyName "azure_subscription_id"
$azureTenantId = $azureConfig.azure_tenant_id
$isTenantIdValid = Read-AzureConfigStringValue -ConfigStringValue $azureTenantId -ConfigStringPropertyName "azure_tenant_id"
$azureClientId = $azureConfig.azure_client_id
$isClientIdValid = Read-AzureConfigStringValue -ConfigStringValue $azureClientId -ConfigStringPropertyName "azure_client_id"
$azureClientSecret = $azureConfig.azure_client_secret
$isClientSecretValid = Read-AzureConfigStringValue -ConfigStringValue $azureClientSecret -ConfigStringPropertyName "azure_client_secret"
if (-not ($isSubscriptionIdValid -and $isTenantIdValid -and $isClientIdValid -and $isClientSecretValid)) {
Write-Output "Please ensure the Azure credentials are up to date in .\azure.tfvars.json file" -ForegroundColor Yellow
exit 1
}
}
function Test-PowerShellEnvironment {
try {
$psVersion = $psversiontable.psversion
if ($psVersion.Major -lt 5 -or ($psVersion.Major -eq 5 -and $psVersion.Minor -lt 1)) {
throw "Incompatible powershell version. Please install powershell version 5.1 or above. Reference link: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3"
}
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw "Administrator Privilege missing! Please run powershell in Administrator mode before running this script!"
}
}
catch {
Write-Output $_.Exception.Message -ForegroundColor Red
exit
}
}
function Test-TerraformEnvironment {
$terraformCmd = Get-Command -Name terraform -ErrorAction SilentlyContinue
if (-not $terraformCmd) {
Write-Output "Terraform cannot be found, please install terraform with version 1.1.0 or higher." -ForegroundColor Red
exit 1
}
else {
$tfVersionJson = ConvertFrom-Json ([string](terraform -version -json))
$tfVersion = [version]$tfVersionJson.terraform_version
if (($tfVersion.Major -lt 1) -or ($tfVersion.Minor -lt 1)) {
Write-Output "The local Terraform has version $($tfVersionJson.terraform_version), please install terraform with version 1.1.0 or higher." -ForegroundColor Red
exit 1
}
}
}
Export-ModuleMember -Function Add-*
Export-ModuleMember -Function Install-*
Export-ModuleMember -Function Invoke-*
Export-ModuleMember -Function New-*
Export-ModuleMember -Function Remove-*
Export-ModuleMember -Function Show-*
Export-ModuleMember -Function Test-*