Browse Source

PowerShell: fix "Nuke-Everything" failing to remove images

I noticed this error in CI:

```
20:37:25 INFO: Non-base image count on control daemon to delete is 9
20:37:25 "docker rmi" requires at least 1 argument.
20:37:25 See 'docker rmi --help'.
20:37:25
20:37:25 Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]
20:37:25
20:37:25 Remove one or more images
```

Which indicated that the PowerShell script managed to find images to delete, but
not actually passing the images to `docker rmi`.

The reason for this failing was that the script attempted to convert the
collection/array to a string, which produces;

```powershell
Write-Output $(docker images --format "{{.Repository}}:{{.ID}}" | `
>>         select-string -NotMatch "windowsservercore" | `
>>         select-string -NotMatch "nanoserver" | `
>>         select-string -NotMatch "docker" `
>>         ).ToString()
System.Object[]
```

Which, when trying to split by the chosen separator (`:`), will return the same;

```powershell
Write-Output "System.Object[]".Split(":")[0]
```

This patch:

- Adds an intermediate variable (`$allImages`) to make the code better readable
- Switches the separator to `#`, to prevent breaking on images pulled from a
  repository with a port in its name (`myregistry:5000/my/image`)
- Switches to use a comma-separated list for `-NotMatch` (for readability)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 5580b79813d3a5d2a7b83bcf1607839a5b0f468d)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 6 years ago
parent
commit
74fe2c044f
1 changed files with 6 additions and 10 deletions
  1. 6 10
      hack/ci/windows.ps1

+ 6 - 10
hack/ci/windows.ps1

@@ -136,18 +136,14 @@ Function Nuke-Everything {
             if ($(docker ps -aq | Measure-Object -line).Lines -gt 0) {
             if ($(docker ps -aq | Measure-Object -line).Lines -gt 0) {
                 docker rm -f $(docker ps -aq)
                 docker rm -f $(docker ps -aq)
             }
             }
-            $imageCount=($(docker images --format "{{.Repository}}:{{.ID}}" | `
-                            select-string -NotMatch "windowsservercore" | `
-                            select-string -NotMatch "nanoserver" | `
-                            select-string -NotMatch "docker" | `
-                            Measure-Object -line).Lines)
+
+            $allImages  = $(docker images --format "{{.Repository}}#{{.ID}}")
+            $toRemove   = ($allImages | Select-String -NotMatch "windowsservercore","nanoserver","docker")
+            $imageCount = ($toRemove | Measure-Object -line).Lines
+
             if ($imageCount -gt 0) {
             if ($imageCount -gt 0) {
                 Write-Host -Foregroundcolor green "INFO: Non-base image count on control daemon to delete is $imageCount"
                 Write-Host -Foregroundcolor green "INFO: Non-base image count on control daemon to delete is $imageCount"
-                docker rmi -f `
-                    $(docker images --format "{{.Repository}}:{{.ID}}" | `
-                            select-string -NotMatch "windowsservercore" | `
-                            select-string -NotMatch "nanoserver" | `
-                            select-string -NotMatch "docker").ToString().Split(":")[1]
+                docker rmi -f ($toRemove | Foreach-Object { $_.ToString().Split("#")[1] })
             }
             }
         } else {
         } else {
             Write-Host -ForegroundColor Magenta "WARN: Skipping cleanup of images and containers"
             Write-Host -ForegroundColor Magenta "WARN: Skipping cleanup of images and containers"