-
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 installer (#92)
# Pull Request ## Issue Not applicable. ## Description This is an enhancement to ensure the dependency on a recent version of Terraform is handled. This PR checks for and installs the latest version into a local path if needed. The install script supports multiple os and architecture. ## 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
299566c
commit c5bd475
Showing
7 changed files
with
140 additions
and
63 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,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 | ||
} | ||
} |
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,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 | ||
} |
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