Skip to content

Commit 7c32ebe

Browse files
committed
Fix uninstall command
1 parent 92558b1 commit 7c32ebe

File tree

4 files changed

+65
-67
lines changed

4 files changed

+65
-67
lines changed

containers-toolkit/Public/BuildkitTools.psm1

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function Install-Buildkit {
4646

4747
[Parameter(ParameterSetName = 'Setup')]
4848
[Parameter(HelpMessage = "Path to Windows CNI plugin. Defaults to `$ENV:ProgramFiles\Containerd\cni")]
49-
[string]$WinCNIPath="$ENV:ProgramFiles\Containerd\cni",
49+
[string]$WinCNIPath = "$ENV:ProgramFiles\Containerd\cni",
5050

5151
[Parameter(ParameterSetName = 'Install')]
5252
[Parameter(ParameterSetName = 'Setup')]
@@ -140,7 +140,7 @@ function Install-Buildkit {
140140
}
141141
}
142142
catch {
143-
Write-Warning "Failed to registed and start Buildkitd service. $_"
143+
Write-Warning "Failed to setup Buildkitd service. $_"
144144
}
145145

146146
if ($showCommands) {
@@ -213,10 +213,10 @@ function Register-BuildkitdService {
213213
)]
214214
param(
215215
[parameter(HelpMessage = "Windows CNI plugin path. Defaults to `$ENV:ProgramFiles\Containerd\cni")]
216-
[String]$WinCNIPath= "$ENV:ProgramFiles\Containerd\cni",
216+
[String]$WinCNIPath = "$ENV:ProgramFiles\Containerd\cni",
217217

218218
[parameter(HelpMessage = "Buildkit path. Defaults to `$ENV:ProgramFiles\Buildkit")]
219-
[String]$BuildKitPath= "$ENV:ProgramFiles\Buildkit",
219+
[String]$BuildKitPath = "$ENV:ProgramFiles\Buildkit",
220220

221221
[parameter(HelpMessage = "Specify to start Buildkitd service after registration is complete")]
222222
[Switch]$Start,
@@ -285,7 +285,7 @@ function Register-BuildkitdService {
285285
Write-Debug "CNI conf path: $cniConfPath"
286286

287287
# Register buildkit service
288-
$command = "$buildkitdExecutable --register-service --debug --containerd-worker=true --containerd-cni-config-path=`"$cniConfPath`" --containerd-cni-binary-dir=`"$cniBinDir`" --service-name buildkitd"
288+
$arguments = "--register-service --debug --containerd-worker=true --containerd-cni-config-path=`"$cniConfPath`" --containerd-cni-binary-dir=`"$cniBinDir`" --service-name buildkitd"
289289
if (Test-ConfFileEmpty -Path $cniConfPath) {
290290

291291
$consent = $force
@@ -295,20 +295,16 @@ function Register-BuildkitdService {
295295

296296
if ($consent) {
297297
Write-Warning "Containerd conf file not found at $cniConfPath. Buildkit service will be registered without Containerd cni configurations."
298-
$command = "$buildkitdExecutable --register-service --debug --containerd-worker=true --service-name buildkitd"
298+
$arguments = "--register-service --debug --containerd-worker=true --service-name buildkitd"
299299
}
300300
else {
301301
Write-Error "Failed to register buildkit service. Containerd conf file not found at $cniConfPath.`n`t1. Ensure that the required CNI plugins are installed or you can install them using 'Install-WinCNIPlugin'.`n`t2. Create the file to resolve this issue .`n`t3. Rerun this command 'Register-BuildkitdService'"
302302
Throw "Failed to register buildkit service. Containerd conf file not found at $cniConfPath."
303303
}
304304
}
305305

306-
# remove the executable extension from the command
307-
$escapedPath = [regex]::Escape($buildkitdExecutable)
308-
$arguments = ($command -replace "$escapedPath", "").Trim()
309-
310306
# Register the service
311-
$output = Invoke-ExecutableCommand -Executable $buildkitdExecutable -Arguments $arguments
307+
$output = Invoke-ExecutableCommand -Executable "$buildkitdExecutable" -Arguments $arguments
312308
if ($output.ExitCode -ne 0) {
313309
Throw "Failed to register buildkitd service. $($output.StandardError.ReadToEnd())"
314310
}
@@ -386,10 +382,10 @@ function Uninstall-Buildkit {
386382

387383
process {
388384
if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) {
389-
if (Test-EmptyDirectory -Path "$path") {
390-
Write-Output "$tool does not exist at '$Path' or the directory is empty"
391-
return
392-
}
385+
# if (Test-EmptyDirectory -Path "$path") {
386+
# Write-Output "$tool does not exist at '$Path' or the directory is empty"
387+
# return
388+
# }
393389

394390
$consent = $force
395391
if (!$ENV:PESTER) {
@@ -426,23 +422,20 @@ function Uninstall-BuildkitHelper {
426422
[Switch] $Purge
427423
)
428424

429-
if (Test-EmptyDirectory -Path "$Path") {
430-
Write-Error "Buildkit does not exist at '$Path' or the directory is empty."
431-
return
432-
}
433-
434-
try {
435-
if (Test-ServiceRegistered -Service 'Buildkitd') {
436-
Stop-BuildkitdService
437-
Unregister-Buildkitd -BuildkitPath "$Path"
425+
if (-not (Test-EmptyDirectory -Path "$Path")) {
426+
try {
427+
if (Test-ServiceRegistered -Service 'Buildkitd') {
428+
Stop-BuildkitdService
429+
Unregister-Buildkitd -BuildkitPath "$Path"
430+
}
431+
}
432+
catch {
433+
Throw "Could not stop or unregister buildkitd service. $_"
438434
}
439-
}
440-
catch {
441-
Throw "Could not stop or unregister buildkitd service. $_"
442-
}
443435

444-
# Remove the folder where buildkit is installed and related folders
445-
Remove-Item -Path "$Path" -Recurse -Force
436+
# Remove the folder where buildkit is installed and related folders
437+
Remove-Item -Path "$Path" -Recurse -Force
438+
}
446439

447440
if ($Purge) {
448441
Write-Output "Purging Buildkit program data"
@@ -494,10 +487,10 @@ function Unregister-Buildkitd($buildkitPath) {
494487
}
495488

496489
# Unregister buildkit service
497-
$buildkitdExecutable = "$buildkitPath\bin\buildkitd.exe"
490+
$buildkitdExecutable = (Get-ChildItem -Path "$buildkitPath" -Recurse -Filter "buildkitd.exe").FullName | Select-Object -First 1
498491

499-
Write-Debug "Buildkitd path: $buildkitdExecutable "
500-
$output = Invoke-ExecutableCommand -Executable $buildkitdExecutable -Arguments "--unregister-service"
492+
Write-Debug "Buildkitd path: '$buildkitdExecutable'"
493+
$output = Invoke-ExecutableCommand -Executable "$buildkitdExecutable" -Arguments "--unregister-service"
501494
if ($output.ExitCode -ne 0) {
502495
Throw "Could not unregister buildkitd service. $($output.StandardError.ReadToEnd())"
503496
}

containers-toolkit/Public/ContainerNetworkTools.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ function Import-HNSModule {
383383
$ModuleName = 'HNS'
384384
# https://www.powershellgallery.com/packages/HNS/0.2.4
385385
if (Get-Module -ListAvailable -Name $ModuleName) {
386-
Import-Module -Name $ModuleName -DisableNameChecking -Force:$Force
386+
Get-Module -ListAvailable -Name "$ModuleName" | Import-Module -DisableNameChecking -Force:$force
387387
return
388388
}
389389

containers-toolkit/Public/ContainerdTools.psm1

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ function Register-ContainerdService {
242242

243243
Write-Output "Configuring containerd service"
244244

245-
Add-MpPreference -ExclusionProcess $containerdExecutable
245+
Add-MpPreference -ExclusionProcess "$containerdExecutable"
246246

247247
# Get default containerd config and write to file
248248
$containerdConfigFile = "$ContainerdPath\config.toml"
249249
Write-Debug "Containerd config file: $containerdConfigFile"
250250

251-
$output = Invoke-ExecutableCommand -Executable $containerdExecutable -Arguments "config default"
251+
$output = Invoke-ExecutableCommand -Executable "$containerdExecutable" -Arguments "config default"
252252
$output.StandardOutput.ReadToEnd() | Out-File -FilePath $containerdConfigFile -Encoding ascii -Force
253253

254254
# Check config file is not empty
@@ -260,7 +260,7 @@ function Register-ContainerdService {
260260
Write-Output "Review containerd configutations at $containerdConfigFile"
261261

262262
# Register containerd service
263-
$output = Invoke-ExecutableCommand -Executable $containerdExecutable -Arguments "--register-service --log-level debug --service-name containerd --log-file `"$env:TEMP\containerd.log`""
263+
$output = Invoke-ExecutableCommand -Executable "$containerdExecutable" -Arguments "--register-service --log-level debug --service-name containerd --log-file `"$env:TEMP\containerd.log`""
264264
if ($output.ExitCode -ne 0) {
265265
Throw "Failed to register containerd service. $($output.StandardError.ReadToEnd())"
266266
}
@@ -331,10 +331,10 @@ function Uninstall-Containerd {
331331

332332
process {
333333
if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) {
334-
if (Test-EmptyDirectory -Path $path) {
335-
Write-Output "$tool does not exist at $Path or the directory is empty"
336-
return
337-
}
334+
# if (Test-EmptyDirectory -Path $path) {
335+
# Write-Output "$tool does not exist at $Path or the directory is empty"
336+
# return
337+
# }
338338

339339
$consent = $force
340340
if (!$ENV:PESTER) {
@@ -372,23 +372,23 @@ function Uninstall-ContainerdHelper {
372372
[Switch] $Purge
373373
)
374374

375-
if (Test-EmptyDirectory -Path "$Path") {
376-
Write-Error "Containerd does not exist at $Path or the directory is empty."
377-
return
378-
}
375+
if (-not (Test-EmptyDirectory -Path "$Path")) {
376+
# Write-Error "Containerd does not exist at $Path or the directory is empty."
377+
# return
379378

380-
try {
381-
if (Test-ServiceRegistered -Service 'containerd') {
382-
Stop-ContainerdService
383-
Unregister-Containerd -ContainerdPath "$Path"
379+
try {
380+
if (Test-ServiceRegistered -Service 'containerd') {
381+
Stop-ContainerdService
382+
Unregister-Containerd -ContainerdPath "$Path"
383+
}
384+
}
385+
catch {
386+
Throw "Could not stop or unregister containerd service. $_"
384387
}
385-
}
386-
catch {
387-
Throw "Could not stop or unregister containerd service. $_"
388-
}
389388

390-
# Remove the folder where containerd is installed and related folders
391-
Remove-Item -Path "$Path" -Recurse -Force
389+
# Remove the folder where containerd is installed and related folders
390+
Remove-Item -Path "$Path" -Recurse -Force
391+
}
392392

393393
if ($Purge) {
394394
Write-Output "Purging Containerd program data"
@@ -417,7 +417,7 @@ function Unregister-Containerd ($containerdPath) {
417417

418418
# Unregister containerd service
419419
$containerdExecutable = (Get-ChildItem -Path "$ContainerdPath" -Recurse -Filter "containerd.exe").FullName | Select-Object -First 1
420-
$output = Invoke-ExecutableCommand -Executable $containerdExecutable -Arguments "--unregister-service"
420+
$output = Invoke-ExecutableCommand -Executable "$containerdExecutable" -Arguments "--unregister-service"
421421
if ($output.ExitCode -ne 0) {
422422
Throw "Could not unregister containerd service. $($output.StandardError.ReadToEnd())"
423423
}

containers-toolkit/Public/NerdctlTools.psm1

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,11 @@ function Uninstall-Nerdctl {
224224

225225
process {
226226
if ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, $WhatIfMessage)) {
227-
if (Test-EmptyDirectory -Path "$path") {
228-
Write-Output "$tool does not exist at '$Path' or the directory is empty"
229-
return
230-
}
227+
228+
# if (Test-EmptyDirectory -Path "$path") {
229+
# Write-Output "$tool does not exist at '$Path' or the directory is empty"
230+
# return
231+
# }
231232

232233
$consent = $force
233234
if (!$ENV:PESTER) {
@@ -239,11 +240,15 @@ function Uninstall-Nerdctl {
239240
return
240241
}
241242

243+
$executablePath = (Get-ChildItem -Path "$path" -Filter "nerdctl.exe" -Recurse | Select-Object -First 1).FullName
244+
Write-Debug "nerdctl executable path: $executablePath"
245+
242246
Write-Warning "Uninstalling preinstalled $tool at the path '$path'.`n$WhatIfMessage"
243247
try {
244-
if ($Purge) {
248+
if ($executablePath -and $Purge) {
245249
# Remove all unused images, not just dangling ones
246-
$cmdOutput = Invoke-ExecutableCommand -Executable "$path\nerdctl.exe" -Arguments "system prune --all"
250+
Write-Debug "Pruning all unused images..."
251+
$cmdOutput = Invoke-ExecutableCommand -Executable "$executablePath" -Arguments "system prune --all --force"
247252
if ($cmdOutput.ExitCode -ne 0) {
248253
Write-Warning "Couldn't prune images. $($cmdOutput.StandardError.ReadToEnd())"
249254
}
@@ -275,12 +280,12 @@ function Uninstall-NerdctlHelper {
275280
)
276281

277282
if (Test-EmptyDirectory -Path "$Path") {
278-
Write-Error "nerdctl does not exist at '$Path' or the directory is empty."
279-
return
280-
}
283+
# Write-Error "nerdctl does not exist at '$Path' or the directory is empty."
284+
# return
281285

282-
# Remove the folder where nerdctl is installed and related folders
283-
Remove-Item -Path "$Path" -Recurse -Force
286+
# Remove the folder where nerdctl is installed and related folders
287+
Remove-Item -Path "$Path" -Recurse -Force
288+
}
284289

285290
if ($Purge) {
286291
Write-Output "Purging nerdctl program data"

0 commit comments

Comments
 (0)