diff --git a/src/ALZ/Private/Get-HCLParserTool.ps1 b/src/ALZ/Private/Get-HCLParserTool.ps1 index 1772c17f..353f918d 100644 --- a/src/ALZ/Private/Get-HCLParserTool.ps1 +++ b/src/ALZ/Private/Get-HCLParserTool.ps1 @@ -9,47 +9,11 @@ function Get-HCLParserTool { ) if ($PSCmdlet.ShouldProcess("Download Terraform Tools", "modify")) { - $os = "" - if ($IsWindows) { - $os = "windows" - } - if($IsLinux) { - $os = "linux" - } - if($IsMacOS) { - $os = "darwin" - } - - # Enum values can be seen here: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=net-7.0#fields - $architecture = ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture).ToString().ToLower() - - if($architecture -eq "x64") { - $architecture = "amd64" - } - if($architecture -eq "x86") { - $architecture = "386" - } - - $osAndArchitecture = "$($os)_$($architecture)" - - $supportedOsAndArchitectures = @( - "darwin_amd64", - "darwin_arm64", - "linux_386", - "linux_amd64", - "linux_arm64", - "windows_386", - "windows_amd64" - ) - - if($supportedOsAndArchitectures -notcontains $osAndArchitecture) { - Write-Error "Unsupported OS and architecture combination: $osAndArchitecture" - exit 1 - } + $osArchitecture = Get-OSArchitecture - $toolFileName = "hcl2json_$osAndArchitecture" + $toolFileName = "hcl2json_$($osArchitecture.osAndArchitecture)" - if($os -eq "windows") { + if($osArchitecture.os -eq "windows") { $toolFileName = "$($toolFileName).exe" } @@ -59,7 +23,7 @@ function Get-HCLParserTool { Invoke-WebRequest -Uri "https://github.com/tmccombs/hcl2json/releases/download/$($toolVersion)/$($toolFileName)" -OutFile "$toolFilePath" | Out-String | Write-Verbose } - if($os -ne "windows") { + if($osArchitecture.os -ne "windows") { $isExecutable = $(test -x $toolFilePath; 0 -eq $LASTEXITCODE) if(!($isExecutable)) { chmod +x $toolFilePath diff --git a/src/ALZ/Private/Get-OsArchitecture.ps1 b/src/ALZ/Private/Get-OsArchitecture.ps1 new file mode 100644 index 00000000..c3fe3b6e --- /dev/null +++ b/src/ALZ/Private/Get-OsArchitecture.ps1 @@ -0,0 +1,45 @@ +function Get-OSArchitecture { + $os = "" + if ($IsWindows) { + $os = "windows" + } + if($IsLinux) { + $os = "linux" + } + if($IsMacOS) { + $os = "darwin" + } + + # Enum values can be seen here: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=net-7.0#fields + $architecture = ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture).ToString().ToLower() + + if($architecture -eq "x64") { + $architecture = "amd64" + } + if($architecture -eq "x86") { + $architecture = "386" + } + + $osAndArchitecture = "$($os)_$($architecture)" + + $supportedOsAndArchitectures = @( + "darwin_amd64", + "darwin_arm64", + "linux_386", + "linux_amd64", + "linux_arm64", + "windows_386", + "windows_amd64" + ) + + if($supportedOsAndArchitectures -notcontains $osAndArchitecture) { + Write-Error "Unsupported OS and architecture combination: $osAndArchitecture" + exit 1 + } + + return @{ + os = $os + architecture = $architecture + osAndArchitecture = $osAndArchitecture + } +} diff --git a/src/ALZ/Private/Get-TerraformTool.ps1 b/src/ALZ/Private/Get-TerraformTool.ps1 new file mode 100644 index 00000000..bfc7a3a3 --- /dev/null +++ b/src/ALZ/Private/Get-TerraformTool.ps1 @@ -0,0 +1,73 @@ +function Get-TerraformTool { + [CmdletBinding(SupportsShouldProcess = $true)] + param( + [Parameter(Mandatory = $false)] + [string]$version = "latest", + [Parameter(Mandatory = $false)] + [string]$toolsPath = ".\terraform" + ) + + if($version -eq "latest") { + $version = (Invoke-WebRequest -Uri "https://checkpoint-api.hashicorp.com/v1/check/terraform").Content | ConvertFrom-Json | Select-Object -ExpandProperty current_version + } + + $commandDetails = Get-Command -Name terraform -ErrorAction SilentlyContinue + if($commandDetails) { + Write-InformationColored "Terraform already installed in $($commandDetails.Path), checking version" -ForegroundColor Green -InformationAction Continue + $installedVersion = terraform version -json | ConvertFrom-Json + Write-InformationColored "Installed version of Terraform: $($installedVersion.terraform_version) on $($installedVersion.platform)" -ForegroundColor Green -InformationAction Continue + if($installedVersion.terraform_version -eq $version) { + Write-InformationColored "Installed version of Terraform matches required version $version, skipping install" -ForegroundColor Green -InformationAction Continue + return + } + } + + $unzipdir = Join-Path -Path $toolsPath -ChildPath "terraform_$version" + if (Test-Path $unzipdir) { + Write-InformationColored "Terraform $version already installed, adding to Path." -ForegroundColor Green -InformationAction Continue + if($os -eq "windows") { + $env:PATH = "$($unzipdir);$env:PATH" + } else { + $env:PATH = "$($unzipdir):$env:PATH" + } + return + } + + $osArchitecture = Get-OSArchitecture + + $zipfilePath = "$unzipdir.zip" + + $url = "https://releases.hashicorp.com/terraform/$($version)/terraform_$($version)_$($osArchitecture.osAndArchitecture).zip" + + if(!(Test-Path $toolsPath)) { + New-Item -ItemType Directory -Path $toolsPath| Out-String | Write-Verbose + } + + Invoke-WebRequest -Uri $url -OutFile "$zipfilePath" | Out-String | Write-Verbose + + Expand-Archive -Path $zipfilePath -DestinationPath $unzipdir + + $toolFileName = "terraform" + + if($osArchitecture.os -eq "windows") { + $toolFileName = "$($toolFileName).exe" + } + + $toolFilePath = Join-Path -Path $unzipdir -ChildPath $toolFileName + + if($osArchitecture.os -ne "windows") { + $isExecutable = $(test -x $toolFilePath; 0 -eq $LASTEXITCODE) + if(!($isExecutable)) { + chmod +x $toolFilePath + } + } + + if($osArchitecture.os -eq "windows") { + $env:PATH = "$($unzipdir);$env:PATH" + } else { + $env:PATH = "$($unzipdir):$env:PATH" + } + + Remove-Item $zipfilePath + Write-InformationColored "Installed Terraform version $version" -ForegroundColor Green -InformationAction Continue +} diff --git a/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 b/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 index dc0ddc27..0da4c5fc 100644 --- a/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 +++ b/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 @@ -24,6 +24,9 @@ function New-ALZEnvironmentTerraform { ) if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) { + Write-InformationColored "Checking you have the latest version of Terraform installed..." -ForegroundColor Green -InformationAction Continue + $toolsPath = Join-Path -Path $alzEnvironmentDestination -ChildPath ".tools" + Get-TerraformTool -version "latest" -toolsPath $toolsPath Write-InformationColored "Downloading alz-terraform-accelerator Terraform module to $alzEnvironmentDestination" -ForegroundColor Green -InformationAction Continue