Skip to content

Commit e1d7096

Browse files
committed
Added threading to creation of VMs and Templates to improve script time.
1 parent b3f0d79 commit e1d7096

File tree

1 file changed

+108
-70
lines changed

1 file changed

+108
-70
lines changed

upi/vsphere/upi.ps1

Lines changed: 108 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$ErrorActionPreference = "Stop"
77

88
# since we do not have ca for vsphere certs, we'll just set insecure
9-
Set-PowerCLIConfiguration -InvalidCertificateAction:Ignore -ParticipateInCEIP $false -Confirm:$false | Out-Null
9+
Set-PowerCLIConfiguration -InvalidCertificateAction:Ignore -Confirm:$false | Out-Null
1010
$Env:GOVC_INSECURE = 1
1111

1212
# Connect to vCenter
@@ -49,10 +49,11 @@ if ($downloadInstaller) {
4949
}
5050

5151
if ($uploadTemplateOva) {
52-
Write-Output "Downloading RHCOS OVA"
52+
Write-Output "Checking for RHCOS OVA"
5353

5454
# If the OVA doesn't exist on the path, determine the url from openshift-install and download it.
5555
if (-Not (Test-Path -Path "template-$($Version).ova")) {
56+
Write-Output "Downloading RHCOS OVA"
5657
Start-Process -Wait -Path ./openshift-install -ArgumentList @("coreos", "print-stream-json") -RedirectStandardOutput coreos.json
5758

5859
$coreosData = Get-Content -Path ./coreos.json | ConvertFrom-Json -AsHashtable
@@ -139,6 +140,9 @@ if (-Not $?) {
139140
$tag = New-Tag -Category $tagCategory -Name "$($metadata.infraID)"
140141
}
141142

143+
$jobs = @()
144+
$templateInProgress = @()
145+
142146
# Check each failure domain for ova template
143147
foreach ($fd in $fds)
144148
{
@@ -161,29 +165,50 @@ foreach ($fd in $fds)
161165
$template = Get-VM -Name $vm_template -Location $fd.datacenter -ErrorAction continue
162166

163167
# Otherwise import the ova to a random host on the vSphere cluster
164-
if (-Not$?)
168+
if (-Not $? -And -Not $templateInProgress.Contains($fd.datacenter))
165169
{
170+
$templateInProgress += $fd.datacenter
166171
$vmhost = Get-Random -InputObject (Get-VMHost -Location (Get-Cluster $fd.cluster))
167-
$ovfConfig = Get-OvfConfiguration -Ovf "template-$( $Version ).ova"
172+
$ovfConfig = Get-OvfConfiguration -Ovf "template-$($Version).ova"
168173
$ovfConfig.NetworkMapping.VM_Network.Value = $fd.network
169-
$template = Import-Vapp -Source "template-$( $Version ).ova" -Name $vm_template -OvfConfiguration $ovfConfig -VMHost $vmhost -Datastore $datastoreInfo -InventoryLocation $folder -Force:$true
170-
171-
$templateVIObj = Get-View -VIObject $template.Name
172-
# Need to look into upgrading hardware. For me it keeps throwing exception.
173-
<# try {
174+
Write-Output "OVF: $($ovfConfig)"
175+
$jobs += Start-ThreadJob -n "upload-template-$($fd.cluster)" -ScriptBlock {
176+
param($Version,$vm_template,$ovfConfig,$vmhost,$datastoreInfo,$folder,$tag)
177+
. .\variables.ps1
178+
. .\upi-functions.ps1
179+
Write-Output "Version: $($Version)"
180+
Write-Output "VM Template: $($vm_template)"
181+
Write-Output "OVF Config: $($ovfConfig)"
182+
Write-Output "VM Host: $($vmhost)"
183+
Connect-VIServer -Server $vcenter -Credential (Import-Clixml $vcentercredpath)
184+
$template = Import-Vapp -Source "template-$($Version).ova" -Name $vm_template -OvfConfiguration $ovfConfig -VMHost $vmhost -Datastore $datastoreInfo -InventoryLocation $folder -Force:$true
185+
186+
$templateVIObj = Get-View -VIObject $template.Name
187+
# Need to look into upgrading hardware. For me it keeps throwing exception.
188+
<# try {
174189
$templateVIObj.UpgradeVM($hardwareVersion)
175190
}
176191
catch {
177192
Write-Output "Something happened setting VM hardware version"
178193
Write-Output $_
179194
} #>
180195

181-
New-TagAssignment -Entity $template -Tag $tag
182-
Set-VM -VM $template -MemoryGB 16 -NumCpu 4 -CoresPerSocket 4 -Confirm:$false > $null
183-
Get-HardDisk -VM $template | Select-Object -First 1 | Set-HardDisk -CapacityGB 120 -Confirm:$false > $null
184-
New-AdvancedSetting -Entity $template -name "disk.EnableUUID" -value 'TRUE' -confirm:$false -Force > $null
185-
New-AdvancedSetting -Entity $template -name "guestinfo.ignition.config.data.encoding" -value "base64" -confirm:$false -Force > $null
186-
#$snapshot = New-Snapshot -VM $template -Name "linked-clone" -Description "linked-clone" -Memory -Quiesce
196+
New-TagAssignment -Entity $template -Tag $tag
197+
Set-VM -VM $template -MemoryGB 16 -NumCpu 4 -CoresPerSocket 4 -Confirm:$false > $null
198+
Get-HardDisk -VM $template | Select-Object -First 1 | Set-HardDisk -CapacityGB 120 -Confirm:$false > $null
199+
New-AdvancedSetting -Entity $template -name "disk.EnableUUID" -value 'TRUE' -confirm:$false -Force > $null
200+
New-AdvancedSetting -Entity $template -name "guestinfo.ignition.config.data.encoding" -value "base64" -confirm:$false -Force > $null
201+
#$snapshot = New-Snapshot -VM $template -Name "linked-clone" -Description "linked-clone" -Memory -Quiesce
202+
} -ArgumentList @($Version,$vm_template,$ovfConfig,$vmhost,$datastoreInfo,$folder,$tag)
203+
}
204+
}
205+
206+
# If jobs were started, lets wait till they are done
207+
if ($jobs.count -gt 0)
208+
{
209+
Wait-Job -Job $jobs
210+
foreach ($job in $jobs) {
211+
Receive-Job -Job $job
187212
}
188213
}
189214

@@ -210,70 +235,83 @@ $vmHash = ConvertFrom-Json -InputObject $virtualmachines -AsHashtable
210235

211236
Write-Progress -id 222 -Activity "Creating virtual machines" -PercentComplete 0
212237

238+
$jobs = @()
213239
$vmStep = (100 / $vmHash.virtualmachines.Count)
214240
$vmCount = 1
215241
foreach ($key in $vmHash.virtualmachines.Keys) {
216242
$node = $vmHash.virtualmachines[$key]
217243

218-
$name = "$($metadata.infraID)-$($key)"
219-
Write-Output "Creating $($name)"
220-
221-
$rp = Get-Cluster -Name $node.cluster -Server $node.server
222-
##$datastore = Get-Datastore -Name $node.datastore -Server $node.server
223-
$datastoreInfo = Get-Datastore -Name $node.datastore -Location $node.datacenter
224-
225-
# Pull network config for each node
226-
if ($node.type -eq "master") {
227-
$numCPU = $control_plane_num_cpus
228-
$memory = $control_plane_memory
229-
} elseif ($node.type -eq "worker") {
230-
$numCPU = $compute_num_cpus
231-
$memory = $compute_memory
232-
} else {
233-
# should only be bootstrap
234-
$numCPU = $control_plane_num_cpus
235-
$memory = $control_plane_memory
236-
}
237-
$ip = $node.ip
238-
$network = New-VMNetworkConfig -Hostname $name -IPAddress $ip -Netmask $netmask -Gateway $gateway -DNS $dns
239-
240-
# Get the content of the ignition file per machine type (bootstrap, master, worker)
241-
$bytes = Get-Content -Path "./$($node.type).ign" -AsByteStream
242-
$ignition = [Convert]::ToBase64String($bytes)
243-
244-
# Get correct template / folder
245-
$folder = Get-Folder -Name $metadata.infraID -Location $node.datacenter
246-
$template = Get-VM -Name $vm_template -Location $($node.datacenter)
247-
248-
# Clone the virtual machine from the imported template
249-
#$vm = New-OpenShiftVM -Template $template -Name $name -ResourcePool $rp -Datastore $datastoreInfo -Location $folder -LinkedClone -ReferenceSnapshot $snapshot -IgnitionData $ignition -Tag $tag -Networking $network -NumCPU $numCPU -MemoryMB $memory
250-
$vm = New-OpenShiftVM -Template $template -Name $name -ResourcePool $rp -Datastore $datastoreInfo -Location $folder -IgnitionData $ignition -Tag $tag -Networking $network -Network $node.network -NumCPU $numCPU -MemoryMB $memory
251-
252-
# Assign tag so we can later clean up
253-
# New-TagAssignment -Entity $vm -Tag $tag
254-
# New-AdvancedSetting -Entity $vm -name "guestinfo.ignition.config.data" -value $ignition -confirm:$false -Force > $null
255-
# New-AdvancedSetting -Entity $vm -name "guestinfo.hostname" -value $name -Confirm:$false -Force > $null
256-
257-
if ($node.type -eq "master" -And $delayVMStart) {
258-
# To give bootstrap some time to start, lets wait 2 minutes
259-
Start-ThreadJob -ThrottleLimit 5 -InputObject $vm {
260-
Start-Sleep -Seconds 90
261-
$input | Start-VM
244+
$jobs += Start-ThreadJob -n "create-vm-$($metadata.infraID)-$($key)" -ScriptBlock {
245+
param($key,$node,$vm_template,$metadata,$tag)
246+
. .\variables.ps1
247+
. .\upi-functions.ps1
248+
Connect-VIServer -Server $vcenter -Credential (Import-Clixml $vcentercredpath)
249+
250+
$name = "$($metadata.infraID)-$($key)"
251+
Write-Output "Creating $($name)"
252+
253+
$rp = Get-Cluster -Name $node.cluster -Server $node.server
254+
##$datastore = Get-Datastore -Name $node.datastore -Server $node.server
255+
$datastoreInfo = Get-Datastore -Name $node.datastore -Location $node.datacenter
256+
257+
# Pull network config for each node
258+
if ($node.type -eq "master") {
259+
$numCPU = $control_plane_num_cpus
260+
$memory = $control_plane_memory
261+
} elseif ($node.type -eq "worker") {
262+
$numCPU = $compute_num_cpus
263+
$memory = $compute_memory
264+
} else {
265+
# should only be bootstrap
266+
$numCPU = $control_plane_num_cpus
267+
$memory = $control_plane_memory
262268
}
263-
} elseif ($node.type -eq "worker" -And $delayVMStart) {
264-
# Workers are not needed right away, gotta wait till masters
265-
# have started machine-server. wait 7 minutes to start.
266-
Start-ThreadJob -ThrottleLimit 5 -InputObject $vm {
267-
Start-Sleep -Seconds 600
268-
$input | Start-VM
269+
$ip = $node.ip
270+
$network = New-VMNetworkConfig -Hostname $name -IPAddress $ip -Netmask $netmask -Gateway $gateway -DNS $dns
271+
272+
# Get the content of the ignition file per machine type (bootstrap, master, worker)
273+
$bytes = Get-Content -Path "./$($node.type).ign" -AsByteStream
274+
$ignition = [Convert]::ToBase64String($bytes)
275+
276+
# Get correct template / folder
277+
$folder = Get-Folder -Name $metadata.infraID -Location $node.datacenter
278+
$template = Get-VM -Name $vm_template -Location $($node.datacenter)
279+
280+
# Clone the virtual machine from the imported template
281+
#$vm = New-OpenShiftVM -Template $template -Name $name -ResourcePool $rp -Datastore $datastoreInfo -Location $folder -LinkedClone -ReferenceSnapshot $snapshot -IgnitionData $ignition -Tag $tag -Networking $network -NumCPU $numCPU -MemoryMB $memory
282+
$vm = New-OpenShiftVM -Template $template -Name $name -ResourcePool $rp -Datastore $datastoreInfo -Location $folder -IgnitionData $ignition -Tag $tag -Networking $network -Network $node.network -NumCPU $numCPU -MemoryMB $memory
283+
284+
# Assign tag so we can later clean up
285+
# New-TagAssignment -Entity $vm -Tag $tag
286+
# New-AdvancedSetting -Entity $vm -name "guestinfo.ignition.config.data" -value $ignition -confirm:$false -Force > $null
287+
# New-AdvancedSetting -Entity $vm -name "guestinfo.hostname" -value $name -Confirm:$false -Force > $null
288+
289+
if ($node.type -eq "master" -And $delayVMStart) {
290+
# To give bootstrap some time to start, lets wait 2 minutes
291+
Start-ThreadJob -ThrottleLimit 5 -InputObject $vm {
292+
Start-Sleep -Seconds 90
293+
$input | Start-VM
294+
}
295+
} elseif ($node.type -eq "worker" -And $delayVMStart) {
296+
# Workers are not needed right away, gotta wait till masters
297+
# have started machine-server. wait 7 minutes to start.
298+
Start-ThreadJob -ThrottleLimit 5 -InputObject $vm {
299+
Start-Sleep -Seconds 600
300+
$input | Start-VM
301+
}
269302
}
270-
}
271-
else {
272-
$vm | Start-VM
273-
}
303+
else {
304+
$vm | Start-VM
305+
}
306+
} -ArgumentList @($key,$node,$vm_template,$metadata,$tag)
274307
Write-Progress -id 222 -Activity "Creating virtual machines" -PercentComplete ($vmStep * $vmCount)
275308
$vmCount++
276309
}
310+
Wait-Job -Job $jobs
311+
foreach ($job in $jobs) {
312+
Receive-Job -Job $job
313+
}
314+
277315
Write-Progress -id 222 -Activity "Completed virtual machines" -PercentComplete 100 -Completed
278316

279317
## This is nice to have to clear screen when doing things manually. Maybe i'll
@@ -537,4 +575,4 @@ if ($waitForComplete)
537575
Get-Job | Remove-Job
538576

539577

540-
Write-Output "Install Complete!"
578+
Write-Output "Install Complete!"

0 commit comments

Comments
 (0)