Simplify time.Duration parsing

This commit is contained in:
Vladislav Rassokhin 2019-07-12 11:29:41 +03:00
parent d3591540e1
commit 9b254b2f53
3 changed files with 18 additions and 43 deletions

View File

@ -12,27 +12,18 @@ import (
) )
type ShutdownConfig struct { type ShutdownConfig struct {
Command string `mapstructure:"shutdown_command"` Command string `mapstructure:"shutdown_command"`
RawTimeout string `mapstructure:"shutdown_timeout"` Timeout time.Duration `mapstructure:"shutdown_timeout"`
Timeout time.Duration
} }
func (c *ShutdownConfig) Prepare() []error { func (c *ShutdownConfig) Prepare() []error {
var errs []error var errs []error
if c.RawTimeout != "" { if c.Timeout == 0 {
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 {
c.Timeout = 5 * time.Minute c.Timeout = 5 * time.Minute
} }
return nil return errs
} }
type StepShutdown struct { type StepShutdown struct {

View File

@ -11,9 +11,7 @@ import (
) )
type WaitIpConfig struct { type WaitIpConfig struct {
SettleTimeout string `mapstructure:"ip_settle_timeout"` SettleTimeout time.Duration `mapstructure:"ip_settle_timeout"`
settleTimeout time.Duration
} }
type StepWaitForIp struct { type StepWaitForIp struct {
@ -23,14 +21,8 @@ type StepWaitForIp struct {
func (c *WaitIpConfig) Prepare() []error { func (c *WaitIpConfig) Prepare() []error {
var errs []error var errs []error
if c.SettleTimeout == "" { if c.SettleTimeout == 0 {
c.SettleTimeout = "5s" c.SettleTimeout = 5 * time.Second
}
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))
} }
return errs return errs
@ -71,11 +63,11 @@ func doGetIp(vm *driver.VirtualMachine, ctx context.Context, c *WaitIpConfig, er
var prevIp = "" var prevIp = ""
var stopTime time.Time var stopTime time.Time
var interval time.Duration var interval time.Duration
if c.settleTimeout.Seconds() >= 120 { if c.SettleTimeout.Seconds() >= 120 {
interval = 30 * time.Second interval = 30 * time.Second
} else if c.settleTimeout.Seconds() >= 60 { } else if c.SettleTimeout.Seconds() >= 60 {
interval = 15 * time.Second interval = 15 * time.Second
} else if c.settleTimeout.Seconds() >= 10 { } else if c.SettleTimeout.Seconds() >= 10 {
interval = 5 * time.Second interval = 5 * time.Second
} else { } else {
interval = 1 * time.Second interval = 1 * time.Second
@ -93,7 +85,7 @@ loop:
log.Printf("VM IP changed from %s to %s", prevIp, ip) log.Printf("VM IP changed from %s to %s", prevIp, ip)
} }
prevIp = ip prevIp = ip
stopTime = time.Now().Add(c.settleTimeout) stopTime = time.Now().Add(c.SettleTimeout)
goto loop goto loop
} else { } else {
log.Printf("VM IP is still the same: %s", prevIp) log.Printf("VM IP is still the same: %s", prevIp)

View File

@ -18,11 +18,9 @@ import (
) )
type BootConfig struct { type BootConfig struct {
BootCommand []string `mapstructure:"boot_command"` BootCommand []string `mapstructure:"boot_command"`
RawBootWait string `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s" BootWait time.Duration `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s"
HTTPIP string `mapstructure:"http_ip"` HTTPIP string `mapstructure:"http_ip"`
bootWait time.Duration
} }
type bootCommandTemplateData struct { type bootCommandTemplateData struct {
@ -34,14 +32,8 @@ type bootCommandTemplateData struct {
func (c *BootConfig) Prepare() []error { func (c *BootConfig) Prepare() []error {
var errs []error var errs []error
if c.RawBootWait == "" { if c.BootWait == 0 {
c.RawBootWait = "10s" c.BootWait = 10 * time.Second
}
var err error
c.bootWait, err = time.ParseDuration(c.RawBootWait)
if err != nil {
errs = append(errs, fmt.Errorf("failed parsing boot_wait: %s", err))
} }
return errs return errs
@ -98,8 +90,8 @@ func (s *StepBootCommand) Run(_ context.Context, state multistep.StateBag) multi
return multistep.ActionContinue return multistep.ActionContinue
} }
ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.bootWait)) ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.BootWait))
wait := time.After(s.Config.bootWait) wait := time.After(s.Config.BootWait)
WAITLOOP: WAITLOOP:
for { for {
select { select {