-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathDeploy-Accelerator.ps1
More file actions
376 lines (324 loc) · 18.6 KB
/
Deploy-Accelerator.ps1
File metadata and controls
376 lines (324 loc) · 18.6 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
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
function Deploy-Accelerator {
<#
.SYNOPSIS
Deploys an accelerator according to the supplied inputs.
.DESCRIPTION
This function is used to deploy accelerators consisting or bootstrap and optionally starter modules. The accelerators are designed to simplify and speed up configuration of common Microsoft patterns, such as CI / CD for Azure Landing Zones.
.EXAMPLE
Deploy-Accelerator
.EXAMPLE
Deploy-Accelerator -c "./config.yaml" -o "."
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(
Mandatory = $false,
HelpMessage = "[REQUIRED] The configuration inputs in json, yaml or tfvars format. Environment variable: ALZ_input_config_path"
)]
[Alias("inputs")]
[Alias("c")]
[Alias("inputConfigFilePath")]
[string[]] $inputConfigFilePaths = @(),
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The additional files or folders to be copied directly to the starter module root folder. Environment variable: ALZ_starter_additional_files. Config file input: starter_additional_files."
)]
[Alias("saf")]
[Alias("starterAdditionalFiles")]
[string[]] $starter_additional_files = @(),
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The target directory for the accelerator working set of files. Defaults to current working folder. Environment variable: ALZ_output_folder_path. Config file input: output_folder_path."
)]
[Alias("output")]
[Alias("o")]
[Alias("targetDirectory")]
[string] $output_folder_path = ".",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The version tag of the bootstrap module release to download. Defaults to latest. Environment variable: ALZ_bootstrap_module_version. Config file input: bootstrap_module_version."
)]
[Alias("bv")]
[Alias("bootstrapRelease")]
[string] $bootstrap_module_version = "latest",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The version tag of the starter module release to download. Defaults to latest. Environment variable: ALZ_starter_module_version. Config file input: starter_module_version."
)]
[Alias("sv")]
[Alias("starterRelease")]
[string] $starter_module_version = "latest",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Determines whether to deploy the bootstrap without prompting for approval. This is used for automation. Environment variable: ALZ_auto_approve. Config file input: auto_approve."
)]
[Alias("aa")]
[Alias("autoApprove")]
[switch] $auto_approve,
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Determines that this run is to destroup the bootstrap. This is used to cleanup experiments. Environment variable: ALZ_destroy. Config file input: destroy."
)]
[Alias("d")]
[switch] $destroy,
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The bootstrap modules reposiotry url. This can be overridden for custom modules. Environment variable: ALZ_bootstrap_module_url. Config file input: bootstrap_module_url."
)]
[Alias("bu")]
[Alias("bootstrapModuleUrl")]
[string] $bootstrap_module_url = "https://github.com/Azure/accelerator-bootstrap-modules",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The bootstrap modules release artifact name. This can be overridden for custom modules. Environment variable: ALZ_bootstrap_module_release_artifact_name. Config file input: bootstrap_module_release_artifact_name."
)]
[Alias("ba")]
[Alias("bootstrapModuleReleaseArtifactName")]
[string] $bootstrap_module_release_artifact_name = "bootstrap_modules.zip",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The bootstrap config file path within the bootstrap module. This can be overridden for custom modules. Environment variable: ALZ_bootstrap_config_path. Config file input: bootstrap_config_path."
)]
[Alias("bc")]
[Alias("bootstrapConfigPath")]
[string] $bootstrap_config_path = ".config/ALZ-Powershell.config.json",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The folder that containes the bootstrap modules in the bootstrap repo. This can be overridden for custom modules. Environment variable: ALZ_bootstrap_source_folder. Config file input: bootstrap_source_folder."
)]
[Alias("bf")]
[Alias("bootstrapSourceFolder")]
[string] $bootstrap_source_folder = ".",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Used to override the bootstrap folder source. This can be used to provide a folder locally in restricted environments or dev. Environment variable: ALZ_bootstrapModuleOverrideFolderPath. Config file input: bootstrap_module_override_folder_path."
)]
[Alias("bo")]
[Alias("bootstrapModuleOverrideFolderPath")]
[string] $bootstrap_module_override_folder_path = "",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Used to override the starter folder source. This can be used to provide a folder locally in restricted environments. Environment variable: ALZ_starterModuleOverrideFolderPath. Config file input: starter_module_override_folder_path."
)]
[Alias("so")]
[Alias("starterModuleOverrideFolderPath")]
[string] $starter_module_override_folder_path = "",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Whether to skip checks that involve internet connection. The can allow running in restricted environments. Environment variable: ALZ_skip_internet_checks. Config file input: skip_internet_checks."
)]
[Alias("si")]
[Alias("skipInternetChecks")]
[switch] $skip_internet_checks,
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Whether to overwrite bootstrap and starter modules if they already exist. Warning, this may result in unexpected behaviour and should only be used for local development purposes. Environment variable: ALZ_replace_files. Config file input: replace_files."
)]
[Alias("rf")]
[Alias("replaceFiles")]
[switch] $replace_files,
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] An extra level of logging that is turned off by default for easier debugging. Environment variable: ALZ_write_verbose_logs. Config file input: write_verbose_logs."
)]
[Alias("v")]
[Alias("writeVerboseLogs")]
[switch] $write_verbose_logs,
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Determines whether to convert tfvars input files to tfvars.json files. Environment variable: ALZ_convert_tfvars_to_json. Config file input: convert_tfvars_to_json."
)]
[Alias("tj")]
[Alias("convertTfvarsToJson")]
[switch] $convert_tfvars_to_json,
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Determines whether to skip the requirements check. This is not recommended."
)]
[Alias("skipRequirementsCheck")]
[switch] $skip_requirements_check,
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Determines whether to skip the requirements check for the ALZ PowerShell Module version only. This is not recommended."
)]
[Alias("skipAlzModuleVersionRequirementsCheck")]
[switch] $skip_alz_module_version_requirements_check,
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Determines whether Clean the bootstrap folder of Terraform meta files. Only use for development purposes."
)]
[switch] $cleanBootstrapFolder
)
$ProgressPreference = "SilentlyContinue"
if ($skip_requirements_check.IsPresent) {
Write-InformationColored "WARNING: Skipping the software requirements check..." -ForegroundColor Yellow -InformationAction Continue
} else {
Write-InformationColored "Checking the software requirements for the Accelerator..." -ForegroundColor Green -InformationAction Continue
Test-Tooling -skipAlzModuleVersionCheck:$skip_alz_module_version_requirements_check.IsPresent
}
Write-InformationColored "Getting ready to deploy the accelerator with you..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
if ($PSCmdlet.ShouldProcess("Accelerator setup", "modify")) {
# Check and install tools needed
$toolsPath = Join-Path -Path $output_folder_path -ChildPath ".tools"
if ($skipInternetChecks) {
Write-InformationColored "Skipping Terraform tool check as you used the skipInternetCheck parameter. Please ensure you have the most recent version of Terraform installed" -ForegroundColor Yellow -InformationAction Continue
} else {
Write-InformationColored "Checking you have the latest version of Terraform installed..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
Get-TerraformTool -version "latest" -toolsPath $toolsPath
$hclParserToolPath = Get-HCLParserTool -toolVersion "v0.6.0" -toolsPath $toolsPath
}
# Get User Inputs from the input config file
$inputConfig = $null
if ($inputConfigFilePaths.Length -eq 0) {
$envInputConfigPaths = $env:ALZ_input_config_path
if ($null -ne $envInputConfigPaths -and $envInputConfigPaths -ne "") {
$inputConfigFilePaths = $envInputConfigPaths -split ","
} else {
Write-InformationColored "No input configuration file path has been provided. Please provide the path(s) to your configuration file(s)..." -ForegroundColor Red -InformationAction Continue
throw "No input configuration file path has been provided. Please provide the path(s) to your configuration file(s)..."
}
}
# Get the input config from yaml and json files
foreach ($inputConfigFilePath in $inputConfigFilePaths) {
$inputConfig = Get-ALZConfig -configFilePath $inputConfigFilePath -inputConfig $inputConfig -hclParserToolPath $hclParserToolPath
}
# Set accelerator input config from input file, environment variables or parameters
$parameters = (Get-Command -Name $MyInvocation.InvocationName).Parameters
$parametersWithValues = @{}
foreach ($parameterKey in $parameters.Keys) {
$parameter = $parameters[$parameterKey]
if ($parameter.IsDynamic) {
continue
}
$parameterValue = Get-Variable -Name $parameterKey -ValueOnly -ErrorAction SilentlyContinue
if ($null -ne $parameterValue) {
$parametersWithValues[$parameterKey] = @{
type = $parameters[$parameterKey].ParameterType.Name
value = $parameterValue
aliases = $parameter.Aliases
}
}
}
$inputConfig = Convert-ParametersToInputConfig -inputConfig $inputConfig -parameters $parametersWithValues
# Throw if IAC type is not specified
if (!$inputConfig.iac_type.Value) {
Write-InformationColored "No Infrastructure as Code type has been specified. Please supply the IAC type you wish to deploy..." -ForegroundColor Red -InformationAction Continue
throw "No Infrastructure as Code type has been specified. Please supply the IAC type you wish to deploy..."
}
if ($inputConfig.iac_type.Value.ToString() -like "bicep*") {
Write-InformationColored "Although you have selected Bicep, the Accelerator leverages the Terraform tool to bootstrap your Version Control System and Azure. This will not impact your choice of Bicep post this initial bootstrap. Please refer to our documentation for further details..." -ForegroundColor Yellow -InformationAction Continue
}
Write-Verbose "Initial Input config: $(ConvertTo-Json $inputConfig -Depth 100)"
# Download the bootstrap modules
$bootstrapReleaseTag = ""
$bootstrapPath = ""
$bootstrapTargetFolder = "bootstrap"
Write-InformationColored "Checking and Downloading the bootstrap module..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
$versionAndPath = New-ModuleSetup `
-targetDirectory $inputConfig.output_folder_path.Value `
-targetFolder $bootstrapTargetFolder `
-sourceFolder $inputConfig.bootstrap_source_folder.Value `
-url $inputConfig.bootstrap_module_url.Value `
-release $inputConfig.bootstrap_module_version.Value `
-releaseArtifactName $inputConfig.bootstrap_module_release_artifact_name.Value `
-moduleOverrideFolderPath $inputConfig.bootstrap_module_override_folder_path.Value `
-skipInternetChecks $inputConfig.skip_internet_checks.Value `
-replaceFile:$inputConfig.replace_files.Value
$bootstrapReleaseTag = $versionAndPath.releaseTag
$bootstrapPath = $versionAndPath.path
# Configure the starter module path
$starterFolder = "starter"
$starterModuleTargetFolder = $starterFolder
# Setup the variables for bootstrap and starter modules
$hasStarterModule = $false
$starterModuleUrl = ""
$starterModuleSourceFolder = "."
$starterReleaseArtifactName = ""
$starterConfigFilePath = ""
$bootstrapDetails = $null
$zonesSupport = $null
# Request the bootstrap type if not already specified
if(!$inputConfig.bootstrap_module_name.Value) {
Write-InformationColored "No bootstrap module has been specified. Please supply the bootstrap module you wish to deploy..." -ForegroundColor Red -InformationAction Continue
throw "No bootstrap module has been specified. Please supply the bootstrap module you wish to deploy..."
}
$bootstrap_module_name = $inputConfig.bootstrap_module_name.Value.Trim()
$bootstrapAndStarterConfig = Get-BootstrapAndStarterConfig `
-iac $inputConfig.iac_type.Value `
-bootstrap $bootstrap_module_name `
-bootstrapPath $bootstrapPath `
-bootstrapConfigPath $inputConfig.bootstrap_config_path.Value `
-toolsPath $toolsPath
$bootstrapDetails = $bootstrapAndStarterConfig.bootstrapDetails
$hasStarterModule = $bootstrapAndStarterConfig.hasStarterModule
$starterModuleUrl = $bootstrapAndStarterConfig.starterModuleUrl
$starterModuleSourceFolder = $bootstrapAndStarterConfig.starterModuleSourceFolder
$starterReleaseArtifactName = $bootstrapAndStarterConfig.starterReleaseArtifactName
$starterConfigFilePath = $bootstrapAndStarterConfig.starterConfigFilePath
$zonesSupport = $bootstrapAndStarterConfig.zonesSupport
# Download the starter modules
$starterReleaseTag = ""
$starterConfig = $null
if ($hasStarterModule) {
Write-InformationColored "Checking and downloading the starter module..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
$versionAndPath = New-ModuleSetup `
-targetDirectory $inputConfig.output_folder_path.Value `
-targetFolder $starterModuleTargetFolder `
-sourceFolder $starterModuleSourceFolder `
-url $starterModuleUrl `
-release $inputConfig.starter_module_version.Value `
-releaseArtifactName $starterReleaseArtifactName `
-moduleOverrideFolderPath $inputConfig.starter_module_override_folder_path.Value `
-skipInternetChecks $inputConfig.skip_internet_checks.Value `
-replaceFile:$inputConfig.replace_files.Value
$starterReleaseTag = $versionAndPath.releaseTag
$starterPath = $versionAndPath.path
$starterConfig = Get-StarterConfig -starterPath $starterPath -starterConfigPath $starterConfigFilePath
}
# Set computed interface inputs
$inputConfig | Add-Member -MemberType NoteProperty -Name "bicep_config_file_path" -Value @{
Value = $starterConfigFilePath
Source = "calculated"
}
$inputConfig | Add-Member -MemberType NoteProperty -Name "on_demand_folder_repository" -Value @{
Value = $starterModuleUrl
Source = "calculated"
}
$inputConfig | Add-Member -MemberType NoteProperty -Name "on_demand_folder_artifact_name" -Value @{
Value = $starterReleaseArtifactName
Source = "calculated"
}
$inputConfig | Add-Member -MemberType NoteProperty -Name "release_version" -Value @{
Value = ($starterReleaseTag -eq "local" ? $inputConfig.starter_module_version.Value : $starterReleaseTag)
Source = "calculated"
}
$inputConfig | Add-Member -MemberType NoteProperty -Name "time_stamp" -Value @{
Value = (Get-Date).ToString("yyyy-MM-dd-HH-mm-ss")
Source = "calculated"
}
# Run the bootstrap
$bootstrapTargetPath = Join-Path $inputConfig.output_folder_path.Value $bootstrapTargetFolder
$starterTargetPath = Join-Path $inputConfig.output_folder_path.Value $starterFolder
New-Bootstrap `
-iac $inputConfig.iac_type.Value `
-bootstrapDetails $bootstrapDetails `
-inputConfig $inputConfig `
-bootstrapTargetPath $bootstrapTargetPath `
-bootstrapRelease $bootstrapReleaseTag `
-hasStarter:$hasStarterModule `
-starterTargetPath $starterTargetPath `
-starterRelease $starterReleaseTag `
-starterConfig $starterConfig `
-autoApprove:$inputConfig.auto_approve.Value `
-destroy:$inputConfig.destroy.Value `
-zonesSupport $zonesSupport `
-writeVerboseLogs:$inputConfig.write_verbose_logs.Value `
-hclParserToolPath $hclParserToolPath `
-convertTfvarsToJson:$inputConfig.convert_tfvars_to_json.Value `
-inputConfigFilePaths $inputConfigFilePaths `
-starterAdditionalFiles $inputConfig.starter_additional_files.Value `
-cleanBootstrapFolder:$cleanBootstrapFolder.IsPresent
}
$ProgressPreference = "Continue"
return
}