Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added update script from 1.1.1 to 1.3.0 #36

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
98 changes: 98 additions & 0 deletions update.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#region variables

$script:rancherDesktopExe = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\Rancher Desktop.exe"
abcarcol marked this conversation as resolved.
Show resolved Hide resolved
$script:rancherDesktopExeHash = "CF7E00240316A3654AB66802A8AAA281478824650C4032C1862123C317CF0885"
$script:rancherDesktopVersion = "1.1.1"
$script:windowsBinariesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\win32\bin"
abcarcol marked this conversation as resolved.
Show resolved Hide resolved
$script:linuxBinariesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\linux\bin"
abcarcol marked this conversation as resolved.
Show resolved Hide resolved
$script:rancherDesktopTargetVersion = "1.3.0"
$script:rancherDesktopInstallerName = "Rancher.Desktop.Setup.$script:rancherDesktopTargetVersion"
$script:rancherDesktopUrl = "https://github.com/rancher-sandbox/rancher-desktop/releases/download/v$script:rancherDesktopTargetVersion/$script:rancherDesktopInstallerName.exe"
$script:rancherDesktopInstallerHash = "92108CBBD8C98F99B00A608D8F7D21E12FAECA76F16890585EF212CC5BF1C779"

#endregion

#region functions

function RenameBinariesFunction
{
Write-Host "Renaming the Rancher Desktop binaries..." -ForegroundColor Blue
Rename-Item -Path "$script:windowsBinariesPath\docker.exe" -NewName dockerw.exe
Rename-Item -Path "$script:windowsBinariesPath\docker-compose.exe" -NewName dockerw-compose.exe
Copy-Item "$script:windowsBinariesPath\nerdctl.exe" "$script:windowsBinariesPath\docker.exe" -Force

Rename-Item -Path "$script:linuxBinariesPath\docker" -NewName docker.old
Copy-Item "$script:linuxBinariesPath\nerdctl" "$script:linuxBinariesPath\docker" -Force
Write-Host "Renaming done." -ForegroundColor Green
}

function UpdateRancherDesktop
{

Write-Host "Updating Rancher Desktop to version $script:rancherDesktopTargetVersion..." -ForegroundColor Blue

if(!(Test-Path -Path $script:rancherDesktopExe))
{
Write-Host "Rancher Desktop was not found in the system." -ForegroundColor Red
exit 1
}
elseif ((Get-FileHash -Algorithm SHA256 "$script:rancherDesktopExe").Hash -ne "$script:rancherDesktopExeHash")
{
Write-Host "Wrong Rancher Desktop version detected. Please make sure that the version installed is: $script:rancherDesktopVersion" -ForegroundColor Red
exit 2
}
else # All update preconditions passed
{
# Create backup of dockerd
Copy-Item "$script:windowsBinariesPath\dockerd.exe" "C:\" -Recurse -Force
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better create a temporary file (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-temporaryfile?view=powershell-7.2) and copy the file there.

Please apply also that change to installation to avoid writing directly on C: when decompressing dockerd


$BinariesRenamed = Test-Path -Path "$script:linuxBinariesPath\docker.old"

if(!(Test-Path -Path "$script:rancherDesktopInstallerName.exe") -or (Get-FileHash -Algorithm SHA256 "$script:rancherDesktopInstallerName.exe").Hash -ne "$script:rancherDesktopInstallerHash")
{
Invoke-WebRequest $script:rancherDesktopUrl -OutFile "$script:rancherDesktopInstallerName.exe"
if((Get-FileHash -Algorithm SHA256 "$script:rancherDesktopInstallerName.exe").Hash -ne "$script:rancherDesktopInstallerHash")
{
Write-Host "Checksum validation of Rancher Desktop installer failed." -ForegroundColor Red
exit 3
}
}

Invoke-Expression ".\$script:rancherDesktopInstallerName.exe"

$setupId = (Get-Process $script:rancherDesktopInstallerName).id 2> $null

Wait-Process -Id $setupId

Write-Host "Rancher Desktop successfully updated to version $script:rancherDesktopTargetVersion." -ForegroundColor Green

# Restore and remove backup of dockerd
Stop-Service -Name "docker"
Copy-Item "C:\dockerd.exe" $script:windowsBinariesPath -Recurse -Force
Remove-Item "C:\dockerd.exe"
Start-Service -Name "docker"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, this and the above assume that the user installed Windows containers support.


if ($BinariesRenamed)
{
RenameBinariesFunction
}

}

}

#endregion

#region main

# Elevate script if needed.
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" $($script:parameters)" -Verb RunAs; exit }

UpdateRancherDesktop

Write-Host "Update process finished." -ForegroundColor Green
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
Stop-Process -Force -Id $PID

#endregion