move `boot_wait` into `boot_command`

This commit is contained in:
Michael Kuzmin 2018-05-06 17:46:40 +03:00
parent c44221a800
commit 0be06451cc
3 changed files with 37 additions and 32 deletions

View File

@ -6,29 +6,16 @@ import (
"fmt" "fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver" "github.com/jetbrains-infra/packer-builder-vsphere/driver"
"strings" "strings"
"time"
"context" "context"
) )
type RunConfig struct { 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 { func (c *RunConfig) Prepare() []error {
var errs []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 return errs
} }
@ -40,8 +27,7 @@ func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.Ste
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine) vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Power on VM...") ui.Say("Set boot order...")
if s.Config.BootOrder != "" { if s.Config.BootOrder != "" {
if err := vm.SetBootOrder(strings.Split(s.Config.BootOrder, ",")); err != nil { if err := vm.SetBootOrder(strings.Split(s.Config.BootOrder, ",")); err != nil {
state.Put("error", fmt.Errorf("error selecting boot order: %v", err)) 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() err := vm.PowerOn()
if err != nil { if err != nil {
state.Put("error", fmt.Errorf("error powering on VM: %v", err)) state.Put("error", fmt.Errorf("error powering on VM: %v", err))
return multistep.ActionHalt 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 return multistep.ActionContinue
} }

View File

@ -36,6 +36,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs := new(packer.MultiError) errs := new(packer.MultiError)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.RunConfig.Prepare()...) 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.ConnectConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...) errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...)

View File

@ -17,10 +17,25 @@ 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
} }
func (c *BootConfig) Prepare() []error { 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 { type StepBootCommand struct {
@ -68,6 +83,24 @@ func (s *StepBootCommand) Run(_ context.Context, state multistep.StateBag) multi
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine) 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...") ui.Say("Typing boot command...")
var keyAlt bool var keyAlt bool