Skip to content

Commit bbb8f1e

Browse files
committed
Make reinstall check reusable
1 parent 38108f6 commit bbb8f1e

File tree

5 files changed

+125
-102
lines changed

5 files changed

+125
-102
lines changed

containers-toolkit/Private/CommonToolUtilities.psm1

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,107 @@ $HASH_FUNCTIONS_STR = $HASH_FUNCTIONS -join '|' # SHA1|SHA256|SHA384|SHA512|MD5
7272
$NERDCTL_CHECKSUM_FILE_PATTERN = "(?<hashfunction>(?:^({0})))" -f ($HASH_FUNCTIONS -join '|')
7373
$NERDCTL_FILTER_SCRIPTBLOCK_STR = { (("{0}" -match "$NERDCTL_CHECKSUM_FILE_PATTERN") -and "{0}" -notmatch ".*.asc$") }.ToString()
7474

75-
function Get-LatestToolVersion($repository) {
75+
$CONTAINERD_REPO = "containerd/containerd"
76+
$BUILDKIT_REPO = "moby/buildkit"
77+
$NERDCTL_REPO = "containerd/nerdctl"
78+
$WINCNI_PLUGIN_REPO = "microsoft/Windows-Container-Networking"
79+
$CLOUDNATIVE_CNI_REPO = "containernetworking/plugins"
80+
81+
82+
function Get-LatestToolVersion($tool) {
83+
# Get the repository based on the tool
84+
$repository = switch ($tool.ToLower()) {
85+
"containerd" { $CONTAINERD_REPO }
86+
"buildkit" { $BUILDKIT_REPO }
87+
"nerdctl" { $NERDCTL_REPO }
88+
"wincniplugin" { $WINCNI_PLUGIN_REPO }
89+
"cloudnativecni" { $CLOUDNATIVE_CNI_REPO }
90+
Default { Throw "Couldn't get latest $tool version. Invalid tool: $tool" }
91+
}
92+
93+
# Get the latest release version URL string
94+
$uri = "https://api.github.com/repos/$repository/releases/latest"
95+
96+
Write-Debug "Getting the latest $tool version from $uri"
97+
98+
# Get the latest release version
7699
try {
77-
$uri = "https://api.github.com/repos/$repository/releases/latest"
78100
$response = Invoke-WebRequest -Uri $uri -UseBasicParsing
79101
$version = ($response.content | ConvertFrom-Json).tag_name
80102
return $version.TrimStart("v")
81103
}
82104
catch {
83-
$tool = ($repository -split "/")[1]
84-
Throw "Could not get $tool latest version. $($_.Exception.Message)"
105+
Throw "Couldn't get latest $tool version from $uri. $($_.Exception.Message)"
85106
}
86107
}
87108

88-
function Test-IsLatestVersion($Tool, $Version, $LatestVersion) {
89-
$currentVersion = [System.Version]$Version
90-
$latestVersion = [System.Version]$latestVersion
109+
function Test-IsLatestVersion($Tool, $Version) {
110+
$targetVersion = [System.Version]$Version
111+
112+
# Get the latest version of the tool
113+
$latestVersion = Get-LatestToolVersion -Tool $Tool
114+
Write-Debug "Latest $Tool version: $latestVersion"
115+
$latestVersion = [System.Version](Get-LatestToolVersion -Tool $Tool)
91116

92-
$isLatest = ($currentVersion -eq $latestVersion)
117+
$isLatest = ($targetVersion -eq $latestVersion)
93118
if (-not $isLatest) {
94-
Write-Warning "A newer version of $tool is available. { Version: $currentVersion, Latest version: $latestVersion }"
119+
Write-Warning "A newer version of $tool is available. { Target Version: $targetVersion, Latest Version: $latestVersion }"
95120
}
96121
return $isLatest
97122
}
98123

124+
function Test-ToolReinstall {
125+
param(
126+
[string]$tool,
127+
[string]$targetVersion,
128+
[string]$executable
129+
)
130+
131+
Write-Debug "Tool: $tool, Target Version: $targetVersion, Executable: $executable"
132+
133+
# Check if the target version is the latest version
134+
if ($targetVersion -eq 'latest') {
135+
$targetVersion = Get-LatestToolVersion -Tool $tool
136+
}
137+
else {
138+
# Check if a newer version is available
139+
Test-IsLatestVersion -Tool "$tool" -Version $targetVersion | Out-Null
140+
}
141+
142+
# Check if the tool is already installed
143+
Write-Debug "$tool executable: $executable"
144+
$isInstalled = ($executable -and (Test-Path -Path $executable))
145+
146+
# If tool is not installed, the tool is a new installation.
147+
if (-not $isInstalled) {
148+
return $false
149+
}
150+
151+
# Check if the installed version is the same as the target version
152+
$cmdOutput = Invoke-ExecutableCommand -Executable "$executable" -Arguments "--version"
153+
if ($cmdOutput.ExitCode -ne 0) {
154+
Write-Warning "Failed to get nerdctl version: $($cmdOutput.StandardError.ReadToEnd())"
155+
return $true
156+
}
157+
158+
# Extract version from the output
159+
# containerd github.com/containerd/containerd/v2 v2.0.2 c507a0257ea6462fbd6f5ba4f5c74facb04021f4
160+
# buildkitd github.com/moby/buildkit v0.19.0 3637d1b15a13fc3cdd0c16fcf3be0845ae68f53d
161+
# nerdctl version v7.9.8
162+
$installedVersion = $cmdOutput.StandardOutput.ReadToEnd().Trim()
163+
$installedVersion = ($installedVersion.Split(' ')[2]).TrimStart('v')
164+
Write-Debug "{ Target Version: $targetVersion, Installed Version: $installedVersion }"
165+
166+
# Compare the installed version with the target version
167+
if ($targetVersion -eq $installedVersion) {
168+
Write-Warning "Installed $tool version is the same as the requested version, '$installedVersion'."
169+
} else {
170+
Write-Warning "$tool version '$installedVersion' is installed."
171+
}
172+
173+
return $true
174+
}
175+
99176
function Test-EmptyDirectory($path) {
100177
if (-not (Test-Path -Path $path)) {
101178
return $true
@@ -524,7 +601,8 @@ function Test-FileChecksum {
524601
$isValid = $downloadedChecksum.Hash -eq $checksum
525602
$found = $true
526603
return
527-
} else {
604+
}
605+
else {
528606
Write-Debug "File name does not match. {checksum file: $filename, downloaded file: $downloadedFileName}"
529607
}
530608
}
@@ -903,8 +981,10 @@ function Invoke-ExecutableCommand {
903981
}
904982

905983

984+
# Export-ModuleMember -Variable CONTAINERD_REPO, BUILDKIT_REPO, NERDCTL_REPO, WINCNI_PLUGIN_REPO, CLOUDNATIVE_CNI_REPO
906985
Export-ModuleMember -Function Get-LatestToolVersion
907986
Export-ModuleMember -Function Test-IsLatestVersion
987+
Export-ModuleMember -Function Test-ToolReinstall
908988
Export-ModuleMember -Function Get-DefaultInstallPath
909989
Export-ModuleMember -Function Test-EmptyDirectory
910990
Export-ModuleMember -Function Get-InstallationFile

containers-toolkit/Public/BuildkitTools.psm1

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ModuleParentPath = Split-Path -Parent $PSScriptRoot
1313
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1414

1515
function Get-BuildkitLatestVersion {
16-
$latestVersion = Get-LatestToolVersion -Repository "moby/buildkit"
16+
$latestVersion = Get-LatestToolVersion -Tool "buildkit"
1717
return $latestVersion
1818
}
1919

@@ -56,6 +56,8 @@ function Install-Buildkit {
5656
)
5757

5858
begin {
59+
$tool = 'Buildkit'
60+
5961
# Check if Buildkit is alread installed
6062
$blktdexe = (Get-ChildItem -Path $InstallPath -Recurse -Filter "buildkitd.exe" | Select-Object -First 1).FullName
6163
$isInstalled = ($null -ne $blktdexe)
@@ -81,44 +83,23 @@ function Install-Buildkit {
8183
}
8284
$Version = $Version.TrimStart('v')
8385

84-
# Check if a newer version is available
85-
$userVersion = $Version
86-
if ($Version -ne 'latest') {
87-
Test-IsLatestVersion -Tool 'Buildkit' -Version $Version -LatestVersion $latestVersion | Out-Null
88-
}
89-
else {
90-
$userVersion = $latestVersion
91-
}
92-
93-
# Check if tool already exists at specified location
94-
if ($isInstalled) {
95-
$errMsg = "Buildkit already exists at $InstallPath or the directory is not empty"
86+
# Check if we need to reinstall
87+
$reinstall = Test-ToolReinstall $tool $Version $blktdexe
88+
if ($reinstall) {
89+
$errMsg = "$tool already exists at $InstallPath."
9690
Write-Warning $errMsg
9791

98-
# Check if user wants to install an already installed version
99-
Write-Debug "Buildkit executable: $blktdexe"
100-
$cmdOutput = Invoke-ExecutableCommand -Executable "$blktdexe" -Arguments "--version"
101-
if ($cmdOutput.ExitCode -eq 0) {
102-
# buildkitd github.com/moby/buildkit v0.19.0 3637d1b15a13fc3cdd0c16fcf3be0845ae68f53d
103-
$blktVersion = $cmdOutput.StandardOutput.ReadToEnd().Trim()
104-
105-
# Extract version from the output
106-
$blktVersion = ($blktVersion.Split(' ')[2]).TrimStart('v')
107-
Write-Debug "{ User Version: $userVersion, Current Version: $blktVersion }"
108-
if ($userVersion -eq $blktVersion) {
109-
Write-Warning "Installed Buildkit version is the same as the requested version. Please uninstall the existing version using 'Uninstall-Containerd' or use -Force to reinstall."
110-
if (!$Force) {
111-
return
112-
}
113-
}
92+
if (!$Force) {
93+
Write-Warning "Installation cancelled. $errMsg Please uninstall the existing version using 'Uninstall-Buildkit' or use -Force to reinstall."
94+
return
11495
}
11596

11697
# Uninstall if tool exists at specified location. Requires user consent
11798
try {
118-
Uninstall-Buildkit -Path "$InstallPath" -Force:$Force -Confirm:$false | Out-Null
99+
Uninstall-Buildkit -Path "$InstallPath" -Confirm:$false -Force:$Force | Out-Null
119100
}
120101
catch {
121-
Throw "Buildkit installation failed. $_"
102+
Throw "nerdctl installation failed. $_"
122103
}
123104
}
124105

containers-toolkit/Public/ContainerNetworkTools.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ModuleParentPath = Split-Path -Parent $PSScriptRoot
1313
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1414

1515
function Get-WinCNILatestVersion {
16-
$latestVersion = Get-LatestToolVersion -Repository "microsoft/windows-container-networking"
16+
$latestVersion = Get-LatestToolVersion -Tool "WinCNIPlugin"
1717
return $latestVersion
1818
}
1919

containers-toolkit/Public/ContainerdTools.psm1

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1313

1414

1515
function Get-ContainerdLatestVersion {
16-
$latestVersion = Get-LatestToolVersion -Repository "containerd/containerd"
16+
$latestVersion = Get-LatestToolVersion -Tool "containerd"
1717
return $latestVersion
1818
}
1919

@@ -45,6 +45,8 @@ function Install-Containerd {
4545
)
4646

4747
begin {
48+
$tool = 'Containerd'
49+
4850
# Check if Containerd is alread installed
4951
$ctrexe = (Get-ChildItem -Path $InstallPath -Recurse -Filter "containerd.exe" | Select-Object -First 1).FullName
5052
$isInstalled = ($null -ne $ctrexe)
@@ -70,47 +72,26 @@ function Install-Containerd {
7072
}
7173
$Version = $Version.TrimStart('v')
7274

73-
# Check if a newer version is available
74-
$userVersion = $Version
75-
if ($Version -ne 'latest') {
76-
Test-IsLatestVersion -Tool 'Containerd' -Version $Version -LatestVersion $latestVersion | Out-Null
77-
}
78-
else {
79-
$userVersion = $latestVersion
80-
}
81-
82-
# Check if tool already exists at specified location
83-
if ($isInstalled) {
84-
$errMsg = "Containerd already exists at $InstallPath or the directory is not empty"
75+
# Check if we need to reinstall
76+
$reinstall = Test-ToolReinstall $tool $Version $ctrexe
77+
if ($reinstall) {
78+
$errMsg = "$tool already exists at $InstallPath."
8579
Write-Warning $errMsg
8680

87-
# Check if user wants to install an already installed version
88-
Write-Debug "Containerd executable: $ctrexe"
89-
$cmdOutput = Invoke-ExecutableCommand -Executable "$ctrexe" -Arguments "--version"
90-
if ($cmdOutput.ExitCode -eq 0) {
91-
# Sample: containerd github.com/containerd/containerd/v2 v2.0.2 c507a0257ea6462fbd6f5ba4f5c74facb04021f4
92-
$ctrexeVersion = $cmdOutput.StandardOutput.ReadToEnd().Trim()
93-
94-
# Extract version from the output
95-
$ctrexeVersion = ($ctrexeVersion.Split(' ')[2]).TrimStart('v')
96-
Write-Debug "{ User Version: $userVersion, Current Version: $ctrexeVersion }"
97-
98-
if ($userVersion -eq $ctrexeVersion) {
99-
Write-Warning "Installed Containerd version is the same as the requested version. Please uninstall the existing version using 'Uninstall-Containerd' or use -Force to reinstall."
100-
if (!$Force) {
101-
return
102-
}
103-
}
81+
if (!$Force) {
82+
Write-Warning "Installation cancelled. $errMsg Please uninstall the existing version using 'Uninstall-Containerd' or use -Force to reinstall."
83+
return
10484
}
10585

10686
# Uninstall if tool exists at specified location. Requires user consent
10787
try {
10888
Uninstall-Containerd -Path "$InstallPath" -Confirm:$false -Force:$Force | Out-Null
10989
}
11090
catch {
111-
Throw "Containerd installation failed. $_"
91+
Throw "nerdctl installation failed. $_"
11292
}
11393
}
94+
11495
Write-Output "Downloading and installing Containerd v$version at $InstallPath"
11596

11697
# Download files

containers-toolkit/Public/NerdctlTools.psm1

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $ModuleParentPath = Split-Path -Parent $PSScriptRoot
1212
Import-Module -Name "$ModuleParentPath\Private\CommonToolUtilities.psm1" -Force
1313

1414
function Get-NerdctlLatestVersion {
15-
$latestVersion = Get-LatestToolVersion -Repository "containerd/nerdctl"
15+
$latestVersion = Get-LatestToolVersion -Tool "nerdctl"
1616
return $latestVersion
1717
}
1818

@@ -86,6 +86,8 @@ function Install-Nerdctl {
8686
)
8787

8888
begin {
89+
$tool = 'nerdctl'
90+
8991
# Check if Containerd is alread installed
9092
$nerdctlexe = (Get-ChildItem -Path $InstallPath -Recurse -Filter "nerdctl.exe" | Select-Object -First 1).FullName
9193
$isInstalled = ($null -ne $nerdctlexe)
@@ -118,36 +120,15 @@ function Install-Nerdctl {
118120
}
119121
$Version = $Version.TrimStart('v')
120122

121-
# Check if a newer version is available
122-
$userVersion = $Version
123-
if ($Version -ne 'latest') {
124-
Test-IsLatestVersion -Tool 'nerdctl' -Version $Version -LatestVersion $latestVersion | Out-Null
125-
}
126-
else {
127-
$userVersion = $latestVersion
128-
}
129-
130-
# Check if tool already exists at specified location
131-
if ($isInstalled) {
132-
$errMsg = "nerdctl already exists at $InstallPath or the directory is not empty"
123+
# Check if we need to reinstall
124+
$reinstall = Test-ToolReinstall $tool $Version $nerdctlexe
125+
if ($reinstall) {
126+
$errMsg = "$tool already exists at $InstallPath."
133127
Write-Warning $errMsg
134128

135-
Write-Debug "nerdctl executable: $nerdctlexe"
136-
$cmdOutput = Invoke-ExecutableCommand -Executable "$nerdctlexe" -Arguments "--version"
137-
if ($cmdOutput.ExitCode -eq 0) {
138-
# Sample: nerdctl version v7.9.8
139-
$nerdctlVersion = $cmdOutput.StandardOutput.ReadToEnd().Trim()
140-
141-
# Extract version from the output
142-
$nerdctlVersion = ($nerdctlVersion.Split(' ')[2]).TrimStart('v')
143-
Write-Debug "{ User Version: $userVersion, Current Version: $nerdctlVersion }"
144-
145-
if ($userVersion -eq $nerdctlVersion) {
146-
Write-Warning "Installed nerdctl version is the same as the requested version. Please uninstall the existing version using 'Uninstall-Containerd' or use -Force to reinstall."
147-
if (!$Force) {
148-
return
149-
}
150-
}
129+
if (!$Force) {
130+
Write-Warning "Installation cancelled. $errMsg Please uninstall the existing version using 'Uninstall-Nerdctl' or use -Force to reinstall."
131+
return
151132
}
152133

153134
# Uninstall if tool exists at specified location. Requires user consent

0 commit comments

Comments
 (0)