forked from Azure/ALZ-PowerShell-Module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-Bootstrap.ps1
More file actions
287 lines (231 loc) · 14.4 KB
/
New-Bootstrap.ps1
File metadata and controls
287 lines (231 loc) · 14.4 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
function New-Bootstrap {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $false)]
[string] $iac,
[Parameter(Mandatory = $false)]
[PSCustomObject] $bootstrapDetails,
[Parameter(Mandatory = $false)]
[PSCustomObject] $validationConfig,
[Parameter(Mandatory = $false)]
[PSCustomObject] $inputConfig,
[Parameter(Mandatory = $false)]
[string] $bootstrapTargetPath,
[Parameter(Mandatory = $false)]
[switch] $hasStarter,
[Parameter(Mandatory = $false)]
[string] $starterTargetPath,
[Parameter(Mandatory = $false)]
[PSCustomObject] $starterConfig = $null,
[Parameter(Mandatory = $false)]
[string] $bootstrapRelease,
[Parameter(Mandatory = $false)]
[string] $starterRelease,
[Parameter(Mandatory = $false)]
[switch] $autoApprove,
[Parameter(Mandatory = $false)]
[switch] $destroy,
[Parameter(Mandatory = $false)]
[switch] $planOnly,
[Parameter(Mandatory = $false)]
[PSCustomObject] $zonesSupport = $null,
[Parameter(Mandatory = $false, HelpMessage = "An extra level of logging that is turned off by default for easier debugging.")]
[switch]
$writeVerboseLogs,
[Parameter(Mandatory = $false)]
[string]
$hclParserToolPath,
[Parameter(Mandatory = $false)]
[switch]
$convertTfvarsToJson,
[Parameter(Mandatory = $false)]
[string[]]
$inputConfigFilePaths = @(),
[Parameter(Mandatory = $false)]
[string[]]
$starterAdditionalFiles = @()
)
if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) {
$bootstrapPath = Join-Path $bootstrapTargetPath $bootstrapRelease
$starterPath = Join-Path $starterTargetPath $starterRelease
$bootstrapModulePath = Join-Path -Path $bootstrapPath -ChildPath $bootstrapDetails.Value.location
Write-Verbose "Bootstrap Module Path: $bootstrapModulePath"
# Run upgrade
Invoke-FullUpgrade `
-bootstrapModuleFolder $bootstrapDetails.Value.location `
-bootstrapRelease $bootstrapRelease `
-bootstrapPath $bootstrapTargetPath `
-autoApprove:$autoApprove.IsPresent
# Get starter module
$starterModulePath = ""
$starterRootModuleFolder = ""
$starterRootModuleFolderPath = ""
$starterFoldersToRetain = @()
if($hasStarter) {
if($inputConfig.starter_module_name.Value -eq "") {
$inputConfig.starter_module_name = @{
Value = Request-SpecialInput -type "starter" -starterConfig $starterConfig
Source = "user"
}
}
$chosenStarterConfig = $starterConfig.starter_modules.Value.$($inputConfig.starter_module_name.Value)
Write-Verbose "Selected Starter: $($inputConfig.starter_module_name.Value))"
$starterModulePath = (Resolve-Path (Join-Path -Path $starterPath -ChildPath $chosenStarterConfig.location)).Path
$starterRootModuleFolderPath = $starterModulePath
Write-Verbose "Starter Module Path: $starterModulePath"
if($chosenStarterConfig.PSObject.Properties.Name -contains "additional_retained_folders") {
$starterFoldersToRetain = $chosenStarterConfig.additional_retained_folders
Write-Verbose "Starter Additional folders to retain: $($starterFoldersToRetain -join ",")"
}
if($chosenStarterConfig.PSObject.Properties.Name -contains "root_module_folder") {
$starterRootModuleFolder = $chosenStarterConfig.root_module_folder
# Retain the root module folder
$starterFoldersToRetain += $starterRootModuleFolder
# Add the root module folder to bootstrap input config
$inputConfig | Add-Member -NotePropertyName "root_module_folder_relative_path" -NotePropertyValue @{
Value = $starterRootModuleFolder
Source = "caluated"
}
# Set the starter root module folder full path
$starterRootModuleFolderPath = Join-Path -Path $starterModulePath -ChildPath $starterRootModuleFolder
Write-Verbose "Starter root module folder: $starterRootModuleFolder"
Write-Verbose "Starter final folders to retain: $($starterFoldersToRetain -join ",")"
}
}
# Getting configuration for the bootstrap module user input
$bootstrapParameters = [PSCustomObject]@{}
Write-Verbose "Getting the bootstrap configuration..."
$terraformFiles = Get-ChildItem -Path $bootstrapModulePath -Filter "*.tf" -File
foreach($terraformFile in $terraformFiles) {
$bootstrapParameters = Convert-HCLVariablesToInputConfig -targetVariableFile $terraformFile.FullName -hclParserToolPath $hclParserToolPath -validators $validationConfig -appendToObject $bootstrapParameters
}
# Getting the configuration for the starter module user input
$starterParameters = [PSCustomObject]@{}
if($hasStarter) {
Write-Verbose "Getting the starter configuration..."
if($iac -eq "terraform") {
$terraformFiles = Get-ChildItem -Path $starterRootModuleFolderPath -Filter "*.tf" -File
foreach($terraformFile in $terraformFiles) {
$starterParameters = Convert-HCLVariablesToInputConfig -targetVariableFile $terraformFile.FullName -hclParserToolPath $hclParserToolPath -validators $validationConfig -appendToObject $starterParameters
}
}
if($iac -eq "bicep") {
$starterParameters = Convert-BicepConfigToInputConfig -bicepConfig $starterConfig.starter_modules.Value.$($inputConfig.starter_module_name.Value) -validators $validationConfig
}
}
# Set computed inputs
$inputConfig | Add-Member -NotePropertyName "module_folder_path" -NotePropertyValue @{
Value = $starterModulePath
Source = "calculated"
}
$inputConfig | Add-Member -NotePropertyName "availability_zones_bootstrap" -NotePropertyValue @{
Value = @(Get-AvailabilityZonesSupport -region $inputConfig.bootstrap_location.Value -zonesSupport $zonesSupport)
Source = "calculated"
}
if($inputConfig.PSObject.Properties.Name -contains "starter_location" -and $inputConfig.PSObject.Properties.Name -notcontains "starter_locations") {
Write-Verbose "Converting starter_location $($inputConfig.starter_location.Value) to starter_locations..."
$inputConfig | Add-Member -NotePropertyName "starter_locations" -NotePropertyValue @{
Value = @($inputConfig.starter_location.Value)
Source = "calculated"
}
}
if($inputConfig.PSObject.Properties.Name -contains "starter_locations") {
$availabilityZonesStarter = @()
foreach($region in $inputConfig.starter_locations.Value) {
$availabilityZonesStarter += , @(Get-AvailabilityZonesSupport -region $region -zonesSupport $zonesSupport)
}
$inputConfig | Add-Member -NotePropertyName "availability_zones_starter" -NotePropertyValue @{
Value = $availabilityZonesStarter
Source = "calculated"
}
}
Write-Verbose "Final Input config: $(ConvertTo-Json $inputConfig -Depth 100)"
# Getting the input for the bootstrap module
Write-Verbose "Setting the configuration for the bootstrap module..."
$bootstrapConfiguration = Set-Config `
-configurationParameters $bootstrapParameters `
-inputConfig $inputConfig
# Getting the input for the starter module
Write-Verbose "Setting the configuration for the starter module..."
$starterConfiguration = Set-Config `
-configurationParameters $starterParameters `
-inputConfig $inputConfig `
-copyEnvVarToConfig
Write-Verbose "Final Starter Parameters: $(ConvertTo-Json $starterParameters -Depth 100)"
# Creating the tfvars files for the bootstrap and starter module
$tfVarsFileName = "terraform.tfvars.json"
$bootstrapTfvarsPath = Join-Path -Path $bootstrapModulePath -ChildPath $tfVarsFileName
$starterTfvarsPath = Join-Path -Path $starterRootModuleFolderPath -ChildPath "terraform.tfvars.json"
$starterBicepVarsPath = Join-Path -Path $starterModulePath -ChildPath "parameters.json"
# Write the tfvars file for the bootstrap and starter module
Write-TfvarsJsonFile -tfvarsFilePath $bootstrapTfvarsPath -configuration $bootstrapConfiguration
if($iac -eq "terraform") {
if($starterFoldersToRetain.Length -gt 0) {
Write-Verbose "Removing unwanted folders from the starter module..."
$folders = Get-ChildItem -Path $starterModulePath -Directory
foreach($folder in $folders) {
if($starterFoldersToRetain -notcontains $folder.Name) {
Write-Verbose "Removing folder: $($folder.FullName)"
Remove-Item -Path $folder.FullName -Recurse -Force
} else {
Write-Verbose "Retaining folder: $($folder.FullName)"
Remove-TerraformMetaFileSet -path $folder.FullName -writeVerboseLogs:$writeVerboseLogs.IsPresent
}
}
}
Remove-TerraformMetaFileSet -path $starterModulePath -writeVerboseLogs:$writeVerboseLogs.IsPresent
if($convertTfvarsToJson) {
Write-TfvarsJsonFile -tfvarsFilePath $starterTfvarsPath -configuration $starterConfiguration
} else {
$inputsFromTfvars = $inputConfig.PSObject.Properties | Where-Object { $_.Value.Source -eq ".tfvars" } | Select-Object -ExpandProperty Name
Write-TfvarsJsonFile -tfvarsFilePath $starterTfvarsPath -configuration $starterConfiguration -skipItems $inputsFromTfvars
foreach($inputConfigFilePath in $inputConfigFilePaths | Where-Object { $_ -like "*.tfvars" }) {
$fileName = [System.IO.Path]::GetFileName($inputConfigFilePath)
$fileName = $fileName.Replace(".tfvars", ".auto.tfvars")
$destination = Join-Path -Path $starterRootModuleFolderPath -ChildPath $fileName
Write-Verbose "Copying tfvars file $inputConfigFilePath to $destination"
Copy-Item -Path $inputConfigFilePath -Destination $destination -Force
}
}
# Copy additional files
foreach($additionalFile in $starterAdditionalFiles) {
if(Test-Path $additionalFile -PathType Container) {
$folderName = ([System.IO.DirectoryInfo]::new($additionalFile)).Name
$destination = Join-Path -Path $starterRootModuleFolderPath -ChildPath $folderName
Write-Verbose "Copying folder $additionalFile to $destination"
Copy-Item -Path "$additionalFile/*" -Destination $destination -Recurse -Force
} else {
$fileName = [System.IO.Path]::GetFileName($inputConfigFilePath)
$destination = Join-Path -Path $starterRootModuleFolderPath -ChildPath $fileName
Write-Verbose "Copying file $additionalFile to $destination"
Copy-Item -Path $additionalFile -Destination $destination -Force
}
}
}
if($iac -eq "bicep") {
Copy-ParametersFileCollection -starterPath $starterModulePath -configFiles $starterConfig.starter_modules.Value.$($inputConfig.starter_module_name.Value).deployment_files
Set-ComputedConfiguration -configuration $starterConfiguration
Edit-ALZConfigurationFilesInPlace -alzEnvironmentDestination $starterModulePath -configuration $starterConfiguration
Write-JsonFile -jsonFilePath $starterBicepVarsPath -configuration $starterConfiguration
# Remove unrequired files
$foldersOrFilesToRetain = $starterConfig.starter_modules.Value.$($inputConfig.starter_module_name.Value).folders_or_files_to_retain
$foldersOrFilesToRetain += "parameters.json"
$foldersOrFilesToRetain += "config"
$foldersOrFilesToRetain += "starter-cache.json"
foreach($deployment_file in $starterConfig.starter_modules.Value.$($inputConfig.starter_module_name.Value).deployment_files) {
$foldersOrFilesToRetain += $deployment_file.templateParametersSourceFilePath
}
$subFoldersOrFilesToRemove = $starterConfig.starter_modules.Value.$($inputConfig.starter_module_name.Value).subfolders_or_files_to_remove
Remove-UnrequiredFileSet -path $starterModulePath -foldersOrFilesToRetain $foldersOrFilesToRetain -subFoldersOrFilesToRemove $subFoldersOrFilesToRemove -writeVerboseLogs:$writeVerboseLogs.IsPresent
}
# Running terraform init and apply
Write-InformationColored "Thank you for providing those inputs, we are now initializing and applying Terraform to bootstrap your environment..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
if($autoApprove) {
Invoke-Terraform -moduleFolderPath $bootstrapModulePath -autoApprove -destroy:$destroy.IsPresent -planOnly:$planOnly.IsPresent
} else {
Write-InformationColored "Once the plan is complete you will be prompted to confirm the apply." -ForegroundColor Green -NewLineBefore -InformationAction Continue
Invoke-Terraform -moduleFolderPath $bootstrapModulePath -destroy:$destroy.IsPresent -planOnly:$planOnly.IsPresent
}
Write-InformationColored "Bootstrap has completed successfully! Thanks for using our tool. Head over to Phase 3 in the documentation to continue..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
}
}