Skip to content

Commit

Permalink
Merge pull request #122 from phillipsj/feature/suc
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipsj authored Jun 18, 2022
2 parents 8abecf4 + 65c7ba8 commit 95afccf
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package/windows/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 3 additions & 0 deletions scripts/package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -52,3 +54,4 @@ if ($LASTEXITCODE -ne 0) {
}

Write-Host "Built $IMAGE`n"
Set-Location -Path $SRC_PATH
135 changes: 135 additions & 0 deletions suc/run.ps1
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 95afccf

Please sign in to comment.