From 65c7ba87c3de86b101cc329aff3d2795af9940b1 Mon Sep 17 00:00:00 2001 From: Jamie Phillips Date: Mon, 13 Jun 2022 10:50:29 -0400 Subject: [PATCH] Created system-upgrade-controller image and script. The script supports traditional way that wins can upgrade itself and host process containers. Signed-off-by: Jamie Phillips --- package/windows/Dockerfile | 5 +- scripts/package.ps1 | 3 + suc/run.ps1 | 135 +++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 suc/run.ps1 diff --git a/package/windows/Dockerfile b/package/windows/Dockerfile index 6fba9b95..4ef03b1d 100644 --- a/package/windows/Dockerfile +++ b/package/windows/Dockerfile @@ -9,6 +9,9 @@ SHELL ["powershell", "-NoLogo", "-Command", "$ErrorActionPreference = 'Stop'; $P RUN New-Item -ItemType SymbolicLink -Target "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Path "C:\Windows\System32\WindowsPowerShell\v1.0\pwsh.exe" COPY ./wins.exe /Windows/ +COPY ./wins.exe wins.exe +COPY ./install.ps1 install.ps1 +COPY ./run.ps1 run.ps1 #USER ContainerAdministrator -CMD ["wins.exe", "--version"] +ENTRYPOINT [ "powershell", "-Command", "./run.ps1"] diff --git a/scripts/package.ps1 b/scripts/package.ps1 index b9659d9a..4ef091cb 100644 --- a/scripts/package.ps1 +++ b/scripts/package.ps1 @@ -8,6 +8,8 @@ $SRC_PATH = (Resolve-Path "$DIR_PATH\..").Path # Reference binary in ./bin/wins.exe Copy-Item -Force -Path $SRC_PATH\bin\wins.exe -Destination $SRC_PATH\package\windows | Out-Null +Copy-Item -Force -Path $SRC_PATH\install.ps1 -Destination $SRC_PATH\package\windows | Out-Null +Copy-Item -Force -Path $SRC_PATH\suc\run.ps1 -Destination $SRC_PATH\package\windows | Out-Null Set-Location -Path $SRC_PATH\package\windows @@ -52,3 +54,4 @@ if ($LASTEXITCODE -ne 0) { } Write-Host "Built $IMAGE`n" +Set-Location -Path $SRC_PATH diff --git a/suc/run.ps1 b/suc/run.ps1 new file mode 100644 index 00000000..be1d9130 --- /dev/null +++ b/suc/run.ps1 @@ -0,0 +1,135 @@ +<# +.SYNOPSIS + Upgrades Rancher Wins. +.DESCRIPTION + Run the script to upgrade all Rancher Wins related needs. +.NOTES + +.EXAMPLE + +#> +param ( + [Parameter()] + [Switch] + $HostProcess +) + +$ErrorActionPreference = 'Stop' + +function New-Directory { + param ( + [parameter(Mandatory = $false, ValueFromPipeline = $true)] [string]$Path + ) + + if (Test-Path -Path $Path) { + if (-not (Test-Path -Path $Path -PathType Container)) { + # clean the same path file + Remove-Item -Recurse -Force -Path $Path -ErrorAction Ignore | Out-Null + } + return + } + + New-Item -Force -ItemType Directory -Path $Path | Out-Null +} + +function Invoke-Cleanup { + param ( + [parameter(Mandatory = $false, ValueFromPipeline = $true)] [string]$Path + ) + + if (Test-Path -Path $Path) { + Remove-Item -Recurse -Force -Path $Path -ErrorAction Ignore | Out-Null + } +} + +function Start-TransferFile { + param ( + [parameter(Mandatory = $true)] [string]$Source, + [parameter(Mandatory = $true)] [string]$Destination + ) + + if (Test-Path -PathType leaf -Path $Destination) { + $destinationHasher = Get-FileHash -Path $Destination + $sourceHasher = Get-FileHash -Path $Source + if ($destinationHasher.Hash -eq $sourceHasher.Hash) { + return + } + } + + Copy-Item -Force -Path $Source -Destination $Destination | Out-Null +} + +function Invoke-WinsHostProcessUpgrade { + $tmpdirbase = "/etc/rancher/agent" + $tmpdir = Join-Path "C:\host\" -ChildPath $tmpdirbase + + New-Directory -Path $tmpdir + + Start-TransferFile -Source "C:\wins.exe" -Destination $tmpdir + Start-TransferFile -Source "C:\install.ps1" -Destination $tmpdir + + if (-Not $env:CATTLE_ROLE_WORKER) { + $env:CATTLE_ROLE_WORKER = "true" + } + + $env:CATTLE_AGENT_BINARY_LOCAL = "true" + $env:CATTLE_AGENT_BINARY_LOCAL_LOCATION = Join-Path -Path $tmpdir -ChildPath "wins.exe" + + Set-Location -Path $tmpdir + + ./install.ps1 + + Pop-Location + + Remove-Item -Path $tmpdir\wins.exe + Remove-Item -Path $tmpdir\install.ps1 + exit 0 +} + +function Invoke-WinsWinsUpgrade { + $tmpdirbase = "/etc/rancher/wins" + $tmpdir = Join-Path "C:\host\" -ChildPath $tmpdirbase + $tmpdirLocal = Join-Path "C:\" -ChildPath $tmpdirbase + $winsUpgradePath = Join-Path -Path $tmpdir -ChildPath "wins-upgrade.exe" + $winsUpgradePathLocal = Join-Path -Path $tmpdirLocal -ChildPath "wins-upgrade.exe" + + New-Directory -Path $tmpdirLocal + Copy-Item -Force -Path "C:\wins.exe" -Destination $winsUpgradePathLocal | Out-Null + + Write-Host "Transferring file to host..." + Start-TransferFile -Source "C:\wins.exe" -Destination $winsUpgradePath + + Write-Host "Checking if $($winsUpgradePath) exists" + if(Test-Path $winsUpgradePath) { + Write-Host "$($winsUpgradePath) exists..." + } + else { + Write-Host "$($winsUpgradePath) was not copied to host..." + exit 1 + } + + $winsOut = wins.exe cli prc run --path=$winsUpgradePathLocal --args="up" + Write-Host $winsOut + + #Remove-Item -Path $winsUpgradePath + + if ($winsOut -match ".* rpc error: code = Unavailable desc = transport is closing") { + Write-Host "Successfully upgraded" + exit 0 + } + elseif ($LastExitCode -ne 0) { + Write-Host "Returned exit $LastExitCode" + exit $LastExitCode + } + else { + Write-Host "Returned exit 0, but did not receive expected output from .\wins up" + exit 1 + } +} + +if($HostProcess) { + Invoke-WinsHostProcessUpgrade +} +else { + Invoke-WinsWinsUpgrade +}