Merge pull request #9441 from jhawk28/vsphere_httpip_default

use ip_wait_address range to determine the default for the http server IP
This commit is contained in:
Megan Marsh 2020-06-17 14:46:04 -07:00 committed by GitHub
commit e951b3731a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 8 deletions

View File

@ -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
@ -42,6 +43,19 @@ func getHostIP(s string) (string, error) {
return "", err
}
// look for an IP that is contained in the ip_wait_address range
if network != nil {
for _, a := range addrs {
ipnet, ok := a.(*net.IPNet)
if ok && !ipnet.IP.IsLoopback() {
if network.Contains(ipnet.IP) {
return ipnet.IP.String(), nil
}
}
}
}
// fallback to an ipv4 address if an IP is not found in the range
for _, a := range addrs {
ipnet, ok := a.(*net.IPNet)
if ok && !ipnet.IP.IsLoopback() {

View File

@ -2,6 +2,7 @@ package common
import (
"context"
"net"
"testing"
"github.com/hashicorp/packer/helper/multistep"
@ -41,4 +42,42 @@ func TestStepHTTPIPDiscover_Run(t *testing.T) {
if httpIp != ip {
t.Fatalf("bad: Http ip is %s but was supposed to be %s", httpIp, ip)
}
_, ipNet, err := net.ParseCIDR("0.0.0.0/0")
if err != nil {
t.Fatal("error getting ipNet", err)
}
step = new(StepHTTPIPDiscover)
step.Network = ipNet
// without setting HTTPIP with Network
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
if _, ok := state.GetOk("error"); ok {
t.Fatal("should NOT have error")
}
_, ok = state.GetOk("http_ip")
if !ok {
t.Fatal("should have http_ip")
}
// setting HTTPIP with Network
step = &StepHTTPIPDiscover{
HTTPIP: ip,
Network: ipNet,
}
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}
if _, ok := state.GetOk("error"); ok {
t.Fatal("should NOT have error")
}
httpIp, ok = state.GetOk("http_ip")
if !ok {
t.Fatal("should have http_ip")
}
if httpIp != ip {
t.Fatalf("bad: Http ip is %s but was supposed to be %s", httpIp, ip)
}
}

View File

@ -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)

View File

@ -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,

View File

@ -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