@@ -198,4 +198,93 @@ function New-VMNetworkConfig {
198198}
199199"@
200200 return ConvertFrom-Json - InputObject $network
201+ }
202+
203+ function New-OpenshiftVMs {
204+ param (
205+ $NodeType
206+ )
207+
208+ Write-Output " Creating $ ( $NodeType ) VMs"
209+
210+ $jobs = @ ()
211+ $vmStep = (100 / $vmHash.virtualmachines.Count )
212+ $vmCount = 1
213+ foreach ($key in $vmHash.virtualmachines.Keys ) {
214+ $node = $vmHash.virtualmachines [$key ]
215+
216+ if ($NodeType -ne $node.type ) {
217+ continue
218+ }
219+
220+ $jobs += Start-ThreadJob - n " create-vm-$ ( $metadata.infraID ) -$ ( $key ) " - ScriptBlock {
221+ param ($key , $node , $vm_template , $metadata , $tag )
222+ . .\variables.ps1
223+ . .\upi- functions.ps1
224+ Connect-VIServer - Server $vcenter - Credential (Import-Clixml $vcentercredpath )
225+
226+ $name = " $ ( $metadata.infraID ) -$ ( $key ) "
227+ Write-Output " Creating $ ( $name ) "
228+
229+ $rp = Get-ResourcePool - Name $ ($metadata.infraID ) - Location $ (Get-Cluster - Name $ ($node.cluster )) - Server $vcenter
230+ # #$datastore = Get-Datastore -Name $node.datastore -Server $node.server
231+ $datastoreInfo = Get-Datastore - Name $node.datastore - Location $node.datacenter
232+
233+ # Pull network config for each node
234+ if ($node.type -eq " master" ) {
235+ $numCPU = $control_plane_num_cpus
236+ $memory = $control_plane_memory
237+ } elseif ($node.type -eq " worker" ) {
238+ $numCPU = $compute_num_cpus
239+ $memory = $compute_memory
240+ } else {
241+ # should only be bootstrap
242+ $numCPU = $control_plane_num_cpus
243+ $memory = $control_plane_memory
244+ }
245+ $ip = $node.ip
246+ $network = New-VMNetworkConfig - Hostname $name - IPAddress $ip - Netmask $netmask - Gateway $gateway - DNS $dns
247+
248+ # Get the content of the ignition file per machine type (bootstrap, master, worker)
249+ $bytes = Get-Content - Path " ./$ ( $node.type ) .ign" - AsByteStream
250+ $ignition = [Convert ]::ToBase64String($bytes )
251+
252+ # Get correct template / folder
253+ $folder = Get-Folder - Name $metadata.infraID - Location $node.datacenter
254+ $template = Get-VM - Name $vm_template - Location $ ($node.datacenter )
255+
256+ # Clone the virtual machine from the imported template
257+ # $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
258+ $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
259+
260+ # Assign tag so we can later clean up
261+ # New-TagAssignment -Entity $vm -Tag $tag
262+ # New-AdvancedSetting -Entity $vm -name "guestinfo.ignition.config.data" -value $ignition -confirm:$false -Force > $null
263+ # New-AdvancedSetting -Entity $vm -name "guestinfo.hostname" -value $name -Confirm:$false -Force > $null
264+
265+ if ($node.type -eq " master" -And $delayVMStart ) {
266+ # To give bootstrap some time to start, lets wait 2 minutes
267+ Start-ThreadJob - ThrottleLimit 5 - InputObject $vm {
268+ Start-Sleep - Seconds 90
269+ $input | Start-VM
270+ }
271+ } elseif ($node.type -eq " worker" -And $delayVMStart ) {
272+ # Workers are not needed right away, gotta wait till masters
273+ # have started machine-server. wait 7 minutes to start.
274+ Start-ThreadJob - ThrottleLimit 5 - InputObject $vm {
275+ Start-Sleep - Seconds 600
276+ $input | Start-VM
277+ }
278+ }
279+ else {
280+ $vm | Start-VM
281+ }
282+ } - ArgumentList @ ($key , $node , $vm_template , $metadata , $tag )
283+ Write-Progress - id 222 - Activity " Creating virtual machines" - PercentComplete ($vmStep * $vmCount )
284+ $vmCount ++
285+ }
286+ Wait-Job - Job $jobs
287+ foreach ($job in $jobs ) {
288+ Receive-Job - Job $job
289+ }
201290}
0 commit comments