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

Add support for packer init with multiple .hcl files #1271

Open
rhys96 opened this issue Apr 22, 2023 · 1 comment · Fixed by NefixEstrada/terratest#1 · May be fixed by #1336
Open

Add support for packer init with multiple .hcl files #1271

rhys96 opened this issue Apr 22, 2023 · 1 comment · Fixed by NefixEstrada/terratest#1 · May be fixed by #1336
Labels
enhancement New feature or request

Comments

@rhys96
Copy link

rhys96 commented Apr 22, 2023

When the packer configuration is separated across multiple .hcl files, Terratest does not initialise packer when Template in the packer.Options is set to ".". To get Terratest to initialise packer, I have to set Template to the path of a .hcl file.

Is there an additional setting I am missing from my packer options? Or, is a modification to terratest required?

packerOptions := &packer.Options{
  Template: ".",

  WorkingDir: ".",
}
@rhys96
Copy link
Author

rhys96 commented Apr 22, 2023

Example

Create the packer configuration.

/build.pkr.hcl

build {
  sources = [
    "source.amazon-ebs.ubuntu"
  ]

  provisioner "shell" {
    inline = [
      "cloud-init status --wait",
    ]
  }

  provisioner "shell" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get install -y apache2",
      "echo \"<html><body><h1>Hello World!</h1></body></html>\" | sudo tee /var/www/html/index.html",
      "sudo systemctl restart apache2"
    ]
  }
}

/sources.pkr.hcl

locals {
  name = "ubuntu-${formatdate("YYYY-MM-DD-hh-mm", timestamp())}"
}

data "amazon-ami" "ubuntu" {
  most_recent = true

  filters = {
    name = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"
    virtualization-type = "hvm"
  }

  owners = ["099720109477"] # Canonical
}


source "amazon-ebs" "ubuntu" {
  ami_name                = "${local.name}"
  ami_description         = "Managed by Packer"
  ami_virtualization_type = "hvm"

  source_ami    = data.amazon-ami.ubuntu.id
  instance_type = "t3.micro"
  
  communicator = "ssh"
  ssh_username = "ubuntu"

  snapshot_tags = {
    Name = "${local.name}-snapshot"
  }

  tags = {
    "Name" = "${local.name}"
  }
}

Create a test file based on terraform_packer_example_test.go and make the following edit:

/packer_test.go

  // omitted...
  packerOptions := &packer.Options{
    Template: ".",

    WorkingDir: workingDir,
  }
  // omitted...

Then run terratest.

go test -v ./packer_test.go

<omitted output>
TestMultipleStagesExample 2023-04-22T15:33:15+01:00 logger.go:66: Running Packer to generate a custom artifact for template .
TestMultipleStagesExample 2023-04-22T15:33:15+01:00 logger.go:66: Creating a temporary directory for Packer plugins
TestMultipleStagesExample 2023-04-22T15:33:15+01:00 logger.go:66: Running command packer with args [-version]
TestMultipleStagesExample 2023-04-22T15:33:15+01:00 logger.go:66: 1.8.6
TestMultipleStagesExample 2023-04-22T15:33:16+01:00 logger.go:66: Skipping 'packer init' because it is only supported for HCL2 templates
<ommited output>

You can see in the console output that packer init is skipped because it states it is only supported for HCL2 templates.

Proposed Solution

If there isn't an option I've missed in the docs. I'd like to propose defaulting to initialising packer if Template is not provided in the Options. Instead, terratest would be configured to run against the working directory. For example:

 packerOptions := &packer.Options{
    WorkingDir: workingDir,
  }

I believe this solution would still be backwards compatible because a user could still provide an argument for Template, which could continue to have the same behaviour.

@denis256 denis256 added the enhancement New feature or request label Apr 30, 2023
@NefixEstrada NefixEstrada linked a pull request Sep 4, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants