From 5cf5a4d22c113563b83b105ccaa7450f9b31fbe4 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 07:18:10 +0100 Subject: [PATCH 01/13] Update VHD paths in ArcServersLogonScript and DSC YAML files to use naming prefix Signed-off-by: Jan Egil Ring --- .../artifacts/ArcServersLogonScript.ps1 | 8 ++++---- .../artifacts/dsc/virtual_machines_itpro.dsc.yml | 8 ++++---- .../artifacts/dsc/virtual_machines_sql.dsc.yml | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index a909b4f191..067433b2ea 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -407,16 +407,16 @@ $payLoad = @" Write-Header "Fetching Nested VMs" $Win2k19vmName = "$namingPrefix-Win2K19" - $win2k19vmvhdPath = "${Env:ArcBoxVMDir}\ArcBox-Win2K19.vhdx" + $win2k19vmvhdPath = "${Env:ArcBoxVMDir}\$namingPrefix-Win2K19.vhdx" $Win2k22vmName = "$namingPrefix-Win2K22" - $Win2k22vmvhdPath = "${Env:ArcBoxVMDir}\ArcBox-Win2K22.vhdx" + $Win2k22vmvhdPath = "${Env:ArcBoxVMDir}\$namingPrefix-Win2K22.vhdx" $Ubuntu01vmName = "$namingPrefix-Ubuntu-01" - $Ubuntu01vmvhdPath = "${Env:ArcBoxVMDir}\ArcBox-Ubuntu-01.vhdx" + $Ubuntu01vmvhdPath = "${Env:ArcBoxVMDir}\$namingPrefix-Ubuntu-01.vhdx" $Ubuntu02vmName = "$namingPrefix-Ubuntu-02" - $Ubuntu02vmvhdPath = "${Env:ArcBoxVMDir}\ArcBox-Ubuntu-02.vhdx" + $Ubuntu02vmvhdPath = "${Env:ArcBoxVMDir}\$namingPrefix-Ubuntu-02.vhdx" # Verify if VHD files already downloaded especially when re-running this script if (!((Test-Path $win2k19vmvhdPath) -and (Test-Path $Win2k22vmvhdPath) -and (Test-Path $Ubuntu01vmvhdPath) -and (Test-Path $Ubuntu02vmvhdPath))) { diff --git a/azure_jumpstart_arcbox/artifacts/dsc/virtual_machines_itpro.dsc.yml b/azure_jumpstart_arcbox/artifacts/dsc/virtual_machines_itpro.dsc.yml index df8fe96f15..4f597a56ee 100644 --- a/azure_jumpstart_arcbox/artifacts/dsc/virtual_machines_itpro.dsc.yml +++ b/azure_jumpstart_arcbox/artifacts/dsc/virtual_machines_itpro.dsc.yml @@ -9,7 +9,7 @@ properties: settings: Name: namingPrefixStage-Win2K19 SwitchName: 'InternalNATSwitch' - VhdPath: F:\Virtual Machines\ArcBox-Win2K19.vhdx + VhdPath: F:\Virtual Machines\namingPrefixStage-Win2K19.vhdx ProcessorCount: 2 StartupMemory: '4GB' RestartIfNeeded: true @@ -25,7 +25,7 @@ properties: settings: Name: namingPrefixStage-Win2K22 SwitchName: 'InternalNATSwitch' - VhdPath: F:\Virtual Machines\ArcBox-Win2K22.vhdx + VhdPath: F:\Virtual Machines\namingPrefixStage-Win2K22.vhdx ProcessorCount: 2 StartupMemory: '4GB' RestartIfNeeded: true @@ -41,7 +41,7 @@ properties: settings: Name: namingPrefixStage-Ubuntu-01 SwitchName: 'InternalNATSwitch' - VhdPath: F:\Virtual Machines\ArcBox-Ubuntu-01.vhdx + VhdPath: F:\Virtual Machines\namingPrefixStage-Ubuntu-01.vhdx ProcessorCount: 2 StartupMemory: '4GB' RestartIfNeeded: true @@ -57,7 +57,7 @@ properties: settings: Name: namingPrefixStage-Ubuntu-02 SwitchName: 'InternalNATSwitch' - VhdPath: F:\Virtual Machines\ArcBox-Ubuntu-02.vhdx + VhdPath: F:\Virtual Machines\namingPrefixStage-Ubuntu-02.vhdx ProcessorCount: 2 StartupMemory: '4GB' RestartIfNeeded: true diff --git a/azure_jumpstart_arcbox/artifacts/dsc/virtual_machines_sql.dsc.yml b/azure_jumpstart_arcbox/artifacts/dsc/virtual_machines_sql.dsc.yml index f385a8ca0f..418a037687 100644 --- a/azure_jumpstart_arcbox/artifacts/dsc/virtual_machines_sql.dsc.yml +++ b/azure_jumpstart_arcbox/artifacts/dsc/virtual_machines_sql.dsc.yml @@ -8,7 +8,7 @@ properties: settings: Name: namingPrefixStage-SQL SwitchName: 'InternalNATSwitch' - VhdPath: F:\Virtual Machines\ArcBox-SQL.vhdx + VhdPath: F:\Virtual Machines\namingPrefixStage-SQL.vhdx ProcessorCount: 2 StartupMemory: '6GB' RestartIfNeeded: true From 5e1e5d9b4a1543f7ca16c5e69370535bd8d48ba2 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 08:07:59 +0100 Subject: [PATCH 02/13] Refactor VHD file handling in ArcServersLogonScript to use variable for file patterns and add renaming logic based on naming prefix Signed-off-by: Jan Egil Ring --- .../artifacts/ArcServersLogonScript.ps1 | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 067433b2ea..8890b16c68 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -418,12 +418,37 @@ $payLoad = @" $Ubuntu02vmName = "$namingPrefix-Ubuntu-02" $Ubuntu02vmvhdPath = "${Env:ArcBoxVMDir}\$namingPrefix-Ubuntu-02.vhdx" + $files = "ArcBox-Win2K19.vhdx;ArcBox-Win2K22.vhdx;ArcBox-Ubuntu-01.vhdx;ArcBox-Ubuntu-02.vhdx;" + # Verify if VHD files already downloaded especially when re-running this script if (!((Test-Path $win2k19vmvhdPath) -and (Test-Path $Win2k22vmvhdPath) -and (Test-Path $Ubuntu01vmvhdPath) -and (Test-Path $Ubuntu02vmvhdPath))) { <# Action when all if and elseif conditions are false #> $Env:AZCOPY_BUFFER_GB = 4 Write-Output "Downloading nested VMs VHDX files. This can take some time, hold tight..." - azcopy cp $vhdSourceFolder $Env:ArcBoxVMDir --include-pattern "ArcBox-Win2K19.vhdx;ArcBox-Win2K22.vhdx;ArcBox-Ubuntu-01.vhdx;ArcBox-Ubuntu-02.vhdx;" --recursive=true --check-length=false --log-level=ERROR + azcopy cp $vhdSourceFolder $Env:ArcBoxVMDir --include-pattern $files --recursive=true --check-length=false --log-level=ERROR + } + + if ($namingPrefix -ne "ArcBox") { + + # Split the string into an array + $fileList = $files -split ';' | Where-Object { $_ -ne '' } + + # Set the path to search for files + $searchPath = $Env:ArcBoxVMDir + + # Loop through each file and rename if found + foreach ($file in $fileList) { + $filePath = Join-Path -Path $searchPath -ChildPath $file + if (Test-Path $filePath) { + $newFileName = $file -replace "ArcBox", $namingPrefix + + Rename-Item -Path $filePath -NewName $newFileName + Write-Output "Renamed $file to $newFileName" + } + else { + Write-Output "$file not found in $searchPath" + } + } } # Create the nested VMs if not already created From d5dbf225c89371dc868695cd82fca7b99cd893c5 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 08:43:12 +0100 Subject: [PATCH 03/13] Update ArcServersLogonScript to remove unnecessary reboot commands and force restart VMs after hostname changes Signed-off-by: Jan Egil Ring --- .../artifacts/ArcServersLogonScript.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 8890b16c68..c58ece718d 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -512,19 +512,19 @@ $payLoad = @" Invoke-Command -HostName $Ubuntu01VmIp -KeyFilePath "$Env:USERPROFILE\.ssh\id_rsa" -UserName $nestedLinuxUsername -ScriptBlock { - Invoke-Expression "sudo hostnamectl set-hostname $using:ubuntu01vmName;sudo systemctl reboot" + Invoke-Expression "sudo hostnamectl set-hostname $using:ubuntu01vmName" } - Restart-VM -Name $ubuntu01vmName + Restart-VM -Name $ubuntu01vmName -Force Invoke-Command -HostName $Ubuntu02VmIp -KeyFilePath "$Env:USERPROFILE\.ssh\id_rsa" -UserName $nestedLinuxUsername -ScriptBlock { - Invoke-Expression "sudo hostnamectl set-hostname $using:ubuntu02vmName;sudo systemctl reboot" + Invoke-Expression "sudo hostnamectl set-hostname $using:ubuntu02vmName" } - Restart-VM -Name $ubuntu02vmName + Restart-VM -Name $ubuntu02vmName -Force } From eb6c11f47d518e0b922acaba48dcecef14ecbb73 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 10:45:56 +0100 Subject: [PATCH 04/13] Add sleep delay to avoid timing issues when copying authorized_keys to Linux VMs Signed-off-by: Jan Egil Ring --- azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index c58ece718d..e787e14d30 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -503,6 +503,9 @@ $payLoad = @" # Automatically accept unseen keys but will refuse connections for changed or invalid hostkeys. Add-Content -Path "$Env:USERPROFILE\.ssh\config" -Value "StrictHostKeyChecking=accept-new" + # Avoid timing issue with copying the authorized_keys file to the Linux VMs + Start-Sleep -Seconds 5 + Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$Env:TEMP\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath if ($namingPrefix -ne "ArcBox") { From 7dd92fb35b4fcb620c2767a4a31bcc1600b72c26 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 11:33:45 +0100 Subject: [PATCH 05/13] Add sleep delay to prevent timing issues when copying authorized_keys to Linux VMs Signed-off-by: Jan Egil Ring --- azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index e787e14d30..68ffd3ac1a 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -498,14 +498,14 @@ $payLoad = @" $null = New-Item -Path ~ -Name .ssh -ItemType Directory ssh-keygen -t rsa -N '' -f $Env:USERPROFILE\.ssh\id_rsa + # Avoid timing issue with copying the authorized_keys file to the Linux VMs + Start-Sleep -Seconds 5 + Copy-Item -Path "$Env:USERPROFILE\.ssh\id_rsa.pub" -Destination "$Env:TEMP\authorized_keys" # Automatically accept unseen keys but will refuse connections for changed or invalid hostkeys. Add-Content -Path "$Env:USERPROFILE\.ssh\config" -Value "StrictHostKeyChecking=accept-new" - # Avoid timing issue with copying the authorized_keys file to the Linux VMs - Start-Sleep -Seconds 5 - Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$Env:TEMP\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath if ($namingPrefix -ne "ArcBox") { From 7500fff2eac8480ef7f14ca2a7286aadfb16627b Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 14:20:24 +0100 Subject: [PATCH 06/13] Update ArcServersLogonScript to change authorized_keys file path to ArcBoxDir Signed-off-by: Jan Egil Ring --- azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 68ffd3ac1a..41e3b38c72 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -501,12 +501,12 @@ $payLoad = @" # Avoid timing issue with copying the authorized_keys file to the Linux VMs Start-Sleep -Seconds 5 - Copy-Item -Path "$Env:USERPROFILE\.ssh\id_rsa.pub" -Destination "$Env:TEMP\authorized_keys" + Copy-Item -Path "$Env:USERPROFILE\.ssh\id_rsa.pub" -Destination "$($Env:ArcBoxDir)\authorized_keys" # Automatically accept unseen keys but will refuse connections for changed or invalid hostkeys. Add-Content -Path "$Env:USERPROFILE\.ssh\config" -Value "StrictHostKeyChecking=accept-new" - Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$Env:TEMP\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath + Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath if ($namingPrefix -ne "ArcBox") { From ccc2bc9b326c3b3a8551480485b97350ca543f5f Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 15:45:04 +0100 Subject: [PATCH 07/13] Improve handling of authorized_keys file copying by implementing a wait loop until the SSH public key is available Signed-off-by: Jan Egil Ring --- .../artifacts/ArcServersLogonScript.ps1 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 41e3b38c72..77e1f32dda 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -498,8 +498,12 @@ $payLoad = @" $null = New-Item -Path ~ -Name .ssh -ItemType Directory ssh-keygen -t rsa -N '' -f $Env:USERPROFILE\.ssh\id_rsa - # Avoid timing issue with copying the authorized_keys file to the Linux VMs - Start-Sleep -Seconds 5 + # Avoid timing issue with copying the authorized_keys file + do { + Write-Output "Waiting for SSH public key to become available..." + $fileSize = (Get-Item "$Env:USERPROFILE\.ssh\id_rsa.pub").Length + Start-Sleep -Seconds 1 + } while ($fileSize -eq 0) Copy-Item -Path "$Env:USERPROFILE\.ssh\id_rsa.pub" -Destination "$($Env:ArcBoxDir)\authorized_keys" From e23df4d03cd2baff32f872b89ab7ee019868d2d9 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 16:26:35 +0100 Subject: [PATCH 08/13] Add sleep delay to wait for Linux VMs to come online before copying authorized_keys Signed-off-by: Jan Egil Ring --- azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 77e1f32dda..661af56d0b 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -510,6 +510,9 @@ $payLoad = @" # Automatically accept unseen keys but will refuse connections for changed or invalid hostkeys. Add-Content -Path "$Env:USERPROFILE\.ssh\config" -Value "StrictHostKeyChecking=accept-new" + # Waiting for Linux VMs to come online + Start-Sleep -Seconds 10 + Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath if ($namingPrefix -ne "ArcBox") { From bd97c42355ca000f738d74594721319004e15604 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 19:00:29 +0100 Subject: [PATCH 09/13] Refactor authorized_keys file copying to specify individual VM names Signed-off-by: Jan Egil Ring --- azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 661af56d0b..27cb3c5794 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -510,10 +510,8 @@ $payLoad = @" # Automatically accept unseen keys but will refuse connections for changed or invalid hostkeys. Add-Content -Path "$Env:USERPROFILE\.ssh\config" -Value "StrictHostKeyChecking=accept-new" - # Waiting for Linux VMs to come online - Start-Sleep -Seconds 10 - - Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath + Copy-VMFile -Name $Ubuntu01vmName -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath + Copy-VMFile -Name $Ubuntu02vmName -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath if ($namingPrefix -ne "ArcBox") { From 9f66e4c4e1f1d5ca381e79b323505d722b222666 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 20:18:32 +0100 Subject: [PATCH 10/13] Fix race condition in authorized_keys file copying by duplicating the copy command for Ubuntu VMs Signed-off-by: Jan Egil Ring --- azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 27cb3c5794..7f099ff8e5 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -510,8 +510,9 @@ $payLoad = @" # Automatically accept unseen keys but will refuse connections for changed or invalid hostkeys. Add-Content -Path "$Env:USERPROFILE\.ssh\config" -Value "StrictHostKeyChecking=accept-new" - Copy-VMFile -Name $Ubuntu01vmName -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath - Copy-VMFile -Name $Ubuntu02vmName -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath + # Running twice due to a race condition where the target file is sometimes empty + Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath + Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath if ($namingPrefix -ne "ArcBox") { From e1660fd605d25c0cdb87639af6b4893791252ef3 Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 21:08:13 +0100 Subject: [PATCH 11/13] Enhance ArcServersLogonScript to copy installation script to nested Windows VMs and streamline onboarding process for Azure Arc-enabled servers Signed-off-by: Jan Egil Ring --- .../artifacts/ArcServersLogonScript.ps1 | 42 +++++++------------ 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 7f099ff8e5..36c2f462cb 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -488,6 +488,15 @@ $payLoad = @" } + # Copy installation script to nested Windows VMs + Write-Output "Transferring installation script to nested Windows VMs..." + Copy-VMFile $Win2k19vmName -SourcePath "$agentScript\installArcAgent.ps1" -DestinationPath "$Env:ArcBoxDir\installArcAgent.ps1" -CreateFullPath -FileSource Host -Force + Copy-VMFile $Win2k22vmName -SourcePath "$agentScript\installArcAgent.ps1" -DestinationPath "$Env:ArcBoxDir\installArcAgent.ps1" -CreateFullPath -FileSource Host -Force + + # Onboarding the nested VMs as Azure Arc-enabled servers + Write-Output "Onboarding the nested Windows VMs as Azure Arc-enabled servers" + Invoke-Command -VMName $Win2k19vmName,$Win2k22vmName -ScriptBlock { powershell -File $Using:nestedVMArcBoxDir\installArcAgent.ps1 -accessToken $using:accessToken, -tenantId $Using:tenantId, -subscriptionId $Using:subscriptionId, -resourceGroup $Using:resourceGroup, -azureLocation $Using:azureLocation } -Credential $winCreds + # Getting the Ubuntu nested VM IP address $Ubuntu01VmIp = Get-VM -Name $Ubuntu01vmName | Select-Object -ExpandProperty NetworkAdapters | Select-Object -ExpandProperty IPAddresses | Select-Object -Index 0 $Ubuntu02VmIp = Get-VM -Name $Ubuntu02vmName | Select-Object -ExpandProperty NetworkAdapters | Select-Object -ExpandProperty IPAddresses | Select-Object -Index 0 @@ -498,13 +507,6 @@ $payLoad = @" $null = New-Item -Path ~ -Name .ssh -ItemType Directory ssh-keygen -t rsa -N '' -f $Env:USERPROFILE\.ssh\id_rsa - # Avoid timing issue with copying the authorized_keys file - do { - Write-Output "Waiting for SSH public key to become available..." - $fileSize = (Get-Item "$Env:USERPROFILE\.ssh\id_rsa.pub").Length - Start-Sleep -Seconds 1 - } while ($fileSize -eq 0) - Copy-Item -Path "$Env:USERPROFILE\.ssh\id_rsa.pub" -Destination "$($Env:ArcBoxDir)\authorized_keys" # Automatically accept unseen keys but will refuse connections for changed or invalid hostkeys. @@ -523,32 +525,22 @@ $payLoad = @" Invoke-Expression "sudo hostnamectl set-hostname $using:ubuntu01vmName" - } + hostnamectl - Restart-VM -Name $ubuntu01vmName -Force + } Invoke-Command -HostName $Ubuntu02VmIp -KeyFilePath "$Env:USERPROFILE\.ssh\id_rsa" -UserName $nestedLinuxUsername -ScriptBlock { Invoke-Expression "sudo hostnamectl set-hostname $using:ubuntu02vmName" + hostnamectl + } - Restart-VM -Name $ubuntu02vmName -Force } - Get-VM *Ubuntu* | Wait-VM -For IPAddress - - Write-Host "Waiting for the nested Linux VMs to come back online...waiting for 10 seconds" - - Start-Sleep -Seconds 10 - - # Copy installation script to nested Windows VMs - Write-Output "Transferring installation script to nested Windows VMs..." - Copy-VMFile $Win2k19vmName -SourcePath "$agentScript\installArcAgent.ps1" -DestinationPath "$Env:ArcBoxDir\installArcAgent.ps1" -CreateFullPath -FileSource Host -Force - Copy-VMFile $Win2k22vmName -SourcePath "$agentScript\installArcAgent.ps1" -DestinationPath "$Env:ArcBoxDir\installArcAgent.ps1" -CreateFullPath -FileSource Host -Force - - # Update Linux VM onboarding script connect toAzure Arc, get new token as it might have been expired by the time execution reached this line. + # Update Linux VM onboarding script connect to Azure Arc, get new token as it might have been expired by the time execution reached this line. $accessToken = ConvertFrom-SecureString ((Get-AzAccessToken -AsSecureString).Token) -AsPlainText (Get-Content -path "$agentScript\installArcAgentUbuntu.sh" -Raw) -replace '\$accessToken', "'$accessToken'" -replace '\$resourceGroup', "'$resourceGroup'" -replace '\$tenantId', "'$Env:tenantId'" -replace '\$azureLocation', "'$Env:azureLocation'" -replace '\$subscriptionId', "'$subscriptionId'" | Set-Content -Path "$agentScript\installArcAgentModifiedUbuntu.sh" @@ -557,12 +549,6 @@ $payLoad = @" Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$agentScript\installArcAgentModifiedUbuntu.sh" -DestinationPath "/home/$nestedLinuxUsername" -FileSource Host -Force - Write-Header "Onboarding Arc-enabled servers" - - # Onboarding the nested VMs as Azure Arc-enabled servers - Write-Output "Onboarding the nested Windows VMs as Azure Arc-enabled servers" - Invoke-Command -VMName $Win2k19vmName,$Win2k22vmName -ScriptBlock { powershell -File $Using:nestedVMArcBoxDir\installArcAgent.ps1 -accessToken $using:accessToken, -tenantId $Using:tenantId, -subscriptionId $Using:subscriptionId, -resourceGroup $Using:resourceGroup, -azureLocation $Using:azureLocation } -Credential $winCreds - Write-Output "Onboarding the nested Linux VMs as an Azure Arc-enabled servers" $UbuntuSessions = New-PSSession -HostName $Ubuntu01VmIp,$Ubuntu02VmIp -KeyFilePath "$Env:USERPROFILE\.ssh\id_rsa" -UserName $nestedLinuxUsername Invoke-JSSudoCommand -Session $UbuntuSessions -Command "sh /home/$nestedLinuxUsername/installArcAgentModifiedUbuntu.sh" From 3efdf3fef1ec2956f059dd65b048a18d97d87c0c Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 22:01:47 +0100 Subject: [PATCH 12/13] Refactor VM renaming process in ArcServersLogonScript to remove unnecessary restart and improve VM online check Signed-off-by: Jan Egil Ring --- .../artifacts/ArcServersLogonScript.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 36c2f462cb..60e60d3f33 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -477,14 +477,14 @@ $payLoad = @" # Renaming the nested VMs Write-Header "Renaming the nested Windows VMs" - Invoke-Command -VMName $Win2k19vmName -ScriptBlock { Rename-Computer -newName $using:Win2k19vmName -Restart } -Credential $winCreds - Invoke-Command -VMName $Win2k22vmName -ScriptBlock { Rename-Computer -newName $using:Win2k22vmName -Restart } -Credential $winCreds + Invoke-Command -VMName $Win2k19vmName -ScriptBlock { Rename-Computer -NewName $using:Win2k19vmName } -Credential $winCreds + Invoke-Command -VMName $Win2k22vmName -ScriptBlock { Rename-Computer -NewName $using:Win2k22vmName } -Credential $winCreds - Get-VM *Win* | Wait-VM -For IPAddress + Write-Host "Waiting for the nested Windows VMs to come back online..." - Write-Host "Waiting for the nested Windows VMs to come back online...waiting for 10 seconds" + Get-VM *Win* | Restart-VM -Force + Get-VM *Win* | Wait-VM -For Heartbeat - Start-Sleep -Seconds 10 } From dbd49169e040d1e5b78b3a83e29e611da577b35d Mon Sep 17 00:00:00 2001 From: Jan Egil Ring Date: Sat, 21 Dec 2024 22:34:17 +0100 Subject: [PATCH 13/13] Remove local authorized_keys file after copying to Ubuntu VMs in ArcServersLogonScript Signed-off-by: Jan Egil Ring --- azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 index 60e60d3f33..1c56518a69 100644 --- a/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 +++ b/azure_jumpstart_arcbox/artifacts/ArcServersLogonScript.ps1 @@ -516,6 +516,9 @@ $payLoad = @" Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath Get-VM *Ubuntu* | Copy-VMFile -SourcePath "$($Env:ArcBoxDir)\authorized_keys" -DestinationPath "/home/$nestedLinuxUsername/.ssh/" -FileSource Host -Force -CreateFullPath + # Remove the authorized_keys file from the local machine + Remove-Item -Path "$($Env:ArcBoxDir)\authorized_keys" + if ($namingPrefix -ne "ArcBox") { # Renaming the nested linux VMs