Skip to content

Commit 1937604

Browse files
TinaMorTinaMor
andauthored
Fix install when version is provided with leading 'v' (#19)
- Minor fixes: 1. Confirm option not passed to uninstall commands. 2. Install-ContainerTools fails when Initialze-NatNetwork fails. Write-Error instead. Co-authored-by: TinaMor <mor.tina@outlook.com>
1 parent e83f8c5 commit 1937604

3 files changed

Lines changed: 40 additions & 24 deletions

File tree

Tests/AllToolsUtilities.Tests.ps1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ Describe "AllToolsUtilities.psm1" {
213213

214214
It "Should register services and initialize NAT network" {
215215
Install-ContainerTools `
216-
-InstallPath 'TestDrive:\Install Directory' `
217-
-Force -Confirm:$false `
218-
-RegisterServices
216+
-InstallPath 'TestDrive:\Install Directory' `
217+
-Force -Confirm:$false `
218+
-RegisterServices
219219

220220
$RegisterContainerdParams = @{
221-
ContainerdPath = "$InstallPath\Containerd"
221+
ContainerdPath = "$InstallPath\Containerd"
222222
}
223223
$RegisterBuildKitdParams = @{
224224
BuildKitPath = "$InstallPath\Buildkit"
@@ -230,5 +230,12 @@ Describe "AllToolsUtilities.psm1" {
230230
-ParameterFilter { $RegisterBuildKitdParams -and $Start -eq $true -and $Force -eq $true }
231231
Should -Invoke Initialize-NatNetwork -ModuleName 'AllToolsUtilities' -Times 1
232232
}
233+
234+
It "Should not throw an error if initializing NAT network fails" {
235+
Mock Initialize-NatNetwork -ModuleName 'AllToolsUtilities' -MockWith { throw 'Error message' }
236+
237+
{ Install-ContainerTools -InstallPath 'TestDrive:\Install Directory' -Force -Confirm:$false -RegisterServices } | Should -Not -Throw
238+
$Error[0].Exception.Message | Should -Be 'Failed to initialize NAT network. Error message'
239+
}
233240
}
234241
}

containers-toolkit/Private/CommonToolUtilities.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function Get-InstallationFile {
6464
begin {
6565
$functions = {
6666
function Receive-File ($feature) {
67-
Write-Information -InformationAction Continue -MessageData "Downloading $($feature.Feature) version $($feature.Version)"
67+
Write-Information -InformationAction Continue -MessageData "Downloading $($feature.Feature) version v$($feature.Version)"
6868
try {
6969
Invoke-WebRequest -Uri $feature.Uri -OutFile $feature.DownloadPath -UseBasicParsing
7070
}
@@ -107,7 +107,7 @@ function Get-InstallationFile {
107107
}
108108

109109
end {
110-
$jobs | Remove-Job -Force
110+
$jobs | Remove-Job -Force -ErrorAction SilentlyContinue
111111
}
112112
}
113113

containers-toolkit/Public/AllToolsUtilities.psm1

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ function Install-ContainerTools {
7272
)
7373

7474
begin {
75-
$toInstall = @("containerd", "buildkit", "nerdctl")
75+
# Strip leading "v" from version
76+
$containerdVersion = $containerdVersion.TrimStart("v")
77+
$buildKitVersion = $buildKitVersion.TrimStart("v")
78+
$nerdctlVersion = $nerdctlVersion.TrimStart("v")
79+
80+
$toInstall = @("containerd v($containerdVersion)", "buildkit v($buildKitVersion)", "nerdctl v($nerdctlVersion)")
7681
$toInstallString = $($toInstall -join ', ')
7782

7883
$WhatIfMessage = "$toInstallString will be installed"
@@ -88,18 +93,18 @@ function Install-ContainerTools {
8893

8994
process {
9095
if ($PSCmdlet.ShouldProcess($InstallPath, $WhatIfMessage)) {
91-
Write-Debug "Tools to install: $toInstallString"
96+
Write-Output "Tools to install: $toInstallString"
9297

9398
# Global Variables needed for the script
9499
$containerdTarFile = "containerd-${containerdVersion}-windows-amd64.tar.gz"
95-
$BuildKitTarFile = "buildkit-v${BuildKitVersion}.windows-amd64.tar.gz"
100+
$buildKitTarFile = "buildkit-v${buildKitVersion}.windows-amd64.tar.gz"
96101
$nerdctlTarFile = "nerdctl-${nerdctlVersion}-windows-amd64.tar.gz"
97102

98103
# Installation paths
99104
$ContainerdPath = "$InstallPath\Containerd"
100105
$BuildkitPath = "$InstallPath\Buildkit"
101106
$NerdCTLPath = "$InstallPath\nerdctl"
102-
107+
103108
$files = @(
104109
[PSCustomObject]@{
105110
Feature = "Containerd"
@@ -140,7 +145,7 @@ function Install-ContainerTools {
140145
# Uninstall if tool exists at specified location. Requires user consent
141146
if (-not (Test-EmptyDirectory -Path $params.InstallPath) ) {
142147
Write-Warning "Uninstalling $($params.Feature) from $($params.InstallPath)"
143-
Uninstall-ContainerTool -Tool $params.Feature -Path $params.InstallPath -Force $force
148+
Uninstall-ContainerTool -Tool $params.Feature -Path $params.InstallPath -Force:$force
144149
}
145150

146151
# Untar downloaded files to the specified installation path
@@ -156,8 +161,8 @@ function Install-ContainerTools {
156161

157162
if ($RegisterServices) {
158163
$RegisterParams = @{
159-
force = $force
160-
feature = $params.Feature
164+
force = $force
165+
feature = $params.Feature
161166
installPath = $params.InstallPath
162167
}
163168
Register-Service @RegisterParams
@@ -168,21 +173,25 @@ function Install-ContainerTools {
168173
}
169174
}
170175

176+
$isError = $false
171177
if ($RegisterServices) {
172-
Initialize-NatNetwork
178+
try {
179+
Initialize-NatNetwork -Force:$force -Confirm:$false
180+
}
181+
catch {
182+
$isError = $true
183+
Write-Error "Failed to initialize NAT network. $_"
184+
}
173185
}
174186
else {
175-
Write-Information -Tags "Instructions" -MessageData (
176-
"To start containderd service, run 'Start-Service containerd' or 'Start-ContainerdService',",
177-
"To start buildkitd service, run 'Start-Service buildkitd' or 'Start-BuildkitdService'"
178-
)
187+
$message = "To register containerd and buildkitd services, run the following commands:`n`tRegister-ContainerdService -ContainerdPath '$ContainerdPath' -Start`n`tRegister-BuildkitdService -BuildkitPath '$BuildkitPath' -Start"
188+
$message += "`nThen, to create a NAT network for nerdctl, run the following command:`n`tInitialize-NatNetwork"
189+
Write-Information -MessageData $message -Tags "Instructions" -InformationAction Continue
179190
}
180191

181-
Write-Information -MessageData "$($completedInstalls -join ', ') installed successfully." -Tags "Installation" -InformationAction Continue
182-
183-
$message = "To register containerd and buildkitd services, run the following commands:`n`tRegister-ContainerdService -ContainerdPath '$ContainerdPath' -Start`n`tRegister-BuildkitdService -BuildkitPath '$BuildkitPath' -Start"
184-
$message += "`nThen, to create a NAT network for nerdctl, run the following command:`n`tInitialize-NatNetwork"
185-
Write-Information -MessageData $message -Tags "Instructions" -InformationAction Continue
192+
if (!$isError) {
193+
Write-Output "$($completedInstalls -join ', ') installed successfully."
194+
}
186195
}
187196
else {
188197
# Code that should be processed if doing a WhatIf operation
@@ -194,7 +203,7 @@ function Install-ContainerTools {
194203

195204
function Uninstall-ContainerTool($Tool, $Path, $force) {
196205
$uninstallCommand = "Uninstall-$($Tool)"
197-
& $uninstallCommand -Path "$Path" -Force:$Force
206+
& $uninstallCommand -Path "$Path" -Force:$Force -Confirm:$false
198207
}
199208

200209
function Get-InstalledVersion($feature, $Latest) {

0 commit comments

Comments
 (0)