Skip to content

Commit

Permalink
chore: refactor bicep to support remote config (#87)
Browse files Browse the repository at this point in the history
# Pull Request

## Description

This PR refactors the code to default to the `latest` release and pull
the config from the ALZ-Bicep repo. The goal is to de-couple accelerator
releases from ALZ PowerShell releases.

## Testing

Tested with preview release:

`New-ALZEnvironment -iac "bicep" -cicd "github" -o "c:\bicepTest"
-alzVersion "v0.16.6-preview"`

## License

By submitting this pull request, I confirm that my contribution is made
under the terms of the projects associated license.
  • Loading branch information
jaredfholgate authored Oct 17, 2023
1 parent 6669c3b commit 804c131
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 4,547 deletions.
522 changes: 0 additions & 522 deletions src/ALZ/Assets/alz-bicep-config/v0.14.0.config.json

This file was deleted.

556 changes: 0 additions & 556 deletions src/ALZ/Assets/alz-bicep-config/v0.15.0.config.json

This file was deleted.

556 changes: 0 additions & 556 deletions src/ALZ/Assets/alz-bicep-config/v0.16.0.config.json

This file was deleted.

556 changes: 0 additions & 556 deletions src/ALZ/Assets/alz-bicep-config/v0.16.1.config.json

This file was deleted.

556 changes: 0 additions & 556 deletions src/ALZ/Assets/alz-bicep-config/v0.16.2.config.json

This file was deleted.

556 changes: 0 additions & 556 deletions src/ALZ/Assets/alz-bicep-config/v0.16.3.config.json

This file was deleted.

556 changes: 0 additions & 556 deletions src/ALZ/Assets/alz-bicep-config/v0.16.4.config.json

This file was deleted.

556 changes: 0 additions & 556 deletions src/ALZ/Assets/alz-bicep-config/v0.16.5.config.json

This file was deleted.

37 changes: 12 additions & 25 deletions src/ALZ/Private/Get-ALZConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,23 @@ function Get-ALZConfig {
#>
param(
[Parameter(Mandatory = $false)]
[string] $alzVersion = "v0.16.5",
[Parameter(Mandatory = $false)]
[ValidateSet("bicep", "terraform")]
[Alias("Iac")]
[string] $alzIacProvider = "bicep",
[Parameter(Mandatory = $false)]
[string] $configFilePath = ""
)

# Import the config from the json file inside assets and transform it to a PowerShell object
if ($configFilePath -ne "") {
$extension = (Get-Item -Path $configFilePath).Extension.ToLower()
if($extension -eq ".yml" -or $extension -eq ".yaml") {
if (!(Get-Module -ListAvailable -Name powershell-Yaml)) {
Write-Host "Installing YAML module"
Install-Module powershell-Yaml -Force
}
$config = [PSCustomObject](Get-Content -Path $configFilePath | ConvertFrom-Yaml)
} elseif($extension -eq ".json") {
$config = Get-Content -Path $configFilePath | ConvertFrom-Json
} else {
throw "The config file must be a json or yaml/yml file"
# Import the config and transform it to a PowerShell object
$extension = (Get-Item -Path $configFilePath).Extension.ToLower()
if($extension -eq ".yml" -or $extension -eq ".yaml") {
if (!(Get-Module -ListAvailable -Name powershell-Yaml)) {
Write-Host "Installing YAML module"
Install-Module powershell-Yaml -Force
}

return $config
$config = [PSCustomObject](Get-Content -Path $configFilePath | ConvertFrom-Yaml)
} elseif($extension -eq ".json") {
$config = Get-Content -Path $configFilePath | ConvertFrom-Json
} else {
throw "The config file must be a json or yaml/yml file"
}

else {
$config = Get-Content -Path (Join-Path $(Get-ScriptRoot) "../Assets/alz-$alzIacProvider-config" "$alzVersion.config.json" ) | ConvertFrom-Json
return $config
}
return $config
}
23 changes: 19 additions & 4 deletions src/ALZ/Private/New-ALZEnvironmentBicep.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@ function New-ALZEnvironmentBicep {
[string] $alzCicdPlatform
)

$bicepModuleUrl = "https://github.com/Azure/ALZ-Bicep"

if ($PSCmdlet.ShouldProcess("ALZ-Bicep module configuration", "modify")) {

$bicepConfig = Get-ALZConfig -alzVersion $alzVersion
if($alzVersion -ne "latest" -and $alzVersion -notlike "*-preview") {
$lastSupportedLocalVersion = [System.Version]"0.16.5"
$targetVersion = [System.Version]($alzVersion -replace "v", "")

if($targetVersion -le $lastSupportedLocalVersion) {
throw "The version of the ALZ-Bicep accelerator you are targetting is not supported by this version of the ALZ PowerShell module. In order to target versions prior to v0.16.6 you will need to downgrade to version v0.2.20 or lower of this module."
}
}

New-ALZDirectoryEnvironment -alzEnvironmentDestination $alzEnvironmentDestination -alzCicdDestination $alzCicdPlatform | Out-String | Write-Verbose

$alzEnvironmentDestinationInternalCode = Join-Path $alzEnvironmentDestination "upstream-releases"

Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestinationInternalCode -githubRepoUrl $bicepConfig.module_url -releases @($bicepConfig.version) | Out-String | Write-Verbose
# Downloading the latest or specified version of the bicep accelerator module
$releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestinationInternalCode -githubRepoUrl $bicepModuleUrl -release $alzVersion
$releasePath = Join-Path -Path $alzEnvironmentDestinationInternalCode -ChildPath $releaseTag

# Getting the configuration
$configFilePath = Join-Path -Path $releasePath -ChildPath "accelerator/.config/ALZ-Powershell.config.json"
$bicepConfig = Get-ALZConfig -configFilePath $configFilePath

Write-InformationColored "Copying ALZ-Bicep module to $alzEnvironmentDestinationInternalCode" -ForegroundColor Green -InformationAction Continue
Copy-ALZParametersFile -alzEnvironmentDestination $alzEnvironmentDestination -upstreamReleaseDirectory $(Join-Path $alzEnvironmentDestinationInternalCode $bicepConfig.version) -configFiles $bicepConfig.config_files | Out-String | Write-Verbose
Copy-ALZParametersFile -alzEnvironmentDestination $alzEnvironmentDestination -upstreamReleaseDirectory $(Join-Path $alzEnvironmentDestinationInternalCode $bicepConfig.version) -configFiles $bicepConfig.cicd.$alzCicdPlatform | Out-String | Write-Verbose
Copy-ALZParametersFile -alzEnvironmentDestination $alzEnvironmentDestination -upstreamReleaseDirectory $(Join-Path $alzEnvironmentDestinationInternalCode $releaseTag) -configFiles $bicepConfig.config_files | Out-String | Write-Verbose
Copy-ALZParametersFile -alzEnvironmentDestination $alzEnvironmentDestination -upstreamReleaseDirectory $(Join-Path $alzEnvironmentDestinationInternalCode $releaseTag) -configFiles $bicepConfig.cicd.$alzCicdPlatform | Out-String | Write-Verbose
Write-InformationColored "ALZ-Bicep source directory: $alzBicepSourceDirectory" -ForegroundColor Green -InformationAction Continue

$configuration = Request-ALZEnvironmentConfig -configurationParameters $bicepConfig.parameters
Expand Down
7 changes: 3 additions & 4 deletions src/ALZ/Private/New-ALZEnvironmentTerraform.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ function New-ALZEnvironmentTerraform {
}

# Downloading the latest or specified version of the alz-terraform-accelerator module
$releaseObject = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestination -githubRepoUrl $terraformModuleUrl -releases $alzVersion
$release = $($releaseObject.name)
$releasePath = Join-Path -Path $alzEnvironmentDestination -ChildPath $release
$releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestination -githubRepoUrl $terraformModuleUrl -release $alzVersion
$releasePath = Join-Path -Path $alzEnvironmentDestination -ChildPath $releaseTag

# Getting the configuration for the initial bootstrap user input and validators
$bootstrapConfigFilePath = Join-Path -Path $releasePath -ChildPath "bootstrap/.config/ALZ-Powershell.config.json"
Expand All @@ -50,7 +49,7 @@ function New-ALZEnvironmentTerraform {
$hclParserToolPath = Get-HCLParserTool -alzEnvironmentDestination $releasePath -toolVersion "v0.6.0"
$bootstrapParameters = Convert-HCLVariablesToUserInputConfig -targetVariableFile $bootstrapVariableFilesPath -hclParserToolPath $hclParserToolPath -validators $bootstrapConfig.validators

Write-InformationColored "Got configuration and downloaded alz-terraform-accelerator Terraform module version $release to $alzEnvironmentDestination" -ForegroundColor Green -InformationAction Continue
Write-InformationColored "Got configuration and downloaded alz-terraform-accelerator Terraform module version $releaseTag to $alzEnvironmentDestination" -ForegroundColor Green -InformationAction Continue

# Getting the user input for the bootstrap module
$bootstrapConfiguration = Request-ALZEnvironmentConfig -configurationParameters $bootstrapParameters -respectOrdering -userInputOverrides $userInputOverrides -treatEmptyDefaultAsValid $true
Expand Down
116 changes: 51 additions & 65 deletions src/ALZ/Public/Get-ALZGithubRelease.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Get-ALZGithubRelease {

[Parameter(Mandatory = $false, Position = 2, HelpMessage = "The releases to download. Specify 'all' to download all releases or 'latest' to download the latest release. Defaults to the latest release.")]
[array]
$releases = @("latest"),
$release = "latest",

[Parameter(Mandatory = $false, Position = 3, HelpMessage = "The directory to download the releases to. Defaults to the current directory.")]
[string]
Expand All @@ -46,93 +46,79 @@ function Get-ALZGithubRelease {
# Split Repo URL into parts
$repoOrgPlusRepo = $githubRepoUrl.Split("/")[-2..-1] -join "/"

Write-Verbose "=====> Checking for releases on GitHub Repo: $repoOrgPlusRepo"
Write-Verbose "=====> Checking for release on GitHub Repo: $repoOrgPlusRepo"

# Get releases on repo
$repoReleasesUrl = "https://api.github.com/repos/$repoOrgPlusRepo/releases"
$allRepoReleases = Invoke-RestMethod $repoReleasesUrl -RetryIntervalSec 3 -MaximumRetryCount 100

Write-Verbose "=====> All available releases on GitHub Repo: $repoOrgPlusRepo"
$allRepoReleases | Select-Object name, tag_name, published_at, prerelease, draft, html_url | Format-Table -AutoSize | Out-String | Write-Verbose

# Get latest release on repo
$latestRepoRelease = $allRepoReleases | Where-Object { $_.prerelease -eq $false } | Where-Object { $_.draft -eq $false } | Sort-Object -Descending published_at | Select-Object -First 1
# replace latest with the tag of the latest release
if ($releases -contains "latest") {
$releases += $latestRepoRelease.tag_name
$releases = $releases | Where-Object { $_ -ne "latest" }
$repoReleaseUrl = "https://api.github.com/repos/$repoOrgPlusRepo/releases/$release"
if($release -ne "latest") {
$repoReleaseUrl = "https://api.github.com/repos/$repoOrgPlusRepo/releases/tags/$release"
}

Write-Verbose "=====> Latest available release on GitHub Repo: $repoOrgPlusRepo"
$latestRepoRelease | Select-Object name, tag_name, published_at, prerelease, draft, html_url | Format-Table -AutoSize | Out-String | Write-Verbose
$releaseData = Invoke-RestMethod $repoReleaseUrl -SkipHttpErrorCheck -StatusCodeVariable "statusCode"

# Check if directory exists
Write-Verbose "=====> Checking if directory for releases exists: $directoryForReleases"

if (!(Test-Path $directoryForReleases)) {
Write-Verbose "Directory does not exist for releases, will now create: $directoryForReleases"
New-Item -ItemType Directory -Path $directoryForReleases | Out-String | Write-Verbose
if($statusCode -eq 404) {
Write-Error "The release $release does not exist in the GitHub repository $githubRepoUrl - $repoReleaseUrl"
throw "The release $release does not exist in the GitHub repository $githubRepoUrl - $repoReleaseUrl"
}

# if all is specified add all the releases to the array and remove all
if ($releases -contains "all") {
$releases = $allRepoReleases | Select-Object -ExpandProperty tag_name
$releases = $releases | Where-Object { $_ -ne "all" }
# Handle transient errors like throttling
if($statusCode -ge 400 -and $statusCode -le 599) {
Write-InformationColored "Retrying as got the Status Code $statusCode, which may be a tranisent error." -ForegroundColor Yellow -InformationAction Continue
$releaseData = Invoke-RestMethod $repoReleaseUrl -RetryIntervalSec 3 -MaximumRetryCount 100
}

# Remove all the releases that were not found
foreach ($release in $releases) {
if (($allRepoReleases | Where-Object { $_.tag_name -eq $release } | Measure-Object).Count -eq 0) {
Write-Warning "Release $release was not found on GitHub Repo: $repoOrgPlusRepo"
$releases = $releases | Where-Object { $_ -ne $release }
}
$releaseTag = $releaseData.tag_name

if($queryOnly) {
return $releaseTag
}

$selectedReleases = $allRepoReleases | Where-Object { $releases -contains $_.tag_name }
# Check if directory exists
Write-Verbose "=====> Checking if directory for releases exists: $directoryForReleases"

if($queryOnly) {
return $selectedReleases
if (!(Test-Path $directoryForReleases)) {
Write-Verbose "Directory does not exist for releases, will now create: $directoryForReleases"
New-Item -ItemType Directory -Path $directoryForReleases | Out-String | Write-Verbose
}

foreach ($release in $selectedReleases) {
# Check the firectory for this release
$releaseDirectory = "$directoryForReleases/$($release.tag_name)"
# Check the firectory for this release
$releaseDirectory = "$directoryForReleases/$releaseTag"

Write-Verbose "===> Checking if directory for release version exists: $releaseDirectory"
Write-Verbose "===> Checking if directory for release version exists: $releaseDirectory"

if (!(Test-Path $releaseDirectory)) {
Write-Verbose "Directory does not exist for release $($release.tag_name), will now create: $releaseDirectory"
New-Item -ItemType Directory -Path $releaseDirectory | Out-String | Write-Verbose
}
if (!(Test-Path $releaseDirectory)) {
Write-Verbose "Directory does not exist for release $releaseTag, will now create: $releaseDirectory"
New-Item -ItemType Directory -Path $releaseDirectory | Out-String | Write-Verbose
}

Write-Verbose "===> Checking if any content exists inside of $releaseDirectory"
Write-Verbose "===> Checking if any content exists inside of $releaseDirectory"

$contentInReleaseDirectory = Get-ChildItem -Path $releaseDirectory -Recurse -ErrorAction SilentlyContinue
$contentInReleaseDirectory = Get-ChildItem -Path $releaseDirectory -Recurse -ErrorAction SilentlyContinue

if ($null -eq $contentInReleaseDirectory) {
Write-Verbose "===> Pulling and extracting release $($release.tag_name) into $releaseDirectory"
New-Item -ItemType Directory -Path "$releaseDirectory/tmp" | Out-String | Write-Verbose
Invoke-WebRequest -Uri "https://github.com/$repoOrgPlusRepo/archive/refs/tags/$($release.tag_name).zip" -OutFile "$releaseDirectory/tmp/$($release.tag_name).zip" -RetryIntervalSec 3 -MaximumRetryCount 100 | Out-String | Write-Verbose
Expand-Archive -Path "$releaseDirectory/tmp/$($release.tag_name).zip" -DestinationPath "$releaseDirectory/tmp/extracted" | Out-String | Write-Verbose
$extractedSubFolder = Get-ChildItem -Path "$releaseDirectory/tmp/extracted" -Directory
if ($null -eq $contentInReleaseDirectory) {
Write-Verbose "===> Pulling and extracting release $releaseTag into $releaseDirectory"
New-Item -ItemType Directory -Path "$releaseDirectory/tmp" | Out-String | Write-Verbose
Invoke-WebRequest -Uri "https://github.com/$repoOrgPlusRepo/archive/refs/tags/$releaseTag.zip" -OutFile "$releaseDirectory/tmp/$releaseTag.zip" -RetryIntervalSec 3 -MaximumRetryCount 100 | Out-String | Write-Verbose
Expand-Archive -Path "$releaseDirectory/tmp/$releaseTag.zip" -DestinationPath "$releaseDirectory/tmp/extracted" | Out-String | Write-Verbose
$extractedSubFolder = Get-ChildItem -Path "$releaseDirectory/tmp/extracted" -Directory

if ($null -ne $directoryAndFilesToKeep) {
foreach ($path in $directoryAndFilesToKeep) {
Write-Verbose "===> Moving $path into $releaseDirectory."
Move-Item -Path "$($extractedSubFolder.FullName)/$($path)" -Destination "$releaseDirectory" -ErrorAction SilentlyContinue | Out-String | Write-Verbose
}
if ($null -ne $directoryAndFilesToKeep) {
foreach ($path in $directoryAndFilesToKeep) {
Write-Verbose "===> Moving $path into $releaseDirectory."
Move-Item -Path "$($extractedSubFolder.FullName)/$($path)" -Destination "$releaseDirectory" -ErrorAction SilentlyContinue | Out-String | Write-Verbose
}
}

if ($null -eq $directoryAndFilesToKeep) {
Write-Verbose "===> Moving all extracted contents into $releaseDirectory."
Move-Item -Path "$($extractedSubFolder.FullName)/*" -Destination "$releaseDirectory" -ErrorAction SilentlyContinue | Out-String | Write-Verbose
}
if ($null -eq $directoryAndFilesToKeep) {
Write-Verbose "===> Moving all extracted contents into $releaseDirectory."
Move-Item -Path "$($extractedSubFolder.FullName)/*" -Destination "$releaseDirectory" -ErrorAction SilentlyContinue | Out-String | Write-Verbose
}

Remove-Item -Path "$releaseDirectory/tmp" -Force -Recurse
Remove-Item -Path "$releaseDirectory/tmp" -Force -Recurse

} else {
Write-Verbose "===> Content already exists in $releaseDirectory. Skipping"
}
} else {
Write-Verbose "===> Content already exists in $releaseDirectory. Skipping"
}
return $selectedReleases

return $releaseTag
}
8 changes: 1 addition & 7 deletions src/ALZ/Public/New-ALZEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function New-ALZEnvironment {

[Parameter(Mandatory = $false)]
[Alias("alzBicepVersion")]
[string] $alzVersion = "",
[string] $alzVersion = "latest",

[Parameter(Mandatory = $false)]
[ValidateSet("bicep", "terraform")]
Expand All @@ -59,16 +59,10 @@ function New-ALZEnvironment {

if ($PSCmdlet.ShouldProcess("Accelerator setup", "modify")) {
if ($alzIacProvider -eq "bicep") {
if ($alzVersion -eq "") {
$alzVersion = "v0.16.5"
}
New-ALZEnvironmentBicep -alzEnvironmentDestination $alzEnvironmentDestination -alzVersion $alzVersion -alzCicdPlatform $alzCicdPlatform
}

if($alzIacProvider -eq "terraform") {
if($alzVersion -eq "") {
$alzVersion = "latest"
}
if($autoApprove) {
New-ALZEnvironmentTerraform -alzEnvironmentDestination $alzEnvironmentDestination -alzVersion $alzVersion -alzCicdPlatform $alzCicdPlatform -userInputOverridePath $userInputOverridePath -autoApprove
} else {
Expand Down
Loading

0 comments on commit 804c131

Please sign in to comment.