-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf-actions.ps1
89 lines (69 loc) · 2.72 KB
/
tf-actions.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
function tf-init {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$env
)
# remove state files
if (Test-Path .\.terraform\) { Remove-Item .\.terraform -Force }
# set env config file
if (Test-Path ("$(Get-Location)\config\$($env)_backend.tfvars")) {
terraform init -backend-config="$(Get-Location)\config\$($env)_backend.tfvars"
}
}
function tf-plan {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$env
)
# set env config file
if (Test-Path ("$(Get-Location)\config\$($env)_backend.tfvars")) {
terraform plan -var-file="$(Get-Location)\config\$($env).tfvars"
}
}
function tf-apply {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$env
)
# set env config file
if (Test-Path ("$(Get-Location)\config\$($env)_backend.tfvars")) {
terraform apply -var-file="$(Get-Location)\config\$($env).tfvars"
}
}
function tf-version {
[CmdletBinding()]
param (
[Parameter()]
[string]$version = 'latest'
)
#config
If (Test-Path 'D:\apps\terraform\') { $binaryPath = 'D:\apps\terraform\' } else { $binaryPath = 'C:\data\apps\terraform\' }
# set latest version of terraform
if ($version -eq 'latest') {
$version = (Invoke-RestMethod -Uri "https://checkpoint-api.hashicorp.com/v1/check/terraform" -ErrorAction Stop).current_version
Write-host "latest version: $version"
}
# create working folders
if (!(Test-Path $binaryPath\tmp)) { New-Item -ItemType Directory -Path $binaryPath\tmp | out-null }
if (!(Test-Path $binaryPath\versions)) { New-Item -ItemType Directory -Path $binaryPath\versions | out-null }
# download if requested file is not present
if (!(Test-Path "$binaryPath\versions\terraform_$($version).exe")) {
try {
Invoke-WebRequest "https://releases.hashicorp.com/terraform/$($version)/terraform_$($version)_windows_amd64.zip" -OutFile "$binaryPath\tmp\dl.zip" -Erroraction Stop
}
catch {
throw "Could not find version: $version"
Remove-Item $binaryPath\tmp\dl.zip -Force
}
Expand-Archive -LiteralPath "$binaryPath\tmp\dl.zip" -DestinationPath "$binaryPath\tmp" -Force
Remove-Item $binaryPath\tmp\dl.zip -Force
Copy-Item "$binaryPath\tmp\terraform.exe" -Destination "$binaryPath\versions\terraform_$($version).exe"
Copy-Item "$binaryPath\versions\terraform_$($version).exe" -Destination "$binaryPath\terraform.exe" -Force
}
else {
Copy-Item "$binaryPath\versions\terraform_$($version).exe" -Destination "$binaryPath\terraform.exe" -Force
}
}