2018-01-24 06:04:39 -05:00
|
|
|
package common
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-25 07:22:38 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-09 10:23:57 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"fmt"
|
2017-08-23 15:40:57 -04:00
|
|
|
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
|
2018-01-30 12:25:05 -05:00
|
|
|
"strings"
|
2018-02-12 08:07:43 -05:00
|
|
|
"time"
|
2018-04-25 07:22:38 -04:00
|
|
|
"context"
|
2017-05-09 10:23:57 -04:00
|
|
|
)
|
|
|
|
|
2018-01-30 12:25:05 -05:00
|
|
|
type RunConfig struct {
|
2018-02-12 08:07:43 -05:00
|
|
|
BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk"
|
|
|
|
RawBootWait string `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s"
|
|
|
|
bootWait time.Duration ``
|
2018-01-30 12:25:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RunConfig) Prepare() []error {
|
2018-02-12 08:07:43 -05:00
|
|
|
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
|
2018-01-30 12:25:05 -05:00
|
|
|
}
|
|
|
|
|
2017-07-02 07:44:32 -04:00
|
|
|
type StepRun struct {
|
2018-01-30 12:25:05 -05:00
|
|
|
Config *RunConfig
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
|
2018-04-25 07:22:38 -04:00
|
|
|
func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-07-02 07:44:32 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-08-23 20:06:50 -04:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-07-02 07:44:32 -04:00
|
|
|
ui.Say("Power on VM...")
|
2017-08-23 20:06:50 -04:00
|
|
|
|
2018-01-30 12:25:05 -05:00
|
|
|
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))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
err := vm.PowerOn()
|
2017-05-09 10:23:57 -04:00
|
|
|
if err != nil {
|
2018-01-30 12:25:05 -05:00
|
|
|
state.Put("error", fmt.Errorf("error powering on VM: %v", err))
|
2017-05-09 10:23:57 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-02-12 08:07:43 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 10:23:57 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRun) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2017-07-02 16:29:50 -04:00
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2017-07-02 16:29:50 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-08-23 20:06:50 -04:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
2017-07-02 16:29:50 -04:00
|
|
|
|
|
|
|
ui.Say("Power off VM...")
|
2017-08-23 20:06:50 -04:00
|
|
|
|
|
|
|
err := vm.PowerOff()
|
2017-07-02 16:29:50 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
2017-05-09 10:23:57 -04:00
|
|
|
}
|
|
|
|
}
|