forked from seansp-zz/Framework-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_vhd_from_uri.ps1
380 lines (316 loc) · 13.7 KB
/
create_vhd_from_uri.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
371
372
373
374
375
376
377
378
379
380
param (
[Parameter(Mandatory=$false)] [string[]] $Incoming_vmNames,
[Parameter(Mandatory=$false)] [string[]] $Incoming_blobURIs,
[Parameter(Mandatory=$false)] [string] $destRG="jpl_intake_rg",
[Parameter(Mandatory=$false)] [string] $destSA="jplintakestorageacct",
[Parameter(Mandatory=$false)] [string] $destContainer="ready-for-bvt",
[Parameter(Mandatory=$false)] [string] $location="westus",
[Parameter(Mandatory=$false)] [string] $useExistingResources = "False",
[Parameter(Mandatory=$false)] [string] $vnetName = "SmokeVNet",
[Parameter(Mandatory=$false)] [string] $subnetName = "SmokeSubnet-1",
[Parameter(Mandatory=$false)] [string] $NSG = "SmokeNSG",
[Parameter(Mandatory=$false)] [string] $suffix = "-Smoke-1",
[Parameter(Mandatory=$false)] [string] $VMFlavor="standard_d2_v2"
)
$destSA = $destSA.Trim()
$destRG = $destRG.Trim()
$destContainer = $destContainer.Trim()
$location = $location.Trim()
$useExistingResources = $useExistingResources.Trim()
$vnetName = $vnetName.Trim()
$subnetName = $subnetName.Trim()
$NSG = $NSG.Trim()
$suffix = $suffix.Trim()
$VMFlavor = $VMFlavor.Trim()
$suffix = $suffix -replace "_","-"
$overallTimer = [Diagnostics.Stopwatch]::StartNew()
$commandTimer = [Diagnostics.Stopwatch]::StartNew()
get-job | Stop-Job
get-job | remove-job
$timeStarted = (Get-Date -Format s).replace(":","-")
$logName = "C:\temp\transcripts\create_vhd_from_URI_-" + $timeStarted
Start-Transcript -path $logName -force
$vmNames_array=@()
$vmNameArray = {$vmNames_array}.Invoke()
$vmNameArray.Clear()
if ($Incoming_vmNames -like "*,*") {
$vmNameArray = $Incoming_vmNames.Split(',')
} else {
$vmNameArray = $Incoming_vmNames
}
$blobURI_Array=@()
$blobURIArray = {$blobURI_Array}.Invoke()
$blobURIArray.Clear()
if ($IncOming_blobURIs -like "*,*") {
$blobURIArray = $Incoming_blobURIs.Split(',')
} else {
$blobURIArray = $Incoming_blobURIs
}
Write-Host "Names array: " $vmNameArray -ForegroundColor Yellow
$numNames = $vmNameArray.Count
Write-Host "blobs array: " $blobURIArray -ForegroundColor Yellow
$numBlobs = $blobURIArray.Count
if ($vmNameArray.Count -ne $blobURIArray.Count) {
Write-Host "Please provide the same number of names and URIs. You have $numNames names and $numBlobs blobs" -ForegroundColor Red
exit 1
} else {
$numLeft = $vmNameArray.Count
Write-Host "There are $numLeft machines to process..." -ForegroundColor Gray
}
$vmName = $vmNameArray[0]
. "C:\Framework-Scripts\common_functions.ps1"
. "C:\Framework-Scripts\secrets.ps1"
$location=($location.tolower()).Replace(" ","")
# Log in without changing to the RG or SA. This is intentional
login_azure
$saLength = $destSA.Length
Write-Host "Looking for storage account $destSA in resource group $destRG. Length of name is $saLength"
#
$existingGroup = Get-AzureRmResourceGroup -Name $destRG
$status = $?
if ($status -eq $true -and $existingGroup -ne $null -and $useExistingResources -eq "False") {
write-host "Resource group already existed. Deleting resource group." -ForegroundColor Yellow
Remove-AzureRmResourceGroup -Name $destRG -Force
write-host "Creating new resource group $destRG in loction $location"
New-AzureRmResourceGroup -Name $destRG -Location $location
} elseif ($status -eq $false -and $existingGroup -eq $null) {
write-host "Creating new resource group $destRG in loction $location"
New-AzureRmResourceGroup -Name $destRG -Location $location
} else {
write-host "Using existing resource group $destRG"
}
#
#
# Change the name of the SA to include the region, then Now see if the SA exists
$existing = Get-AzureRmStorageAccount -ResourceGroupName $destRG -Name $destSA
if ($? -eq $false -or $existing -eq $null) {
Write-Host "Storage account $destSA did not exist. Creating it and populating with the right containers..." -ForegroundColor Yellow
New-AzureRmStorageAccount -ResourceGroupName $destRG -Name $destSA -Location $location -SkuName Standard_LRS -Kind Storage
write-host "Selecting it as the current SA" -ForegroundColor Yellow
Set-AzureRmCurrentStorageAccount -ResourceGroupName $destRG -Name $destSA
Write-Host "creating the containers" -ForegroundColor Yellow
New-AzureStorageContainer -Name "ready-for-bvt" -Permission Blob
New-AzureStorageContainer -Name "drones" -Permission Blob
Write-Host "Complete." -ForegroundColor Green
}
Set-AzureRmCurrentStorageAccount -ResourceGroupName $destRG -Name $destSA
Get-AzureStorageBlob -Container "ready-for-bvt" -Prefix $vmName
if ($? -eq $false) {
Write-Host "creating the BVT ready container" -ForegroundColor Yellow
New-AzureStorageContainer -Name "ready-for-bvt" -Permission Blob
}
Get-AzureStorageBlob -Container "drones" -Prefix $vmName
if ($? -eq $false) {
New-AzureStorageContainer -Name "drones" -Permission Blob
Write-Host "Complete." -ForegroundColor Green
}
$commandTimer.Stop()
$elapsed = $commandTimer.Elapsed
Write-Host "It required $elapsed to create validate the resource group, storage account, and container"
$commandTimer = [Diagnostics.Stopwatch]::StartNew()
. C:\Framework-Scripts\backend.ps1
# . "$scriptPath\backend.ps1"
## Storage
$vnetAddressPrefix = "10.0.0.0/16"
$vnetSubnetAddressPrefix = "10.0.0.0/24"
Write-Host "Initializing the back-end"
$backendFactory = [BackendFactory]::new()
$azureBackend = $backendFactory.GetBackend("AzureBackend", @(1))
Write-Host "Done Initializing the back-end"
$azureBackend.ResourceGroupName = $destRG
$azureBackend.StorageAccountName = $destSA
$azureBackend.ContainerName = $destContainer
$azureBackend.Location = $location
$azureBackend.VMFlavor = "No-Flavor"
$azureBackend.NetworkName = $vnetName
$azureBackend.SubnetName = $subnetName
$azureBackend.NetworkSecGroupName = $NSG
$azureBackend.addressPrefix = $vnetAddressPrefix
$azureBackend.subnetPrefix = $vnetSubnetAddressPrefix
$azureBackend.blobURI = ""
$azureBackend.suffix = ""
$azureBackend.useInitialPW = "Yes"
$azureInstance = $azureBackend.GetInstanceWrapper("AzureSetup")
$azureInstance.SetupAzureRG()
#
# If the account does not exist, create it.
$scriptBlockString =
{
param ( [string] $vmName,
[string] $VMFlavor,
[string] $blobURI,
[string] $destRG,
[string] $destSA,
[string] $destContainer,
[string] $location,
[string] $suffix,
[string] $NSG,
[string] $vnetName,
[string] $subnetName,
[string] $useExistingResources,
[string] $timeStarted
)
write-host "RG = $destRG"
$logName = "C:\temp\transcripts\create_vhd_from_URI_scriptblock-" + $vmName + "-" + $timeStarted
Start-Transcript -path $logName -force
. C:\Framework-Scripts\common_functions.ps1
. C:\Framework-Scripts\secrets.ps1
$NSG = $NSG
$subnetName = $subnetName
$vnetName = $vnetName
login_azure $destRG $destSA $location
write-host "************* destRG is $destRG"
write-host "************* destSA is $destSA"
Set-AzureRmCurrentStorageAccount -ResourceGroupName $destRG -StorageAccountName $destSA
Write-Host "Deleting any existing VM" -ForegroundColor Green
$runningVMs = Get-AzureRmVm -ResourceGroupName $destRG -status | Where-Object -Property Name -Like "$vmName*" | Remove-AzureRmVM -Force
if ($? -eq $true -and $runningVMs -ne $null) {
deallocate_machines_in_group $runningVMs $destRG $destSA $location
}
Write-Host "Clearing any old images in $destContainer with prefix $vmName..." -ForegroundColor Green
Get-AzureStorageBlob -Container $destContainer -Prefix $vmName | ForEach-Object {Remove-AzureStorageBlob -Blob $_.Name -Container $destContainer}
. C:\Framework-Scripts\backend.ps1
# . "$scriptPath\backend.ps1"
$backendFactory = [BackendFactory]::new()
$azureBackend = $backendFactory.GetBackend("AzureBackend", @(1))
$azureBackend.ResourceGroupName = $destRG
$azureBackend.StorageAccountName = $destSA
$azureBackend.ContainerName = $destContainer
$azureBackend.Location = $location
$azureBackend.UseExistingResources = $useExistingResources
#
# These are intake VHDs, so they don't get a VM Flavor.
$azureBackend.VMFlavor = $VMFlavor
$azureBackend.NetworkName = $vnetName
$azureBackend.SubnetName = $subnetName
$azureBackend.NetworkSecGroupName = $NSG
$azureBackend.addressPrefix = $vnetAddressPrefix
$azureBackend.subnetPrefix = $vnetSubnetAddressPrefix
$azureBackend.blobURI = $blobURI
$azureBackend.suffix = $suffix
$azureBackend.useInitialPW = "Yes"
$azureBackend.enableBootDiagnostics = "No"
$azureInstance = $azureBackend.GetInstanceWrapper($vmName)
$azureInstance.CreateFromGeneralized()
#
# Disable Cloud-Init so it doesn't try to deprovision the machine (known bug in Azure)
write-host "Attempting to contact the machine..." -ForegroundColor Green
$machineIsUp = $false
[int]$sleepCount = 0
while ($false -eq $machineIsUp -and $sleepCount -lt 30) {
[string]$ip=$azureInstance.GetPublicIP()
$machineIsUp = $true
$sleepCount = $sleepCount + 1
if ($ip -eq $null -or $ip.ToLower() -eq "not assigned") {
$machineIsUP = $false
start-sleep -Seconds 10
} else {
$machineIsUp = $true
break
}
}
if ($false -eq $machineIsUp) {
write-host "Could not contact machine $vmName. Machine did not get IP address after 300 seconds"
retURI 1
}
$password=$TEST_USER_ACCOUNT_PAS2
$username=$TEST_USER_ACCOUNT_NAME
#
# Disable cloud-init
$disableCommand0="mv /usr/bin/cloud-init /usr/bin/cloud-init.DO_NOT_RUN_THIS"
$runDisableCommand0="`"echo `'$password`' | sudo -S bash -c `'$disableCommand0`'`""
#
# Eat the prompt and get the host into .known_hosts
$remoteAddress = $ip
$remoteTmp=$remoteAddress + ":/tmp"
Write-Host "Attempting to contact remote macnhine using $remoteAddress" -ForegroundColor Green
$timeOut = 0
while ($true) {
$sslReply=@(echo "y" | C:\azure-linux-automation\tools\pscp -pw $password -l $username C:\Framework-Scripts\README.md $remoteTmp)
Write-Output "SSL Rreply is $sslReply"
if ($sslReply -match "README" ) {
Write-Host "Got a key request" -ForegroundColor Green
break
} else {
Write-Host "No match" -ForegroundColor Yellow
Start-Sleep -Seconds 10
$timeOut = $timeOut + 1
if ($timeOut -ge 60) {
Write-Host "Failed to contact machine at IP $remoteAddress for 600 seconds. Timeout."
Stop-Transcript
retURI 1
}
}
}
$sslReply=@(echo "y" | C:\azure-linux-automation\tools\pscp -pw $password -l $username C:\Framework-Scripts\README.md $remoteTmp)
Write-Host "Setting SELinux into permissive mode" -ForegroundColor Green
try_plink $ip $runDisableCommand0
Write-Host "Deleting the VM so we can harvest the VHD..." -ForegroundColor Green
$azureInstance.RemoveInstance()
Write-Host "And cleaning up..." -ForegroundColor Green
$azureInstance.Cleanup()
Write-Host "Machine $vmName is ready for assimilation..." -ForegroundColor Green
Stop-Transcript
}
$scriptBlock = [scriptblock]::Create($scriptBlockString)
$i = 0
foreach ($vmName in $vmNameArray) {
$blobURI = $blobURIArray[$i]
$i++
Write-Host "Preparing machine $vmName for (URI $blobURI) service as a drone..." -ForegroundColor Green
$jobName=$vmName + "-intake-job"
Start-Job -Name $jobName -ScriptBlock $scriptBlock -ArgumentList $vmName,$VMFlavor,$blobURI,$destRG,$destSA,`
$destContainer,$location,$suffix,$NSG,`
$vnetName,$subnetName,$useExistingResources,$timeStarted
if ($? -ne $true) {
Write-Host "Error starting intake_machine job ($jobName) for $vmName. This VM must be manually examined!!" -ForegroundColor red
Stop-Transcript
exit 1
}
Write-Host "Just launched job $jobName" -ForegroundColor Green
}
$commandTimer.Stop()
$elapsed = $commandTimer.Elapsed
Write-Host "It required $elapsed launch all the machine intake jobs"
$commandTimer = [Diagnostics.Stopwatch]::StartNew()
Start-Sleep -Seconds 10
$notDone = $true
while ($notDone -eq $true) {
write-host "Status at "@(date)"is:" -ForegroundColor Green
$notDone = $false
foreach ($vmName in $vmNameArray) {
$jobName=$vmName + "-intake-job"
$job = get-job $jobName
$jobState = $job.State
if ($jobState -eq "Running") {
$notDone = $true
$useColor = "Yellow"
} elseif ($jobState -eq "Completed") {
$useColor="green"
} elseif ($jobState -eq "Failed") {
$useColor = "Red"
} elseif ($jobState -eq "Blocked") {
$useColor = "Magenta"
}
write-host " Job $jobName is in state $jobState" -ForegroundColor $useColor
$logFileName = "C:\temp\transcripts\create_vhd_from_URI_scriptblock-" + $vmName + "-" + $timeStarted
$logLines = Get-Content -Path $logFileName -Tail 5
if ($? -eq $true) {
Write-Host " Last 5 lines from the log file:" -ForegroundColor Cyan
foreach ($line in $logLines) {
write-host " " $line -ForegroundColor Gray
}
}
}
Start-Sleep -Seconds 10
}
$commandTimer.Stop()
$elapsed = $commandTimer.Elapsed
Write-Host "It required $elapsed for all the machines to complete"
$commandTimer.Stop()
$elapsed = $commandTimer.Elapsed
Write-Host "It required $elapsed for script create_vhd_from_uri to to complete"
Stop-Transcript
#
# Exit with error if we failed to create the VM. THe setup may have failed, but we can't tell that right ow
exit 0