Skip to content

Commit

Permalink
Add terraform installer
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredfholgate committed Dec 11, 2023
1 parent 299566c commit bf19357
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 40 deletions.
44 changes: 4 additions & 40 deletions src/ALZ/Private/Get-HCLParserTool.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/ALZ/Private/Get-OsArchitecture.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
73 changes: 73 additions & 0 deletions src/ALZ/Private/Get-TerraformTool.ps1
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions src/ALZ/Private/New-ALZEnvironmentTerraform.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit bf19357

Please sign in to comment.