Skip to content

Commit

Permalink
Merge pull request #1461 from luthermonson/win-rancher
Browse files Browse the repository at this point in the history
Add Windows Installation for Rancher, Update existing Installer
  • Loading branch information
luthermonson authored Jul 27, 2021
2 parents ee64508 + 0fed293 commit 8fd4a51
Show file tree
Hide file tree
Showing 2 changed files with 529 additions and 42 deletions.
126 changes: 84 additions & 42 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ param (
[String]
$TarPrefix = "C:\usr\local",
[Parameter()]
[Switch]
[String]
$Commit,
[Parameter()]
[String]
Expand All @@ -79,22 +79,40 @@ param (
$ErrorActionPreference = 'Stop'

function Write-InfoLog() {
Write-Info "[INFO] " "$@"
Write-Output "[INFO] $($args -join " ")"
}

function Write-WarnLog() {
Write-Warn "[WARN] " "$@"
Write-Output "[WARN] $($args -join " ")"
}

# fatal logs the given argument at fatal log level.
function Write-FatalLog() {
Write-Output "[ERROR] " "$@"
Write-Output "[ERROR] $($args -join " ")"
if ([string]::IsNullOrEmpty($SUFFIX)) {
Write-Fatal "[ALT] Please visit 'https://github.com/rancher/rke2/releases' directly and download the latest rke2.$SUFFIX.tar.gz"
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()
{
Expand Down Expand Up @@ -130,29 +148,29 @@ function Test-MethodConflict() {
# fatal if architecture not supported.
function Get-ArchitectureInfo() {
$arch = $env:PROCESSOR_ARCHITECTURE.ToLower()
if(-Not ($arch == "amd64")) {
if("$arch" -ne "amd64") {
Write-FatalLog "unsupported architecture $(env:PROCESSOR_ARCHITECTURE)"
exit 1
}
return @{ Suffix = "windows-$(env:ARCH)"; Arch = $arch}

return @{ Suffix = "windows-$arch"; Arch = "$arch"}
}

# --- use desired rke2 version if defined or find version from channel ---
function Get-ReleaseVersion() {
$version = ""
if (-Not $Commit) {
$version = "commit $($Commit)}"
}
elseif (-Not $Version) {
$version = $Version
}
else {
Write-InfoLog "finding release for channel $($Channel)"
$versionUrl = "$INSTALL_RKE2_CHANNEL_URL}/$INSTALL_RKE2_CHANNEL"
$result = [System.Net.HttpWebRequest]::Create($versionUrl).GetResponse().ResponseUri.Segments | Select-Object -Last 1
$lastDot = $result.LastIndexOf('.')
$Version = $result.Substring(0, $lastDot)
}
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 hash from github url.
Expand All @@ -174,20 +192,22 @@ function Get-Checksums() {
[Parameter()]
[String]
$TempChecksums
)
)

$archInfo = Get-ArchitectureInfo
$suffix = $archInfo.Suffix
$suffix = $archInfo.Suffix
$arch = $archInfo.Arch
$checksumsUrl = ""
$checksumsUrl = ""

if (-Not $CommitHash) {
if ($CommitHash) {
$checksumsUrl = "$StorageUrl/rke2.$suffix$CommitHash.tar.gz.sha256sum"
}

else {
$checksumsUrl = "$Rke2GitHubUrl/releases/download/$Rke2Version/sha256sum-$arch.txt"
}
Write-InfoLog "downloading checksums at $checksumsUrl"

Write-Host "downloading checksums at $checksumsUrl"
Invoke-RestMethod -Uri $checksumsUrl -OutFile $TempChecksums
return Find-Checksum -ChecksumFilePath $TempChecksums -Pattern "rke2.$suffix.tar.gz"
}
Expand Down Expand Up @@ -217,7 +237,7 @@ function Get-Tarball() {
$suffix = $archInfo.Suffix

$tarballUrl = ""
if (-Not $CommitHash) {
if ($CommitHash) {
$tarballUrl = "$StorageUrl/rke2.$suffix$CommitHash.tar.gz"
}
else {
Expand Down Expand Up @@ -337,7 +357,6 @@ function Expand-Tarball() {
Write-InfoLog "unpacking tarball file to $InstallPath"
New-Item -Path $InstallPath -Type Directory -Force
tar xzf "$Tarball" -C "$InstallPath"
Write-InfoLog "install complete; you may want to run: `$env:PATH+=`";$INSTALL_RKE2_TAR_PREFIX\bin`""
}

function Find-Checksum() {
Expand Down Expand Up @@ -398,7 +417,7 @@ function Get-AirgapChecksums() {
$TempAirgapChecksums
)

if ($CommitHash){
if (-Not $CommitHash){
return
}

Expand All @@ -411,8 +430,8 @@ function Get-AirgapChecksums() {
$AirgapChecksumsUrl = "$StorageUrl/rke2-images.$suffix$CommitHash.tar.gz.sha256sum"
}
Write-InfoLog "downloading airgap checksums at $AirgapChecksumsUrl"
Invoke-RestMethod -Uri $AirgapChecksumsUrl -OutFile $-TempAirgapChecksums
return Find-Checksum -Path $-TempAirgapChecksums -Pattern "rke2-images.$suffix.tar"
Invoke-RestMethod -Uri $AirgapChecksumsUrl -OutFile $TempAirgapChecksums
return Find-Checksum -Path $TempAirgapChecksums -Pattern "rke2-images.$suffix.tar"
}

# download_airgap_tarball downloads the airgap image tarball.
Expand All @@ -433,7 +452,7 @@ function Get-AirgapTarball() {
$TempAirgapTarball
)

if ($CommitHash){
if (-Not $CommitHash){
return
}

Expand All @@ -455,6 +474,9 @@ function Get-AirgapTarball() {
function Test-AirgapTarballChecksum() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$ExpectedAirGapChecksum,
Expand All @@ -463,11 +485,15 @@ function Test-AirgapTarballChecksum() {
$TempAirGapTarball
)

if (-Not $CommitHash){
return
}

if ($ExpectedAirGapChecksum) {
return
}
Write-InfoLog "verifying airgap tarball"
$actualAirgapChecksum = $(sha256sum "$TempAirGapTarball" | awk '{print $1}')
Write-InfoLog "verifying airgap tarball $TempAirGapTarball"
$actualAirgapChecksum = (Get-FileHash -Algorithm SHA256 -Path "$TempAirGapTarball").Hash.ToLower()
if ($ExpectedAirGapChecksum -ne $actualAirgapChecksum) {
Write-FatalLog "download sha256 does not match $ExpectedAirGapChecksum, got $actualAirgapChecksum"
}
Expand All @@ -477,6 +503,9 @@ function Test-AirgapTarballChecksum() {
function Install-AirgapTarball() {
[CmdletBinding()]
param (
[Parameter()]
[String]
$CommitHash,
[Parameter()]
[String]
$InstallAgentImageDir,
Expand All @@ -492,7 +521,11 @@ function Install-AirgapTarball() {
[Parameter()]
[String]
$TempAirgapChecksums
)
)

if (-Not $CommitHash){
return
}

if ($ExpectedAirGapChecksum) {
return
Expand All @@ -509,7 +542,9 @@ function Install-AirgapTarball() {
$STORAGE_URL = "https://storage.googleapis.com/rke2-ci-builds"
$INSTALL_RKE2_GITHUB_URL = "https://github.com/rancher/rke2"
$DEFAULT_TAR_PREFIX = "C:\usr\local"
$INSTALL_RKE2_TAR_PREFIX = "C:\usr\local"

Confirm-WindowsFeatures -RequiredFeatures @("Containers")
Set-Environment -DefaultTarPrefix $DEFAULT_TAR_PREFIX
Test-MethodConflict

Expand All @@ -522,15 +557,19 @@ switch ($Method) {
elseif($env:TEMP){
$temp = $env:TEMP
}
New-Item -Path $temp -Name "rke2-install" -ItemType "Directory"

if (Test-Path $temp) {
Remove-Item -Path $temp -Force -Recurse
}
New-Item -Path $temp -Name "rke2-install" -ItemType "Directory"

$TMP_DIR = Join-Path -Path $temp -ChildPath "rke2-install"
$TMP_CHECKSUMS = Join-Path -Path $TMP_DIR -ChildPath "rke2.checksums"
$TMP_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 (-Not $ArtifactPath){
if ($ArtifactPath){
$checksums = Copy-LocalChecksums -Path $ArtifactPath -DestinationPath $TMP_AIRGAP_CHECKSUMS
$CHECKSUM_EXPECTED = $checksums.ExpectedChecksum
$AIRGAP_CHECKSUM_EXPECTED = $checksums.ExpectedAirgapChecksum
Expand All @@ -539,18 +578,21 @@ switch ($Method) {
Copy-LocalTarball -Path $ArtifactPath -DestinationPath $TMP_TARBAL
}
else {
Get-ReleaseVersion
Write-InfoLog "using ${Version: -commit $Commit} as release"
$Version = Get-ReleaseVersion
Write-InfoLog "using $Version as release"
$AIRGAP_CHECKSUM_EXPECTED = Get-AirgapChecksums -CommitHash $Commit -AirgapChecksumsUrl $AIRGAP_CHECKSUMS_URL -StorageUrl $STORAGE_URL -TempAirgapChecksums $TMP_AIRGAP_CHECKSUMS
Get-AirgapTarball -CommitHash $Commit -AirgapTarballUrl $AIRGAP_TARBALL_URL -StorageUrl $STORAGE_URL -TempAirgapTarball $TMP_AIRGAP_TARBALL
$CHECKSUM_EXPECTED = Get-Checksums -CommitHash $Commit -StorageUrl $STORAGE_URL -Rke2Version $Version -Rke2GitHubUrl $INSTALL_RKE2_GITHUB_URL -TempChecksums $TMP_CHECKSUMS

Write-Host $Version $Version $STORAGE_URL $INSTALL_RKE2_GITHUB_URL $TMP_CHECKSUMS
$CHECKSUM_EXPECTED = Get-Checksums -CommitHash $Commit -StorageUrl $STORAGE_URL -Rke2Version $Version -Rke2GitHubUrl $INSTALL_RKE2_GITHUB_URL -TempChecksums $TMP_CHECKSUMS
Get-Tarball -CommitHash $Commit -StorageUrl $STORAGE_URL -Rke2Version $Version -Rke2GitHubUrl $INSTALL_RKE2_GITHUB_URL -TempTarball $TMP_TARBALL
}

Test-AirgapTarballChecksum -ExpectedAirGapChecksum $AIRGAP_CHECKSUM_EXPECTED -TempAirGapTarball $TMP_AIRGAP_TARBALL
Install-AirgapTarball -InstallAgentImageDir $INSTALL_RKE2_AGENT_IMAGES_DIR -TempAirgapTarball $TMP_AIRGAP_TARBALL -ExpectedAirGapChecksum $AIRGAP_CHECKSUM_EXPECTED -AirgapTarballFormat $AIRGAP_TARBALL_FORMAT -TempAirgapChecksums $TMP_AIRGAP_CHECKSUMS
Test-AirgapTarballChecksum -CommitHash $Commit -ExpectedAirGapChecksum $AIRGAP_CHECKSUM_EXPECTED -TempAirGapTarball $TMP_AIRGAP_TARBALL
Install-AirgapTarball -CommitHash $Commit -InstallAgentImageDir $INSTALL_RKE2_AGENT_IMAGES_DIR -TempAirgapTarball $TMP_AIRGAP_TARBALL -ExpectedAirGapChecksum $AIRGAP_CHECKSUM_EXPECTED -AirgapTarballFormat $AIRGAP_TARBALL_FORMAT -TempAirgapChecksums $TMP_AIRGAP_CHECKSUMS
Test-TarballChecksum -Tarball $TMP_TARBALL -ExpectedChecksum $CHECKSUM_EXPECTED
Expand-Tarball -InstallPath $INSTALL_RKE2_TAR_PREFIX -Tarball $TMP_TARBALL
Write-InfoLog "install complete; you may want to run: `$env:PATH+=`";$INSTALL_RKE2_TAR_PREFIX\bin;C:\var\lib\rancher\rke2\bin`""
}
"choco" {
Write-FatalLog "Currently unsupported installation method. $Method will be supported soon.."
Expand Down
Loading

0 comments on commit 8fd4a51

Please sign in to comment.