-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerraSwitch.ps1
180 lines (155 loc) · 6.2 KB
/
TerraSwitch.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<#
.SYNOPSIS
This script facilitates the process of changing Terraform versions.
.DESCRIPTION
This script will allow users to quickly and efficiently change the version of Terraform being utilized.
Options are as follows:
-Version : The version of Terraform to be installed.
- The list of versions that are able to be used can be derived from the '-ListVersions' option below.
-ListVersions : List all versions of Terraform available for installation.
-Latest : Automatic installation of the latest version of Terraform.
.EXAMPLE
TerraSwitch.ps1 -Version 1.3.4
.EXAMPLE
TerraSwitch.ps1 -ListVersions
.LINK
https://github.com/jaredbrogan/TerraSwitch
.NOTES
Author: Jared Brogan
Contact: jaredbrogan.github.io
#>
param(
[Parameter(Mandatory=$false)][string]$Version,
[Parameter(Mandatory=$false)][switch]$ListVersions,
[Parameter(Mandatory=$false)][switch]$Latest
)
$bullet = [char]0x2022
function CheckParams {
if ( !$Version -And !$ListVersions -And !$Latest -And !$Revert ){
Write-Host -ForegroundColor Red "[ERROR] No arguments provided. Please try again."
Write-Host -ForegroundColor Red " $bullet Exit Code: 1"
exit 1
}
elseif ( $Version -And !$ListVersions -And !$Latest -And !$Revert ){
if ( $Version -match '^[0-9].{4}.*'){
Write-Host -ForegroundColor Green "[INFO] Proceeding with installation of version $Version`n"
InstallVersion($Version)
}
else{
Write-Host -ForegroundColor Red "[ERROR] Version provided is not valid. Please try again."
Write-Host -ForegroundColor Red " $bullet Exit Code: 2"
exit 2
}
}
elseif ( !$Version -And $ListVersions -And !$Latest ){
Write-Host -ForegroundColor Green "[INFO] Listing available Terraform versions..."
ListVersions
}
elseif ( !$Version -And !$ListVersions -And $Latest ){
Write-Host -ForegroundColor Green "[INFO] Proceeding with installation of the latest version available`n"
$Version = "Latest"
InstallVersion($Version)
}
else {
Write-Host -ForegroundColor Red "[ERROR] Undetermined error found. Exiting..."
Write-Host -ForegroundColor Red " $bullet Exit Code: 3"
exit 3
}
}
function ListVersions {
$Versions = ((Invoke-WebRequest -Uri "https://releases.hashicorp.com/terraform/").Links.href | Select-Object -Skip 1 | %{ $_ -Replace "^/terraform/", "" }).TrimEnd('/')
ForEach ($Version in $Versions){
Write-Host " $bullet $Version"
}
Write-Host ""
exit 0
}
function InstallVersion($Version){
# Variables
$InstallDir = "$env:USERPROFILE\terraform"
$BinDir = "$InstallDir\bin"
$CacheDir = "$InstallDir\cache"
$LocalStatus = $False
# Check to see if Version is specified or set to Latest
if ($Version -eq "Latest"){
$Version = ((Invoke-WebRequest -Uri "https://releases.hashicorp.com/terraform/").Links.href | Select-Object -Skip 1 | %{ $_ -Replace "^/terraform/", "" }).TrimEnd('/') | Select-Object -First 1
}
$DownloadDir = "$CacheDir\$Version\terraform_$Version`_windows_amd64"
# Check if version is available locally, if not check if available externally
if (Test-Path -Path "$CacheDir\$Version\terraform.exe") {
Write-Host -ForegroundColor Green "[INFO] Using previously downloaded Terraform v$Version"
$LocalStatus = $True
}
else{
try{
$StatusCode = (Invoke-WebRequest -Uri "https://releases.hashicorp.com/terraform/$Version" -ErrorAction Stop).StatusCode
}
catch{
$StatusCode = $_.Exception.Response.StatusCode.Value__
}
if ($StatusCode -ne 200){
Write-Host -ForegroundColor Red "[ERROR] Version provided is not available. Exiting..."
Write-Host -ForegroundColor Red " $bullet Exit Code: 4"
exit 4
}
}
# Check if the installation directory is present
if (! (Test-Path -Path $InstallDir) ){
Write-Host -ForegroundColor Yellow "[WARNING] Install directory not present!"
Write-Host -ForegroundColor Yellow " $bullet Creating '$InstallDir' contents now... " -NoNewLine
New-Item -ItemType "directory" -Path "$CacheDir" -Force | Out-Null
New-Item -ItemType "directory" -Path "$BinDir" -Force | Out-Null
if (Test-Path -Path $InstallDir){
Write-Host -ForegroundColor Green "Success!`n"
}
else{
Write-Host -ForegroundColor Red "Failed."
Write-Host -ForegroundColor Red " $bullet Exit Code: 5"
exit 5
}
}
# Checks if the installation directory is in PATH
$UserPath = $env:path.split(";")
$ValidPath = 0
ForEach ($entry in $UserPath){
if ($entry -eq $BinDir){
$ValidPath++
}
}
if ($ValidPath -eq 0){
Write-Host -ForegroundColor Yellow "[WARNING] The '$BinDir' directory is not residing in PATH."
Write-Host -ForegroundColor Yellow -NoNewLine " $bullet Updating PATH now... "
Add-Content -Value `n'$env:Path += ";$env:USERPROFILE\terraform\bin"' -Path "$PROFILE"
& $PROFILE
Write-Host -ForegroundColor Green "Success!`n"
}
# Install Terraform
Write-Host -NoNewLine -ForegroundColor Green "[INFO] Installing Terraform v$Version... "
New-Item -ItemType "directory" -Path "$CacheDir\$Version" -Force | Out-Null
if ($LocalStatus){
Copy-Item "$CacheDir\$Version\terraform.exe" -Destination "$BinDir\terraform.exe" -Force | Out-Null
}
else{
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri "https://releases.hashicorp.com/terraform/$Version/terraform_$Version`_windows_amd64.zip" -OutFile "$DownloadDir.zip" | Out-Null
Expand-Archive -Force -Path "$DownloadDir.zip" -DestinationPath "$DownloadDir" | Out-Null
Copy-Item "$DownloadDir\terraform.exe" -Destination "$BinDir\terraform.exe" -Force | Out-Null
Copy-Item "$DownloadDir\terraform.exe" -Destination "$CacheDir\$Version\terraform.exe" -Force | Out-Null
Remove-Item -Path "$DownloadDir*" -Force -Recurse | Out-Null
}
$TFexe = "$BinDir\terraform.exe"
if (Test-Path $TFexe){
Write-Host -ForegroundColor Green "Success!`n"
}
else{
Write-Host -ForegroundColor Red "Failed."
Write-Host -ForegroundColor Red " $bullet Exit Code: 6"
exit 6
}
Write-Host "====================TERRAFORM_OUTPUT===================="
Start-Process $TFexe -ArgumentList version -NoNewWindow -Wait
Write-Host ""
exit 0
}
#__main__
CheckParams