toowoxx / terraform-provider-packer

Terraform Provider for HashiCorp Packer
Mozilla Public License 2.0
32 stars 7 forks source link

windows - path problem when invoking packer #8

Closed domoran closed 2 years ago

domoran commented 2 years ago

Short Description: When invoking packer via terraform under windows, the path settings seem not to be inherited correctly from the terraform process to the packer process. This yields to a "docker not found in path problem" (but also other potential problems), when building packer images.

My Environment:

Steps to reproduce:

Expected behaviour:

Observed behaviour:

Error: Failed to run packer build
│
│   with packer_image.test,
│   on main.tf line 10, in resource "packer_image" "test":
│   10: resource "packer_image" "test" {
│
│ could not run packer command; output: docker.example: output will be in this color.
│
│ Build 'docker.example' errored after 491 microseconds: exec: "docker": executable file not found in %PATH%

I attached the example files and the cmd log to the issue.

main.tf.txt bug.pkr.hcl.txt log.txt

domoran commented 2 years ago

Adding the path explicitly to the packer call works around the problem:

resource "packer_image" "test" {
   environment = {
      path = "C:\\Program Files\\Docker\\Docker\\resources\\bin"
   }
   file = "bug.pkr.hcl"
}
simaotwx commented 2 years ago

Thanks for your report. Please try the keep_environment attribute. If you set it to true, all env variables should get passed through.

domoran commented 2 years ago

Yes, that could serve as a workaround. However this "keep_environment" variable does exist and is turned off by default for a reason - in that case the packer provider does not work as expected, because it assumes packer to be on the path but allows the PATH to be empty.

So my suggestion would be to set sane defaults for PATH and for "TMP" so, that the provider works as exepected. Alternatively we could also put a big hint in the docs to use the keep_environment variable under windows. Not sure how the behaviour under linux is?

What do you think?

simaotwx commented 2 years ago

... in that case the packer provider does not work as expected, because it assumes packer to be on the path but allows the PATH to be empty

In cases where Packer doesn't need external tools (which is the case for most if not all cloud deployments), having no environment passed through works fine.

... So my suggestion would be to set sane defaults for PATH and for "TMP" so, that the provider works as exepected.

That's a good suggestion but the problem here is that it's difficult to really determine for all platforms which environment variables are considered "sane" defaults and which are not.

Not sure how the behaviour under linux is?

While Linux would be able to have the same problem, it appears as though usually Windows users are affected.

What do you think?

I think the best solution is to set keep_environment to true by default and allow turning it off in case it's necessary.

simaotwx commented 2 years ago

Coming back to another point

in that case the packer provider does not work as expected, because it assumes packer to be on the path

Packer is embedded in the provider itself so Packer does not actually need to be in the path. And even if it is, it is not used.