diff --git a/common/step_run.go b/common/step_run.go index c7f3bedfa..8da3663d0 100644 --- a/common/step_run.go +++ b/common/step_run.go @@ -6,29 +6,16 @@ import ( "fmt" "github.com/jetbrains-infra/packer-builder-vsphere/driver" "strings" - "time" "context" ) type RunConfig struct { 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 { 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 } @@ -40,8 +27,7 @@ func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.Ste ui := state.Get("ui").(packer.Ui) vm := state.Get("vm").(*driver.VirtualMachine) - ui.Say("Power on VM...") - + ui.Say("Set boot order...") if s.Config.BootOrder != "" { if err := vm.SetBootOrder(strings.Split(s.Config.BootOrder, ",")); err != nil { state.Put("error", fmt.Errorf("error selecting boot order: %v", err)) @@ -49,28 +35,13 @@ func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.Ste } } + ui.Say("Power on VM...") err := vm.PowerOn() if err != nil { state.Put("error", fmt.Errorf("error powering on VM: %v", err)) 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 } diff --git a/iso/config.go b/iso/config.go index 31bdc7357..d5caf61b5 100644 --- a/iso/config.go +++ b/iso/config.go @@ -36,6 +36,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs := new(packer.MultiError) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare()...) + errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ConnectConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...) diff --git a/iso/step_boot_command.go b/iso/step_boot_command.go index 2e86f954b..7ece87075 100644 --- a/iso/step_boot_command.go +++ b/iso/step_boot_command.go @@ -17,10 +17,25 @@ import ( type BootConfig struct { BootCommand []string `mapstructure:"boot_command"` + RawBootWait string `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s" + + bootWait time.Duration } func (c *BootConfig) 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 StepBootCommand struct { @@ -68,6 +83,24 @@ func (s *StepBootCommand) Run(_ context.Context, state multistep.StateBag) multi ui := state.Get("ui").(packer.Ui) vm := state.Get("vm").(*driver.VirtualMachine) + if s.Config.BootCommand == nil { + return multistep.ActionContinue + } + + 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 + } + } + } + ui.Say("Typing boot command...") var keyAlt bool