-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathStart-AzDeploy.psm1
391 lines (335 loc) · 15.5 KB
/
Start-AzDeploy.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
#Requires -PSEdition Core
<#
.SYNOPSIS
Short description
.DESCRIPTION
Long description
.EXAMPLE
PS C:/> <example usage>
Explanation of what the example does
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
General notes
#>
Function global:Start-AzDeploy
{
[CmdletBinding()]
param (
[string] $Artifacts = (Get-Item -Path "$PSScriptRoot/.."),
[string] $DSCSourceFolder = $Artifacts + '/ext-DSC',
[alias('TF')]
[string] $TemplateFile = "$Artifacts/01-deploy-ALL.json",
[parameter(mandatory)]
[alias('DP')]
[string] $Deployment,
[ValidateScript({
$tenants = (Get-ChildItem -Path $PSScriptRoot/.. -Filter Tenants -Recurse | Get-ChildItem | ForEach-Object Name)
if ($_ -in $tenants) { $true }else { throw "Tenant [$_] not found in [$tenants]" }
})]
[alias('AppName')]
[string] $App,
[validateset('AEU1', 'AEU2', 'ACU1', 'AWCU', 'AWU1', 'AWU2', 'AWU3')]
[String] $Prefix,
[alias('CommonName')]
[string] $CN = '.',
[alias('ChildName')]
[string] $CN2 = '.',
# When deploying VM's, this is a subset of AppServers e.g. AppServers, SQLServers, ADPrimary
[string] $DeploymentName = ($Prefix + '-' + $App + '-' + $Deployment + '-' + (Get-ChildItem $TemplateFile).BaseName),
[switch] $FullUpload,
[switch] $WhatIf,
[switch] $NoPackage,
# [switch] $Legacy,
[validateset('ResourceIdOnly', 'FullResourcePayloads')]
[String] $WhatIfFormat = 'ResourceIdOnly',
[switch] $noresource
)
$Global = @{ }
# Read in the Rolegroups Lookup.
$RolesGroupsLookup = Get-Content -Path $Artifacts/tenants/$App/Global-Config.json | ConvertFrom-Json -Depth 10 | ForEach-Object RolesGroupsLookup
$Global.Add('RolesGroupsLookup', ($RolesGroupsLookup | ConvertTo-Json -Compress -Depth 10))
# Read in the Prefix Lookup for the Region.
$PrefixLookup = Get-Content $Artifacts/bicep/global/prefix.json | ConvertFrom-Json
$Global.Add('PrefixLookup', ($PrefixLookup | ConvertTo-Json -Compress -Depth 10))
$ResourceGroupLocation = $PrefixLookup | ForEach-Object $Prefix | ForEach-Object location
$GlobalGlobal = Get-Content -Path $Artifacts/tenants/$App/Global-Global.json | ConvertFrom-Json -Depth 10 | ForEach-Object Global
$Regional = Get-Content -Path $Artifacts/tenants/$App/Global-$Prefix.json | ConvertFrom-Json -Depth 10 | ForEach-Object Global
$PrimaryLocation = $GlobalGlobal.PrimaryLocation
$LocationLookup = Get-Content -Path $PSScriptRoot/../bicep/global/region.json | ConvertFrom-Json
$PrimaryPrefix = $LocationLookup.$PrimaryLocation.Prefix
$GlobalSA = $GlobalGlobal.GlobalSA
$saglobalsuffix = $GlobalSA.name
$ResourceGroupName = $prefix + '-' + $GlobalGlobal.OrgName + '-' + $App + '-RG-' + $Deployment
# Convert any objects back to string so they are not deserialized
$GlobalGlobal | Get-Member -MemberType NoteProperty | ForEach-Object {
if ($_.Definition -match 'PSCustomObject')
{
$Object = $_.Name
$String = $GlobalGlobal.$Object | ConvertTo-Json -Compress -Depth 10
$GlobalGlobal.$Object = $String
}
}
# Convert any objects back to string so they are not deserialized
$Regional | Get-Member -MemberType NoteProperty | ForEach-Object {
if ($_.Definition -match 'PSCustomObject')
{
$Object = $_.Name
$String = $Regional.$Object | ConvertTo-Json -Compress -Depth 10
$Regional.$Object = $String
}
}
# Merge regional with Global
$Regional | Get-Member -MemberType NoteProperty | ForEach-Object {
$Property = $_.Name
$Value = $Regional.$Property
$GlobalGlobal | Add-Member NoteProperty -Name $Property -Value $Value
}
$GlobalGlobal | Get-Member -MemberType NoteProperty | ForEach-Object {
$Property = $_.Name
$Global.Add($Property, $GlobalGlobal.$Property)
}
$Global.Add('CN', $CN)
$Global.Add('CN2', $CN2)
# $Global
#region Only needed for extensions such as DSC or Script extension
$StorageAccountName = ("{0}{1}{2}{3}sa${saglobalsuffix}" -f ($GlobalSA.Prefix ?? $PrimaryPrefix),
($GlobalSA.OrgName ?? $Global.OrgName), ($GlobalSA.AppName ?? $Global.AppName), ($GlobalSA.RG ?? 'g1')).tolower()
$StorageAccount = Get-AzStorageAccount | Where-Object { $_.StorageAccountName -eq $StorageAccountName }
$User = ((Get-AzContext | ForEach-Object account | ForEach-Object id) -split '@')[0]
$StorageContainerName = "$Prefix-$App-stageartifacts-$User".ToLowerInvariant()
$TemplateURIBase = $StorageAccount.Context.BlobEndPoint + $StorageContainerName
Write-Verbose "Storage Account is: [$StorageAccountName] and container is: [$StorageContainerName]" -Verbose
$null = (Get-Content -Path $TemplateFile -Raw) -match "targetScope.*=.*'(?<scope>.+)'"
$DeploymentScope = $Matches.scope ?? 'resourceGroup'
# Do not create the Resource Groups in this file anymore, only validate that it exists.
if ($DeploymentScope -eq 'ResourceGroup' -and ! $noresource)
{
if ( -not (Get-AzResourceGroup -Name $ResourceGroupName -Verbose -ErrorAction SilentlyContinue))
{
$globalstorage = Get-AzStorageAccount | Where-Object StorageAccountName -Match g1saglobal | ForEach-Object ResourceGroupName
Write-Output "`n"
$Message = "[$ResourceGroupName] does not exist, switch Subscription OR SubscriptionDeploy, currently using: [$globalstorage]!!!"
Write-Verbose -Message "$('*' * ($Message.length + 8))" -Verbose
Write-Error -Message $Message -EA continue
Write-Verbose -Message "$('*' * ($Message.length + 8))" -Verbose
break
}
}
#region prepare upload artifacts
if ($Deployment -ne 'G1' -and $DeploymentScope -eq 'ResourceGroup')
{
<#
Generate SAS in the deployment now
$SASParams = @{
Container = $StorageContainerName
Context = $StorageAccount.Context
Permission = 'r'
ExpiryTime = (Get-Date).AddHours(4)
}
$queryString = (New-AzStorageContainerSASToken @SASParams).Substring(1)
$Global.Add('_artifactsLocationSasToken', "?${queryString}")
#>
$Global.Add('_artifactsLocation', $TemplateURIBase)
# Create the storage container only if it doesn't already exist
if ( -not (Get-AzStorageContainer -Name $StorageContainerName -Context $StorageAccount.Context -Verbose -ErrorAction SilentlyContinue))
{
New-AzStorageContainer -Name $StorageContainerName -Context $StorageAccount.Context -ErrorAction SilentlyContinue *>&1
}
}
#endregion
if ( -not $FullUpload -and $Deployment -ne 'G1' -and $DeploymentScope -eq 'ResourceGroup')
{
$Include = @(
"$Artifacts/ext-DSC/"
)
# Create DSC configuration archive only for the files that changed
git -C $DSCSourceFolder diff --diff-filter d --name-only $Include |
Where-Object { $_ -match 'ps1$' } | ForEach-Object {
# ignore errors on git diff for deleted files
$File = Get-Item -EA Ignore -Path (Join-Path -ChildPath $_ -Path (Split-Path -Path $Artifacts))
if ($File)
{
$DSCArchiveFilePath = $File.FullName.Substring(0, $File.FullName.Length - 4) + '.zip'
Publish-AzVMDscConfiguration $File.FullName -OutputArchivePath $DSCArchiveFilePath -Force -Verbose
}
else
{
Write-Verbose -Message "File not found, assume deleted, will not upload [$_]"
}
}
# Upload only files that changes since last git add, i.e. only for the files that changed,
# use -fullupload to upload ALL files
# only look in the 3 templates directories for uploading files
$Include = @(
"$Artifacts/ext-DSC/",
"$Artifacts/ext-CD/",
"$Artifacts/ext-Scripts/"
)
git -C $Artifacts diff --diff-filter d --name-only $Include | ForEach-Object {
# ignore errors on git diff for deleted files
# added --diff-filter above, so likely don't need this anymore, will leave it anyway
$File = Get-Item -EA Ignore -Path (Join-Path -ChildPath $_ -Path (Split-Path -Path $Artifacts))
if ($File)
{
$StorageParams = @{
File = $File.FullName
Blob = $File.FullName.Substring($Artifacts.length + 1)
Container = $StorageContainerName
Context = $StorageAccount.Context
Force = $true
}
Set-AzStorageBlobContent @StorageParams | Select-Object Name, Length, LastModified
}
else
{
Write-Verbose -Message "File not found, assume deleted, will not upload [$_]"
}
}
Start-Sleep -Seconds 2
}
elseif ($Deployment -ne 'G1' -and $DeploymentScope -eq 'ResourceGroup')
{
if ((Test-Path $DSCSourceFolder) -and (-not $NoPackage))
{
Get-ChildItem $DSCSourceFolder -File -Filter '*.ps1' | ForEach-Object {
$DSCArchiveFilePath = $_.FullName.Substring(0, $_.FullName.Length - 4) + '.zip'
Publish-AzVMDscConfiguration $_.FullName -OutputArchivePath $DSCArchiveFilePath -Force -Verbose
}
}
$Include = @(
# no longer uploading any templates only extensions
'ext-DSC', 'ext-CD', 'ext-Scripts'
)
Get-ChildItem -Path $Artifacts -Include $Include -Recurse -Directory |
Get-ChildItem -File -Include *.json, *.zip, *.psd1, *.sh, *.ps1 | ForEach-Object {
# $_.FullName.Substring($Artifacts.length)
$StorageParams = @{
File = $_.FullName
Blob = $_.FullName.Substring($Artifacts.length + 1 )
Container = $StorageContainerName
Context = $StorageAccount.Context
Force = $true
}
Set-AzStorageBlobContent @StorageParams
} | Select-Object Name, Length, LastModified
}
#endregion
$TemplateArgs = @{ }
$OptionalParameters = @{ }
$OptionalParameters['Global'] = $Global
$OptionalParameters['Environment'] = $Deployment.substring(0, 1)
$OptionalParameters['DeploymentID'] = $Deployment.substring(1)
$BaseParam = "$Artifacts/tenants/$App/$Prefix.$Deployment"
$TemplateParametersFile = (Test-Path -Path "${BaseParam}.bicepparam") ? "${BaseParam}.bicepparam" : "${BaseParam}.parameters.json"
Write-Warning -Message "Using parameter file: [$TemplateParametersFile]"
$TemplateArgs.Add('TemplateParameterFile', $TemplateParametersFile)
Write-Warning -Message "Using template file: [$TemplateFile]"
$TemplateFile = Get-Item -Path $TemplateFile | ForEach-Object FullName
Write-Warning -Message "Using template File: [$TemplateFile]"
$TemplateArgs.Add('TemplateFile', $TemplateFile)
$OptionalParameters.getenumerator() | ForEach-Object {
Write-Verbose $_.Key -Verbose
Write-Warning $_.Value
}
$TemplateArgs.getenumerator() | Where-Object Key -NE 'queryString' | ForEach-Object {
Write-Verbose $_.Key -Verbose
Write-Warning $_.Value
}
$Common = @{
Name = $DeploymentName
Location = $ResourceGroupLocation
Verbose = $true
ErrorAction = 'Continue'
ErrorVariable = 'e'
}
Write-Verbose -Message "Deployment scope is [$DeploymentScope]" -Verbose
switch ($Deployment)
{
# Tenant
'T0'
{
Write-Output 'T0'
if ($WhatIf)
{
$Common.Remove('Name')
$Common['ResultFormat'] = $WhatIfFormat
Get-AzTenantDeploymentWhatIfResult @Common @TemplateArgs @OptionalParameters
}
else
{
$global:r = New-AzTenantDeployment @Common @TemplateArgs @OptionalParameters
}
}
# ManagementGroup
'M0'
{
Write-Output 'M0'
$MGName = Get-AzManagementGroup | Where-Object displayname -Match 'Root Management Group|Tenant Root Group' | ForEach-Object Name
if ($WhatIf)
{
$Common.Remove('Name')
$Common['ResultFormat'] = $WhatIfFormat
Get-AzManagementGroupDeploymentWhatIfResult @Common @TemplateArgs @OptionalParameters -ManagementGroupId $MGName
}
else
{
$global:r = New-AzManagementGroupDeployment @Common @TemplateArgs @OptionalParameters -ManagementGroupId $MGName
}
}
Default
{
# Subscription
if ($DeploymentScope -eq 'Subscription')
{
if ($WhatIf)
{
$Common.Remove('Name')
$Common['ResultFormat'] = $WhatIfFormat
Get-AzDeploymentWhatIfResult @Common @TemplateArgs @OptionalParameters
}
else
{
$global:r = New-AzDeployment @Common @TemplateArgs @OptionalParameters
}
}
# ResourceGroup
else
{
$Common.Remove('Location')
$Common['ResourceGroupName'] = $ResourceGroupName
if ($WhatIf)
{
$Common.Remove('Name')
$Common['ResultFormat'] = $WhatIfFormat
Get-AzResourceGroupDeploymentWhatIfResult @Common @TemplateArgs @OptionalParameters
}
else
{
try
{
# $Common['ErrorAction'] = 'stop'
$global:r = New-AzResourceGroupDeployment @Common @TemplateArgs @OptionalParameters
}
catch
{
# Add logging trying to get info on the following:
# The request was canceled due to the configured
# HttpClient.Timeout of 100 seconds elapsing
$global:err = Resolve-AzError -Last
$err
$m = $err | ForEach-Object Message
throw $m
}
}
}
}
}
$Properties = 'ResourceGroupName', 'DeploymentName', 'ProvisioningState', 'Timestamp', 'Mode', 'CorrelationId'
$r | Select-Object -Property $Properties | Format-Table -AutoSize
} # Start-AzDeploy
New-Alias -Name AzDeploy -Value Start-AzDeploy -Force -Scope global
# Azure warnings suppression
Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings 'true'