Add fixes for Linux environment and add Windows environment
This commit is contained in:
@@ -7,10 +7,9 @@ $ServerTemplateName Template VM used for server clones.
|
||||
$GuiTemplateName Template VM used for the workstation clone.
|
||||
|
||||
This script expects these port groups to already exist:
|
||||
WAN-Linux
|
||||
INT-Linux
|
||||
DMZ-Linux
|
||||
VPN-Linux
|
||||
PG-VLAN10 (WAN)
|
||||
PG-VLAN11 (INT)
|
||||
PG-VLAN12 (DMZ)
|
||||
|
||||
What the script does:
|
||||
- Creates the WS2024 Linux topology VMs as linked clones
|
||||
@@ -29,9 +28,11 @@ param()
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
$TemplateFolderName = 'Templates'
|
||||
$TargetParentFolderName = 'Worldskills 2024'
|
||||
$TargetFolderName = 'Linux'
|
||||
$DatacenterName = 'Ilica'
|
||||
$TemplateFolderName = 'Templates'
|
||||
$SkillsFolderName = 'Skills'
|
||||
$TargetParentFolderName = 'WS2024'
|
||||
$TargetFolderName = 'Linux'
|
||||
$ServerTemplateName = 'debian-13-template'
|
||||
$GuiTemplateName = 'debian-13-gui-template'
|
||||
$ReferenceSnapshotName = 'Start'
|
||||
@@ -47,10 +48,14 @@ $CloudInitShutdownTimeoutSeconds = 900
|
||||
function Get-SingleFolder {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Name
|
||||
[string]$Name,
|
||||
|
||||
$Location = $null
|
||||
)
|
||||
|
||||
$folders = @(Get-Folder -Name $Name -ErrorAction SilentlyContinue)
|
||||
$params = @{ Name = $Name; Type = 'VM'; ErrorAction = 'SilentlyContinue' }
|
||||
if ($Location) { $params['Location'] = $Location }
|
||||
$folders = @(Get-Folder @params)
|
||||
|
||||
if ($folders.Count -eq 0) {
|
||||
throw "Folder '$Name' was not found."
|
||||
@@ -72,7 +77,7 @@ function Get-SingleChildFolder {
|
||||
$ParentFolder
|
||||
)
|
||||
|
||||
$folders = @(Get-Folder -Location $ParentFolder -Name $Name -ErrorAction SilentlyContinue)
|
||||
$folders = @(Get-Folder -Location $ParentFolder -Name $Name -Type VM -ErrorAction SilentlyContinue)
|
||||
|
||||
if ($folders.Count -eq 0) {
|
||||
throw "Folder '$Name' was not found under '$($ParentFolder.Name)'."
|
||||
@@ -205,7 +210,9 @@ function New-CloudInitUserData {
|
||||
[string]$Username,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Password
|
||||
[string]$Password,
|
||||
|
||||
[string]$NetworkConfig = ''
|
||||
)
|
||||
|
||||
return @"
|
||||
@@ -242,14 +249,49 @@ function Set-CloudInitUserData {
|
||||
[string]$Username,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$Password
|
||||
[string]$Password,
|
||||
|
||||
[string]$NetworkConfig = ''
|
||||
)
|
||||
|
||||
$userData = New-CloudInitUserData -VmName $VM.Name -Username $Username -Password $Password
|
||||
$encodedUserData = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($userData))
|
||||
|
||||
Set-AdvancedSettingValue -Entity $VM -Name 'guestinfo.userdata' -Value $encodedUserData
|
||||
Set-AdvancedSettingValue -Entity $VM -Name 'guestinfo.userdata.encoding' -Value 'base64'
|
||||
|
||||
if ($NetworkConfig) {
|
||||
$metadata = @"
|
||||
instance-id: $($VM.Name)
|
||||
local-hostname: $($VM.Name)
|
||||
$NetworkConfig
|
||||
"@
|
||||
$encodedMetadata = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($metadata))
|
||||
Set-AdvancedSettingValue -Entity $VM -Name 'guestinfo.metadata' -Value $encodedMetadata
|
||||
Set-AdvancedSettingValue -Entity $VM -Name 'guestinfo.metadata.encoding' -Value 'base64'
|
||||
}
|
||||
}
|
||||
|
||||
function New-NetConfig {
|
||||
param([hashtable[]]$Nics)
|
||||
|
||||
$yaml = "network:`n version: 2`n ethernets:"
|
||||
|
||||
foreach ($nic in $Nics) {
|
||||
$addrs = @($nic.Ipv4, $nic.Ipv6) | Where-Object { $_ } | ForEach-Object { "`"$_`"" }
|
||||
$yaml += "`n $($nic.Name):"
|
||||
$yaml += "`n addresses: [$($addrs -join ', ')]"
|
||||
|
||||
$routes = @()
|
||||
if ($nic.Gw4) { $routes += "- to: default`n via: $($nic.Gw4)" }
|
||||
if ($nic.Gw6) { $routes += "- to: `"::/0`"`n via: `"$($nic.Gw6)`"" }
|
||||
if ($routes) {
|
||||
$yaml += "`n routes:"
|
||||
$routes | ForEach-Object { $yaml += "`n $_" }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $yaml
|
||||
}
|
||||
|
||||
function Wait-ForVmToPowerOff {
|
||||
@@ -276,9 +318,11 @@ function Wait-ForVmToPowerOff {
|
||||
throw "VM '$($VM.Name)' did not power off within $TimeoutSeconds seconds."
|
||||
}
|
||||
|
||||
$templateFolder = Get-SingleFolder -Name $TemplateFolderName
|
||||
$targetParentFolder = Get-SingleFolder -Name $TargetParentFolderName
|
||||
$targetFolder = Get-SingleChildFolder -Name $TargetFolderName -ParentFolder $targetParentFolder
|
||||
$datacenter = Get-Datacenter -Name $DatacenterName -ErrorAction Stop
|
||||
$skillsFolder = Get-SingleFolder -Name $SkillsFolderName -Location $datacenter
|
||||
$templateFolder = Get-SingleChildFolder -Name $TemplateFolderName -ParentFolder $skillsFolder
|
||||
$targetParentFolder = Get-SingleChildFolder -Name $TargetParentFolderName -ParentFolder $skillsFolder
|
||||
$targetFolder = Get-SingleChildFolder -Name $TargetFolderName -ParentFolder $targetParentFolder
|
||||
|
||||
$serverTemplate = Get-SingleVmFromFolder -Name $ServerTemplateName -Folder $templateFolder
|
||||
$guiTemplate = Get-SingleVmFromFolder -Name $GuiTemplateName -Folder $templateFolder
|
||||
@@ -286,15 +330,21 @@ $guiTemplate = Get-SingleVmFromFolder -Name $GuiTemplateName -Folder $templateFo
|
||||
$serverReferenceSnapshot = Get-OrCreateReferenceSnapshot -VM $serverTemplate -SnapshotName $ReferenceSnapshotName
|
||||
$guiReferenceSnapshot = Get-OrCreateReferenceSnapshot -VM $guiTemplate -SnapshotName $ReferenceSnapshotName
|
||||
|
||||
# ── Network segment variables ─────────────────────────────────────────────────
|
||||
$WanGw4 = '1.1.1.10'; $WanGw6 = '2001:db8:1111::10'
|
||||
$IntGw4 = '10.1.10.1'; $IntGw6 = '2001:db8:1001:10::1'
|
||||
$DmzGw4 = '10.1.20.1'; $DmzGw6 = '2001:db8:1001:20::1'
|
||||
|
||||
# ── VM definitions ────────────────────────────────────────────────────────────
|
||||
$vmDefinitions = @(
|
||||
@{ Name = 'fw'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('WAN-Linux', 'INT-Linux', 'DMZ-Linux', 'VPN-Linux') },
|
||||
@{ Name = 'int-srv01'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('INT-Linux') },
|
||||
@{ Name = 'mail'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('DMZ-Linux') },
|
||||
@{ Name = 'ha-prx01'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('DMZ-Linux') },
|
||||
@{ Name = 'ha-prx02'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('DMZ-Linux') },
|
||||
@{ Name = 'web01'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('DMZ-Linux') },
|
||||
@{ Name = 'web02'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('DMZ-Linux') },
|
||||
@{ Name = 'jamie-ws01'; Template = $guiTemplate; Snapshot = $guiReferenceSnapshot; Networks = @('WAN-Linux', 'VPN-Linux') }
|
||||
@{ Name = 'fw'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('PG-VLAN10', 'PG-VLAN11', 'PG-VLAN12') },
|
||||
@{ Name = 'int-srv01'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('PG-VLAN11') },
|
||||
@{ Name = 'mail'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('PG-VLAN12') },
|
||||
@{ Name = 'ha-prx01'; Template = $guiTemplate; Snapshot = $guiReferenceSnapshot; Networks = @('PG-VLAN12') },
|
||||
@{ Name = 'ha-prx02'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('PG-VLAN12') },
|
||||
@{ Name = 'web01'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('PG-VLAN12') },
|
||||
@{ Name = 'web02'; Template = $serverTemplate; Snapshot = $serverReferenceSnapshot; Networks = @('PG-VLAN12') },
|
||||
@{ Name = 'jamie-ws01'; Template = $guiTemplate; Snapshot = $guiReferenceSnapshot; Networks = @('PG-VLAN10') }
|
||||
)
|
||||
|
||||
foreach ($definition in $vmDefinitions) {
|
||||
@@ -323,7 +373,12 @@ foreach ($definition in $vmDefinitions) {
|
||||
|
||||
Ensure-ExtraDisks -VM $vm -DiskCount $ExtraDiskCount -DiskSizeGB $ExtraDiskSizeGB
|
||||
Set-ExactNetworkAdapters -VM $vm -PortGroupNames $definition.Networks
|
||||
Set-CloudInitUserData -VM $vm -Username $DefaultUsername -Password $DefaultPassword
|
||||
Set-CloudInitUserData -VM $vm -Username $DefaultUsername -Password $DefaultPassword -NetworkConfig $definition.NetworkConfig
|
||||
$templateCdroms = @(Get-CDDrive -VM $definition.Template | Sort-Object Name)
|
||||
$vmCdroms = @(Get-CDDrive -VM $vm | Sort-Object Name)
|
||||
for ($i = 0; $i -lt $templateCdroms.Count; $i++) {
|
||||
$vmCdroms[$i] | Set-CDDrive -IsoPath $templateCdroms[$i].IsoPath -StartConnected $true -Confirm:$false | Out-Null
|
||||
}
|
||||
|
||||
$startSnapshot = Get-Snapshot -VM $vm -Name $StartSnapshotName -ErrorAction SilentlyContinue
|
||||
|
||||
|
||||
Reference in New Issue
Block a user