hashicorp / packer-plugin-googlecompute

Packer plugin for Google Compute Builder
https://www.packer.io/docs/builders/googlecompute
Mozilla Public License 2.0
24 stars 54 forks source link

builder `googlecompute` uses hostname instead of instance name to set instance metadata #9

Open ghost opened 3 years ago

ghost commented 3 years ago

This issue was originally opened by @alexfouche as hashicorp/packer#7789. It was migrated here as a result of the Packer plugin split. The original body of the issue is below.


This issue applies to the googlecompute builder

The issue appears when the hostname or FQDN of the linux instance does not match the instance name set when creating the instance. Or when hostname is modified (via a custom startup script or whatever at linux start, ...) or is set with "instance_name": in the builder json block

The builder, when instanciating a VM adds a metadata startup-script. In this script, there is a shell function SetMetadata(), which is called at the end of the startup script to set instance metadata startup-script-status=done

SetMetadata () {
  gcloud compute instances add-metadata ${HOSTNAME} --metadata ${1}=${2} --zone ${ZONE}
}

See startup.go#L27

The problem lies in the fact that ${HOSTNAME} is used. The Google instance name should be used, not the hostname.

Using the GetMetadata() function of the script, the instance name can be retrieved with

GetMetadata name
aamundsen1234 commented 3 years ago

I was running into the same issue on CentOS 8 with Packer. Setting SELinux to permissive mode in the user-supplied startup script fixed my issue and now the Packer build runs with no problems.

Just added this to the end of my startup script:

# Set SE Linux to permissive
setenforce 0