2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type WaitIpConfig
|
|
|
|
|
2018-02-12 07:56:29 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
2018-02-12 07:56:29 -05:00
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
"log"
|
2020-06-04 01:35:29 -04:00
|
|
|
"net"
|
2020-02-14 11:42:29 -05:00
|
|
|
"time"
|
|
|
|
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-02-12 07:56:29 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2018-02-12 07:56:29 -05:00
|
|
|
)
|
|
|
|
|
2018-05-30 07:52:27 -04:00
|
|
|
type WaitIpConfig struct {
|
2020-01-07 19:59:31 -05:00
|
|
|
// Amount of time to wait for VM's IP, similar to 'ssh_timeout'.
|
2020-06-04 01:35:29 -04:00
|
|
|
// Defaults to 30m (30 minutes). See the Golang
|
2020-01-07 19:59:31 -05:00
|
|
|
// [ParseDuration](https://golang.org/pkg/time/#ParseDuration) documentation
|
|
|
|
// for full details.
|
|
|
|
WaitTimeout time.Duration `mapstructure:"ip_wait_timeout"`
|
|
|
|
// Amount of time to wait for VM's IP to settle down, sometimes VM may
|
|
|
|
// report incorrect IP initially, then its recommended to set that
|
|
|
|
// parameter to apx. 2 minutes. Examples 45s and 10m. Defaults to
|
|
|
|
// 5s(5 seconds). See the Golang
|
|
|
|
// [ParseDuration](https://golang.org/pkg/time/#ParseDuration) documentation
|
|
|
|
// for full details.
|
2019-07-12 04:29:41 -04:00
|
|
|
SettleTimeout time.Duration `mapstructure:"ip_settle_timeout"`
|
2020-06-04 22:27:59 -04:00
|
|
|
// Set this to a CIDR address to cause the service to wait for an address that is contained in
|
|
|
|
// this network range. Defaults to "0.0.0.0/0" for any ipv4 address. Examples include:
|
|
|
|
//
|
|
|
|
// * empty string ("") - remove all filters
|
2020-06-17 01:36:55 -04:00
|
|
|
// * `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
|
2020-06-04 22:27:59 -04:00
|
|
|
WaitAddress *string `mapstructure:"ip_wait_address"`
|
2020-06-04 01:35:29 -04:00
|
|
|
ipnet *net.IPNet
|
2019-07-12 05:44:40 -04:00
|
|
|
|
|
|
|
// WaitTimeout is a total timeout, so even if VM changes IP frequently and it doesn't settle down we will end waiting.
|
2018-05-30 07:52:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type StepWaitForIp struct {
|
|
|
|
Config *WaitIpConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WaitIpConfig) Prepare() []error {
|
|
|
|
var errs []error
|
|
|
|
|
2019-07-12 04:29:41 -04:00
|
|
|
if c.SettleTimeout == 0 {
|
|
|
|
c.SettleTimeout = 5 * time.Second
|
2018-05-30 07:52:27 -04:00
|
|
|
}
|
2019-07-12 05:44:40 -04:00
|
|
|
if c.WaitTimeout == 0 {
|
2019-07-12 06:22:28 -04:00
|
|
|
c.WaitTimeout = 30 * time.Minute
|
2019-07-12 05:44:40 -04:00
|
|
|
}
|
2020-06-04 22:27:59 -04:00
|
|
|
if c.WaitAddress == nil {
|
|
|
|
addr := "0.0.0.0/0"
|
|
|
|
c.WaitAddress = &addr
|
|
|
|
}
|
2018-05-30 07:52:27 -04:00
|
|
|
|
2020-06-04 22:27:59 -04:00
|
|
|
if *c.WaitAddress != "" {
|
2020-06-04 01:35:29 -04:00
|
|
|
var err error
|
2020-06-04 22:27:59 -04:00
|
|
|
_, c.ipnet, err = net.ParseCIDR(*c.WaitAddress)
|
2020-06-04 01:35:29 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("unable to parse \"ip_wait_address\": %w", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 07:52:27 -04:00
|
|
|
return errs
|
|
|
|
}
|
2018-02-12 07:56:29 -05:00
|
|
|
|
2020-06-17 01:36:55 -04:00
|
|
|
func (c *WaitIpConfig) GetIPNet() *net.IPNet {
|
|
|
|
return c.ipnet
|
|
|
|
}
|
|
|
|
|
2018-05-16 09:05:08 -04:00
|
|
|
func (s *StepWaitForIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-02-12 07:56:29 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-08-31 04:34:41 -04:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachineDriver)
|
2018-02-12 07:56:29 -05:00
|
|
|
|
2019-07-12 05:44:40 -04:00
|
|
|
var ip string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
sub, cancel := context.WithCancel(ctx)
|
|
|
|
waitDone := make(chan bool, 1)
|
2019-07-13 14:02:15 -04:00
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
}()
|
2018-02-12 07:56:29 -05:00
|
|
|
|
|
|
|
go func() {
|
2019-07-12 05:44:40 -04:00
|
|
|
ui.Say("Waiting for IP...")
|
|
|
|
ip, err = doGetIp(vm, sub, s.Config)
|
|
|
|
waitDone <- true
|
2018-02-12 07:56:29 -05:00
|
|
|
}()
|
|
|
|
|
2019-07-12 05:44:40 -04:00
|
|
|
log.Printf("[INFO] Waiting for IP, up to total timeout: %s, settle timeout: %s", s.Config.WaitTimeout, s.Config.SettleTimeout)
|
|
|
|
timeout := time.After(s.Config.WaitTimeout)
|
2018-02-12 07:56:29 -05:00
|
|
|
for {
|
|
|
|
select {
|
2019-07-12 05:44:40 -04:00
|
|
|
case <-timeout:
|
2020-08-12 04:39:12 -04:00
|
|
|
cancel()
|
|
|
|
<-waitDone
|
|
|
|
if ip != "" {
|
|
|
|
state.Put("ip", ip)
|
|
|
|
log.Printf("[WARN] API timeout waiting for IP but one IP was found. Using IP: %s", ip)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2019-07-12 05:44:40 -04:00
|
|
|
err := fmt.Errorf("Timeout waiting for IP.")
|
2018-02-12 07:56:29 -05:00
|
|
|
state.Put("error", err)
|
2019-07-12 05:44:40 -04:00
|
|
|
ui.Error(err.Error())
|
2018-02-12 07:56:29 -05:00
|
|
|
return multistep.ActionHalt
|
2018-05-16 09:05:08 -04:00
|
|
|
case <-ctx.Done():
|
2019-07-12 05:44:40 -04:00
|
|
|
cancel()
|
|
|
|
log.Println("[WARN] Interrupt detected, quitting waiting for IP.")
|
2018-05-16 09:05:08 -04:00
|
|
|
return multistep.ActionHalt
|
2019-07-12 05:44:40 -04:00
|
|
|
case <-waitDone:
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-02-12 07:56:29 -05:00
|
|
|
state.Put("ip", ip)
|
|
|
|
ui.Say(fmt.Sprintf("IP address: %v", ip))
|
|
|
|
return multistep.ActionContinue
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 04:34:41 -04:00
|
|
|
func doGetIp(vm *driver.VirtualMachineDriver, ctx context.Context, c *WaitIpConfig) (string, error) {
|
2018-05-30 07:52:27 -04:00
|
|
|
var prevIp = ""
|
|
|
|
var stopTime time.Time
|
|
|
|
var interval time.Duration
|
2019-07-12 04:29:41 -04:00
|
|
|
if c.SettleTimeout.Seconds() >= 120 {
|
2018-05-30 07:52:27 -04:00
|
|
|
interval = 30 * time.Second
|
2019-07-12 04:29:41 -04:00
|
|
|
} else if c.SettleTimeout.Seconds() >= 60 {
|
2018-05-30 07:52:27 -04:00
|
|
|
interval = 15 * time.Second
|
2019-07-12 04:29:41 -04:00
|
|
|
} else if c.SettleTimeout.Seconds() >= 10 {
|
2018-05-30 07:52:27 -04:00
|
|
|
interval = 5 * time.Second
|
|
|
|
} else {
|
|
|
|
interval = 1 * time.Second
|
|
|
|
}
|
|
|
|
loop:
|
2020-06-04 01:35:29 -04:00
|
|
|
ip, err := vm.WaitForIP(ctx, c.ipnet)
|
2018-05-30 07:52:27 -04:00
|
|
|
if err != nil {
|
2019-07-12 05:44:40 -04:00
|
|
|
return "", err
|
2018-05-30 07:52:27 -04:00
|
|
|
}
|
2020-08-10 12:16:19 -04:00
|
|
|
|
|
|
|
// Check for ctx cancellation to avoid printing any IP logs at the timeout
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-08-12 04:39:12 -04:00
|
|
|
return ip, fmt.Errorf("IP wait cancelled.")
|
2020-08-10 12:16:19 -04:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-05-30 07:52:27 -04:00
|
|
|
if prevIp == "" || prevIp != ip {
|
|
|
|
if prevIp == "" {
|
|
|
|
log.Printf("VM IP aquired: %s", ip)
|
|
|
|
} else {
|
|
|
|
log.Printf("VM IP changed from %s to %s", prevIp, ip)
|
|
|
|
}
|
|
|
|
prevIp = ip
|
2019-07-12 04:29:41 -04:00
|
|
|
stopTime = time.Now().Add(c.SettleTimeout)
|
2018-05-30 07:52:27 -04:00
|
|
|
goto loop
|
|
|
|
} else {
|
|
|
|
log.Printf("VM IP is still the same: %s", prevIp)
|
|
|
|
if time.Now().After(stopTime) {
|
|
|
|
log.Printf("VM IP seems stable enough: %s", ip)
|
2019-07-12 05:44:40 -04:00
|
|
|
return ip, nil
|
2018-05-30 07:52:27 -04:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-07-12 05:44:40 -04:00
|
|
|
return "", fmt.Errorf("IP wait cancelled")
|
2018-05-30 07:52:27 -04:00
|
|
|
case <-time.After(interval):
|
|
|
|
goto loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-06 17:26:04 -04:00
|
|
|
func (s *StepWaitForIp) Cleanup(state multistep.StateBag) {}
|