forked from Azure/aks-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure-windows-vhd.ps1
330 lines (287 loc) · 13.9 KB
/
configure-windows-vhd.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<#
.SYNOPSIS
Used to produce Windows AKS images.
.DESCRIPTION
This script is used by packer to produce Windows AKS images.
#>
param()
$ErrorActionPreference = "Stop"
filter Timestamp {"$(Get-Date -Format o): $_"}
$global:containerdPackageUrl = "https://marosset.blob.core.windows.net/pub/containerd/containerd-0.0.87-public.zip"
function Write-Log($Message)
{
$msg = $message | Timestamp
Write-Output $msg
}
function Disable-WindowsUpdates
{
# See https://docs.microsoft.com/en-us/windows/deployment/update/waas-wu-settings
# for additional information on WU related registry settings
Write-Log "Disabling automatic windows upates"
$WindowsUpdatePath = "HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
$AutoUpdatePath = "HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
if (Test-Path -Path $WindowsUpdatePath)
{
Remove-Item -Path $WindowsUpdatePath -Recurse
}
New-Item -Path $WindowsUpdatePath | Out-Null
New-Item -Path $AutoUpdatePath | Out-Null
Set-ItemProperty -Path $AutoUpdatePath -Name NoAutoUpdate -Value 1 | Out-Null
}
function Get-ContainerImages
{
param (
$containerRuntime
)
$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")
if ($containerRuntime -eq 'containerd') {
foreach ($image in $imagesToPull) {
& ctr.exe -n k8s.io images pull $image
}
} else {
foreach ($image in $imagesToPull) {
docker pull $image
}
}
}
function Get-FilesToCacheOnVHD
{
Write-Log "Caching misc files on VHD"
$map = @{
"c:\akse-cache\" = @(
"https://github.com/Azure/aks-engine/raw/master/scripts/collect-windows-logs.ps1",
"https://github.com/Microsoft/SDN/raw/master/Kubernetes/flannel/l2bridge/cni/win-bridge.exe",
"https://github.com/microsoft/SDN/raw/master/Kubernetes/windows/debug/collectlogs.ps1",
"https://github.com/microsoft/SDN/raw/master/Kubernetes/windows/debug/dumpVfpPolicies.ps1",
"https://github.com/microsoft/SDN/raw/master/Kubernetes/windows/debug/portReservationTest.ps1",
"https://github.com/microsoft/SDN/raw/master/Kubernetes/windows/debug/starthnstrace.cmd",
"https://github.com/microsoft/SDN/raw/master/Kubernetes/windows/debug/startpacketcapture.cmd",
"https://github.com/microsoft/SDN/raw/master/Kubernetes/windows/debug/stoppacketcapture.cmd",
"https://github.com/Microsoft/SDN/raw/master/Kubernetes/windows/debug/VFP.psm1",
"https://github.com/microsoft/SDN/raw/master/Kubernetes/windows/helper.psm1",
"https://github.com/Microsoft/SDN/raw/master/Kubernetes/windows/hns.psm1",
"https://globalcdn.nuget.org/packages/microsoft.applicationinsights.2.11.0.nupkg",
"https://kubernetesartifacts.azureedge.net/aks-engine/windows/provisioning/signedscripts-v0.0.2.zip"
);
"c:\akse-cache\containerd\" = @(
$global:containerdPackageUrl
);
"c:\akse-cache\win-k8s\" = @(
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.10-azs/windowszip/v1.15.10-azs-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.11-azs/windowszip/v1.15.11-azs-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.12-azs/windowszip/v1.15.12-azs-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.16.13-azs/windowszip/v1.16.13-azs-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.17.9-azs/windowszip/v1.17.9-azs-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.10/windowszip/v1.15.10-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.11/windowszip/v1.15.11-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.11-hotfix.20200714/windowszip/v1.15.11-hotfix.20200714-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.12/windowszip/v1.15.12-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.12-hotfix.20200714/windowszip/v1.15.12-hotfix.20200714-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.15.12-hotfix.20200623/windowszip/v1.15.12-hotfix.20200623-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.16.12/windowszip/v1.16.12-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.16.13/windowszip/v1.16.13-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.16.13-hotfix.20200714/windowszip/v1.16.13-hotfix.20200714-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.16.14/windowszip/v1.16.14-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.17.7/windowszip/v1.17.7-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.17.7-hotfix.20200714/windowszip/v1.17.7-hotfix.20200714-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.17.8/windowszip/v1.17.8-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.17.9/windowszip/v1.17.9-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.17.9-hotfix.20200714/windowszip/v1.17.9-hotfix.20200714-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.18.5/windowszip/v1.18.5-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.18.6/windowszip/v1.18.6-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.18.8/windowszip/v1.18.8-1int.zip",
"https://kubernetesartifacts.azureedge.net/kubernetes/v1.19.0-rc.4/windowszip/v1.19.0-rc.4-1int.zip"
);
"c:\akse-cache\win-vnet-cni\" = @(
"https://kubernetesartifacts.azureedge.net/azure-cni/v1.1.2/binaries/azure-vnet-cni-singletenancy-windows-amd64-v1.1.2.zip",
"https://kubernetesartifacts.azureedge.net/azure-cni/v1.1.3/binaries/azure-vnet-cni-singletenancy-windows-amd64-v1.1.3.zip"
"https://kubernetesartifacts.azureedge.net/azure-cni/v1.1.6/binaries/azure-vnet-cni-singletenancy-windows-amd64-v1.1.6.zip"
)
}
foreach ($dir in $map.Keys)
{
New-Item -ItemType Directory $dir -Force | Out-Null
foreach ($URL in $map[$dir])
{
$fileName = [IO.Path]::GetFileName($URL)
$dest = [IO.Path]::Combine($dir, $fileName)
Write-Log "Downloading $URL to $dest"
Invoke-WebRequest -UseBasicParsing -Uri $URL -OutFile $dest
}
}
}
function Install-ContainerD {
Write-Log "Getting containerD binaries from $global:containerdPackageUrl"
$installDir = "c:\program files\containerd"
$zipPath = [IO.Path]::Combine($installDir, "containerd.zip")
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
$newPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";$installDir"
[Environment]::SetEnvironmentVariable("Path", $newPath, [EnvironmentVariableTarget]::Machine)
$env:Path += ";$installDir"
Write-Log "Registering containerd as a service"
& containerd.exe --register-service
$svc = Get-Service -Name "containerd" -ErrorAction SilentlyContinue
if ($null -eq $svc) {
throw "containerd.exe did not get installed as a service correctly."
}
Write-Log "Starting containerd service"
$svc | Start-Service
if ($svc.Status -ne "Running") {
throw "containerd service is not running"
}
}
function Install-Docker
{
$defaultDockerVersion = "19.03.11"
Write-Log "Attempting to install Docker version $defaultDockerVersion"
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
# 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")
foreach ($patchUrl in $patchUrls)
{
$pathOnly = $patchUrl.Split("?")[0]
$fileName = Split-Path $pathOnly -Leaf
$fileExtension = [IO.Path]::GetExtension($fileName)
$fullPath = [IO.Path]::Combine($env:TEMP, $fileName)
switch ($fileExtension)
{
".msu"
{
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"
Wait-Process -InputObject $proc
switch ($proc.ExitCode)
{
0
{
Write-Log "Finished install of $fileName"
}
3010
{
WRite-Log "Finished install of $fileName. Reboot required"
}
default
{
Write-Log "Error during install of $fileName. ExitCode: $($proc.ExitCode)"
exit 1
}
}
}
default
{
Write-Log "Installing patches with extension $fileExtension is not currently supported."
exit 1
}
}
}
}
function Set-AllowedSecurityProtocols
{
$allowedProtocols = @()
$insecureProtocols = @([System.Net.SecurityProtocolType]::SystemDefault, [System.Net.SecurityProtocolType]::Ssl3)
foreach ($protocol in [System.Enum]::GetValues([System.Net.SecurityProtocolType]))
{
if ($insecureProtocols -notcontains $protocol)
{
$allowedProtocols += $protocol
}
}
Write-Log "Settings allowed security protocols to: $allowedProtocols"
[System.Net.ServicePointManager]::SecurityProtocol = $allowedProtocols
}
function Set-WinRmServiceAutoStart
{
Write-Log "Setting WinRM service start to auto"
sc.exe config winrm start=auto
}
function Set-WinRmServiceDelayedStart
{
# Hyper-V messes with networking components on startup after the feature is enabled
# causing issues with communication over winrm and setting winrm to delayed start
# gives Hyper-V enough time to finish configuration before having packer continue.
Write-Log "Setting WinRM service start to delayed-auto"
sc.exe config winrm start=delayed-auto
}
function Update-DefenderSignatures
{
Write-Log "Updating windows defender signatures."
Update-MpSignature
}
function Update-WindowsFeatures
{
$featuresToEnable = @(
"Containers",
"Hyper-V",
"Hyper-V-PowerShell")
foreach ($feature in $featuresToEnable)
{
Write-Log "Enabling Windows feature: $feature"
Install-WindowsFeature $feature
}
}
# Disable progress writers for this session to greatly speed up operations such as Invoke-WebRequest
$ProgressPreference = 'SilentlyContinue'
$containerRuntime = $env:ContainerRuntime
$validContainerRuntimes = @('containerd', 'docker')
if (-not ($validContainerRuntimes -contains $containerRuntime)) {
Write-Host "Unsupported container runtime: $containerRuntime"
exit 1
}
switch ($env:ProvisioningPhase)
{
"1"
{
Write-Log "Performing actions for provisioning phase 1"
Set-WinRmServiceDelayedStart
Set-AllowedSecurityProtocols
Disable-WindowsUpdates
Install-WindowsPatches
Update-DefenderSignatures
Install-OpenSSH
Update-WindowsFeatures
}
"2"
{
Write-Log "Performing actions for provisioning phase 2 for container runtime '$containerRuntime'"
Set-WinRmServiceAutoStart
# TODO: make decision on if we want to install docker along with containerd (will need to update CSE too,)
Install-Docker
if ($containerRuntime -eq 'containerd') {
Install-ContainerD
}
Get-ContainerImages -containerRuntime $containerRuntime
Get-FilesToCacheOnVHD
(New-Guid).Guid | Out-File -FilePath 'c:\vhd-id.txt'
}
default
{
Write-Log "Unable to determine provisiong phase... exiting"
exit 1
}
}