Skip to content

Instantly share code, notes, and snippets.

@rleap-m
Last active October 24, 2024 19:09
Show Gist options
  • Save rleap-m/ecebfb919d199ab02fad1db4d9eb6da9 to your computer and use it in GitHub Desktop.
Save rleap-m/ecebfb919d199ab02fad1db4d9eb6da9 to your computer and use it in GitHub Desktop.
Wrapper script for 'genUrlFromVersionAndChannel' function with support for v25 versioning and resolving to latest pre-release build
<#
.SYNOPSIS
Wrapper script for 'genUrlFromVersionAndChannel' function with support for v25 versioning and resolving to latest
pre-release build
.PARAMETER DownloadUrl
Specify the URL from which to obtain the package
.PARAMETER Channel
Specify the release channel like 'test' or 'stable', etc.
.PARAMETER DockerVersion
Specify the docker version you intend to install
.PARAMETER ContainerdVersion
Specify the containerd version you intend to install
.EXAMPLE
Invoke-McrGenUrlRoutine.ps1 -DockerVersion 23.0.16 -Verbose
.EXAMPLE
Invoke-McrGenUrlRoutine.ps1 -DockerVersion 25.0.7 -Channel 'test-25.0' -Verbose
.NOTES
Changes to genUrl... function based on https://github.com/Mirantis/docker-install-ee/pull/161
#>
[CmdletBinding(PositionalBinding=$FALSE, DefaultParameterSetName = 'engine')]
param (
[string]$DownloadUrl = 'https://repos-internal.mirantis.com',
[string]$Channel = 'test',
[Parameter(ParameterSetName='engine', Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$DockerVersion,
[Parameter(ParameterSetName='containerd', Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$ContainerdVersion
)
# Making script scope variables like the parent script install.ps1 has
[string] $intDownloadUrl = $DownloadUrl
[string] $intChannel = $Channel
[string] $genUrl = ''
if ($DockerVersion) {
$version = $DockerVersion
$packageName = 'docker'
}
elseif ($ContainerdVersion) {
$version = $ContainerdVersion
$packageName = 'containerd'
}
function verboseLog {
Write-Verbose "$args"
}
function genUrlFromVersionAndChannel {
# All parameters below will always have a value due to defaults
param($pkgname, $channel, $version)
if (($pkgname -eq 'Docker') -and (!($version -match '-'))) {
Write-Verbose "Resolving latest build based on [$pkgname] version [$version]..."
$url = "$script:intDownloadUrl`/win`/static`/$script:intChannel`/x86_64`/"
$resp = Invoke-WebRequest -Uri $url -UseBasicParsing
if ($resp.StatusCode -eq 200) {
# RegEx and custom object code stolen from 'parsePackageVersions' function
$tmpPkg = $resp.Links.href | findstr $pkgname-[0-9]
$tmpPkgs = $tmpPkg.split(' ')
# old $packageRegex = '^\s*(?<package>\w*)-(?<major>\d\d?)\.(?<minor>\d\d?)\.(?<patch>\d\d?)(m(?<mver>\d))?(-(?<pre>tp|rc|beta|nightly)\.?(?<prever>\d))?\.zip(\r|\n|\r\n)*$'
$packageRegex = '^\s*(?<fullVersion>(?<major>\d\d?)\.(?<minor>\d\d?)\.(?<patch>\d\d?)(m(?<mver>\d))?((-|~+)(?<pre>tp|rc|beta|nightly|dev)\.?(?<prever>\d)?)?(\+fips)?)$'
$releases = $tmpPkgs | Select-String -CaseSensitive -Pattern $packageRegex | ForEach-Object {
[PSCustomObject]@{
name = $_.Matches.Value
package = $_.Matches[0].Groups['package'].Value
major = [int]$_.Matches[0].Groups['major'].Value
minor = [int]$_.Matches[0].Groups['minor'].Value
patch = [int]$_.Matches[0].Groups['patch'].Value
mver = [int]$_.Matches[0].Groups['mver'].Value
pre = $_.Matches[0].Groups['pre'].Value
prever = [int]$_.Matches[0].Groups['prever'].Value
}
}
$releases = $releases | Sort-Object -Property major,minor,patch,mver,@{Expression={
switch ($_.pre)
{
'' {0}
'rc' {1}
'tp' {2}
}
}; Descending=$true },prever
$semverDockerVer = [System.Version] $version
$latestBuild = $releases | Where-Object -FilterScript {
($_.major -eq $semVerDockerVer.Major) -and ($_.minor -eq $semVerDockerVer.Minor) -and ($_.patch -eq $semVerDockerVer.Build)
} | Select-Object -Last 1
if ($latestBuild) {
Write-Verbose "Resolving latest build to [$($latestBuild.name)]."
$version = ($latestBuild.name.substring(0,$latestBuild.name.LastIndexOf('.'))).Substring($pkgname.Length+1)
}
else {
Write-Warning "Unable to resolve a [$pkgname] build corresponding to version [$version]"
}
}
else {
Write-Warning "Unexpected response code [$($resp.StatusCode)] when querying [$url]"
}
}
$script:genUrl = ("$script:intDownloadUrl`/win`/static`/$channel`/x86_64`/$pkgname`-$version`.zip").ToLower()
verboseLog "genUrl: $pkgname $channel $version $script:genUrl"
}
genUrlFromVersionAndChannel -pkgname $packageName -channel $Channel -version $version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment