Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerShell at Windows container ignores errors #137

Open
nbyavuz opened this issue Jan 25, 2023 · 0 comments
Open

PowerShell at Windows container ignores errors #137

nbyavuz opened this issue Jan 25, 2023 · 0 comments
Labels

Comments

@nbyavuz
Copy link

nbyavuz commented Jan 25, 2023

Hi Packer team,

Overview of the Issue

PowerShell provisioner ignores some type of errors although $ErrorActionPreference = 'Stop' is set and there is [INFO] 259 bytes written for 'stderr' log when docker builder and windows container is used. There is a related issue on PowerShell provisioner hashicorp/packer#4916, however this issue can be solved by putting $ErrorActionPreference = 'Stop' to the top of the scripts(like in the simplified packer buildfile) when googlecompute builder is used but same solution doesn't work for docker builder.

Helpful(?) notes

When I tried to replicate what docker provisioner does by running same command(docker exec -i ${container.id} powershell (${command})) from my local, it shows the same output and sets $LastExitCode to 1 on my local. So, I thought there could be some issues when checking errors after running commands on powershell.

Reproduction Steps

Run packer build.

Plugin and Packer version

packer: 1.8.4
plugin: 1.0.8

Simplified Packer Buildfile

packer {
  required_plugins {
    docker = {
      version = "1.0.8"
      source  = "github.com/hashicorp/docker"
    }
  }
}

source "docker" "windows" {
  image = "docker.io/mcr.microsoft.com/windows/servercore:ltsc2022"
  windows_container = true
  discard = true
}

build {
  name = "windows_image"
  sources = ["source.docker.windows"]

  provisioner "powershell" {
    inline = [
        "$ErrorActionPreference = 'Stop'",
        "echo 'Will throw an error'",
        "throw 'cmdfail'",
    ]
  }
}

Operating system and Environment details

Windows 11 Laptop, Docker version 20.10.22, build 3a2c30b

Log Fragments and crash.log files

==> windows_image.docker.windows: Using dockerWindowsContainer communicator to connect:
2023/01/25 15:34:10 packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe plugin: 2023/01/25 15:34:10 Running the provision hook
2023/01/25 15:34:10 [INFO] (telemetry) Starting provisioner powershell
==> windows_image.docker.windows: Provisioning with Powershell...
2023/01/25 15:34:10 packer.exe plugin: Found command: $ErrorActionPreference = 'Stop'
2023/01/25 15:34:10 packer.exe plugin: Found command: echo 'Will throw an error'
2023/01/25 15:34:10 packer.exe plugin: Found command: throw 'cmdfail'
==> windows_image.docker.windows: Provisioning with powershell script: C:\Users\NAZIRY~1\AppData\Local\Temp\powershell-provisioner133813514
2023/01/25 15:34:10 packer.exe plugin: Opening C:\Users\NAZIRY~1\AppData\Local\Temp\powershell-provisioner133813514 for reading
2023/01/25 15:34:10 packer.exe plugin: Uploading env vars to c:/Windows/Temp/packer-ps-env-vars-63d121c2-c933-ee29-115f-58428ec45e10.ps1
2023/01/25 15:34:10 packer.exe plugin: [INFO] 69 bytes written for 'uploadData'
2023/01/25 15:34:10 [INFO] 69 bytes written for 'uploadData'
2023/01/25 15:34:10 packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe plugin: 2023/01/25 15:34:10 Executing docker exec -i 9cc26487e49e0bbcc84f47406a4dbca0ee2268a0477e7362bd12d7172bd7b462 powershell (Copy-Item -Path c:/packer-files/upload2656082275 -Destination c:/Windows/Temp/packer-ps-env-vars-63d121c2-c933-ee29-115f-58428ec45e10.ps1):
2023/01/25 15:34:12 packer.exe plugin: [INFO] 75 bytes written for 'uploadData'
2023/01/25 15:34:12 [INFO] 75 bytes written for 'uploadData'
2023/01/25 15:34:12 packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe plugin: 2023/01/25 15:34:12 Executing docker exec -i 9cc26487e49e0bbcc84f47406a4dbca0ee2268a0477e7362bd12d7172bd7b462 powershell (Copy-Item -Path c:/packer-files/upload4134780674 -Destination c:/Windows/Temp/script-63d121c2-9cd1-0134-9dcc-b34b157990b4.ps1):
2023/01/25 15:34:13 packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe plugin: 2023/01/25 15:34:13 Executing docker exec -i 9cc26487e49e0bbcc84f47406a4dbca0ee2268a0477e7362bd12d7172bd7b462 powershell (powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-63d121c2-c933-ee29-115f-58428ec45e10.ps1; &'c:/Windows/Temp/script-63d121c2-9cd1-0134-9dcc-b34b157990b4.ps1'; exit $LastExitCode }"):
==> windows_image.docker.windows: cmdfail
==> windows_image.docker.windows: At C:\Windows\Temp\script-63d121c2-9cd1-0134-9dcc-b34b157990b4.ps1:3 char:1
==> windows_image.docker.windows: + throw 'cmdfail'
==> windows_image.docker.windows: + ~~~~~~~~~~~~~~~
==> windows_image.docker.windows:     + CategoryInfo          : OperationStopped: (cmdfail:String) [], RuntimeEx
==> windows_image.docker.windows:    ception
==> windows_image.docker.windows:     + FullyQualifiedErrorId : cmdfail
==> windows_image.docker.windows:
    windows_image.docker.windows: Will throw an error
2023/01/25 15:34:16 packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe plugin: 2023/01/25 15:34:16 [INFO] RPC endpoint: Communicator ended with: 0
2023/01/25 15:34:16 [INFO] 259 bytes written for 'stderr'
2023/01/25 15:34:16 [INFO] 21 bytes written for 'stdout'
2023/01/25 15:34:16 [INFO] RPC client: Communicator ended with: 0
2023/01/25 15:34:16 [INFO] RPC endpoint: Communicator ended with: 0
2023/01/25 15:34:16 packer.exe plugin: [INFO] 21 bytes written for 'stdout'
2023/01/25 15:34:16 packer.exe plugin: [INFO] 259 bytes written for 'stderr'
2023/01/25 15:34:16 packer.exe plugin: [INFO] RPC client: Communicator ended with: 0
2023/01/25 15:34:16 packer.exe plugin: c:/Windows/Temp/script-63d121c2-9cd1-0134-9dcc-b34b157990b4.ps1 returned with exit code 0
2023/01/25 15:34:16 packer.exe plugin: [INFO] 511 bytes written for 'uploadData'
2023/01/25 15:34:16 [INFO] 511 bytes written for 'uploadData'
2023/01/25 15:34:16 packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe plugin: 2023/01/25 15:34:16 Executing docker exec -i 9cc26487e49e0bbcc84f47406a4dbca0ee2268a0477e7362bd12d7172bd7b462 powershell (Copy-Item -Path c:/packer-files/upload1183844614 -Destination c:/Windows/Temp/packer-cleanup-63d121c2-4108-6f8d-01f7-cf6ac4f19e82.ps1):
2023/01/25 15:34:17 packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe plugin: 2023/01/25 15:34:17 Executing docker exec -i 9cc26487e49e0bbcc84f47406a4dbca0ee2268a0477e7362bd12d7172bd7b462 powershell (powershell -executionpolicy bypass "& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. c:/Windows/Temp/packer-ps-env-vars-63d121c2-c933-ee29-115f-58428ec45e10.ps1; &'c:/Windows/Temp/packer-cleanup-63d121c2-4108-6f8d-01f7-cf6ac4f19e82.ps1'; exit $LastExitCode }"):
2023/01/25 15:34:19 packer-plugin-docker_v1.0.8_x5.0_windows_amd64.exe plugin: 2023/01/25 15:34:19 [INFO] RPC endpoint: Communicator ended with: 0
2023/01/25 15:34:19 [INFO] 0 bytes written for 'stderr'
2023/01/25 15:34:19 [INFO] 0 bytes written for 'stdout'
2023/01/25 15:34:19 [INFO] RPC client: Communicator ended with: 0
2023/01/25 15:34:19 [INFO] RPC endpoint: Communicator ended with: 0
2023/01/25 15:34:19 packer.exe plugin: [INFO] 0 bytes written for 'stdout'
2023/01/25 15:34:19 packer.exe plugin: [INFO] 0 bytes written for 'stderr'
2023/01/25 15:34:19 packer.exe plugin: [INFO] RPC client: Communicator ended with: 0
2023/01/25 15:34:19 [INFO] (telemetry) ending powershell
==> windows_image.docker.windows: Killing the container: 9cc26487e49e0bbcc84f47406a4dbca0ee2268a0477e7362bd12d7172bd7b462
2023/01/25 15:34:19 [INFO] (telemetry) ending docker.windows
Build 'windows_image.docker.windows' finished after 15 seconds 471 milliseconds.
==> Wait completed after 15 seconds 471 milliseconds

==> Builds finished. The artifacts of successful builds are:
==> Wait completed after 15 seconds 471 milliseconds
@nbyavuz nbyavuz added the bug label Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant