forked from libre-devops/ci-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun-Packer.ps1
More file actions
291 lines (248 loc) 路 9.25 KB
/
Copy pathRun-Packer.ps1
File metadata and controls
291 lines (248 loc) 路 9.25 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<#
.SYNOPSIS
This PowerShell script automates tasks related to Packer, including initialization, validation, and build processes.
.DESCRIPTION
'Run-Packer.ps1' is a PowerShell script designed to streamline the use of HashiCorp's Packer tool. It checks for the necessary environment setup, such as the presence of the Packer executable and required files, and then proceeds to perform initialization, validation, and building of Packer configurations. The script is configurable via parameters to control the execution of these stages.
.PARAMETERS
RunPackerInit
Specifies whether to run the Packer initialization process. Accepts 'true' or 'false'.
RunPackerValidate
Specifies whether to run the Packer validation process. Accepts 'true' or 'false'.
RunPackerBuild
Specifies whether to run the Packer build process. Accepts 'true' or 'false'.
PackerFileName
The name of the Packer file to be used, typically a .pkr.hcl file.
WorkingDirectory
The working directory where the Packer file is located and where Packer commands will be executed.
DebugMode
Enables or disables debug mode. Accepts 'true' or 'false'.
PackerVersion
Specifies the version of Packer to use. Default is 'latest'.
.FUNCTIONS
Check-PackerFileExists
Checks if the specified Packer file exists in the working directory.
Check-PkenvExists
Verifies the presence of pkenv, a Packer version management tool.
Check-PackerExists
Checks if Packer is installed and available in the system's PATH.
Ensure-PackerVersion
Ensures that the desired version of Packer is installed using pkenv.
Convert-ToBoolean
Converts string parameters to boolean values.
Run-PackerInit
Runs the 'packer init' command with the specified options.
Run-PackerValidate
Executes the 'packer validate' command for the Packer file.
Run-PackerBuild
Performs the 'packer build' process using the specified Packer file.
.EXAMPLE
.\Run-Packer.ps1 -RunPackerInit "true" -RunPackerValidate "true" -RunPackerBuild "true" -PackerFileName "example.pkr.hcl"
This example runs the script with all Packer processes enabled using the 'example.pkr.hcl' file.
.NOTES
Ensure that all prerequisites, such as Packer and pkenv, are installed and properly configured before running this script.
Modify the script parameters based on your specific Packer setup and requirements.
Author: Craig Thacker
Date: 11/12/2023
#>
param (
[string]$RunPackerInit = "true",
[string]$RunPackerValidate = "true",
[string]$RunPackerBuild = "true",
[string]$PackerFileName = "packer.pkr.hcl",
[string]$WorkingDirectory = (Get-Location).Path,
[string]$DebugMode = "false",
[string]$PackerVersion = "latest"
)
# Function to check if the Packer file exists
function Check-PackerFileExists {
$filePath = Join-Path -Path $WorkingDirectory -ChildPath $PackerFileName
if (-not (Test-Path -Path $filePath)) {
Write-Error "Error: Packer file not found at $filePath. Exiting."
exit 1
}
else {
Write-Host "Success: Packer file found at: $filePath" -ForegroundColor Green
}
}
# Function to check if Tfenv is installed
function Check-PkenvExists {
try {
$pkenvPath = Get-Command pkenv -ErrorAction Stop
Write-Host "Success: pkenv found at: $($pkenvPath.Source)" -ForegroundColor Green
return $true
}
catch {
Write-Warning "Warning: pkenv is not installed or not in PATH. Skipping version checking."
return $false
}
}
# Function to check if Packer is installed
function Check-PackerExists {
try {
$packerPath = Get-Command packer -ErrorAction Stop
Write-Host "Success: Packer found at: $($packerPath.Source)" -ForegroundColor Green
}
catch {
Write-Error "Error: Packer is not installed or not in PATH. Exiting."
exit 1
}
}
# Function to ensure the desired version of Packer is installed
function Ensure-PackerVersion {
# Check if the specified version is already installed
$pkrVersion = $PackerVersion.ToLower()
if ($pkrVersion -eq 'latest') {
Write-Host "Success: Packer version is set to 'latest', running install and use" -ForegroundColor Green
pkenv install $pkrVersion
pkenv use $pkrVersion
}
else {
try {
Write-Information "Info: Installing Packer version $Version using pkenv..."
pkenv install $Version
pkenv use $Version
Write-Host "Success: Installed and set Packer version $Version" -ForegroundColor Green
}
catch {
Write-Error "Error: Failed to install Packer version $Version"
exit 1
}
}
}
# Function to convert string to boolean
function Convert-ToBoolean($value) {
$valueLower = $value.ToLower()
if ($valueLower -eq "true") {
return $true
}
elseif ($valueLower -eq "false") {
return $false
}
else {
Write-Error "Error: Invalid value - $value. Exiting."
exit 1
}
}
$pkenvExists = Check-PkenvExists
if ($pkenvExists) {
Ensure-PackerVersion -Version $PackerVersion
}
if (Check-PackerFileExists) {
$filePath = Join-Path -Path $WorkingDirectory -ChildPath $PackerFileName
}
# Convert string parameters to boolean
$RunPackerInit = Convert-ToBoolean $RunPackerInit
$RunPackerValidate = Convert-ToBoolean $RunPackerValidate
$RunPackerBuild = Convert-ToBoolean $RunPackerBuild
$DebugMode = Convert-ToBoolean $DebugMode
# Enable debug mode if DebugMode is set to $true
if ($DebugMode) {
$DebugPreference = "Continue"
}
# Diagnostic output
Write-Debug "RunPackerInit: $RunPackerinit"
Write-Debug "RunPackerValidate: $RunPackerValidate"
Write-Debug "RunPackerBuild: $RunPackerBuild"
Write-Debug "DebugMode: $DebugMode"
if ($RunPackerInit -eq $false) {
Write-Error "Error: You must run packer init to use this script, it does not support false use of it at this time."
exit 1
}
# Change to the specified working directory
try {
$CurrentWorkingDirectory = (Get-Location).path
Set-Location -Path $WorkingDirectory
}
catch {
Write-Error "Error: Unable to change to directory: $WorkingDirectory"
exit 1
}
Check-PackerFileExists
function Run-PackerInit {
if ($RunPackerInit -eq $true) {
try {
if ($isWindows) {
Write-Host "Info: Running Packer init in $WorkingDirectory" -ForegroundColor Green
packer init -upgrade $PackerFileName | Out-Host
if ($LASTEXITCODE -eq 0) {
return $true
}
else {
Write-Error "Error: Packer init failed with exit code $LASTEXITCODE"
return $false
}
}
else {
Write-Host "Info: Running Packer init in $WorkingDirectory" -ForegroundColor Green
packer init -force $PackerFileName | Out-Host
if ($LASTEXITCODE -eq 0) {
return $true
}
else {
Write-Error "Error: Packer init failed with exit code $LASTEXITCODE"
return $false
}
}
}
catch {
Write-Error "Error: Packer init encountered an exception"
return $false
}
}
return $false
}
function Run-PackerValidate {
if ($RunPackerValidate -eq $true) {
try {
Write-Host "Info: Running Packer validate in $WorkingDirectory" -ForegroundColor Green
packer validate $PackerFileName | Out-Host
if ($LASTEXITCODE -eq 0) {
return $true
}
else {
Write-Error "Error: Packer validate failed with exit code $LASTEXITCODE"
return $false
}
}
catch {
Write-Error "Error: Packer validate encountered an exception"
return $false
}
}
return $false
}
function Run-PackerBuild {
if ($RunPackerBuild -eq $true) {
try {
Write-Host "Info: Running Packer build in $WorkingDirectory" -ForegroundColor Green
packer build $PackerFileName | Out-Host
if ($LASTEXITCODE -eq 0) {
return $true
}
else {
Write-Error "Error: Packer build failed with exit code $LASTEXITCODE"
return $false
}
}
catch {
Write-Error "Error: Packer build encountered an exception"
return $false
}
}
return $false
}
# Execution flow
$initSuccess = Run-PackerInit
if ($initSuccess -eq $true) {
$validateSuccess = Run-PackerValidate
if ($validateSuccess -eq $true) {
Run-PackerBuild
}
else {
Write-Host "Packer validate failed. Skipping Packer build."
}
}
else {
Write-Host "Packer init failed. Skipping Packer validate and Packer build."
}
Set-Location $CurrentWorkingDirectory