From aaf91033309a87c0de1fb7e81bac12ae26fb9dd4 Mon Sep 17 00:00:00 2001 From: Joshua Foster Date: Wed, 17 Jun 2020 01:36:55 -0400 Subject: [PATCH] use the ip_wait_address range to determine the default for the http server IP --- builder/vsphere/common/step_http_ip_discover.go | 9 +++++---- builder/vsphere/common/step_wait_for_ip.go | 8 ++++++-- builder/vsphere/iso/builder.go | 3 ++- .../builder/vsphere/common/WaitIpConfig-not-required.mdx | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/builder/vsphere/common/step_http_ip_discover.go b/builder/vsphere/common/step_http_ip_discover.go index 627fbe52b..04fe4f775 100644 --- a/builder/vsphere/common/step_http_ip_discover.go +++ b/builder/vsphere/common/step_http_ip_discover.go @@ -12,11 +12,12 @@ import ( // which guests use to reach the vm host // To make sure the IP is set before boot command and http server steps type StepHTTPIPDiscover struct { - HTTPIP string + HTTPIP string + Network *net.IPNet } func (s *StepHTTPIPDiscover) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { - ip, err := getHostIP(s.HTTPIP) + ip, err := getHostIP(s.HTTPIP, s.Network) if err != nil { state.Put("error", err) return multistep.ActionHalt @@ -28,7 +29,7 @@ func (s *StepHTTPIPDiscover) Run(ctx context.Context, state multistep.StateBag) func (s *StepHTTPIPDiscover) Cleanup(state multistep.StateBag) {} -func getHostIP(s string) (string, error) { +func getHostIP(s string, network *net.IPNet) (string, error) { if s != "" { if net.ParseIP(s) != nil { return s, nil @@ -45,7 +46,7 @@ func getHostIP(s string) (string, error) { for _, a := range addrs { ipnet, ok := a.(*net.IPNet) if ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { + if network.Contains(ipnet.IP) { return ipnet.IP.String(), nil } } diff --git a/builder/vsphere/common/step_wait_for_ip.go b/builder/vsphere/common/step_wait_for_ip.go index 3d1617f01..3c05916b3 100644 --- a/builder/vsphere/common/step_wait_for_ip.go +++ b/builder/vsphere/common/step_wait_for_ip.go @@ -32,8 +32,8 @@ type WaitIpConfig struct { // this network range. Defaults to "0.0.0.0/0" for any ipv4 address. Examples include: // // * empty string ("") - remove all filters - // * "0:0:0:0:0:0:0:0/0" - allow only ipv6 addresses - // * "192.168.1.0/24 - only allow ipv4 addresses from 192.168.1.1 to 192.168.1.254 + // * `0:0:0:0:0:0:0:0/0` - allow only ipv6 addresses + // * `192.168.1.0/24` - only allow ipv4 addresses from 192.168.1.1 to 192.168.1.254 WaitAddress *string `mapstructure:"ip_wait_address"` ipnet *net.IPNet @@ -69,6 +69,10 @@ func (c *WaitIpConfig) Prepare() []error { return errs } +func (c *WaitIpConfig) GetIPNet() *net.IPNet { + return c.ipnet +} + func (s *StepWaitForIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) vm := state.Get("vm").(*driver.VirtualMachine) diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index 2c224b642..f755f2367 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -91,7 +91,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads, }, &common.StepHTTPIPDiscover{ - HTTPIP: b.config.BootConfig.HTTPIP, + HTTPIP: b.config.BootConfig.HTTPIP, + Network: b.config.WaitIpConfig.GetIPNet(), }, &packerCommon.StepHTTPServer{ HTTPDir: b.config.HTTPDir, diff --git a/website/pages/partials/builder/vsphere/common/WaitIpConfig-not-required.mdx b/website/pages/partials/builder/vsphere/common/WaitIpConfig-not-required.mdx index 09da727a7..34cdeb1c1 100644 --- a/website/pages/partials/builder/vsphere/common/WaitIpConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/common/WaitIpConfig-not-required.mdx @@ -16,6 +16,6 @@ this network range. Defaults to "0.0.0.0/0" for any ipv4 address. Examples include: * empty string ("") - remove all filters - * "0:0:0:0:0:0:0:0/0" - allow only ipv6 addresses - * "192.168.1.0/24 - only allow ipv4 addresses from 192.168.1.1 to 192.168.1.254 + * `0:0:0:0:0:0:0:0/0` - allow only ipv6 addresses + * `192.168.1.0/24` - only allow ipv4 addresses from 192.168.1.1 to 192.168.1.254 \ No newline at end of file