hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.11k stars 3.33k forks source link

Packer crash #11876

Closed js185606 closed 2 years ago

js185606 commented 2 years ago

Community Note

When filing a bug, please include the following headings if possible. Any example text in this template can be deleted.

Overview of the Issue

panic: value is not a collection goroutine 1 [running]: github.com/zclconf/go-cty/cty.Value.LengthInt({{{0x962bba8, 0xc00013c5a9}}, {0x7dfcd80, 0xc00103b370}}) github.com/zclconf/go-cty@v1.10.0/cty/value_ops.go:1087 +0x1aa github.com/zclconf/go-cty/cty.Value.AsValueMap({{{0x962bba8, 0xc00013c5a9}}, {0x7dfcd80, 0xc00103b370}}) github.com/zclconf/go-cty@v1.10.0/cty/value_ops.go:1318 +0x71 github.com/hashicorp/packer/hcl2template.(*Parser).decodeProvisioner(0x0, 0xc0010301a0, 0x0) github.com/hashicorp/packer/hcl2template/types.build.provisioners.go:110 +0x8ec github.com/hashicorp/packer/hcl2template.(*Parser).decodeBuildConfig(0xc0004e4060, 0xc0004e61a0, 0xc001022000) github.com/hashicorp/packer/hcl2template/types.build.go:168 +0x8bb github.com/hashicorp/packer/hcl2template.(*Parser).parseConfig(0xc001022000, 0xc00103f650, 0xc001022000) github.com/hashicorp/packer/hcl2template/parser.go:373 +0x1eb github.com/hashicorp/packer/hcl2template.(*PackerConfig).Initialize(0xc001022000, {0x0}) github.com/hashicorp/packer/hcl2template/parser.go:326 +0x59a github.com/hashicorp/packer/command.(*BuildCommand).RunContext(0xc000b68f60, {0x9629c60, 0xc0009a9a00}, 0xc00014e000) github.com/hashicorp/packer/command/build.go:153 +0x82 github.com/hashicorp/packer/command.(*BuildCommand).Run(0xc000b68f60, {0xc000116070, 0x3, 0x3}) github.com/hashicorp/packer/command/build.go:40 +0xc5 github.com/mitchellh/cli.(*CLI).Run(0xc000328780) github.com/mitchellh/cli@v1.1.2/cli.go:262 +0x5f8 main.wrappedMain() github.com/hashicorp/packer/main.go:262 +0xaf2 main.realMain() github.com/hashicorp/packer/main.go:49 +0xf3 main.main() github.com/hashicorp/packer/main.go:35 +0x19

packer build -on-error="ask" -only="test.googlecompute.rocky8" testing/packer/linux/

!!!!!!!!!!!!!!!!!!!!!!!!!!! PACKER CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Packer crashed! This is always indicative of a bug within Packer. A crash log has been placed at "crash.log" relative to your current working directory. It would be immensely helpful if you could please report the crash with Packer1 so that we can fix this.

!!!!!!!!!!!!!!!!!!!!!!!!!!! PACKER CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Reproduction Steps

Added rocky linux source to existing packer build config

Packer version

Packer v1.8.0

Simplified Packer Template

``` build { name = "test" source "source.googlecompute.centos7" { image_name = "dbk-centos7-base-testing-v${formatdate("YYYYMMDDhhmmss", timestamp())}" image_description = "NCR Digital Banking CentOS 7 Base Test Image" image_family = "dbk-centos-7-base-testing" machine_type = "n1-standard-2" image_labels = var.centos7_labels source_image_project_id = [var.project_id] region = var.region zone = var.zone network_project_id = var.network_project_id subnetwork = var.subnetwork disk_size = var.disk_size service_account_email = var.service_account_email account_file = var.account_file communicator = "ssh" ssh_username = "packer" use_iap = true use_internal_ip = true use_os_login = false omit_external_ip = true skip_create_image = var.skip_create_image scopes = ["https://www.googleapis.com/auth/cloud-platform"] metadata = { enable-guest-attributes = "TRUE", enable-osconfig = "TRUE", enable-oslogin = "FALSE", } } source "source.googlecompute.centos8" { image_name = "dbk-centos8-base-testing-v${formatdate("YYYYMMDDhhmmss", timestamp())}" image_description = "NCR Digital Banking CentOS 8 Base Test Image" image_family = "dbk-centos-8-base-testing" machine_type = "n1-standard-2" image_labels = var.centos8_labels source_image_project_id = [var.centos_source_image_project_id] region = var.region zone = var.zone network_project_id = var.network_project_id subnetwork = var.subnetwork disk_size = var.disk_size service_account_email = var.service_account_email account_file = var.account_file communicator = "ssh" ssh_username = "packer" use_iap = true use_internal_ip = true use_os_login = false omit_external_ip = true skip_create_image = var.skip_create_image scopes = ["https://www.googleapis.com/auth/cloud-platform"] metadata = { enable-guest-attributes = "TRUE", enable-osconfig = "TRUE", enable-oslogin = "FALSE", } } source "source.googlecompute.rocky8" { image_name = "dbk-rocky8-base-testing-v${formatdate("YYYYMMDDhhmmss", timestamp())}" image_description = "NCR Digital Banking Rocky 8 Base Test Image" image_family = "dbk-rocky-8-base-testing" machine_type = "n1-standard-2" image_labels = var.rocky8_labels source_image_project_id = [var.rocky_source_image_project_id] region = var.region zone = var.zone network_project_id = var.network_project_id subnetwork = var.subnetwork disk_size = var.disk_size service_account_email = var.service_account_email account_file = var.account_file communicator = "ssh" ssh_username = "packer" use_iap = true use_internal_ip = true use_os_login = false omit_external_ip = true skip_create_image = var.skip_create_image scopes = ["https://www.googleapis.com/auth/cloud-platform"] metadata = { enable-guest-attributes = "TRUE", enable-osconfig = "TRUE", enable-oslogin = "FALSE", } } source "source.googlecompute.redhat7" { image_name = "dbk-rhel7-base-testing-v${formatdate("YYYYMMDDhhmmss", timestamp())}" image_description = "NCR Digital Banking RedHat 7 Base Test Image" image_family = "dbk-redhat7-base-testing" machine_type = "n1-standard-2" source_image_project_id = [var.project_id] image_labels = var.rhel7_labels region = var.region zone = var.zone network_project_id = var.network_project_id subnetwork = var.subnetwork disk_size = var.disk_size service_account_email = var.service_account_email account_file = var.account_file communicator = "ssh" ssh_username = "packer" use_iap = true use_internal_ip = true use_os_login = false omit_external_ip = true skip_create_image = var.skip_create_image scopes = ["https://www.googleapis.com/auth/cloud-platform"] metadata = { enable-guest-attributes = "TRUE", enable-osconfig = "TRUE", enable-oslogin = "FALSE", } } provisioner "shell" { inline = [ "sudo mkdir /ansible", "sudo chown -R packer:packer /ansible", "sudo yum update -y", "sudo yum -y install epel-release", "sudo yum install python3-pip -y", "sudo pip3 install --upgrade pyyaml==5.4.1", "sudo yum -y install ansible", "sudo install -d -o ${build.User} ${local.packer_work_dir} ${local.ansible_staging_dir}", "sudo systemctl stop firewalld.service", "sudo systemctl disable firewalld.service", "ansible-galaxy collection install community.general", "ansible-galaxy collection install google.cloud", "ansible-galaxy collection install ansible.posix", ] override = { centos8 = { inline = [ "sudo yum -y install epel-release", "sudo yum install python38-pip -y", "sudo yum install python38-pyyaml -y", "sudo yum -y install ansible", "sudo install -d -o packer ${local.packer_work_dir} ${local.ansible_staging_dir}", "sudo systemctl stop firewalld.service", "sudo systemctl disable firewalld.service", "ansible-galaxy collection install community.general", "ansible-galaxy collection install google.cloud", "ansible-galaxy collection install ansible.posix", ] } redhat6 = { inline = [ "sudo yum -y update", "sudo yum -y install epel-release", "sudo yum -y install ansible", ] } remote_folder = "/var/tmp" } } provisioner "ansible-local" { extra_arguments = [ "-vvvvv", "--extra-vars", "'INSTANCES=ungrouped group_name=SaaS-GCP-Prod-Monitored jfrog_username=${var.jfrog_username} jfrog_token=${var.jfrog_token}'", ] playbook_dir = "./ansible" playbook_file = "./ansible/playbook.yaml" staging_directory = local.ansible_staging_dir } provisioner "shell" { scripts = [ "./scripts/aidefix.sh", "./scripts/cleanup.sh", ] remote_folder = "/var/tmp" valid_exit_codes = [0, 100] } post-processor "checksum" { checksum_types = ["md5", "sha512"] } post-processor "manifest" { output = "manifest.json" strip_path = true } } ```

Operating system and Environment details

MacOS 12.2.1

Log Fragments and crash.log files

crash.log

js185606 commented 2 years ago

Upgraded to Packer 1.8.2 and Google compute plugin 1.0.11, still getting same error

lbajolet-hashicorp commented 2 years ago

Hi @js185606,

Still looking into the bug you've uncovered, it stems from the override block expecting its values to be an HCL collection, so in this case the remote_folder = "/var/tmp" in the override block of the shell provisioner is to blame for the configuration not being valid.

This is definitely a Packer bug though, as we should point out the type error instead of crashing, I'll fix this and open-up a PR to follow, hopefully this will be included in the next release.

Thanks for bringing that up!

js185606 commented 2 years ago

@lbajolet-hashicorp interesting, because that particular line has been in that template for quite some time, i even had it in multiple places (after the initial shell provisioner and both overrides) before i reduced it to just the one. is there a buffer or size limitation for HCL templates? I only ask because it seems like more than a coincidence that this happened immediately after.i added an additional source stanza, going from 3 (centos7 and 8 and rhel7) to 4 (those 3 plus the newly released Rocky Linux) so i thought maybe my template had just gotten too big, is that a possibility?

Thanks so much for getting back to me, i'll update the remote_folder and see what happens!

lbajolet-hashicorp commented 2 years ago

Yes, if you had it in both overrides this would work, as it was inside an object, therefore the casts we do without checking work in this situation.

However because they've been replaced by the other one, it does not work anymore, and instead of returning a diagnostic message, Packer simply crashes because the method we call to perform the type casting in the cty lib panics if the value is not the expected type.

The PR I opened should take care of that problem, so Packer won't crash anymore but will return an error message (which admittedly could be more accurate but with the current parsing logic that won't be possible so easily I'm afraid).

js185606 commented 2 years ago

well, i moved the remote definition, and no more crashes. Updated config:

provisioner "shell" {
    remote_folder = "/var/tmp"
    inline = [
      "sudo mkdir /ansible",
      "sudo chown -R packer:packer /ansible",
      "sudo yum update -y",
      "sudo yum -y install epel-release",
      "sudo yum install python3-pip -y",
      "sudo pip3 install --upgrade pyyaml==5.4.1",
      "sudo yum -y install ansible",
      "sudo install -d -o ${build.User} ${local.packer_work_dir} ${local.ansible_staging_dir}",
      "sudo systemctl stop firewalld.service",
      "sudo systemctl disable firewalld.service",
      "ansible-galaxy collection install community.general",
      "ansible-galaxy collection install google.cloud",
      "ansible-galaxy collection install ansible.posix",
    ]
    override = {
      centos8 = {
        inline = [
          "sudo yum -y install epel-release",
          "sudo yum install python38-pip -y",
          "sudo yum install python38-pyyaml -y",
          "sudo yum -y install ansible",
          "sudo install -d -o packer ${local.packer_work_dir} ${local.ansible_staging_dir}",
          "sudo systemctl stop firewalld.service",
          "sudo systemctl disable firewalld.service",
          "ansible-galaxy collection install community.general",
          "ansible-galaxy collection install google.cloud",
          "ansible-galaxy collection install ansible.posix",
        ]
      }
      redhat6 = {
        inline = [
          "sudo yum -y update",
          "sudo yum -y install epel-release",
          "sudo yum -y install ansible",
        ]
      }
    }
  }

that might have also been why a source i specified in the override block (centos8, in this case) was for some reason executing the original shell provisioner inline commands as well as the inline commands in the override?

This is probably a given, but i thought i would also note that "packer validate" also crashed in the same way, which is i think what you are going to try and address with your PR? I'll be happy to be a guinea pig if you need one to test the build once the fix is in :)

Thanks so much!

lbajolet-hashicorp commented 2 years ago

Both paths go through the same parsing logic so yes, both commands will not crash anymore :)

Feel free to test the branch I pushed on your old config, I did that locally and it did point to the error instead of crashing, but we're never too cautious!

github-actions[bot] commented 2 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.