toowoxx / terraform-provider-packer

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

additional_params option doesn't pass the parameters correctly to Packer #15

Closed fwolfheimer closed 10 months ago

fwolfheimer commented 10 months ago

Describe the bug It seems that whenever additional parameters are specified in the packer_image resource Packer errors out (it's printing its help output instead of creating the image).

To Reproduce main.tf:

terraform {
  required_version = ">= 1.5.7"

  required_providers {
    packer = {
      source = "toowoxx/packer"
      version = "~> 0.15.0"
    }
  }
}

provider "packer" {}

data "packer_version" "version" {}

data "packer_files" "config" {
  file = "${path.module}/config.pkr.hcl"
}

resource "packer_image" "image" {
  file = data.packer_files.config.file
  directory = dirname(data.packer_files.config.file)
  additional_params = ["-parallel-builds=1"]
  force = true

  variables = {
    image_name = "ubuntu:latest"  
  }

  triggers = {
    packer_version = data.packer_version.version.version
    files_hash     = data.packer_files.config.files_hash
  }
}

config.pkr.hcl:

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

variable "image_name" {
type = string
}

# Pull the pre-built image from Dockerhub
source "docker" "image" {
  image  = var.image_name
  commit = true
}

# Cache the image in own repo
build {
  sources = ["source.docker.image"]

  post-processors {

    post-processor "docker-tag" {
      repository = "my-repo/my-image"
      tags       = ["latest"]
    }

  }
}

Expected behavior I expect that Packer builds the image.

Additional information (please complete the following information): The additional parameters are placed incorrectly on the Packer command line. The error message when trying to run terraform init and terraform apply on the minimal example above shows the following Packer command line in the output:

packer_image.image: Creating...
╷
│ Warning: Failed to run command /tmp/felix_wolfheimer/devbox-manager/registry.terraform.io/toowoxx/packer/0.15.0/linux_amd64/terraform-provider-packer_v0.15.0 build -var image_name=ubuntu:latest -force ./config.pkr.hcl -parallel-builds=1

The additional parameter -parallel-builds=1 is placed after the template file on the command line. However, Packer requires that the template file is the last argument on the command line. The error seems to be here: https://github.com/toowoxx/terraform-provider-packer/blob/main/provider/resource_packer_image.go#L189-L190 . These two lines need to be swapped.

simaotwx commented 10 months ago

Thanks for reporting. Fixed in 0.15.1. Please test and let me know.

fwolfheimer commented 10 months ago

Yes, 0.15.1 fixed the problem. Thanks a lot for the lightning-fast fix!

simaotwx commented 10 months ago

Glad to hear that. You're welcome!