This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_hv_box.ps1
228 lines (187 loc) · 6.83 KB
/
build_hv_box.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
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
# packer HV build script
# https://docs.microsoft.com/en-us/powershell/module/hyper-v/?view=win10-ps
# vars
$packerinput = "packer-conf/rocky.pkr.hcl"
$CheckVM = "centos8-hv-build"
$packertemplatef = "hyperv-iso.rocky-hyperv"
# log start date
$timestart = (Get-Date)
$LogStamp = (Get-date -Format ddMMyy) + "_" + (get-date -format hhmmsstt)
$BuildLog = ".\logs\build." + $LogStamp + ".log"
$packerlogloc = ".\logs\build.$LogStamp.packer.log"
$Outmsg = "Started at $timestart"
Write-Host "[*] $Outmsg" -ForegroundColor green -BackgroundColor black;
Add-Content $BuildLog $Outmsg
#
# environment checks
#
# check privs
$userhasadmin = ( [Security.Principal.WindowsPrincipal] ` [Security.Principal.WindowsIdentity]::GetCurrent() ` ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($userhasadmin -eq $true) {
Write-Host "[*] In RoleSecurity Principal admin" -ForegroundColor green -BackgroundColor black;
}
if ($userhasadmin -eq $false) {
Write-Host "[*] NO Administrator priv" -ForegroundColor red -BackgroundColor black;
exit 1;
}
# check vagrant and packer are in path
if(!(Get-Command vagrant.exe -ErrorAction SilentlyContinue)) {
Add-Content $BuildLog "ERROR vagrant.exe not in path"
Write-Host "[*] vagrant.exe not in path" -ForegroundColor red -BackgroundColor black;
exit 1;
}
if(!(Get-Command packer.exe -ErrorAction SilentlyContinue)) {
Add-Content $BuildLog "ERROR packer.exe not in path"
Write-Host "[*] packer.exe not in path" -ForegroundColor red -BackgroundColor black;
exit 1;
}
# stop on fail
$ErrorActionPreference = "Stop"
#
# init
#
# create a log file
out-file -Filepath $BuildLog -append -Force -NoClobber
# dump info to log file
$scriptName = $MyInvocation.MyCommand.Name
Write-Host "[*] Log file: $BuildLog" -ForegroundColor green -BackgroundColor black
Write-Host "[*] Script name: $scriptName" -ForegroundColor green -BackgroundColor black
Write-Host "[*] Script location: $PSScriptRoot" -ForegroundColor green -BackgroundColor black
Write-Host "[*] Run from: $(Get-Location)" -ForegroundColor green -BackgroundColor black
Add-Content $BuildLog "script: $PSScriptRoot\$scriptName"
# create an ID for this build
$buildid = ([guid]::NewGuid().ToString())
# log it
$Outmsg = "Build id $($buildid) "
Write-Host "[*] $Outmsg" -ForegroundColor green -BackgroundColor black;
Add-Content $BuildLog $Outmsg
# set as env var
$Env:cos8vm_id = "$buildid"
# enable all Hyper-V tools PowerShell mods
Write-Host "[*] Enable PowerShell Hyper-V mods " -ForegroundColor green -BackgroundColor black;
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Tools-All | Out-Null
# check Packer VM is not already running
$VMName = Get-VM -name $CheckVM -ErrorAction SilentlyContinue
if ($VMname) {
$Outmsg = "ERROR Hyper-V VM $CheckVM is running"
Write-Host "[*] $Outmsg" -ForegroundColor red -BackgroundColor black;
Add-Content $BuildLog $Outmsg
exit 1;
}
if (!$VMname) {
Write-Host "[*] Hyper-V VM $CheckVM not running (good)" -ForegroundColor green -BackgroundColor black;
}
# check HyperV switch used by Packer exists
$VMswitch = Get-VMSwitch -SwitchType External -Name PackerSwitch
if (!$VMswitch) {
$Outmsg = "ERROR Hyper-V Switch $VMswitch is missing"
Write-Host "[*] $Outmsg" -ForegroundColor red -BackgroundColor black;
Add-Content $BuildLog $Outmsg
exit 1;
}
if ($VMswitch) {
Write-Host "[*] Hyper-V PackerSwtich good" -ForegroundColor green -BackgroundColor black;
}
#
# clean up old build
#
$Outmsg = "Clean old builds"
Write-Host "[*] $Outmsg" -ForegroundColor green -BackgroundColor black;
Add-Content $BuildLog $Outmsg
# delete old build items
Remove-Item -LiteralPath ".\temp\" -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item -LiteralPath ".\boxes\" -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item -LiteralPath ".\output-centos8\" -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item -LiteralPath ".\packer_cache\" -Force -Recurse -ErrorAction SilentlyContinue
# remove box
#vagrant box remove file://boxes/CentOS8.box
#
# Packer
#
# environment vars for packer
$env:PACKER_LOG = 3
$env:PACKER_LOG_PATH = "$packerlogloc"
$env:PACKER_CACHE_DIR = "./temp/cache/"
Add-Content $BuildLog "packer log: packer_$LogStamp.log"
# validate
$Outmsg = "validate packer HCL"
Write-Host "[*] $Outmsg" -ForegroundColor green -BackgroundColor black;
Add-Content $BuildLog $Outmsg
try {
Start-Process -NoNewWindow -Wait -ArgumentList "validate", "-syntax-only", "$packerinput" packer.exe
}
catch {
$Outmsg = "invalid packer HCL"
Write-Host "[*] $Outmsg" -ForegroundColor red -BackgroundColor black;
Add-Content $BuildLog $Outmsg
exit 1;
}
Start-Sleep -Seconds 1
# build box
$Outmsg = "start packer build"
Write-Host "[*] $Outmsg" -ForegroundColor green -BackgroundColor black;
Add-Content $BuildLog $Outmsg
try {
Start-Process -NoNewWindow -Wait -ArgumentList 'build', "-only=$packertemplatef", "$packerinput" packer.exe
}
catch {
$Outmsg = "error building"
Write-Host "[*] $Outmsg" -ForegroundColor red -BackgroundColor black;
Add-Content $BuildLog $Outmsg
exit 1;
}
finally {
# show build output
Write-Host "[*] Build artefacts:" -ForegroundColor green -BackgroundColor black;
(Get-ChildItem .\boxes\ | Format-Table -HideTableHeaders | Out-String).Trim()
}
#
# Vagrant
#
# check the Vagrant VM is not running
#$CheckVM = "centos8vm"
#$VMName = Get-VM -name $CheckVM -ErrorAction SilentlyContinue
#if ($VMname) {
# Write-Host "[*] ERROR Hyper-V VM '$CheckVM running" -ForegroundColor green -BackgroundColor black;
# exit 1;
#}
#$env:VAGRANT_DEFAULT_PROVIDER = hyperv
# validate vagrant file
$Outmsg = "validate the Vagrantfile"
Write-Host "[*] $Outmsg" -ForegroundColor green -BackgroundColor black;
Add-Content $BuildLog $Outmsg
try {
Start-Process -NoNewWindow -Wait -ArgumentList "validate", ".\Vagrantfile" vagrant.exe
}
catch {
$Outmsg = "Vagrantfile validation falied"
Write-Host $Outmsg -ForegroundColor red -BackgroundColor black;
Add-Content $BuildLog $Outmsg
exit 1;
}
# remove old box
# vagrant.exe box remove centos8vm
# add new box
try {
$Outmsg = "add the box $newhvbox to vagrant store"
Write-Host "[*] $Outmsg" -ForegroundColor green -BackgroundColor black;
Add-Content $BuildLog $Outmsg
# get box name
$newhvbox = Get-ChildItem .\boxes\ -Name *.box
# add
Start-Process -NoNewWindow -Wait -ArgumentList "box", "add", "centos8vm", ".\boxes\$newhvbox" vagrant.exe
}
catch {
$Outmsg = "failed to add box to vagrant store"
Write-Host $Outmsg -ForegroundColor red -BackgroundColor black;
Add-Content $BuildLog $Outmsg
exit 1;
}
#
# done
#
$timefinish = (Get-Date)
$timetaken = $(($timefinish - $timestart).totalseconds)
$Outmsg = "Done. Build took $timetaken seconds."
Write-Host "[*] $Outmsg" -ForegroundColor green -BackgroundColor black;
Add-Content $BuildLog $Outmsg