Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

feat: Updating Windows VHD build files to support building for multiple OS versions #3847

Merged
merged 9 commits into from
Sep 23, 2020
Merged
11 changes: 6 additions & 5 deletions .pipelines/vhd-builder-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should 2019 be an environment variable so it can be configured on the job?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly. We still need to have a discussion around what images aks-engine should actually be producing. If we want to produce more than just Windows Server 2019 + Docker I think I would rather just update the pipepline to build all the images as different stages and avoid the need to trigger multiple pipelines.
I'm hoping we can resolve that with a different PR tho.

${DEIS_GO_DEV_IMAGE} make run-packer-windows
displayName: Building windows VHD

Expand All @@ -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)" && \
Expand All @@ -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:
Expand All @@ -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)" && \
Expand All @@ -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)" && \
Expand Down
2 changes: 1 addition & 1 deletion packer.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 62 additions & 23 deletions vhd/packer/configure-windows-vhd.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -140,13 +156,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)
Expand All @@ -170,26 +186,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]
Expand All @@ -202,14 +234,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)"
Expand Down Expand Up @@ -279,13 +311,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
Expand All @@ -298,7 +337,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'
}
Expand Down
7 changes: 7 additions & 0 deletions vhd/packer/windows-2004-vars.json
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 7 additions & 0 deletions vhd/packer/windows-2019-vars.json
Original file line number Diff line number Diff line change
@@ -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"
}
23 changes: 15 additions & 8 deletions vhd/packer/windows-vhd-builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand All @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will change from ws2019 to 2019 by default, is that okay?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be ok. This is only used in the paths in the storage account the VHD gets captured into correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah should be fine, we get the URI directly from the output so we don't expect it to be in a specific place 👍

"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",
Expand All @@ -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"
Expand All @@ -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"
Expand Down