packer-cn/common/step_wait_for_ip.go

128 lines
3.1 KiB
Go
Raw Normal View History

package common
import (
2018-10-31 17:42:24 -04:00
"context"
"fmt"
2018-10-31 17:42:24 -04:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
"log"
"time"
)
type WaitIpConfig struct {
WaitTimeout time.Duration `mapstructure:"ip_wait_timeout"`
2019-07-12 04:29:41 -04:00
SettleTimeout time.Duration `mapstructure:"ip_settle_timeout"`
// WaitTimeout is a total timeout, so even if VM changes IP frequently and it doesn't settle down we will end waiting.
}
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
}
if c.WaitTimeout == 0 {
// Same default value as default timeout for 'ssh_timeout' in StepConnect
c.SettleTimeout = 5 * time.Minute
}
return errs
}
2018-05-16 09:05:08 -04:00
func (s *StepWaitForIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
var ip string
var err error
sub, cancel := context.WithCancel(ctx)
waitDone := make(chan bool, 1)
go func() {
ui.Say("Waiting for IP...")
ip, err = doGetIp(vm, sub, s.Config)
waitDone <- true
}()
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)
for {
select {
case <-timeout:
err := fmt.Errorf("Timeout waiting for IP.")
state.Put("error", err)
ui.Error(err.Error())
cancel()
return multistep.ActionHalt
2018-05-16 09:05:08 -04:00
case <-ctx.Done():
cancel()
log.Println("[WARN] Interrupt detected, quitting waiting for IP.")
2018-05-16 09:05:08 -04:00
return multistep.ActionHalt
case <-waitDone:
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
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
}
}
}
}
func doGetIp(vm *driver.VirtualMachine, ctx context.Context, c *WaitIpConfig) (string, error) {
var prevIp = ""
var stopTime time.Time
var interval time.Duration
2019-07-12 04:29:41 -04:00
if c.SettleTimeout.Seconds() >= 120 {
interval = 30 * time.Second
2019-07-12 04:29:41 -04:00
} else if c.SettleTimeout.Seconds() >= 60 {
interval = 15 * time.Second
2019-07-12 04:29:41 -04:00
} else if c.SettleTimeout.Seconds() >= 10 {
interval = 5 * time.Second
} else {
interval = 1 * time.Second
}
loop:
ip, err := vm.WaitForIP(ctx)
if err != nil {
return "", err
}
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)
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)
return ip, nil
}
select {
case <-ctx.Done():
return "", fmt.Errorf("IP wait cancelled")
case <-time.After(interval):
goto loop
}
}
}
2018-05-06 17:26:04 -04:00
func (s *StepWaitForIp) Cleanup(state multistep.StateBag) {}