From 1a3556e26bfacc09f9a2ed78f103aacff7b72621 Mon Sep 17 00:00:00 2001 From: Mark Rossetti Date: Wed, 23 Sep 2020 15:10:45 -0700 Subject: [PATCH] feat: Updating Windows VHD build files to support building for multiple OS versions (#3847) * Updating configure-windows-vhd.ps1 to support multiple Windows OS versions * Updating configured-windows-vhd.ps1 to not use containerd from a personal storage account * Updating windows packer file to take in windows os version specific vars from a file * updating windows build pipeline/packer.mk to pass new vars file to packer * formatting configure-windows-vhd-ps1 * vhd-builder-windows.yaml adding missing WINDOWS_SERVER_VERSION to make run-packer-windows-call and added succeeded condition checks everywher * fixup! updating windows build pipeline/packer.mk to pass new vars file to packer * fixup! Updating windows packer file to take in windows os version specific vars from a file * fixup! Updating windows packer file to take in windows os version specific vars from a file --- .pipelines/vhd-builder-windows.yaml | 11 ++-- packer.mk | 2 +- vhd/packer/configure-windows-vhd.ps1 | 85 ++++++++++++++++++++-------- vhd/packer/windows-2004-vars.json | 7 +++ vhd/packer/windows-2019-vars.json | 7 +++ vhd/packer/windows-vhd-builder.json | 23 +++++--- 6 files changed, 98 insertions(+), 37 deletions(-) create mode 100644 vhd/packer/windows-2004-vars.json create mode 100644 vhd/packer/windows-2019-vars.json diff --git a/.pipelines/vhd-builder-windows.yaml b/.pipelines/vhd-builder-windows.yaml index df6797c888..0d307c4a9c 100644 --- a/.pipelines/vhd-builder-windows.yaml +++ b/.pipelines/vhd-builder-windows.yaml @@ -40,6 +40,7 @@ jobs: -e BUILD_ID=$(Build.BuildId) \ -e BUILD_NUMBER=$(Build.BuildNumber) \ -e CONTAINER_RUNTIME=$(CONTAINER_RUNTIME) \ + -e WINDOWS-SERVER-VERSION=2019 \ ${DEIS_GO_DEV_IMAGE} make run-packer-windows displayName: Building windows VHD @@ -49,7 +50,7 @@ jobs: -w /go/src/github.com/Azure/aks-engine \ ${DEIS_GO_DEV_IMAGE} make all displayName: build aks-engine-test - make all - condition: eq(variables.COPY_VHD, 'False') + condition: and(succeeded(), eq(variables.COPY_VHD, 'False')) - script: | OS_DISK_URI="$(cat packer-output | grep "OSDiskUri:" | cut -d " " -f 2)" && \ @@ -68,7 +69,7 @@ jobs: -e WINDOWS_NODE_VHD_URL=${OS_DISK_URI} \ ${DEIS_GO_DEV_IMAGE} make test-kubernetes displayName: run e2e tests - condition: eq(variables.COPY_VHD, 'False') + condition: and(succeeded(), eq(variables.COPY_VHD, 'False')) - task: PublishPipelineArtifact@1 inputs: @@ -90,7 +91,7 @@ jobs: -e VHD_NAME=${VHD_NAME} \ ${DEIS_GO_DEV_IMAGE} make az-copy displayName: Copying resource to Classic Storage Account - condition: eq(variables.COPY_VHD, 'True') + condition: and(succeeded(), eq(variables.COPY_VHD, 'True')) - script: | OS_DISK_SAS="$(cat packer-output | grep "OSDiskUriReadOnlySas:" | cut -d " " -f 2)" && \ @@ -106,13 +107,13 @@ jobs: -e VHD_NAME=${VHD_NAME} \ ${DEIS_GO_DEV_IMAGE} make windows-vhd-publishing-info displayName: Generating publishing info for Windows VHD - condition: eq(variables.COPY_VHD, 'True') + condition: and(succeeded(), eq(variables.COPY_VHD, 'True')) - task: PublishPipelineArtifact@1 inputs: artifact: 'publishing-info' path: 'windows-vhd-publishing-info.json' - condition: eq(variables.COPY_VHD, 'True') + condition: and(succeeded(), eq(variables.COPY_VHD, 'True')) - script: | SA_NAME="$(cat packer-output | grep "storage name:" | cut -d " " -f 3)" && \ diff --git a/packer.mk b/packer.mk index cb92fb324f..5f60fe5157 100644 --- a/packer.mk +++ b/packer.mk @@ -5,7 +5,7 @@ build-packer-ubuntu-gen2: @packer build -var-file=vhd/packer/settings.json vhd/packer/vhd-image-builder-ubuntu-gen2.json build-packer-windows: - @packer build -var-file=vhd/packer/settings.json vhd/packer/windows-vhd-builder.json + @packer build -var-file=vhd/packer/settings.json -var-file=vhd/packer/windows-${WINDOWS-SERVER-VERSION}-vars.json vhd/packer/windows-vhd-builder.json init-packer: @./vhd/packer/init-variables.sh diff --git a/vhd/packer/configure-windows-vhd.ps1 b/vhd/packer/configure-windows-vhd.ps1 index 4280cd5069..ddd3a3834c 100644 --- a/vhd/packer/configure-windows-vhd.ps1 +++ b/vhd/packer/configure-windows-vhd.ps1 @@ -12,7 +12,7 @@ $ErrorActionPreference = "Stop" filter Timestamp { "$(Get-Date -Format o): $_" } -$global:containerdPackageUrl = "https://marosset.blob.core.windows.net/pub/containerd/containerd-0.0.87-public.zip" +$global:containerdPackageUrl = "https://github.com/containerd/containerd/releases/download/v1.4.1/containerd-1.4.1-windows-amd64.tar.gz" function Write-Log($Message) { $msg = $message | Timestamp @@ -38,14 +38,30 @@ function Disable-WindowsUpdates { function Get-ContainerImages { param ( - $containerRuntime + $containerRuntime, + $windowsServerVersion ) - $imagesToPull = @( - "mcr.microsoft.com/windows/servercore:ltsc2019", - "mcr.microsoft.com/windows/nanoserver:1809", - "mcr.microsoft.com/oss/kubernetes/pause:1.4.0", - "mcr.microsoft.com/oss/kubernetes-csi/livenessprobe:v2.0.1-alpha.1-windows-1809-amd64", - "mcr.microsoft.com/oss/kubernetes-csi/csi-node-driver-registrar:v1.2.1-alpha.1-windows-1809-amd64") + + switch ($windowsServerVersion) { + '2019' { + $imagesToPull = @( + "mcr.microsoft.com/windows/servercore:ltsc2019", + "mcr.microsoft.com/windows/nanoserver:1809", + "mcr.microsoft.com/oss/kubernetes/pause:1.4.0", + "mcr.microsoft.com/oss/kubernetes-csi/livenessprobe:v2.0.1-alpha.1-windows-1809-amd64", + "mcr.microsoft.com/oss/kubernetes-csi/csi-node-driver-registrar:v1.2.1-alpha.1-windows-1809-amd64") + } + '2004' { + $imagesToPull = @( + "mcr.microsoft.com/windows/servercore:2004", + "mcr.microsoft.com/windows/nanoserver:2004", + "mcr.microsoft.com/oss/kubernetes/pause:1.4.0") + } + default { + $imagesToPull = @() + } + } + if ($containerRuntime -eq 'containerd') { foreach ($image in $imagesToPull) { @@ -138,13 +154,13 @@ function Install-ContainerD { Write-Log "Getting containerD binaries from $global:containerdPackageUrl" $installDir = "c:\program files\containerd" - $zipPath = [IO.Path]::Combine($installDir, "containerd.zip") + $tarPath = [IO.Path]::Combine($installDir, "containerd.tar.gz") Write-Log "Installing containerd to $installDir" New-Item -ItemType Directory $installDir -Force | Out-Null - Invoke-WebRequest -UseBasicParsing -Uri $global:containerdPackageUrl -OutFile $zipPath - Expand-Archive -Path $zipPath -DestinationPath $installDir - Remove-Item -Path $zipPath | Out-null + Invoke-WebRequest -UseBasicParsing -Uri $global:containerdPackageUrl -OutFile $tarPath + tar -xzf $tarPath --strip=1 -C $installDir + Remove-Item -Path $tarPath | Out-Null $newPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";$installDir" [Environment]::SetEnvironmentVariable("Path", $newPath, [EnvironmentVariableTarget]::Machine) @@ -168,26 +184,42 @@ function Install-Docker { $defaultDockerVersion = "19.03.11" Write-Log "Attempting to install Docker version $defaultDockerVersion" - Install-PackageProvider -Name DockerMsftProvider -Force -ForceBootstrap | Out-null + Install-PackageProvider -Name DockerMsftProvider -Force -ForceBootstrap | Out-Null $package = Find-Package -Name Docker -ProviderName DockerMsftProvider -RequiredVersion $defaultDockerVersion Write-Log "Installing Docker version $($package.Version)" $package | Install-Package -Force | Out-Null Start-Service docker } - function Install-OpenSSH { Write-Log "Installing OpenSSH Server" Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 } function Install-WindowsPatches { - # Windows Server 2019 update history can be found at https://support.microsoft.com/en-us/help/4464619 - # then you can get download links by searching for specific KBs at http://www.catalog.update.microsoft.com/home.aspx + param ( + $windowsServerVersion + ) - # KB4558998 contains August 11, 2020 cumulative updates for Windows Server 2019 - # https://www.catalog.update.microsoft.com/Search.aspx?q=KB4565349 - $patchUrls = @("http://download.windowsupdate.com/d/msdownload/update/software/secu/2020/08/windows10.0-kb4565349-x64_919b9f31d4ccfa91183fbb9bab8c2975529e66b6.msu") + switch ($windowsServerVersion) { + '2019' { + # Windows Server 2019 update history can be found at https://support.microsoft.com/en-us/help/4464619 + # then you can get download links by searching for specific KBs at http://www.catalog.update.microsoft.com/home.aspx + + # KB4558998 contains August 11, 2020 cumulative updates for Windows Server 2019 + # https://www.catalog.update.microsoft.com/Search.aspx?q=KB4565349 + $patchUrls = @("http://download.windowsupdate.com/d/msdownload/update/software/secu/2020/08/windows10.0-kb4565349-x64_919b9f31d4ccfa91183fbb9bab8c2975529e66b6.msu") + } + '2004' { + # Windows Server, Version 2004 update history can be found at https://support.microsoft.com/en-us/help/4555932 + # then you can get download links by searching for specific KBs at http://www.catalog.update.microsoft.com/home.aspx + + $patchUrls = @() + } + default { + $patchUrls = @() + } + } foreach ($patchUrl in $patchUrls) { $pathOnly = $patchUrl.Split("?")[0] @@ -200,14 +232,14 @@ function Install-WindowsPatches { Write-Log "Downloading windows patch from $pathOnly to $fullPath" Invoke-WebRequest -UseBasicParsing $patchUrl -OutFile $fullPath Write-Log "Starting install of $fileName" - $proc = Start-Process -Passthru -FilePath wusa.exe -ArgumentList "$fullPath /quiet /norestart" + $proc = Start-Process -PassThru -FilePath wusa.exe -ArgumentList "$fullPath /quiet /norestart" Wait-Process -InputObject $proc switch ($proc.ExitCode) { 0 { Write-Log "Finished install of $fileName" } 3010 { - WRite-Log "Finished install of $fileName. Reboot required" + Write-Log "Finished install of $fileName. Reboot required" } default { Write-Log "Error during install of $fileName. ExitCode: $($proc.ExitCode)" @@ -277,13 +309,20 @@ if (-not ($validContainerRuntimes -contains $containerRuntime)) { exit 1 } +$windowsServerVersion = $env:WindowsServerVersion +$validWindowsServerContainers = @('2019', '2004') +if (-not ($validWindowsServerContainers -contains $windowsServerVersion)) { + Write-Host "Unsupported Windows Server version: $windowsServerVersion" + exit 1 +} + switch ($env:ProvisioningPhase) { "1" { Write-Log "Performing actions for provisioning phase 1" Set-WinRmServiceDelayedStart Set-AllowedSecurityProtocols Disable-WindowsUpdates - Install-WindowsPatches + Install-WindowsPatches -WindowsServerVersion $windowsServerVersion Update-DefenderSignatures Install-OpenSSH Update-WindowsFeatures @@ -296,7 +335,7 @@ switch ($env:ProvisioningPhase) { if ($containerRuntime -eq 'containerd') { Install-ContainerD } - Get-ContainerImages -containerRuntime $containerRuntime + Get-ContainerImages -containerRuntime $containerRuntime -WindowsServerVersion $windowsServerVersion Get-FilesToCacheOnVHD (New-Guid).Guid | Out-File -FilePath 'c:\vhd-id.txt' } diff --git a/vhd/packer/windows-2004-vars.json b/vhd/packer/windows-2004-vars.json new file mode 100644 index 0000000000..bb3a60ad39 --- /dev/null +++ b/vhd/packer/windows-2004-vars.json @@ -0,0 +1,7 @@ +{ + "image_publisher": "MicrosoftWindowsServer", + "image_offer": "WindowsServer", + "image_sku": "datacenter-core-2004-with-containers-smalldisk", + "image_version": "19041.508.2009070256", + "windows_server_version": "2004" +} \ No newline at end of file diff --git a/vhd/packer/windows-2019-vars.json b/vhd/packer/windows-2019-vars.json new file mode 100644 index 0000000000..ed23dbefe6 --- /dev/null +++ b/vhd/packer/windows-2019-vars.json @@ -0,0 +1,7 @@ +{ + "image_publisher": "MicrosoftWindowsServer", + "image_offer": "WindowsServer", + "image_sku": "2019-Datacenter-Core-smalldisk", + "image_version": "17763.1339.2007101755", + "windows_server_version": "2019" +} \ No newline at end of file diff --git a/vhd/packer/windows-vhd-builder.json b/vhd/packer/windows-vhd-builder.json index 696dfa2b1e..924f5cbeae 100644 --- a/vhd/packer/windows-vhd-builder.json +++ b/vhd/packer/windows-vhd-builder.json @@ -8,10 +8,15 @@ "client_id": "{{env `AZURE_CLIENT_ID`}}", "client_secret": "{{env `AZURE_CLIENT_SECRET`}}", "container_runtime": "{{env `CONTAINER_RUNTIME`}}", + "image_publisher": null, + "image_offer": null, + "image_sku": null, + "image_version": null, "tenant_id": "{{env `AZURE_TENANT_ID`}}", "subscription_id": "{{env `AZURE_SUBSCRIPTION_ID`}}", "location": "{{env `AZURE_LOCATION`}}", - "vm_size": "{{env `AZURE_VM_SIZE`}}" + "vm_size": "{{env `AZURE_VM_SIZE`}}", + "windows_server_version": null }, "builders": [ { @@ -23,12 +28,12 @@ "location": "{{user `location`}}", "vm_size": "{{user `vm_size`}}", "os_type": "Windows", - "image_publisher": "MicrosoftWindowsServer", - "image_offer": "WindowsServer", - "image_sku": "2019-Datacenter-Core-smalldisk", - "image_version": "17763.1339.2007101755", + "image_publisher": "{{user `image_publisher`}}", + "image_offer": "{{user `image_offer`}}", + "image_sku": "{{user `image_sku`}}", + "image_version": "{{user `image_version`}}", "resource_group_name": "{{user `resource_group_name`}}", - "capture_container_name": "aksengine-vhds-windows-ws2019", + "capture_container_name": "aksengine-vhds-windows-{{user `windows_server_version`}}", "capture_name_prefix": "aksengine-{{user `create_time`}}", "storage_account": "{{user `storage_account_name`}}", "communicator": "winrm", @@ -49,7 +54,8 @@ "elevated_password": "{{.WinRMPassword}}", "environment_vars": [ "ProvisioningPhase=1", - "ContainerRuntime={{user `container_runtime`}}" + "ContainerRuntime={{user `container_runtime`}}", + "windowsServerVersion={{user `windows_server_version`}}" ], "type": "powershell", "script": "vhd/packer/configure-windows-vhd.ps1" @@ -67,7 +73,8 @@ "elevated_password": "{{.WinRMPassword}}", "environment_vars": [ "ProvisioningPhase=2", - "ContainerRuntime={{user `container_runtime`}}" + "ContainerRuntime={{user `container_runtime`}}", + "windowsServerVersion={{user `windows_server_version`}}" ], "type": "powershell", "script": "vhd/packer/configure-windows-vhd.ps1"