From d7545c4ba03eae31dd3b77297b2940c94bad1a08 Mon Sep 17 00:00:00 2001 From: Tom-Byrne Date: Thu, 5 Jan 2017 02:15:16 +0000 Subject: [PATCH] support OpenStack instance metadata --- builder/openstack/builder.go | 1 + builder/openstack/run_config.go | 33 ++++++++++++------- builder/openstack/step_run_source_server.go | 2 ++ .../source/docs/builders/openstack.html.md | 5 +++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go index 7eeb18b4b..b25229325 100644 --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -90,6 +90,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe UserData: b.config.UserData, UserDataFile: b.config.UserDataFile, ConfigDrive: b.config.ConfigDrive, + InstanceMetadata: b.config.InstanceMetadata, }, &StepGetPassword{ Debug: b.config.PackerDebug, diff --git a/builder/openstack/run_config.go b/builder/openstack/run_config.go index 3f126a9c9..114936a83 100644 --- a/builder/openstack/run_config.go +++ b/builder/openstack/run_config.go @@ -2,6 +2,7 @@ package openstack import ( "errors" + "fmt" "github.com/mitchellh/packer/helper/communicator" "github.com/mitchellh/packer/template/interpolate" @@ -15,17 +16,18 @@ type RunConfig struct { SSHInterface string `mapstructure:"ssh_interface"` SSHIPVersion string `mapstructure:"ssh_ip_version"` - SourceImage string `mapstructure:"source_image"` - SourceImageName string `mapstructure:"source_image_name"` - Flavor string `mapstructure:"flavor"` - AvailabilityZone string `mapstructure:"availability_zone"` - RackconnectWait bool `mapstructure:"rackconnect_wait"` - FloatingIpPool string `mapstructure:"floating_ip_pool"` - FloatingIp string `mapstructure:"floating_ip"` - SecurityGroups []string `mapstructure:"security_groups"` - Networks []string `mapstructure:"networks"` - UserData string `mapstructure:"user_data"` - UserDataFile string `mapstructure:"user_data_file"` + SourceImage string `mapstructure:"source_image"` + SourceImageName string `mapstructure:"source_image_name"` + Flavor string `mapstructure:"flavor"` + AvailabilityZone string `mapstructure:"availability_zone"` + RackconnectWait bool `mapstructure:"rackconnect_wait"` + FloatingIpPool string `mapstructure:"floating_ip_pool"` + FloatingIp string `mapstructure:"floating_ip"` + SecurityGroups []string `mapstructure:"security_groups"` + Networks []string `mapstructure:"networks"` + UserData string `mapstructure:"user_data"` + UserDataFile string `mapstructure:"user_data_file"` + InstanceMetadata map[string]string `mapstructure:"instance_metadata"` ConfigDrive bool `mapstructure:"config_drive"` @@ -56,5 +58,14 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { errs = append(errs, errors.New("SSH IP version must be either 4 or 6")) } + for key, value := range c.InstanceMetadata { + if len(key) > 255 { + errs = append(errs, fmt.Errorf("Instance metadata key too long (max 255 bytes): %s", key)) + } + if len(value) > 255 { + errs = append(errs, fmt.Errorf("Instance metadata value too long (max 255 bytes): %s", value)) + } + } + return errs } diff --git a/builder/openstack/step_run_source_server.go b/builder/openstack/step_run_source_server.go index 599c1e998..2d02173fa 100644 --- a/builder/openstack/step_run_source_server.go +++ b/builder/openstack/step_run_source_server.go @@ -21,6 +21,7 @@ type StepRunSourceServer struct { UserData string UserDataFile string ConfigDrive bool + InstanceMetadata map[string]string server *servers.Server } @@ -65,6 +66,7 @@ func (s *StepRunSourceServer) Run(state multistep.StateBag) multistep.StepAction UserData: userData, ConfigDrive: &s.ConfigDrive, ServiceClient: computeClient, + Metadata: s.InstanceMetadata, } var serverOptsExt servers.CreateOptsBuilder diff --git a/website/source/docs/builders/openstack.html.md b/website/source/docs/builders/openstack.html.md index dcea1991b..618b280ea 100644 --- a/website/source/docs/builders/openstack.html.md +++ b/website/source/docs/builders/openstack.html.md @@ -106,6 +106,11 @@ builder. - `metadata` (object of key/value strings) - Glance metadata that will be applied to the image. +- `instance_metadata` (object of key/value strings) - Metadata that is + applied to the server instance created by Packer. Also called server + properties in some documentation. The strings have a max size of 255 bytes + each. + - `networks` (array of strings) - A list of networks by UUID to attach to this instance.