This repository was archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.build.ps1
297 lines (276 loc) · 14.2 KB
/
.build.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
param (
# Path to the environment JSON file used to identify the vCenter and Rubrik servers
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[String]$EnvironmentFile,
# Path to the configuration JSON file used to describe the applications being tested
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[String]$ConfigFile,
# Path to the folder that contains XML credential files for this build
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[String]$IdentityPath
)
# Synopsis: Pull configuration details from the root config.json file
task GetConfig {
$script:Environment = Get-Content -Path $EnvironmentFile | ConvertFrom-Json
$script:Config = Get-Content -Path $ConfigFile | ConvertFrom-Json
# If a trailing backslash is omitted, this will make sure it's added to correct for future path + filename activities
if ($IdentityPath.Substring($IdentityPath.Length - 1) -ne '\') {
$script:IdentityPath += '\'
}
}
# Synopsis: Establish connectivity to a Rubrik Cluster
task ConnectRubrik {
Write-Verbose -Message "Importing Credential file: $($IdentityPath + $Environment.rubrikCred)"
$Credential = Import-Clixml -Path ($IdentityPath + $Environment.rubrikCred)
$null = Connect-Rubrik -Server $Environment.rubrikServer -Credential $Credential
Write-Verbose -Message "Rubrik Status: Connected to $($rubrikConnection.server)" -Verbose
}
# Synopsis: Establish connectivity to a VMware vCenter Server
task ConnectVMware {
Write-Verbose -Message "Importing Credential file: $($IdentityPath + $Environment.vmwareCred)"
$Credential = Import-Clixml -Path ($IdentityPath + $Environment.vmwareCred)
$null = Connect-VIServer -Server $Environment.vmwareServer -Credential $Credential
Write-Verbose -Message "VMware Status: Connected to $($global:DefaultVIServer.Name)" -Verbose
}
# Synopsis: Create a Live Mount of the intended virtual machine(s)
task CreateLiveMount {
$i = 0
# Uses a null array of Mount IDs that will be used to track the request process
[Array]$Script:MountArray = $null
foreach ($VM in $Config.virtualMachines) {
# Check if there is already an existing live mount with the same name
if ( ( $MountTest = (Get-RubrikMount).mountedVmId | Where-Object {$_.total -ne 0} ) ) {
if ($MountTest |
ForEach-Object {
(Get-RubrikVM -ID $_ -EA 0).name
} | Select-String -Pattern "^$($VM.mountName)$" ) {
throw "The live mount $($VM.mountName) already exists. Please remove manually."
}
}
# The resulting Live Mount has the network interface disabled
$MountRequest = Get-RubrikVM $VM.name |
Get-RubrikSnapshot -Date (Get-Date) | Where-Object {$_.id} | ForEach-Object {
New-RubrikMount -Id $_.id -MountName $VM.mountName -PowerOn:$true -DisableNetwork:$true -Confirm:$false
}
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) Request Created: $($MountRequest.id)" -Verbose
$Script:MountArray += $MountRequest
$i++
}
}
# Synopsis: Validate the health of the Live Mount request and power state
task ValidateLiveMount {
$i = 0
foreach ($Mount in $MountArray) {
while ($true) {
$ValidateRequest = (Get-RubrikRequest -id $Mount.id -Type vmware/vm).status
$ValidatePowerOn = (Get-VM -Name $Config.virtualMachines[$i].mountName -ErrorAction:SilentlyContinue).PowerState
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) Status: Request is $ValidateRequest, PowerState is $ValidatePowerOn" -Verbose
if ($ValidateRequest -in @('RUNNING','ACQUIRING','QUEUED','FINISHING')) {
Start-Sleep 5
} elseif ($ValidateRequest -eq 'SUCCEEDED' -and $ValidatePowerOn -eq 'PoweredOn') {
break
} else {
throw "$($Config.virtualMachines[$i].mountName) mount failed, exiting Build script. Previously live mounted VMs will continue running"
}
}
$i++
}
}
# Synopsis: Validate the health of the Live Mount VMware Tools
task ValidateLiveMountTools {
$i = 0
foreach ($Mount in $MountArray) {
while ($true) {
$ValidateTools = (Get-VM -Name $Config.virtualMachines[$i].mountName).ExtensionData.Guest.ToolsRunningStatus
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) VMware Tools Status: $ValidateTools" -Verbose
if ($ValidateTools -ne 'guestToolsRunning') {
Start-Sleep 5
} else {
break
}
}
$i++
}
}
task ValidateRemoteScriptExecution {
$i = 0
foreach ($Mount in $MountArray) {
$LoopCount = 1
# Keeping the guest credential value local since it may only apply to the individual virtual machine in some cases
# Try per vm guest credentials first
if ( Get-Variable -Name "$Config.virtualMachines[$i].guestCred" -ErrorAction SilentlyContinue ) {
Write-Verbose -Message "Importing Credential file: $($IdentityPath + $($Config.virtualMachines[$i].guestCred))" -Verbose
$GuestCredential = Import-Clixml -Path ($IdentityPath + $($Config.virtualMachines[$i].guestCred))
}
# Use global guest credentials
else {
Write-Verbose -Message "Importing Credential file: $($IdentityPath + "guestCred.XML")" -Verbose
$GuestCredential = Import-Clixml -Path ($IdentityPath + "guestCred.XML")
}
while ($true) {
Write-Verbose -Message "Testing script execution on '$($Config.virtualMachines[$i].mountName)', attempt '$LoopCount'..." -Verbose
$splat = @{
ScriptText = 'hostname'
ScriptType = 'PowerShell'
VM = $Config.virtualMachines[$i].mountName
GuestCredential = $GuestCredential
}
try {
$VMScript_return = Invoke-VMScript @splat -ErrorAction Stop
Write-Verbose -Message "ValidateRemoteScriptExecution returned '$VMScript_return'"
break
} catch { }
$LoopCount++
Sleep -Seconds 5
if ($LoopCount -gt 5) {
throw "Could not execute script on: $($Config.virtualMachines[$i].mountName)..."
}
}
$i++
}
}
# Synopsis: Move a Live Mount to a test network
task MoveLiveMountNetwork {
$i = 0
foreach ($Mount in $MountArray) {
$SplatNetAdapter = @{
NetworkName = $Config.virtualMachines[$i].testNetwork
Connected = $true
Confirm = $false
}
$ValidateNetwork = Get-NetworkAdapter -VM $Config.virtualMachines[$i].mountName |
Set-NetworkAdapter @SplatNetAdapter
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) Network Status: $($ValidateNetwork.NetworkName) is $($ValidateNetwork.ConnectionState)" -Verbose
$i++
}
}
# Synopsis: Move a Live Mount to a test address
task MoveLiveMountNetworkAddress {
$i = 0
foreach ($Mount in $MountArray) {
# Keeping the guest credential value local since it may only apply to the individual virtual machine in some cases
# Try per vm guest credentials first
if ( Get-Variable -Name "$Config.virtualMachines[$i].guestCred" -ErrorAction SilentlyContinue ) {
Write-Verbose -Message "Importing Credential file: $($IdentityPath + $($Config.virtualMachines[$i].guestCred))" -Verbose
$GuestCredential = Import-Clixml -Path ($IdentityPath + $($Config.virtualMachines[$i].guestCred))
}
# Use global guest credentials
else {
Write-Verbose -Message "Importing Credential file: $($IdentityPath + "guestCred.XML")" -Verbose
$GuestCredential = Import-Clixml -Path ($IdentityPath + "guestCred.XML")
}
# Find the first network interface's MAC
$TestInterfaceMAC = ((Get-NetworkAdapter -VM $Config.virtualMachines[$i].mountName | Select-Object -first 1).MacAddress).ToLower() -replace ":","-"
$splat = @{
ScriptText = 'Get-NetAdapter | where {($_.MacAddress).ToLower() -eq "' + $TestInterfaceMAC + '"} | Remove-NetRoute -Confirm:$false -ErrorAction SilentlyContinue;`
Get-NetAdapter | where {($_.MacAddress).ToLower() -eq "' + $TestInterfaceMAC + '"} | Get-NetIPAddress | Remove-NetIPAddress -confirm:$false;`
Get-NetAdapter | where {($_.MacAddress).ToLower() -eq "' + $TestInterfaceMAC + '"} | Set-NetIPInterface -DHCP Disable;`
Get-NetAdapter | where {($_.MacAddress).ToLower() -eq "' + $TestInterfaceMAC + '"} | `
New-NetIPAddress -IPAddress ' + $Config.virtualMachines[$i].testIp + ' -PrefixLength ' + $Config.virtualMachines[$i].testSubnet + `
' -DefaultGateway ' + $Config.virtualMachines[$i].testGateway
ScriptType = 'PowerShell'
VM = $Config.virtualMachines[$i].mountName
GuestCredential = $GuestCredential
}
Write-Verbose -Message "Changing ip of $($Config.virtualMachines[$i].mountName) to $($Config.virtualMachines[$i].testIp)." -Verbose
$output = Invoke-VMScript @splat -ErrorAction Stop
$splat = @{
ScriptText = 'function Elevate-Process {
param ([string]$exe = $(Throw "Pleave provide the name and path of an executable"),[string]$arguments)
$startinfo = new-object System.Diagnostics.ProcessStartInfo
$startinfo.FileName = $exe
$startinfo.Arguments = $arguments
$startinfo.verb = "RunAs"
$process = [System.Diagnostics.Process]::Start($startinfo)
}
function QueryIP {
Get-NetAdapter| where {($_.MacAddress).ToLower() -eq "' + $TestInterfaceMAC + '"}
| Get-NetIPAddress -AddressFamily IPv4).IPAddress
}
Elevate-Process -Exe powershell.exe -Arguments "-noninteractive -command QueryIP > C:\rubrik.txt"'
ScriptType = 'PowerShell'
VM = $Config.virtualMachines[$i].mountName
GuestCredential = $GuestCredential
}
Write-Verbose -Message "Verifying new ip of $($Config.virtualMachines[$i].mountName)." -Verbose
$output = Invoke-VMScript @splat -ErrorAction Stop
$new_ip = $($output.ScriptOutput | Out-String).Trim().Split("`r`n")[-1]
if ( $new_ip -eq $Config.virtualMachines[$i].testIp ) {
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) Network Address Status: Assigned to $($new_ip)"
}
else {
throw "$($Config.virtualMachines[$i].mountName) changing ip to $($Config.virtualMachines[$i].testIp) failed, exiting Build script. Previously live mounted VMs will continue running"
}
$i++
}
}
# Synopsis: Validate the Live Mount against one or more tests to verify the backup copy is operational
task LiveMountTest {
$i = 0
foreach ($Mount in $MountArray) {
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) Test Status: Loading the following tests - $($Config.virtualMachines[$i].tasks)" -Verbose
# Keeping the guest credential value local since it may only apply to the individual virtual machine in some cases
# Not all tests will need a guest credential, but it's there in case required
# Try per vm guest credentials first
if ( Get-Variable -Name "$Config.virtualMachines[$i].guestCred" -ErrorAction SilentlyContinue ) {
Write-Verbose -Message "Importing Credential file: $($IdentityPath + $($Config.virtualMachines[$i].guestCred))" -Verbose
$GuestCredential = Import-Clixml -Path ($IdentityPath + $($Config.virtualMachines[$i].guestCred))
}
# Use global guest credentials
else {
Write-Verbose -Message "Importing Credential file: $($IdentityPath + "guestCred.XML")" -Verbose
$GuestCredential = Import-Clixml -Path ($IdentityPath + "guestCred.XML")
}
Invoke-Build -File .\tests.ps1 -Task $Config.virtualMachines[$i].tasks -Config $Config.virtualMachines[$i] -GuestCredential $GuestCredential
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) Test Status: Testing complete" -Verbose
$i++
}
}
# Synopsis: Remove any remaining Live Mount artifacts
task Cleanup {
$i = 0
foreach ($Mount in $MountArray) {
# The request may take a few seconds to complete, but it's not worth holding up the build waiting for the task
$UnmountRequest = Get-RubrikMount -id (Get-RubrikRequest -id $Mount.id -Type vmware/vm).links.href[0].split('/')[-1] | Remove-RubrikMount -Force -Confirm:$false
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) Removal Status: $($UnmountRequest.id) is $($UnmountRequest.status)" -Verbose
$i++
}
}
task CleanAll {
foreach ($VM in $Config.virtualMachines) {
# Check if there is already an existing live mount with the same name
if ( $MountTest = (Get-RubrikMount -VMID (Get-RubrikVM -VM "$VM.name").id) | Where-Object {$_.total -ne 0} ) {
$MountTest | ForEach-Object {
If ( (Get-RubrikVM -ID $_ -EA 0).name -eq $VM.mountName ) {
Write-Verbose -Message "$($Config.virtualMachines[$i].mountName) removing this live mount" -Verbose
Remove-RubrikMount $_
}
}
}
}
}
task 1_Init `
GetConfig
task 2_Connect `
ConnectRubrik,
ConnectVMware
task 3_LiveMount `
CreateLiveMount,
ValidateLiveMount,
ValidateLiveMountTools,
ValidateRemoteScriptExecution
task 4_LiveMountNetwork `
MoveLiveMountNetworkAddress,
MoveLiveMountNetwork
task 5_Testing `
LiveMountTest
task . `
1_Init,
2_Connect,
3_LiveMount,
4_LiveMountNetwork,
5_Testing,
Cleanup