-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement add terraform rerun and destroy (#93)
# Pull Request ## Issue Issue #, if available: Azure/alz-terraform-accelerator#70 ## Description Description of changes: This PR adds re-run, upgrade and destroy features for Terraform. These features are designed to simplify usage of the accelerator, provide any easy way to apply changes, fix issues and upgrade to a newer version. The destroy will make it much easier to clean up test environments. ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
- Loading branch information
1 parent
c5bd475
commit 176ec96
Showing
8 changed files
with
221 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
function Invoke-Upgrade { | ||
[CmdletBinding(SupportsShouldProcess = $true)] | ||
param ( | ||
[Parameter(Mandatory = $false)] | ||
[string] $alzEnvironmentDestination, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $bootstrapCacheFileName, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $starterCacheFileNamePattern, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $stateFilePathAndFileName, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[string] $currentVersion, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[switch] $autoApprove | ||
) | ||
|
||
if ($PSCmdlet.ShouldProcess("Upgrade Release", "Operation")) { | ||
|
||
$directories = Get-ChildItem -Path $alzEnvironmentDestination -Filter "v*" -Directory | ||
$previousBootstrapCachedValuesPath = $null | ||
$previousStarterCachedValuesPath = $null | ||
$previousStateFilePath = $null | ||
$previousVersion = $null | ||
$foundPreviousRelease = $false | ||
|
||
foreach ($directory in $directories | Sort-Object -Descending -Property Name) { | ||
$releasePath = Join-Path -Path $alzEnvironmentDestination -ChildPath $directory.Name | ||
$releaseBootstrapCachedValuesPath = Join-Path -Path $releasePath -ChildPath $bootstrapCacheFileName | ||
$releaseStateFilePath = Join-Path -Path $releasePath -ChildPath $stateFilePathAndFileName | ||
|
||
if(Test-Path $releaseBootstrapCachedValuesPath) { | ||
$previousBootstrapCachedValuesPath = $releaseBootstrapCachedValuesPath | ||
} | ||
|
||
$starterCacheFiles = Get-ChildItem -Path $releasePath -Filter $starterCacheFileNamePattern -File | ||
|
||
if($starterCacheFiles) { | ||
$previousStarterCachedValuesPath = $starterCacheFiles[0].FullName | ||
} | ||
|
||
if(Test-Path $releaseStateFilePath) { | ||
$previousStateFilePath = $releaseStateFilePath | ||
} | ||
|
||
if($null -ne $previousStateFilePath) { | ||
if($directory.Name -eq $currentVersion) { | ||
# If the current version has already been run, then skip the upgrade process | ||
break | ||
} | ||
|
||
$foundPreviousRelease = $true | ||
$previousVersion = $directory.Name | ||
break | ||
} | ||
} | ||
|
||
if($foundPreviousRelease) { | ||
Write-InformationColored "AUTOMATIC UPGRADE: We found version $previousVersion that has been previously run. You can upgrade from this version to the new version $currentVersion" -ForegroundColor Yellow -InformationAction Continue | ||
$upgrade = "" | ||
if($autoApprove) { | ||
$upgrade = "upgrade" | ||
} else { | ||
$upgrade = Read-Host "If you would like to upgrade, enter 'upgrade' or just hit 'enter' to continue with a new environment. (upgrade/exit)" | ||
} | ||
|
||
if($upgrade.ToLower() -eq "upgrade") { | ||
$currentPath = Join-Path -Path $alzEnvironmentDestination -ChildPath $currentVersion | ||
$currentBootstrapCachedValuesPath = Join-Path -Path $currentPath -ChildPath $bootstrapCacheFileName | ||
$currentStarterCachedValuesPath = $currentPath | ||
$currentStateFilePath = Join-Path -Path $currentPath -ChildPath $stateFilePathAndFileName | ||
|
||
# Copy the previous cached values to the current release | ||
if($null -ne $previousBootstrapCachedValuesPath) { | ||
Write-InformationColored "AUTOMATIC UPGRADE: Copying $previousBootstrapCachedValuesPath to $currentBootstrapCachedValuesPath" -ForegroundColor Green -InformationAction Continue | ||
Copy-Item -Path $previousBootstrapCachedValuesPath -Destination $currentBootstrapCachedValuesPath -Force | Out-String | Write-Verbose | ||
} | ||
if($null -ne $previousStarterCachedValuesPath) { | ||
Write-InformationColored "AUTOMATIC UPGRADE: Copying $previousStarterCachedValuesPath to $currentStarterCachedValuesPath" -ForegroundColor Green -InformationAction Continue | ||
Copy-Item -Path $previousStarterCachedValuesPath -Destination $currentStarterCachedValuesPath -Force | Out-String | Write-Verbose | ||
} | ||
|
||
Write-InformationColored "AUTOMATIC UPGRADE: Copying $previousStateFilePath to $currentStateFilePath" -ForegroundColor Green -InformationAction Continue | ||
Copy-Item -Path $previousStateFilePath -Destination $currentStateFilePath -Force | Out-String | Write-Verbose | ||
|
||
Write-InformationColored "AUTOMATIC UPGRADE: Upgrade complete. If any files in the starter have been updated, you will need to remove branch protection in order for the Terraform apply to succeed..." -ForegroundColor Yellow -InformationAction Continue | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function Write-ConfigurationCache { | ||
[CmdletBinding(SupportsShouldProcess = $true)] | ||
param ( | ||
[Parameter(Mandatory = $false)] | ||
[string] $filePath, | ||
|
||
[Parameter(Mandatory = $false)] | ||
[PSObject] $configuration | ||
) | ||
|
||
if ($PSCmdlet.ShouldProcess("Download Terraform Tools", "modify")) { | ||
|
||
if(Test-Path $filePath) { | ||
Remove-Item -Path $filePath | ||
} | ||
|
||
$cache = [PSCustomObject]@{} | ||
foreach ($configurationItem in $configuration.PSObject.Properties) { | ||
$cache | Add-Member -NotePropertyName $configurationItem.Name -NotePropertyValue $configurationItem.Value.Value | ||
} | ||
|
||
$cache | ConvertTo-Json | Out-File -FilePath $filePath | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters