From 0fe61fa1e0b41db8d05ca063fbca298f0390be1a Mon Sep 17 00:00:00 2001 From: Matt Page Date: Mon, 6 Oct 2014 14:18:34 -0700 Subject: [PATCH] fix: Respect metadata supplied for GCE builders. Previously, any per instance metadata supplied via the GCE builder was ignored. Test plan: - make test - Manual testing via: -- Created a packer config that contained a GCE builder with custom metadata set. -- Ran `packer build`. -- Verified the instance had the correct metadata in the GCE console. --- builder/googlecompute/step_create_instance.go | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/builder/googlecompute/step_create_instance.go b/builder/googlecompute/step_create_instance.go index 7e6a68973..015bcde6c 100644 --- a/builder/googlecompute/step_create_instance.go +++ b/builder/googlecompute/step_create_instance.go @@ -24,6 +24,25 @@ func (config *Config) getImage() Image { return Image{Name: config.SourceImage, ProjectId: project} } +func (config *Config) getInstanceMetadata(sshPublicKey string) map[string]string { + instanceMetadata := make(map[string]string) + + // Copy metadata from config + for k, v := range config.Metadata { + instanceMetadata[k] = v + } + + // Merge any existing ssh keys with our public key + sshMetaKey := "sshKeys" + sshKeys := fmt.Sprintf("%s:%s", config.SSHUsername, sshPublicKey) + if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists { + sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys) + } + instanceMetadata[sshMetaKey] = sshKeys + + return instanceMetadata +} + // Run executes the Packer build step that creates a GCE instance. func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*Config) @@ -39,9 +58,7 @@ func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction DiskSizeGb: config.DiskSizeGb, Image: config.getImage(), MachineType: config.MachineType, - Metadata: map[string]string{ - "sshKeys": fmt.Sprintf("%s:%s", config.SSHUsername, sshPublicKey), - }, + Metadata: config.getInstanceMetadata(sshPublicKey), Name: name, Network: config.Network, Tags: config.Tags,