-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdeploy.ps1
651 lines (558 loc) · 25.2 KB
/
deploy.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
###############################################################################################################
##
## Azure Subscription Builder Deployment Script
##
###############################################################################################################
# Intake and set parameters
$parameters = Get-Content ./deployParams.json | ConvertFrom-Json
$location = $parameters.location
$rootManagementGroup = $parameters.rootManagementGroup
$Name = $parameters.Name.ToLower()
$pfxCertPath = $parameters.pfxCertPath
$webserverSshKey = $parameters.webserverSshKey
$webserverAdminCidr = $parameters.webserverAdminCidr
$certPass = ConvertTo-SecureString $parameters.certPass -AsPlainText -Force
$tenantId = (Get-AzContext).Tenant.Id
$subId = (Get-AzContext).Subscription.Id
$baseStorageUrl = "https://$($Name)stgacct.blob.core.windows.net/sub-builder"
$logFile = "./deploy_$(get-date -format `"yyyyMMddhhmmsstt`").log"
# Set preference variables
$ErrorActionPreference = "Stop"
#Validate Name
Function ValidateName
{
param (
[ValidateLength(6,17)]
[ValidatePattern('^(?!-)(?!.*--)[a-z]')]
[parameter(Mandatory=$true)]
[string]
$Name
)
write-host "Name is"$Name
}
ValidateName $Name
# Validate Location
$validLocations = Get-AzLocation
Function ValidateLocation {
if ($location -in ($validLocations | Select-Object -ExpandProperty Location)) {
foreach ($l in $validLocations) {
if ($location -eq $l.Location) {
$script:locationName = $l.DisplayName
}
}
}
else {
Write-Host "ERROR: Location provided is not a valid Azure Region!" -ForegroundColor red
exit
}
}
ValidateLocation $location
# Create resource group if it doesn't already exist
$rgcheck = Get-AzResourceGroup -Name "$Name-rg" -ErrorAction SilentlyContinue
if (!$rgcheck) {
Write-Host "INFO: Creating new resource group: $Name-rg" -ForegroundColor green
Write-Verbose -Message "Creating new resource group: $Name-rg"
New-AzResourceGroup -Name "$Name-rg" -Location $location
}
else {
Write-Warning -Message "Resource Group: $Name-rg already exists. Continuing with deployment..."
}
# Gather enrollment account information
Write-Host "INFO: Gathering EA Information" -ForegroundColor green
function Show-Menu
{
param ([string]$Title)
Write-Host "======== $Title ========"
$Menu = @{}
try {
(Get-AzEnrollmentAccount).ObjectId | ForEach-Object -Begin {$i = 1} {
Write-Host "Press '$i' for: $_"
$Menu.add("$i",$_)
$i++
}
Write-Host "Q: Press 'Q' to quit."
$Selection = Read-Host "Please make a selection"
if ($Selection -eq 'Q') { return } else { $Menu.$Selection }
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to retrieve Enrollment Account information due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
}
$eaObjectId = Show-Menu -Title 'Enterprise Agreement Enrollments'
if (!$eaObjectId) {
Write-Warning -Message "No Enterprise Agreement Enrollment was selected, exiting deployment!"
exit
}
else {
Write-Host "INFO: Proceeding with EA Enrollment: $eaObjectId" -ForegroundColor green
}
# Offer optional Web Front end
Write-Host "======== Front End Selection ========"
Write-Host "Would you like to deploy this package with the included optional Web Server? (Default is No)"
try {
[ValidateSet('Y', 'N')] $selection = Read-Host " ( Y / N ) "
switch ($selection)
{
Y {Write-Host "INFO: You chose 'Yes', included Web Server will be deployed" -ForegroundColor Green; $webserver = $true}
N {Write-Warning "You chose 'No', included webserver will not be deployed"; $webserver = $false}
Default {Write-Warning "No selection was made, skipping webserver deployment.."; $webserver = $false}
}
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Front End Selection failed due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
# Build azure run as service principal certificate
Write-Host "INFO: Building Azure Run As Certificate" -ForegroundColor green
Write-Verbose -Message "Building Azure Run As Certificate"
$flags = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable `
-bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet `
-bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet
try {
$PfxCert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @($pfxCertPath, $certPass, $flags)
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to create PFX certificate due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
# Export certificate and convert into base 64 string
$Base64Value = [System.Convert]::ToBase64String($PfxCert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12))
$Thumbprint = $PfxCert.Thumbprint
try {
# Create azure service principal
Write-Host "INFO: Creating new Azure Service Principal" -ForegroundColor green
Write-Verbose -Message "Creating new Azure Service Principal"
$now = [System.DateTime]::Now
$6mofrmnow = $now.AddMonths(6)
$sp = New-AzADServicePrincipal `
-DisplayName $Name `
-CertValue $Base64Value `
-StartDate $now `
-EndDate $6mofrmnow
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to create Service Principal due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
# Add client secret to service principal
Write-Host "INFO: Creating Azure Service Principal client secret" -ForegroundColor green
Write-Verbose -Message "Creating Azure Service Principal client secret"
New-AzADAppCredential `
-DisplayName $sp.DisplayName `
-Password $certPass `
-StartDate $now `
-EndDate $6mofrmnow
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to create Service Principal secret due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
# Check to see if service principal already has management group owner role assigned
$spMgmtGroupRole = Get-AzRoleAssignment `
-RoleDefinitionName "Owner" `
-ObjectId $sp.Id `
-Scope "/providers/Microsoft.Management/managementGroups/$rootManagementGroup"
if ($spMgmtGroupRole) {
Write-Warning -Message "Owner Role for Management Group: $rootManagementGroup is already assigned to $($sp.DisplayName), continuing with deployment..."
}
else {
Write-Host "INFO: Assigning Owner Role to Service Principal: $($sp.DisplayName) for Management Group: $rootManagementGroup" -ForegroundColor green
do {
# Assign owner role to service principal for management group
Write-Verbose -Message "Assigning Owner Role to Service Principal: $($sp.DisplayName) for Management Group: $rootManagementGroup"
$mgmtGroupRole = New-AzRoleAssignment `
-RoleDefinitionName "Owner" `
-ObjectId $sp.Id `
-Scope "/providers/Microsoft.Management/managementGroups/$rootManagementGroup" `
-ErrorAction SilentlyContinue
Start-Sleep -Seconds 10
} while (!$mgmtGroupRole)
}
# Check to see if service principal already has enrollment account owner role assigned
$spEaRole = Get-AzRoleAssignment `
-RoleDefinitionName "Owner" `
-ObjectId $sp.Id `
-Scope "/providers/Microsoft.Billing/enrollmentAccounts/$eaObjectId"
if ($spEaRole) {
Write-Warning -Message "Owner Role for Enrollment Account: $eaObjectId is already assigned to $($sp.DisplayName), continuing with deployment..."
}
else {
Write-Host "INFO: Assigning Owner Role to Service Principal: $($sp.DisplayName) for Enrollment Account: $eaObjectId" -ForegroundColor green
do {
# Assign owner role to service principal for enrollment account
Write-Verbose -Message "Assigning EA Owner Role to Service Principal"
$eaRole = New-AzRoleAssignment `
-RoleDefinitionName "Owner" `
-ObjectId $sp.Id `
-Scope "/providers/Microsoft.Billing/enrollmentAccounts/$eaObjectId" `
-ErrorAction SilentlyContinue
Start-Sleep -Seconds 10
} while (!$eaRole)
}
# Obtain current user principal name for granting full access to Key Vault and secret
Write-Host "INFO: Obtaining current user context for granting administrative rights to Key Vault" -ForegroundColor green
Write-Verbose -Message "Obtaining current user context for granting administrative rights to Key Vault"
$context = Get-AzContext
$owner = Get-AzADUser -UserPrincipalName $context.Account.Id
try {
# Deploy ARM template to create log analytics workspace
Write-Host "INFO: Deploying ARM template to create Log Analytics Workspace" -ForegroundColor green
Write-Verbose -Message "Deploying ARM template to create Log Analytics Workspace"
$workspaceParams = @{
'workspaceName' = "$Name-workspace"
}
New-AzResourceGroupDeployment `
-ResourceGroupName "$Name-rg" `
-TemplateFile ./infra_templates/monitor/logAnalyticsWorkspace.json `
-TemplateParameterObject $workspaceParams
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to deploy Log Analytics Workspace ARM template due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
# Deploy ARM to create Key Vault and Secret
Write-Host "INFO: Deploying ARM template to create Key Vault and Secret" -ForegroundColor green
Write-Verbose -Message "Deploying ARM template to create Key Vault and Secret"
$keyVaultParams = @{
'keyVaultName' = "$Name-keyvault";
'diagSettingsName' = "$Name-keyvault-diagsettings";
'workspaceId' = "/subscriptions/$subId/resourcegroups/$Name-rg/providers/microsoft.operationalinsights/workspaces/$Name-workspace";
'ownerObjectId' = $owner.Id;
'spObjectId' = $sp.Id;
'secretName' = "$Name-secret";
'secretValue' = $parameters.certPass # Need to pass string version as ARM deployment will convert to secure string
}
New-AzResourceGroupDeployment `
-ResourceGroupName "$Name-rg" `
-TemplateFile ./infra_templates/keyVault/keyVault.json `
-TemplateParameterObject $keyVaultParams
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to deploy Key Vault ARM template due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
# Deploy ARM template to create storage account and blob container
Write-Host "INFO: Deploying ARM template to create Storage Account and Blob Container" -ForegroundColor green
Write-Verbose -Message "Deploying ARM template to create Storage Account and Blob Container"
$storageParams = @{
'storageAccountName' = "$($Name)stgacct"
}
New-AzResourceGroupDeployment `
-ResourceGroupName "$Name-rg" `
-TemplateFile ./infra_templates/storageAccount/storageAcct.json `
-TemplateParameterObject $storageParams
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to deploy Storage Account ARM template due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
# Get storage account context for artifact upload
Write-Host "INFO: Obtaining Storage Account context for artifact uploads..." -ForegroundColor green
Write-Verbose -Message "Obtaining Storage Account context for artifact uploads..."
$storageAccount = Get-AzStorageAccount -ResourceGroupName "$Name-rg" -Name "$($Name)stgacct"
if (!$storageAccount) {
Write-Host "ERROR: Unable to obtain storage context, exiting script!" -ForegroundColor red
exit
}
else {
try {
# Find/replace "rootMgmtGroup" for one provided in deployParams.json
Write-Host "INFO: Dynamically updating automation runbooks" -ForegroundColor green
Write-Verbose -Message "Dynamically updating automation runbooks"
Get-ChildItem `
-File `
-Path ./runbooks/* `
-Exclude *-dynamic.ps1 | `
ForEach-Object {
$findString = [regex]::escape('rootMgmtGroup')
$newRunbook = @()
Get-Content $_ | `
ForEach-Object {
if ($_ -match $findString) {
$newRunbook += ($_ -replace $findString, $rootManagementGroup)
}
else {
$newRunbook += $_
}
}
$newRunbook | set-content "./runbooks/$($_.BaseName)-dynamic.ps1"
}
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to dynamically update Automation Runbooks due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
# Upload powershell runbooks to storage account
Write-Host "INFO: Uploading automation runbooks" -ForegroundColor green
Write-Verbose -Message "Uploading automation runbooks"
Get-ChildItem `
-File `
-Path ./runbooks/* `
-Include *-dynamic.ps1 | `
ForEach-Object {
Set-AzStorageBlobContent `
-Container "sub-builder" `
-File "$_" `
-Blob "runbooks/$($_.Name)" `
-Context $storageAccount.Context
}
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to upload Automation Runbooks due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
}
# Import blueprint if it doesn't already exist
$blueprint = Get-AzBlueprint `
-ManagementGroupId $rootManagementGroup `
-Name "sub-poc-blueprint" `
-ErrorAction SilentlyContinue
if ($blueprint) {
Write-Warning -Message "Blueprint has already been imported into Management Group: $rootManagementGroup, continuing with deployment..."
}
else {
try {
Write-Host "INFO: Importing Blueprint: 'sub-poc-blueprint' into $rootManagementGroup" -ForegroundColor green
Write-Verbose -Message "Importing Blueprint: 'sub-poc-blueprint' into $rootManagementGroup"
Import-AzBlueprintWithArtifact `
-Name "sub-poc-blueprint" `
-ManagementGroupId $rootManagementGroup `
-InputPath ./blueprints/poc-blueprint/
$blueprint = Get-AzBlueprint `
-ManagementGroupId $rootManagementGroup `
-Name "sub-poc-blueprint"
# Publish blueprint
Write-Host "INFO: Publishing Blueprint: sub-poc-blueprint to Management Group: $rootManagementGroup" -ForegroundColor green
Write-Verbose -Message "Publishing Blueprint: sub-poc-blueprint to Management Group: $rootManagementGroup"
Publish-AzBlueprint `
-Blueprint $blueprint `
-Version '1.0'
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to publish Blueprint due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
}
try {
# Deploy ARM template to create automation account, runbook, and modules
Write-Host "INFO: Deploying ARM template to create Automation Account, Modules and Runbooks" -ForegroundColor green
Write-Verbose -Message "Deploying ARM Template to create Automation Account, Modules and Runbooks"
$automationParams = @{
'baseStorageUrl' = $baseStorageUrl;
'automationAccountName' = "$Name-autoacct";
'diagSettingsName' = "$Name-autoacct-diagsettings";
'workspaceId' = "/subscriptions/$subId/resourcegroups/$Name-rg/providers/microsoft.operationalinsights/workspaces/$Name-workspace";
'appId' = $sp.ApplicationId;
'tenantId' = $tenantId;
'thumbprint' = $Thumbprint;
'certValue' = $Base64Value
}
New-AzResourceGroupDeployment `
-ResourceGroupName "$Name-rg" `
-TemplateFile ./infra_templates/automationAccount/automationAcct.json `
-TemplateParameterObject $automationParams
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to deploy Automation Account ARM Template due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
# Deploy ARM template to create Cosmos DB Account and Database
Write-Host "INFO: Deploying ARM template to create Cosmos DB Database" -ForegroundColor green
Write-Verbose -Message "Deploying ARM template to create Cosmos DB Database"
$dbParams = @{
'containerName' = "$Name-container";
'dbAccountName' = "$Name-dbacct";
'dbName' = "$Name-db";
'diagSettingsName' = "$Name-dbacct-diagsettings";
'locationName' = "$locationName";
'workspaceId' = "/subscriptions/$subId/resourcegroups/$Name-rg/providers/microsoft.operationalinsights/workspaces/$Name-workspace"
}
New-AzResourceGroupDeployment `
-ResourceGroupName "$Name-rg" `
-TemplateFile ./infra_templates/cosmosDB/cosmosDb.json `
-TemplateParameterObject $dbParams
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to deploy Cosmos DB ARM template due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
# Build Logic App parameter file dynamically
Write-Host "INFO: Deploying ARM template to create Logic App Workflow" -ForegroundColor green
Write-Verbose -Message "Building Logic App Parameter file... "
$laParamObj = (Get-Content -Path ./infra_templates/logicApp/LogicAppParams.json | ConvertFrom-Json)
$laParams = $laParamObj.parameters
$laParams.logicAppName.value = "$Name-logicapp"
$laParams.dbAccountName.value = "$Name-dbacct"
$laParams.dbContainerName.value = "$Name-container"
$laParams.dbName.value = "$Name-db"
$laParams.diagSettingsName.value = "$Name-logicapp-diagsettings"
$laParams.workspaceId.value = "/subscriptions/$subId/resourcegroups/$Name-rg/providers/microsoft.operationalinsights/workspaces/$Name-workspace"
$laParams.automationAccountName.value = "$Name-autoacct"
$laParams.appId.value = $sp.ApplicationId
$laParams.appSecret.reference.keyVault.id = "/subscriptions/$subId/resourceGroups/$Name-rg/providers/Microsoft.KeyVault/vaults/$Name-keyvault"
$laParams.appSecret.reference.secretName = "$Name-secret"
$laParams.tenantId.value = $tenantId
$laParamObj | ConvertTo-Json -Depth 100 | Out-File -FilePath "./infra_templates/logicApp/LogicAppDynamicParams.json" -Force
# Deploy ARM template to create logic app workflow
Write-Verbose -Message "Deploying ARM Template to create logic app workflow"
$la = New-AzResourceGroupDeployment `
-ResourceGroupName "$Name-rg" `
-TemplateFile ./infra_templates/logicApp/LogicApp.json `
-TemplateParameterFile ./infra_templates/logicApp/LogicAppDynamicParams.json
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to deploy Logic App ARM Template due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
if ($webserver -eq $true) {
# Create webserver resource group if it doesn't already exist
$webrgcheck = Get-AzResourceGroup -Name "$Name-webserver-rg" -ErrorAction SilentlyContinue
if (!$webrgcheck) {
Write-Host "INFO: Creating new resource group: $Name-webserver-rg" -ForegroundColor green
Write-Verbose -Message "Creating new resource group: $Name-webserver-rg"
New-AzResourceGroup -Name "$Name-webserver-rg" -Location $location
}
else {
Write-Warning -Message "Resource Group: $Name-webserver-rg already exists. Continuing with deployment..."
}
try {
# Enable Static Website and upload contents if selected
# Gather Logic App endpoint and edit/create the "webFormDynamic.html" for website content
$laEndpoint = $la.Outputs.Values.Value
$test_regex = [regex]::escape('fetch(')
$la_regex = [regex]::escape('LogicAppURL')
$new_html = @()
$test = $false
# Replace 'LogicAppURL' with actual Logic App endpoint.
Write-Host "INFO: Dynamically updating webForm.html" -ForegroundColor green
Write-Verbose -Message "Dynamically updating webForm.html"
get-content ./webserver/webForm.html |
ForEach-Object {
if ($_ -match $test_regex){
$test = $true
}
if ($test){
if ($_ -match $la_regex){
$new_html += ($_ -replace $la_regex, $laEndpoint)
}
else {$new_html += $_}
}
else {$new_html += $_}
}
$new_html | set-content ./webserver/deployments/webFormDynamic.html
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to update webFormDynamic.html with Logic App endpoint, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
Write-Host "INFO: Dynamically updating boostrap script install.sh" -ForegroundColor green
Write-Verbose -Message "Dynamically updating bootstrap script"
$webFormUri = "$($baseStorageUrl)/webserver/webFormDynamic.html"
$errorPageUri = "$($baseStorageUrl)/webserver/errorPage.html"
$imageUri = "$($baseStorageUrl)/webserver/spring-cloud.jpg"
$findWeb = [regex]::escape('wForm')
$findError = [regex]::escape('ePage')
$findImage = [regex]::escape('bImage')
$newInstall = @()
Get-Content ./webserver/install.sh | `
ForEach-Object {
if ($_ -match $findWeb) {
$newInstall += ($_ -replace $findWeb, $webFormUri)
}
elseif ($_ -match $findError) {
$newInstall += ($_ -replace $findError, $errorPageUri)
}
elseif ($_ -match $findImage) {
$newInstall += ($_ -replace $findImage, $imageUri)
}
else {
$newInstall += $_
}
}
$newInstall | set-content "./webserver/deployments/installDynamic.sh"
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to dynamically update Bootstrap Script due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
Write-Host "INFO: Uploading static website content to Storage Account: $($storageAccount.StorageAccountName)" -ForegroundColor Green
Write-Verbose -Message "Uploading static website content to Storage Account: $($storageAccount.StorageAccountName)"
Get-ChildItem `
-File `
-Path ./webserver/deployments/* | `
ForEach-Object {
Set-AzStorageBlobContent `
-Container "sub-builder" `
-File "$_" `
-Blob "webserver/$($_.Name)" `
-Context $storageAccount.Context `
-Properties @{"ContentType" = "text/html"}
}
Set-AzStorageBlobContent `
-Container "sub-builder" `
-File "./images/website/spring-cloud.jpg" `
-Blob "webserver/spring-cloud.jpg" `
-Context $storageAccount.Context `
-Properties @{"ContentType" = "image/jpeg"}
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to upload static website content to Storage Account due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
try {
Write-Host "INFO: Deploying ARM Template to create Webserver and associated resources." -ForegroundColor green
Write-Verbose -Message "Building webserver parameter file..."
$webParamObj = (Get-Content -Path ./infra_templates/webserver/webserverParams.json | ConvertFrom-Json)
$webParams = $webParamObj.parameters
$webParams.adminUsername.value = $Name
$webParams.dnsNameForPublicIP.value = "$Name-webserver"
$webParams.ubuntuOSVersion.value = "18.04-LTS"
$webParams.authenticationType.value = "sshPublicKey"
$webParams.adminPasswordOrKey.value = $webserverSshKey
$webParams.artifactsLocation.value = "$($baseStorageUrl)/webserver/"
$webParams.webserverAdminCidr.value = $webserverAdminCidr
$webParamObj | ConvertTo-Json -Depth 100 | Out-File -FilePath "./infra_templates/webserver/webserverDynamicParams.json" -Force
# Deploy ARM template to create webserver
New-AzResourceGroupDeployment `
-ResourceGroupName "$($Name)-webserver-rg" `
-TemplateFile ./infra_templates/webserver/webserver.json `
-TemplateParameterFile ./infra_templates/webserver/webserverDynamicParams.json
$fqdn = Get-AzPublicIpAddress -ResourceGroupName "$($Name)-webserver-rg"
Write-Host "INFO: Azure Subscription Builder can now be accessed via this URL: $($fqdn.DnsSettings.Fqdn)" -ForegroundColor green
}
catch {
$_ | Out-File -FilePath $logFile -Append
Write-Host "ERROR: Unable to deploy webserver ARM Template due to an exception, see $logFile for detailed information!" -ForegroundColor red
exit
}
}
else {
Write-Warning -Message "Front end deployment is being skipped based on earlier selection"
}
Write-Host "INFO: Subscription Builder deployment has completed successfully!" -ForegroundColor green