forked from rancher/rke2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
737 lines (677 loc) · 26.7 KB
/
install.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
<#
.SYNOPSIS
Installs Rancher RKE2 to create Windows Worker Nodes.
.DESCRIPTION
Run the script to install all Rancher RKE2 related needs.
.Parameter Channel
Channel to use for fetching rke2 download URL.
Defaults to 'stable'.`
.Parameter Method
The installation method to use. Currently tar or choco installation supported.
Default is on Windows systems is "tar".`
.Parameter Type
Type of rke2 service. Only the "agent" type is supported on Windows.
Default is "agent".`
.Parameter Version
Version of rke2 to download from github.`
.Parameter TarPrefix
Installation prefix when using the tar installation method. This needs to match the value of CATTLE_AGENT_BIN_PREFIX
Default is C:/usr/local, unless C:/usr/local is read-only or has a dedicated mount point,
in which case C:/opt/rke2 is used instead.`
.Parameter Commit
Commit of RKE2 to download from temporary cloud storage.
If set, this forces Method=tar.
* (for developer & QA use only)`
.Parameter AgentImagesDir
Installation path for airgap images when installing from CI commit.
Default is C:/var/lib/rancher/rke2/agent/images`
.Parameter ArtifactPath
If set, the install script will use the local path for sourcing the rke2.windows-$SUFFIX and sha256sum-$ARCH.txt files
rather than the downloading the files from the internet.
Default is not set.`
.EXAMPLE
Usage:
Invoke-WebRequest ((New-Object System.Net.WebClient).DownloadString('https://github.com/rancher/rke2/blob/master/install.ps1'))
./install.ps1
.EXAMPLE
Usage:
Invoke-WebRequest ((New-Object System.Net.WebClient).DownloadString('https://github.com/rancher/rke2/blob/master/install.ps1'))
./install.ps1 -Channel Latest
.EXAMPLE
Usage:
Invoke-WebRequest ((New-Object System.Net.WebClient).DownloadString('https://github.com/rancher/rke2/blob/master/install.ps1'))
./install.ps1 -Channel Latest -Method Tar
#>
[CmdletBinding()]
param (
[Parameter()]
[String]
$Channel = "stable",
[Parameter()]
[ValidateSet("choco", "tar")]
[String]
$Method = "tar",
[Parameter()]
[ValidateSet("agent")]
[String]
$Type = "agent",
[Parameter()]
[String]
$Version,
[Parameter()]
[String]
$TarPrefix = "C:/usr/local",
[Parameter()]
[String]
$Commit,
[Parameter()]
[String]
$AgentImagesDir = "C:/var/lib/rancher/rke2/agent/images",
[Parameter()]
[String]
$ArtifactPath = "",
[Parameter()]
[String]
$ChannelUrl = "https://update.rke2.io/v1-release/channels"
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Write-InfoLog() {
Write-Output "[INFO] $($args -join " ")"
}
function Write-WarnLog() {
Write-Output "[WARN] $($args -join " ")"
}
function Write-DebugLog() {
Write-Output "[DEBUG] $($args -join " ")"
}
# fatal logs the given argument at fatal log level.
function Write-FatalLog() {
Write-Output "[ERROR] $($args -join " ")"
if ([string]::IsNullOrEmpty($suffix)) {
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
Write-Output "[ALT] Please visit 'https://github.com/rancher/rke2/releases' directly and download the latest rke2.$suffix.tar.gz"
}
exit 1
}
function Confirm-WindowsFeatures {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[String[]]
$RequiredFeatures
)
foreach ($feature in $RequiredFeatures) {
$f = Get-WindowsFeature -Name $feature
if (-not $f.Installed) {
Write-FatalLog "Windows feature: '$feature' is not installed. Please run: Install-WindowsFeature -Name $feature"
}
else {
Write-InfoLog "Windows feature: '$feature' is installed. Installation will proceed."
}
}
}
# setup_env defines needed environment variables.
function Set-Environment() {
# --- bail if we are not administrator ---
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
$currentRole = [Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()
if (-NOT $currentRole.IsInRole($adminRole)) {
Write-FatalLog "You need to be administrator to perform this install"
}
if ($env:CATTLE_AGENT_BIN_PREFIX) {
$TarPrefix = $env:CATTLE_AGENT_BIN_PREFIX
[System.Environment]::SetEnvironmentVariable('CATTLE_AGENT_BIN_PREFIX', $TarPrefix, 'Machine')
}
else {
[System.Environment]::SetEnvironmentVariable('CATTLE_AGENT_BIN_PREFIX', $TarPrefix, 'Machine')
}
Write-Host "Using $($Channel) channel of rke2 for installation"
}
# check_method_conflict will exit with an error if the user attempts to install
# via tar method on a host with existing chocolatey package.
function Test-MethodConflict() {
if ($Method -eq "choco") {
$ChocoPackages = choco list --localonly
if ($ChocoPackages.Select -Like "rke2") {
Write-FatalLog "Cannot perform $($Method): -tar install on host with existing RKE2 Choco Files - please run rke2-uninstall.ps1 first"
}
}
}
# setup_arch set arch and suffix,
# fatal if architecture not supported.
function Get-ArchitectureInfo() {
$arch = $env:PROCESSOR_ARCHITECTURE.ToLower()
if ("$arch" -ne "amd64") {
Write-FatalLog "unsupported architecture $(env:PROCESSOR_ARCHITECTURE)"
exit 1
}
return @{ Suffix = "windows-$arch"; Arch = "$arch" }
}
# get Windows Server Build Version
function Get-BuildVersion() {
$buildVersion = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
if ("$buildVersion" -eq "2009") {
$buildVersion = "20H2"
}
if ("$buildVersion" -eq "1809" -or "2022" -or "2004" -or "20H2") {
return $buildVersion
}
else {
Write-FatalLog "unsupported build version $buildVersion"
exit 1
}
}
# --- use desired rke2 version if defined or find version from channel ---
function Get-ReleaseVersion() {
if ($Commit) {
$Version = "commit $($Commit)"
}
elseif ($Version) {
$Version = $Version
}
else {
$versionUrl = "$ChannelUrl/$Channel"
$result = New-Object System.Uri($(curl.exe -w "%{url_effective}" -L -s -S $versionUrl -o "$TMP_DIR/version.html"))
$Version = $result.Segments | Select-Object -Last 1
Remove-Item -Path "$TMP_DIR/version.html" -Force
}
return $Version
}
# download_checksums downloads the binary checksums from github or CI storage url
# and prepares the checksum value for later validation.
function Get-BinaryChecksums() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$StorageUrl,
[Parameter()]
[String]
$Rke2Version,
[Parameter()]
[String]
$Rke2GitHubUrl,
[Parameter()]
[String]
$TempBinaryChecksums
)
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$arch = $archInfo.Arch
$binaryChecksumsUrl = ""
if ($CommitHash) {
$binaryChecksumsUrl = "$StorageUrl/rke2.$suffix-$CommitHash.tar.gz.sha256sum"
Write-Host "downloading binary checksum for commit: $CommitHash at $binaryChecksumsUrl"
curl.exe -sfL $binaryChecksumsUrl -o $TempBinaryChecksums
return Find-Checksum -ChecksumFilePath $TempBinaryChecksums -Pattern "rke2.$suffix.tar.gz"
}
else {
$binaryChecksumsUrl = "$Rke2GitHubUrl/releases/download/$Rke2Version/sha256sum-$arch.txt"
Write-Host "downloading binary checksum from $binaryChecksumsUrl"
curl.exe -sfL $binaryChecksumsUrl -o $TempBinaryChecksums
return Find-Checksum -ChecksumFilePath $TempBinaryChecksums -Pattern "rke2.$suffix.tar.gz"
}
}
# download_airgap_checksums downloads the checksum file for the airgap image tarball
# and prepares the checksum value for later validation.
function Get-ImageChecksums() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$StorageUrl,
[Parameter()]
[String]
$Rke2Version,
[Parameter()]
[String]
$Rke2GitHubUrl,
[Parameter()]
[String]
$TempImageChecksums
)
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$arch = $archInfo.Arch
$imageChecksumsUrl = ""
if ($CommitHash) {
$imageChecksumsUrl = "$StorageUrl/rke2-images.$suffix-$CommitHash.tar.zst.sha256sum"
Write-Host "downloading image checksum for commit: $CommitHash at $imageChecksumsUrl"
curl.exe -sfL $imageChecksumsUrl -o $TempImageChecksums
return Find-Checksum -ChecksumFilePath $TempImageChecksums -Pattern "rke2-images.$suffix.tar.zst"
}
else {
$imageChecksumsUrl = "$Rke2GitHubUrl/releases/download/$Rke2Version/sha256sum-$arch.txt"
Write-Host "downloading image checksum from $imageChecksumsUrl"
curl.exe -sfL $imageChecksumsUrl -o $TempImageChecksums
return Find-Checksum -ChecksumFilePath $TempImageChecksums -Pattern "rke2-windows-$BuildVersion-$arch-images.tar.gz"
}
}
# download_tarball downloads binary from github or CI storage url.
function Get-BinaryTarball() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$StorageUrl,
[Parameter()]
[String]
$Rke2Version,
[Parameter()]
[String]
$Rke2GitHubUrl,
[Parameter()]
[String]
$TempTarball
)
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$tarballUrl = ""
if ($CommitHash) {
$tarballUrl = "$StorageUrl/rke2.$suffix-$CommitHash.tar.gz"
Write-Host "downloading binary tarball for commit: $CommitHash at $tarballUrl"
curl.exe -sfL $tarballUrl -o $TempTarball
}
else {
$tarballUrl = "$Rke2GitHubUrl/releases/download/$Rke2Version/rke2.$suffix.tar.gz"
Write-InfoLog "downloading binary tarball at $tarballUrl"
curl.exe -sfL $tarballUrl -o $TempTarball
}
}
# stage_local_checksums stages the binary local checksum hash for validation.
function Copy-LocalBinaryChecksums() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$Path,
[Parameter()]
[String]
$DestinationPath
)
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$arch = $archInfo.Arch
if ($CommitHash) {
Write-InfoLog "staging local binary checksum from $Path/rke2.$suffix-$CommitHash.tar.gz.sha256sum"
Copy-Item -Path "$Path/rke2.$suffix-$CommitHash.tar.gz.sha256sum" -Destination $DestinationPath -Force
$expectedBinaryChecksum = Find-Checksum -ChecksumFilePath $DestinationPath -Pattern "rke2.$suffix.tar.gz"
$expectedBinaryAirgapChecksum = ""
if (Test-Path -Path "$Path/rke2.$suffix-$CommitHash.tar.gz" -PathType Leaf) {
# TODO: Select-String
$expectedBinaryAirgapChecksum = Find-Checksum -ChecksumFilePath $DestinationPath -Pattern "rke2.$suffix.tar.gz"
}
return @{ ExpectedBinaryChecksum = $expectedBinaryChecksum; ExpectedBinaryAirgapChecksum = $expectedBinaryAirgapChecksum }
}
else {
Copy-Item -Path "$Path/sha256sum-$arch.txt" -Destination $DestinationPath -Force
$expectedBinaryChecksum = Find-Checksum -ChecksumFilePath $DestinationPath -Pattern "rke2.$suffix.tar.gz"
$expectedBinaryAirgapChecksum = ""
if (Test-Path -Path "$Path/rke2.$suffix.tar.gz" -PathType Leaf) {
# TODO: Select-String
$expectedBinaryAirgapChecksum = Find-Checksum -ChecksumFilePath $DestinationPath -Pattern "rke2.$suffix.tar.gz"
}
return @{ ExpectedBinaryChecksum = $expectedBinaryChecksum; ExpectedBinaryAirgapChecksum = $expectedBinaryAirgapChecksum }
}
return @{ ExpectedBinaryChecksum = ""; ExpectedBinaryAirgapChecksum = "" }
}
function Copy-LocalImageChecksums() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$Path,
[Parameter()]
[String]
$DestinationPath
)
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$arch = $archInfo.Arch
if ($CommitHash) {
if (Test-Path -Path "$Path/rke2-images.$suffix-$CommitHash.tar.zst" -PathType Leaf) {
$expectedImageAirgapChecksum = ""
$expectedImageChecksum = Find-Checksum -ChecksumFilePath $DestinationPath -Pattern "rke2-images.$suffix.tar.zst"
Write-InfoLog "staging local image checksum from $Path/rke2-images.$suffix-$CommitHash.tar.zst.sha256sum"
Copy-Item -Path "$Path/rke2-images.$suffix-$CommitHash.tar.zst.sha256sum" -Destination $DestinationPath -Force
$expectedImageAirgapChecksum = Find-Checksum -ChecksumFilePath $TempImageChecksums -Pattern "rke2-images.$suffix.tar.zst"
return @{ ExpectedImageChecksum = $expectedImageChecksum ; ExpectedImageAirgapChecksum = $expectedImageAirgapChecksum }
}
}
# TODO: possibly add a condition where commithash is not set and no local checksums are present
else {
if (Test-Path -Path "$Path/rke2-windows-$BuildVersion-$arch-images.tar.gz" -PathType Leaf) {
$expectedImageAirgapChecksum = ""
$expectedImageChecksum = Find-Checksum -ChecksumFilePath $DestinationPath -Pattern "rke2-windows-$BuildVersion-$arch-images.tar.gz"
Copy-Item -Path "$Path/sha256sum-$arch.txt" -Destination $DestinationPath -Force
$expectedImageAirgapChecksum = Find-Checksum -ChecksumFilePath $TempImageChecksums -Pattern "rke2-windows-$BuildVersion-$arch-images.tar.gz"
return @{ ExpectedImageChecksum = $expectedImageChecksum ; ExpectedImageAirgapChecksum = $expectedImageAirgapChecksum }
}
elseif (Test-Path -Path "$Path/rke2-windows-$BuildVersion-$arch-images.tar.zst" -PathType Leaf) {
$expectedImageAirgapChecksum = ""
$expectedImageChecksum = Find-Checksum -ChecksumFilePath $DestinationPath -Pattern "rke2-windows-$BuildVersion-$arch-images.tar.zst"
Write-Host "staging local image checksums from $Path/sha256sum-$arch.txt"
Copy-Item -Path "$Path/sha256sum-$arch.txt" -Destination $DestinationPath -Force
$expectedImageAirgapChecksum = Find-Checksum -ChecksumFilePath $TempImageChecksums -Pattern "rke2-windows-$BuildVersion-$arch-images.tar.zst"
return @{ ExpectedImageChecksum = $expectedImageChecksum ; ExpectedImageAirgapChecksum = $expectedImageAirgapChecksum }
}
}
return @{ ExpectedImageChecksum = ""; ExpectedImageAirgapChecksum = "" }
}
# stage_local_tarball stages the local airgap binary tarball.
function Copy-LocalBinaryTarball() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$Path,
[Parameter()]
[String]
$DestinationPath
)
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
if (-Not $CommitHash) {
Write-InfoLog "staging local binary tarball from $ArtifactPath/rke2.$suffix.tar.gz"
Copy-Item -Path "$Path/rke2.$suffix.tar.gz" -Destination $DestinationPath -Force
}
elseif (Test-Path -Path "$Path/rke2.$suffix-$CommitHash.tar.zst" -PathType Leaf) {
Write-InfoLog "staging local binary tarball from $ArtifactPath/rke2.$suffix-$CommitHash.tar.zst"
Copy-Item -Path "$Path/rke2.$suffix-$CommitHash.tar.zst" -Destination $DestinationPath -Force
]
}
}
# stage_local_airgap_tarball stages the local checksum hash for validation.
function Copy-LocalAirgapTarball() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$Path,
[Parameter()]
[String]
$DestinationPath
)
$archInfo = Get-ArchitectureInfo
$arch = $archInfo.Arch
$suffix = $archInfo.Suffix
if (-Not $CommitHash) {
if (Test-Path -Path "$Path/rke2-windows-$BuildVersion-$arch-images.tar.zst" -PathType Leaf) {
Write-InfoLog "staging local zst airgap image tarball from $Path/rke2-windows-$BuildVersion-$arch-images.tar.zst"
Copy-Item -Path "$Path/rke2-windows-$BuildVersion-$arch-images.tar.zst" -Destination $DestinationPath -Force
}
elseif (Test-Path -Path "$Path/rke2-windows-$BuildVersion-$arch-images.tar.gz" -PathType Leaf) {
Write-InfoLog "staging local gz airgap image tarball from $Path/rke2-windows-$BuildVersion-$arch-images.tar.gz"
Copy-Item -Path "$Path/rke2-windows-$BuildVersion-$arch-images.tar.gz" -Destination $DestinationPath -Force
}
}
elseif (Test-Path -Path "$Path/rke2-images.$suffix-$CommitHash.tar.zst" -PathType Leaf) {
Write-InfoLog "staging local image tarball with commit $CommitHash from $Path/rke2-images.$suffix-$CommitHash.tar.zst"
Copy-Item -Path "$Path/rke2-images.$suffix-$CommitHash.tar.zst" -Destination $DestinationPath -Force
}
}
# verify_tarball verifies the downloaded installer checksum.
function Test-TarballChecksum() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$Tarball,
[Parameter()]
[String]
$ExpectedChecksum
)
Write-InfoLog "verifying tarball"
$actualChecksum = (Get-FileHash -Path $Tarball -Algorithm SHA256).Hash.ToLower()
if ($ExpectedChecksum -ne $actualChecksum) {
Write-FatalLog "downloaded sha256 does not match expected checksum: $ExpectedChecksum, instead got: $actualChecksum"
}
}
# unpack_tarball extracts the tarball, correcting paths as necessary
function Expand-Tarball() {
[CmdletBinding()]
param (
[Parameter()]
[string]
$InstallPath,
[Parameter()]
[String]
$Tarball
)
Write-InfoLog "unpacking tarball file to $InstallPath"
New-Item -Path $InstallPath -Type Directory -Force | Out-Null
tar xzf "$Tarball" -C "$InstallPath"
}
function Find-Checksum() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$ChecksumFilePath,
[Parameter()]
[String]
$Pattern
)
try {
$matchInfo = Select-String -Path $ChecksumFilePath -Pattern $Pattern
if ($matchInfo) {
return $matchInfo.Line.Split(" ")[0]
}
return ""
}
catch {
Write-FatalLog "Checksum file wasn't found: $ChecksumFilePath"
}
}
function Test-Download {
[CmdletBinding()]
param (
[Parameter()]
[string]
$Url
)
try {
curl.exe --head -sfL $Url
return $true
}
catch {
return $false
}
}
# download_airgap_tarball downloads the airgap image tarball.
function Get-AirgapTarball() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$StorageUrl,
[Parameter()]
[String]
$TempAirgapTarball
)
if (-Not $CommitHash) {
return
}
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$arch = $archInfo.Arch
if ($CommitHash) {
$AirgapTarballUrl = "$StorageUrl/rke2-images.$suffix-$CommitHash.tar.zst"
Write-InfoLog "downloading airgap tarball with commit $CommitHash from $AirgapTarballUrl"
curl.exe -sfL $AirgapTarballUrl -o $TempAirgapTarball
}
# prepare for windows airgap image bug fix
else {
$AirgapTarballUrl = "$Rke2GitHubUrl/releases/download/$Rke2Version/rke2-windows-$BuildVersion-$arch-images.tar.gz"
Write-InfoLog "downloading airgap tarball from $AirgapTarballUrl"
curl.exe -sfL $AirgapTarballUrl -o $TempAirgapTarball
}
}
# verify_airgap_tarball compares the airgap image tarball checksum to the value
# calculated by CI when the file was uploaded.
function Test-AirgapTarballChecksum() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$ExpectedImageAirGapChecksum,
[Parameter()]
[String]
$TempAirGapTarball
)
if (-Not $CommitHash) {
return
}
if (-Not $ExpectedImageAirGapChecksum) {
return
}
Write-InfoLog "verifying airgap tarball $TempAirGapTarball"
$actualImageAirgapChecksum = (Get-FileHash -Algorithm SHA256 -Path "$TempAirGapTarball").Hash.ToLower()
if ($ExpectedImageAirGapChecksum -ne $actualImageAirgapChecksum) {
Write-FatalLog "downloaded sha256 does not match $ExpectedImageAirGapChecksum, got $actualImageAirgapChecksum"
}
}
# install_airgap_tarball moves the airgap image tarball into place.
function Install-AirgapTarball() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$InstallAgentImageDir,
[Parameter()]
[String]
$TempAirgapTarball,
[Parameter()]
[String]
$ExpectedImageAirGapChecksum,
[Parameter()]
[String]
$TempImageChecksums
)
if (-Not $CommitHash) {
return
}
if (-Not $ExpectedImageAirGapChecksum) {
return
}
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$arch = $archInfo.Arch
if ( -Not (Test-Path $InstallAgentImageDir)) {
New-Item -Path $InstallAgentImageDir -ItemType Directory | Out-null
}
if ($CommitHash) {
Write-InfoLog "installing airgap tarball with commit $CommitHash to $InstallAgentImageDir"
Move-Item -Path $TempAirgapTarball -Destination "$InstallAgentImageDir/rke2-images.$suffix-$CommitHash.tar.zst" -Force
}
# prepare for windows airgap image bug fix
else {
Write-InfoLog "installing airgap tarball to $InstallAgentImageDir"
if ([IO.Path]::GetExtension($TempAirgapTarball) -eq ".zst") {
Move-Item -Path $TempAirgapTarball -Destination "$InstallAgentImageDir/rke2-windows-$BuildVersion-$arch-images.tar.zst" -Force
}
elseif ([IO.Path]::GetExtension($TempAirgapTarball) -eq ".gz") {
Move-Item -Path $TempAirgapTarball -Destination "$InstallAgentImageDir/rke2-windows-$BuildVersion-$arch-images.tar.gz" -Force
}
}
}
# Globals
$STORAGE_URL = "https://storage.googleapis.com/rke2-ci-builds"
$INSTALL_RKE2_GITHUB_URL = "https://github.com/rancher/rke2"
Confirm-WindowsFeatures -RequiredFeatures @("Containers")
Set-Environment
Test-MethodConflict
switch ($Method) {
"tar" {
$temp = ""
if ($env:TMP) {
$temp = $env:TMP
}
elseif ($env:TEMP) {
$temp = $env:TEMP
}
if (Test-Path "$temp/rke2-install") {
Remove-Item -Path "$temp/rke2-install" -Force -Recurse
}
New-Item -Path $temp -Name rke2-install -ItemType Directory | Out-Null
$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$arch = $archInfo.Arch
$BuildVersion = Get-BuildVersion
$TMP_DIR = Join-Path -Path $temp -ChildPath "rke2-install"
$TMP_BINARY_CHECKSUMS = Join-Path -Path $TMP_DIR -ChildPath "rke2.checksums"
$TMP_BINARY_TARBALL = Join-Path -Path $TMP_DIR -ChildPath "rke2.tarball"
$TMP_AIRGAP_CHECKSUMS = Join-Path -Path $TMP_DIR -ChildPath "rke2-images.checksums"
$TMP_AIRGAP_TARBALL = Join-Path -Path $TMP_DIR -ChildPath "rke2-images.tarball"
if ($ArtifactPath) {
if ($Commit) {
$binaryChecksums = Copy-LocalBinaryChecksums -CommitHash $Commit -Path $ArtifactPath -DestinationPath $TMP_BINARY_CHECKSUMS
$imageChecksums = Copy-LocalImageChecksums -CommitHash $Commit -Path $ArtifactPath -DestinationPath $TMP_AIRGAP_CHECKSUMS
$BINARY_CHECKSUM_EXPECTED = $binaryChecksums.ExpectedBinaryAirgapChecksum
$AIRGAP_CHECKSUM_EXPECTED = $imageChecksums.ExpectedImageAirgapChecksum
Copy-LocalAirgapTarball -Path $ArtifactPath -DestinationPath $TMP_AIRGAP_TARBALL
Copy-LocalBinaryTarball -Path $ArtifactPath -DestinationPath $TMP_BINARY_TARBAL
}
else {
$binaryChecksums = Copy-LocalBinaryChecksums -Path $ArtifactPath -DestinationPath $TMP_BINARY_CHECKSUMS
$imageChecksums = Copy-LocalImageChecksums -Path $ArtifactPath -DestinationPath $TMP_AIRGAP_CHECKSUMS
$BINARY_CHECKSUM_EXPECTED = $binaryChecksums.ExpectedBinaryAirgapChecksum
$AIRGAP_CHECKSUM_EXPECTED = $imageChecksums.ExpectedImageAirgapChecksum
Copy-LocalAirgapTarball -Path $ArtifactPath -DestinationPath $TMP_AIRGAP_TARBALL
Copy-LocalBinaryTarball -Path $ArtifactPath -DestinationPath $TMP_BINARY_TARBALL
}
}
else {
$Version = Get-ReleaseVersion
Write-InfoLog "using $Version as release"
Write-InfoLog "Version: $Version `r`nStorage URL: $STORAGE_URL `r`nGithub URL: $INSTALL_RKE2_GITHUB_URL `r`nBinary Checksums: $TMP_BINARY_CHECKSUMS `r`nImage Checksums: $TMP_AIRGAP_CHECKSUMS"
$AIRGAP_CHECKSUM_EXPECTED = Get-ImageChecksums -CommitHash $Commit -StorageUrl $STORAGE_URL -Rke2Version $Version -Rke2GitHubUrl $INSTALL_RKE2_GITHUB_URL -TempImageChecksums $TMP_AIRGAP_CHECKSUMS
Get-AirgapTarball -CommitHash $Commit -StorageUrl $STORAGE_URL -TempAirgapTarball $TMP_AIRGAP_TARBALL
$BINARY_CHECKSUM_EXPECTED = Get-BinaryChecksums -CommitHash $Commit -StorageUrl $STORAGE_URL -Rke2Version $Version -Rke2GitHubUrl $INSTALL_RKE2_GITHUB_URL -TempBinaryChecksums $TMP_BINARY_CHECKSUMS
Get-BinaryTarball -CommitHash $Commit -StorageUrl $STORAGE_URL -Rke2Version $Version -Rke2GitHubUrl $INSTALL_RKE2_GITHUB_URL -TempTarball $TMP_BINARY_TARBALL
}
Test-AirgapTarballChecksum -CommitHash $Commit -ExpectedImageAirgapChecksum $AIRGAP_CHECKSUM_EXPECTED -TempAirGapTarball $TMP_AIRGAP_TARBALL
Install-AirgapTarball -CommitHash $Commit -InstallAgentImageDir $AgentImagesDir -TempAirgapTarball $TMP_AIRGAP_TARBALL -ExpectedImageAirgapChecksum $AIRGAP_CHECKSUM_EXPECTED -TempImageChecksums $TMP_AIRGAP_CHECKSUMS
Test-TarballChecksum -Tarball $TMP_BINARY_TARBALL -ExpectedChecksum $BINARY_CHECKSUM_EXPECTED
Expand-Tarball -InstallPath $TarPrefix -Tarball $TMP_BINARY_TARBALL
Write-InfoLog "install complete; you may want to run: `$env:PATH+=`";$TarPrefix\bin;C:\var\lib\rancher\rke2\bin`""
}
"choco" {
Write-FatalLog "Currently unsupported installation method. $Method will be supported soon.."
}
Default {
Write-FatalLog "Invalid installation method. $Method not supported."
}
}