-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrun_borg_hyperv.ps1
370 lines (308 loc) · 14 KB
/
run_borg_hyperv.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#
# Run the Basic Operations and Readiness Gateway in Hyper-V. This script will:
# - Copy a VHD from the safe-templates folder to working-vhds
# - Create a VM around the VHD and launch it. It is assumed that the VHD has a
# properly configured RunOnce set up
# - Wait for the VM to tell us it's done. The VM will use PSRP to do a live
# update of a log file on this machine, and will write a sentinel file
# when the install succeeds or fails.
#
# Author: John W. Fawcett, Principal Software Development Engineer, Microsoft
#
param (
[Parameter(Mandatory=$false)] [string] $skipCopy=$false
)
$global:completed=0
$global:elapsed=0
$global:interval=500
$global:boot_timeout_minutes=45
$global:boot_timeout_intervals=$interval*($boot_timeout_minutes*60*(1000/$interval))
$global:num_expected=0
$global:num_remaining=0
$global:failed=$false
$global:booted_version="Unknown"
$global:timer_is_running = 0
class MonitoredMachine {
[string] $name="unknown"
[string] $status="Unitialized"
}
$timer=New-Object System.Timers.Timer
[System.Collections.ArrayList]$global:monitoredMachines = @()
$action={
function checkMachine ([MonitoredMachine]$machine) {
$machineName=$machine.name
$machineStatus=$machine.status
Write-Host " Checking boot results for machine $machineName" -ForegroundColor green
if ($machineStatus -ne "Booting") {
Write-Host " ???? Machine was not in state Booting. Cannot process" -ForegroundColor Red
return
}
$resultsFile="c:\temp\boot_results\" + $machineName
$progressFile="c:\temp\progress_logs\" + $machineName
if ((test-path $resultsFile) -eq $false) {
Write-Host " Unable to locate results file $resultsFile. Cannot process" -ForegroundColor Red
return
}
$results=get-content $resultsFile
$resultsSplit = $results.split(' ')
$resultsWord=$resultsSplit[0]
$resustsgot=$resultsSplit[1]
if ($resultsSplit[0] -ne "Success") {
$resultExpected = $resultsSplit[2]
Write-Host " **** Machine $machineName rebooted, but wrong version detected. Expected $resultExpected but got $resustsgot" -ForegroundColor red
$global:failed=$true
} else {
Write-Host " **** Machine rebooted successfully to kernel version $resustsgot" -ForegroundColor green
$global:booted_version=$resustsgot
}
$machine.status = "Completed"
$global:num_remaining--
}
if ($global:timer_is_running -eq 0) {
return
}
$global:elapsed=$global:elapsed+$global:interval
# write-host "Checking elapsed = $global:elapsed against interval limit of $global:boot_timeout_intervals"
if ($elapsed -ge $global:boot_timeout_intervals) {
write-host "Timer has timed out." -ForegroundColor red
$global:completed=1
}
#
# Check for Hyper-V completion
#
foreach ($localMachine in $global:monitoredMachines) {
[MonitoredMachine]$monitoredMachine=$localMachine
$monitoredMachineName=$monitoredMachine.name
$monitoredMachineStatus=$monitoredMachine.status
$bootFile="c:\temp\boot_results\" + $monitoredMachineName
if (($monitoredMachineStatus -eq "Booting") -and ((test-path $bootFile) -eq $true)) {
checkMachine $monitoredMachine
}
}
if ($global:num_remaining -eq 0) {
write-host "***** All machines have reported in." -ForegroundColor magenta
if ($global:failed -eq $true) {
Write-Host "One or more machines have failed to boot. This job has failed." -ForegroundColor Red
}
write-host "Stopping the timer" -ForegroundColor green
$global:completed=1
return
}
#
# Update the UI
#
if (($global:elapsed % 10000) -eq 0) {
Write-Host ""
Write-Host "Waiting for remote machines to complete all testing. There are $global:num_remaining machines left.." -ForegroundColor green
foreach ($localMachine in $global:monitoredMachines) {
[MonitoredMachine]$monitoredMachine=$localMachine
$monitoredMachineName=$monitoredMachine.name
$logFile="c:\temp\progress_logs\" + $monitoredMachineName
$monitoredMachineStatus=$monitoredMachine.status
if ($monitoredMachineStatus -eq "Booting") {
if ((test-path $logFile) -eq $true) {
write-host " --- Last 3 lines of results from $logFile" -ForegroundColor magenta
get-content $logFile | Select-Object -Last 3 | write-host -ForegroundColor cyan
write-host "" -ForegroundColor magenta
} else {
Write-Host " --- Machine $monitoredMachineName has not checked in yet"
}
}
}
[Console]::Out.Flush()
}
}
Write-Host " " -ForegroundColor green
Write-Host " **********************************************" -ForegroundColor yellow
Write-Host " * *" -ForegroundColor yellow
Write-Host " * Microsoft Linux Kernel *" -ForegroundColor yellow
Write-Host " * Basic Operational Readiness Gateway *" -ForegroundColor yellow
Write-Host " * Host Infrastructure Validation Environment *" -ForegroundColor yellow
Write-Host " * *" -ForegroundColor yellow
Write-Host " * Welcome to the BORG HIVE *" -ForegroundColor yellow
Write-Host " **********************************************" -ForegroundColor yellow
Write-Host " "
Write-Host " Initializing the CUBE (Customizable Universal Base of Execution)" -ForegroundColor yellow
Write-Host " "
#
# Clean up the sentinel files
#
Write-Host "Cleaning up sentinel files..." -ForegroundColor green
remove-item -ErrorAction "silentlycontinue" C:\temp\completed_boots\*
remove-item -ErrorAction "silentlycontinue" C:\temp\boot_results\*
remove-item -ErrorAction "silentlycontinue" C:\temp\progress_logs\*
Write-Host " "
Write-Host " BORG CUBE is initialized" -ForegroundColor Yellow
Write-Host " Starting the Dedicated Remote Nodes of Execution (DRONES)" -ForegroundColor yellow
Write-Host " "
Write-Host "Checking to see which VMs we need to bring up..." -ForegroundColor green
Write-Host "Errors may appear here depending on the state of the system. They're almost all OK. If things go bad, we'll let you know." -ForegroundColor Green
Write-Host "For now, though, please feel free to ignore the following errors..." -ForegroundColor Green
Write-Host " "
Write-Host "*************************************************************************************************************************************"
Write-Host " Stopping and cleaning any existing machines. Any errors here may be ignored." -ForegroundColor green
get-job | Stop-Job > $null
get-job | remove-job > $null
#
# Copy the template VHDs from the safe folder to a working one
#
Get-ChildItem 'D:\azure_images\*.vhd' |
foreach-Object {
$vhdFile=$_.Name
$status="Copying"
$global:num_remaining++
$vhdFileName=$vhdFile.Split('.')[0]
$machine = new-Object MonitoredMachine
$machine.name = $vhdFileName
$machine.status = "Booting" # $status
$global:monitoredMachines.Add($machine)
Write-Host "Stopping and cleaning any existing instances of machine $vhdFileName." -ForegroundColor green
stop-vm -Name $vhdFileName -Force -ErrorAction SilentlyContinue > $null
remove-vm -Name $vhdFileName -Force -ErrorAction SilentlyContinue > $null
$machine.status = "Allocating"
# Copy-Item $sourceFile $destFile -Force
$destFile="d:\working_images\" + $vhdFile
if ($skipCopy -eq $false) {
Remove-Item -Path $destFile -Force > $null
Write-Host "Starting job to copy VHD $vhdFileName to working directory..." -ForegroundColor green
$jobName=$vhdFileName + "_copy_job"
$existingJob = get-job $jobName -ErrorAction SilentlyContinue > $null
if ($? -eq $true) {
stop-job $jobName -ErrorAction SilentlyContinue > $null
remove-job $jobName -ErrorAction SilentlyContinue > $null
}
Start-Job -Name $jobName -ScriptBlock { robocopy /njh /ndl /nc /ns /np /nfl D:\azure_images\ D:\working_images\ $args[0] } -ArgumentList @($vhdFile) > $null
} else {
Write-Host "Skipping copy per command line option"
}
}
Write-Host "*************************************************************************************************************************************"
Write-Host " "
Write-Host " Start paying attention to errors again..." -ForegroundColor green
Write-Host " "
#
# Wait for the background copy jobs to complete before trying to start them up
#
if ($skipCopy -eq $false) {
while ($true) {
Write-Host "Waiting for copying to complete..." -ForegroundColor green
$copy_complete=$true
Get-ChildItem 'D:\azure_images\*.vhd' |
foreach-Object {
$vhdFile=$_.Name
$vhdFileName=$vhdFile.Split('.')[0]
$jobName=$vhdFileName + "_copy_job"
$jobStatus=get-job -Name $jobName -ErrorAction SilentlyContinue
if ($? -eq $true) {
$jobState = $jobStatus.State
} else {
$jobStatus = "Completed"
}
if (($jobState -ne "Completed") -and
($jobState -ne "Failed")) {
Write-Host " Current state of job $jobName is $jobState" -ForegroundColor yellow
$copy_complete = $false
}
elseif ($jobState -eq "Failed")
{
$global:failed = $true
Write-Host "----> Copy job $jobName exited with FAILED state!" -ForegroundColor red
Receive-Job -Name $jobName
}
else
{
Write-Host " Copy job $jobName completed successfully." -ForegroundColor green
remove-job $jobName -ErrorAction SilentlyContinue
}
}
if ($copy_complete -eq $false) {
sleep 30
} else {
break
}
}
if ($global:failed -eq $true) {
write-host "Copy failed. Cannot continue..."
exit 1
}
}
Write-Host "All machines template images have been copied. Starting the VMs in Hyper-V" -ForegroundColor green
#
# Fire them up! When they boot, the runonce should take over and install the new kernel.
#
Get-ChildItem 'D:\working_images\*.vhd' |
foreach-Object {
$vhdFile=$_.Name
$vhdFileName=$vhdFile.Split('.')[0]
foreach ($localMachine in $global:monitoredMachines) {
[MonitoredMachine]$monitoredMachine=$localMachine
$monitoredMachineName=$machine.name
if ($monitoredMachineName -eq $vhdFileName) {
break
}
}
$vhdPath="D:\working_images\"+$vhdFile
Write-Host "BORG DRONE $vhdFileName is starting" -ForegroundColor green
new-vm -Name $vhdFileName -MemoryStartupBytes 7168mb -Generation 1 -SwitchName "External" -VHDPath $vhdPath > $null
$monitoredMachine.status = "Booting"
if ($? -eq $false) {
Write-Host "Unable to create Hyper-V VM. The BORG cannot continue." -ForegroundColor Red
exit 1
}
Start-VM -Name $vhdFileName > $null
if ($? -eq $false) {
Write-Host "Unable to start Hyper-V VM. The BORG cannot continue." -ForegroundColor Red
exit 1
}
}
#
# Wait for the machines to report back
#
write-host " Initiating temporal evaluation loop (Starting the timer)" -ForegroundColor yellow
unregister-event HyperVBORGTimer -ErrorAction SilentlyContinue > $null
Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier HyperVBORGTimer -Action $action > $null
$global:timer_is_running = 1
$timer.Interval = 1000
$timer.Enabled = $true
$timer.start()
while ($global:completed -eq 0) {
start-sleep -s 1
}
write-host " Exiting Temporal Evaluation Loop (Unregistering the timer)" -ForegroundColor yellow
$global:timer_is_running = 0
$timer.stop()
unregister-event HyperVBORGTimer > $null
#
# We either had success or timed out. Figure out which
#
write-host "Checking results" -ForegroundColor green
if ($global:num_remaining -eq 0) {
Write-Host "All machines have come back up. Checking results." -ForegroundColor green
if ($global:failed -eq $true) {
Write-Host "Failures were detected in reboot and/or reporting of kernel version. See log above for details." -ForegroundColor red
write-host " BORG TESTS HAVE FAILED!!" -ForegroundColor red
} else {
Write-Host "All machines rebooted successfully to kernel version $global:booted_version" -ForegroundColor green
write-host " BORG has been passed successfully!" -ForegroundColor yellow
}
} else {
$global:failed = $true
write-host "Not all machines booted in the allocated time!" -ForegroundColor red
Write-Host " Machines states are:" -ForegroundColor red
foreach ($localMachine in $global:monitoredMachines) {
[MonitoredMachine]$monitoredMachine=$localMachine
$monitoredMachineName=$monitoredMachine.name
$monitoredMachineState=$monitoredMachine.status
Write-Host Machine "$monitoredMachineName is in state $monitoredMachineState" -ForegroundColor red
}
}
#
# Thanks for playing!
#
if ($global:failed -eq $false) {
Write-Host " BORG is Exiting with success. Thanks for Playing" -ForegroundColor green
exit 0
} else {
Write-Host " BORG is Exiting with failure. Thanks for Playing" -ForegroundColor red
exit 1
}