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"
|
2018-10-31 17:42:24 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-02-12 07:56:29 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
2018-05-30 07:52:27 -04:00
|
|
|
"log"
|
2018-02-12 07:56:29 -05:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-05-30 07:52:27 -04:00
|
|
|
type WaitIpConfig struct {
|
2019-07-12 05:44:40 -04:00
|
|
|
WaitTimeout time.Duration `mapstructure:"ip_wait_timeout"`
|
2019-07-12 04:29:41 -04:00
|
|
|
SettleTimeout time.Duration `mapstructure:"ip_settle_timeout"`
|
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 {
|
|
|
|
// Same default value as default timeout for 'ssh_timeout' in StepConnect
|
2019-07-12 05:44:40 -04:00
|
|
|
c.WaitTimeout = 5 * time.Minute
|
2019-07-12 05:44:40 -04:00
|
|
|
}
|
2018-05-30 07:52:27 -04:00
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
2018-02-12 07:56:29 -05:00
|
|
|
|
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)
|
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
|
|
|
|
2019-07-12 05:44:40 -04:00
|
|
|
var ip string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
sub, cancel := context.WithCancel(ctx)
|
|
|
|
waitDone := make(chan bool, 1)
|
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:
|
|
|
|
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())
|
|
|
|
cancel()
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 05:44:40 -04:00
|
|
|
func doGetIp(vm *driver.VirtualMachine, 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:
|
|
|
|
ip, err := vm.WaitForIP(ctx)
|
|
|
|
if err != nil {
|
2019-07-12 05:44:40 -04:00
|
|
|
return "", err
|
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) {}
|