From 5f9d4b729fb76b9da0d4e7250d0c98cf92ec6116 Mon Sep 17 00:00:00 2001 From: chbell43 Date: Fri, 3 Aug 2018 20:46:36 +0000 Subject: [PATCH 1/3] add support for ports to the OpenStack builder For networks that have multiple subnets, we may want to target a single subnet. OpenStack doesn't let you target a single subnet in a network and so you need to make a port. --- builder/openstack/builder.go | 1 + builder/openstack/run_config.go | 1 + builder/openstack/step_run_source_server.go | 15 ++++++++++++--- website/source/docs/builders/openstack.html.md | 3 +++ 4 files changed, 17 insertions(+), 3 deletions(-) mode change 100755 => 100644 builder/openstack/builder.go diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go old mode 100755 new mode 100644 index a718c3d59..8ebd65d3c --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -92,6 +92,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe SourceImageName: b.config.SourceImageName, SecurityGroups: b.config.SecurityGroups, Networks: b.config.Networks, + Ports: b.config.Ports, AvailabilityZone: b.config.AvailabilityZone, UserData: b.config.UserData, UserDataFile: b.config.UserDataFile, diff --git a/builder/openstack/run_config.go b/builder/openstack/run_config.go index 6b4e0932e..2562e0c8d 100644 --- a/builder/openstack/run_config.go +++ b/builder/openstack/run_config.go @@ -28,6 +28,7 @@ type RunConfig struct { ReuseIps bool `mapstructure:"reuse_ips"` SecurityGroups []string `mapstructure:"security_groups"` Networks []string `mapstructure:"networks"` + Ports []string `mapstructure:"ports"` UserData string `mapstructure:"user_data"` UserDataFile string `mapstructure:"user_data_file"` InstanceName string `mapstructure:"instance_name"` diff --git a/builder/openstack/step_run_source_server.go b/builder/openstack/step_run_source_server.go index bb09f85b7..0cab455cf 100644 --- a/builder/openstack/step_run_source_server.go +++ b/builder/openstack/step_run_source_server.go @@ -18,6 +18,7 @@ type StepRunSourceServer struct { SourceImageName string SecurityGroups []string Networks []string + Ports []string AvailabilityZone string UserData string UserDataFile string @@ -39,9 +40,17 @@ func (s *StepRunSourceServer) Run(_ context.Context, state multistep.StateBag) m return multistep.ActionHalt } - networks := make([]servers.Network, len(s.Networks)) - for i, networkUuid := range s.Networks { - networks[i].UUID = networkUuid + networks := make([]servers.Network, len(s.Networks) + len(s.Ports)) + i := 0 + if len(s.Ports) > 0 { + for i = 0; i < len(s.Ports); i++ { + networks[i].Port = s.Ports[i] + } + } + if len(s.Networks) > 0 { + for i = len(s.Ports); i < len(networks); i++ { + networks[i].UUID = s.Networks[i] + } } userData := []byte(s.UserData) diff --git a/website/source/docs/builders/openstack.html.md b/website/source/docs/builders/openstack.html.md index c6059da50..00f5e46dd 100644 --- a/website/source/docs/builders/openstack.html.md +++ b/website/source/docs/builders/openstack.html.md @@ -139,6 +139,9 @@ builder. - `networks` (array of strings) - A list of networks by UUID to attach to this instance. +- `ports` (array of strings) - A list of ports by UUID to attach to + this instance. + - `rackconnect_wait` (boolean) - For rackspace, whether or not to wait for Rackconnect to assign the machine an IP address before connecting via SSH. Defaults to false. From aaa42543e6fcab795190aee632309889897a4b83 Mon Sep 17 00:00:00 2001 From: chbell43 Date: Fri, 3 Aug 2018 21:32:34 +0000 Subject: [PATCH 2/3] fix formatting --- builder/openstack/step_run_source_server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/openstack/step_run_source_server.go b/builder/openstack/step_run_source_server.go index 0cab455cf..b905c05c3 100644 --- a/builder/openstack/step_run_source_server.go +++ b/builder/openstack/step_run_source_server.go @@ -40,7 +40,7 @@ func (s *StepRunSourceServer) Run(_ context.Context, state multistep.StateBag) m return multistep.ActionHalt } - networks := make([]servers.Network, len(s.Networks) + len(s.Ports)) + networks := make([]servers.Network, len(s.Networks)+len(s.Ports)) i := 0 if len(s.Ports) > 0 { for i = 0; i < len(s.Ports); i++ { From c962e7b856c82c1b83e1ecba2efd2e9244e0d1e0 Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Wed, 15 Aug 2018 13:12:47 +0200 Subject: [PATCH 3/3] Simplified loop code --- builder/openstack/step_run_source_server.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/builder/openstack/step_run_source_server.go b/builder/openstack/step_run_source_server.go index b905c05c3..28d50ed93 100644 --- a/builder/openstack/step_run_source_server.go +++ b/builder/openstack/step_run_source_server.go @@ -42,15 +42,11 @@ func (s *StepRunSourceServer) Run(_ context.Context, state multistep.StateBag) m networks := make([]servers.Network, len(s.Networks)+len(s.Ports)) i := 0 - if len(s.Ports) > 0 { - for i = 0; i < len(s.Ports); i++ { - networks[i].Port = s.Ports[i] - } + for ; i < len(s.Ports); i++ { + networks[i].Port = s.Ports[i] } - if len(s.Networks) > 0 { - for i = len(s.Ports); i < len(networks); i++ { - networks[i].UUID = s.Networks[i] - } + for ; i < len(networks); i++ { + networks[i].UUID = s.Networks[i] } userData := []byte(s.UserData)