Simplify time.Duration parsing
This commit is contained in:
parent
d3591540e1
commit
9b254b2f53
|
@ -12,27 +12,18 @@ import (
|
|||
)
|
||||
|
||||
type ShutdownConfig struct {
|
||||
Command string `mapstructure:"shutdown_command"`
|
||||
RawTimeout string `mapstructure:"shutdown_timeout"`
|
||||
|
||||
Timeout time.Duration
|
||||
Command string `mapstructure:"shutdown_command"`
|
||||
Timeout time.Duration `mapstructure:"shutdown_timeout"`
|
||||
}
|
||||
|
||||
func (c *ShutdownConfig) Prepare() []error {
|
||||
var errs []error
|
||||
|
||||
if c.RawTimeout != "" {
|
||||
timeout, err := time.ParseDuration(c.RawTimeout)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("Failed parsing shutdown_timeout: %s", err))
|
||||
return errs
|
||||
}
|
||||
c.Timeout = timeout
|
||||
} else {
|
||||
if c.Timeout == 0 {
|
||||
c.Timeout = 5 * time.Minute
|
||||
}
|
||||
|
||||
return nil
|
||||
return errs
|
||||
}
|
||||
|
||||
type StepShutdown struct {
|
||||
|
|
|
@ -11,9 +11,7 @@ import (
|
|||
)
|
||||
|
||||
type WaitIpConfig struct {
|
||||
SettleTimeout string `mapstructure:"ip_settle_timeout"`
|
||||
|
||||
settleTimeout time.Duration
|
||||
SettleTimeout time.Duration `mapstructure:"ip_settle_timeout"`
|
||||
}
|
||||
|
||||
type StepWaitForIp struct {
|
||||
|
@ -23,14 +21,8 @@ type StepWaitForIp struct {
|
|||
func (c *WaitIpConfig) Prepare() []error {
|
||||
var errs []error
|
||||
|
||||
if c.SettleTimeout == "" {
|
||||
c.SettleTimeout = "5s"
|
||||
}
|
||||
|
||||
var err error
|
||||
c.settleTimeout, err = time.ParseDuration(c.SettleTimeout)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed parsing ip_settle_timeout: %s", err))
|
||||
if c.SettleTimeout == 0 {
|
||||
c.SettleTimeout = 5 * time.Second
|
||||
}
|
||||
|
||||
return errs
|
||||
|
@ -71,11 +63,11 @@ func doGetIp(vm *driver.VirtualMachine, ctx context.Context, c *WaitIpConfig, er
|
|||
var prevIp = ""
|
||||
var stopTime time.Time
|
||||
var interval time.Duration
|
||||
if c.settleTimeout.Seconds() >= 120 {
|
||||
if c.SettleTimeout.Seconds() >= 120 {
|
||||
interval = 30 * time.Second
|
||||
} else if c.settleTimeout.Seconds() >= 60 {
|
||||
} else if c.SettleTimeout.Seconds() >= 60 {
|
||||
interval = 15 * time.Second
|
||||
} else if c.settleTimeout.Seconds() >= 10 {
|
||||
} else if c.SettleTimeout.Seconds() >= 10 {
|
||||
interval = 5 * time.Second
|
||||
} else {
|
||||
interval = 1 * time.Second
|
||||
|
@ -93,7 +85,7 @@ loop:
|
|||
log.Printf("VM IP changed from %s to %s", prevIp, ip)
|
||||
}
|
||||
prevIp = ip
|
||||
stopTime = time.Now().Add(c.settleTimeout)
|
||||
stopTime = time.Now().Add(c.SettleTimeout)
|
||||
goto loop
|
||||
} else {
|
||||
log.Printf("VM IP is still the same: %s", prevIp)
|
||||
|
|
|
@ -18,11 +18,9 @@ import (
|
|||
)
|
||||
|
||||
type BootConfig struct {
|
||||
BootCommand []string `mapstructure:"boot_command"`
|
||||
RawBootWait string `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s"
|
||||
HTTPIP string `mapstructure:"http_ip"`
|
||||
|
||||
bootWait time.Duration
|
||||
BootCommand []string `mapstructure:"boot_command"`
|
||||
BootWait time.Duration `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s"
|
||||
HTTPIP string `mapstructure:"http_ip"`
|
||||
}
|
||||
|
||||
type bootCommandTemplateData struct {
|
||||
|
@ -34,14 +32,8 @@ type bootCommandTemplateData struct {
|
|||
func (c *BootConfig) Prepare() []error {
|
||||
var errs []error
|
||||
|
||||
if c.RawBootWait == "" {
|
||||
c.RawBootWait = "10s"
|
||||
}
|
||||
|
||||
var err error
|
||||
c.bootWait, err = time.ParseDuration(c.RawBootWait)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed parsing boot_wait: %s", err))
|
||||
if c.BootWait == 0 {
|
||||
c.BootWait = 10 * time.Second
|
||||
}
|
||||
|
||||
return errs
|
||||
|
@ -98,8 +90,8 @@ func (s *StepBootCommand) Run(_ context.Context, state multistep.StateBag) multi
|
|||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.bootWait))
|
||||
wait := time.After(s.Config.bootWait)
|
||||
ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.BootWait))
|
||||
wait := time.After(s.Config.BootWait)
|
||||
WAITLOOP:
|
||||
for {
|
||||
select {
|
||||
|
|
Loading…
Reference in New Issue