From 47cccb4e0231d856cae31a809e90e4f40a089bcb Mon Sep 17 00:00:00 2001 From: Andrei Tonkikh Date: Mon, 12 Feb 2018 16:07:43 +0300 Subject: [PATCH] Add "boot_wait" parameter --- common/step_run.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/common/step_run.go b/common/step_run.go index 305acdc05..e9ff7c2ee 100644 --- a/common/step_run.go +++ b/common/step_run.go @@ -6,14 +6,29 @@ import ( "fmt" "github.com/jetbrains-infra/packer-builder-vsphere/driver" "strings" + "time" ) type RunConfig struct { - BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk" + BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk" + RawBootWait string `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s" + bootWait time.Duration `` } func (c *RunConfig) Prepare() []error { - return nil + 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)) + } + + return errs } type StepRun struct { @@ -39,6 +54,22 @@ func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } + if int64(s.Config.bootWait) > 0 { + ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.bootWait)) + wait := time.After(s.Config.bootWait) + WAITLOOP: + for { + select { + case <-wait: + break WAITLOOP + case <-time.After(1 * time.Second): + if _, ok := state.GetOk(multistep.StateCancelled); ok { + return multistep.ActionHalt + } + } + } + } + return multistep.ActionContinue }