-
Notifications
You must be signed in to change notification settings - Fork 74
/
hv_generic.ps1
142 lines (114 loc) · 4.2 KB
/
hv_generic.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
<#
.SYNOPSIS
This script validates and builds Packer templates based on the specified parameters.
.DESCRIPTION
The script accepts three parameters: Action, Log, and Version. Based on these parameters, it performs actions like validating and building Packer templates. It also sets the PACKER_LOG environment variable to control Packer's logging behavior.
.PARAMETER Action
Specifies the action to be performed. It can be:
- "verify": Only validates the Packer template.
- "build": Validates and then builds the Packer template (default action).
.PARAMETER Log
Controls the logging behavior of Packer. It can be:
- 0: Disables logging (default).
- 1: Enables logging.
.PARAMETER Version
Specifies the version of the template to be used. If not specified, it defaults to "rockylinux-8.8".
.PARAMETER Template
Specifies the path to the Packer template to be used. If not specified, it defaults to "templates/hv_rhel.pkr.hcl".
#>
param(
[ValidateSet("verify", "build", "")]
[string]$Action = "",
[ValidateSet(0, 1)]
[int]$Log = 0,
[string]$Version = "",
[string]$Template = ""
)
# Set default values if parameters are not specified
if ($Action -eq "") {
$Action = "build"
}
if ($Log -eq 1) {
$PACKER_LOG = 1
} else {
$PACKER_LOG = 0
}
if ($Version -eq "") {
$Version = "rockylinux-8.8"
}
if ($Template -eq "") {
$Template = "rhel"
}
# Define colors for console output
$RED = [ConsoleColor]::Red
$GREEN = [ConsoleColor]::Green
$YELLOW = [ConsoleColor]::Yellow
$BLUE = [ConsoleColor]::Blue
$NC = [ConsoleColor]::White
# Define other variables
$var_file = "variables/variables_$Version.pkvars.hcl"
$template = "templates/hv_$Template.pkr.hcl"
if (!(Test-Path $var_file)) {
Write-Host "Variable file '$var_file' does not exist. Exiting now." -ForegroundColor $RED
exit 1
} else {
Write-Host "Variable file '$var_file' exists." -ForegroundColor $GREEN
}
if (!(Test-Path $template)) {
Write-Host "Template file '$template' does not exist. Exiting now." -ForegroundColor $RED
exit 1
} else {
Write-Host "Template file '$template' exists." -ForegroundColor $GREEN
}
# Set PACKER_LOG environment variable
[Environment]::SetEnvironmentVariable('PACKER_LOG', $PACKER_LOG, [System.EnvironmentVariableTarget]::Process)
# Display variable values
Write-Host "--- Variable Values ---" -ForegroundColor $BLUE
Write-Host "PACKER_LOG: $PACKER_LOG" -ForegroundColor $YELLOW
Write-Host "Version: $Version" -ForegroundColor $YELLOW
Write-Host "Var File: $var_file" -ForegroundColor $YELLOW
Write-Host "Template: $template" -ForegroundColor $YELLOW
Write-Host "-----------------------" -ForegroundColor $BLUE
Write-Host "Action: $Action" -ForegroundColor $YELLOW
# Start message
Write-Host "Starting the Packer script..." -ForegroundColor $YELLOW
# Validate the existence of the var file and template before proceeding
if (-not (Test-Path $var_file)) {
Write-Host "Var file '$var_file' does not exist. Exiting now." -ForegroundColor $RED
exit 1
}
if (-not (Test-Path $template)) {
Write-Host "Template file '$template' does not exist. Exiting now." -ForegroundColor $RED
exit 1
}
# Validate packer template
Write-Host "Validating Packer template..." -ForegroundColor $YELLOW
try {
packer validate --var-file="$var_file" "$template"
} catch {
Write-Host "Packer validate failed - exiting now!" -ForegroundColor $RED
exit 1
}
if ($LASTEXITCODE -ne 0) {
Write-Host "Packer validate failed with exit code $LASTEXITCODE - exiting now" -ForegroundColor $RED
exit 1
} else {
Write-Host "Packer template validation successful!" -ForegroundColor $GREEN
}
# Build packer template if Action is not 'verify'
if ($Action -ne "verify") {
Write-Host "Building Packer template..." -ForegroundColor $YELLOW
try {
packer build --force --var-file="$var_file" "$template"
}
catch {
Write-Host "Packer build failed - exiting now!" -ForegroundColor $RED
exit 1
}
if ($LASTEXITCODE -ne 0) {
Write-Host "Packer build failed with exit code $LASTEXITCODE - exiting now" -ForegroundColor $RED
exit 1
} else {
Write-Host "Packer build completed successfully!" -ForegroundColor $GREEN
}
}