2020-01-07 19:59:31 -05:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type RunConfig
|
|
|
|
|
2018-01-24 06:04:39 -05:00
|
|
|
package common
|
2017-05-09 10:23:57 -04:00
|
|
|
|
|
|
|
import (
|
2018-10-31 17:42:24 -04:00
|
|
|
"context"
|
2020-02-14 11:42:29 -05:00
|
|
|
"strings"
|
|
|
|
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
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"
|
|
|
|
)
|
|
|
|
|
2018-01-30 12:25:05 -05:00
|
|
|
type RunConfig struct {
|
2020-01-07 19:59:31 -05:00
|
|
|
// Priority of boot devices. Defaults to `disk,cdrom`
|
2018-05-06 12:04:51 -04:00
|
|
|
BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk"
|
2018-01-30 12:25:05 -05:00
|
|
|
}
|
|
|
|
|
2017-07-02 07:44:32 -04:00
|
|
|
type StepRun struct {
|
2018-10-31 17:42:24 -04:00
|
|
|
Config *RunConfig
|
2018-10-20 20:57:28 -04:00
|
|
|
SetOrder bool
|
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)
|
2020-08-31 04:34:41 -04:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachineDriver)
|
2017-05-09 10:23:57 -04:00
|
|
|
|
2018-01-30 12:25:05 -05:00
|
|
|
if s.Config.BootOrder != "" {
|
2018-05-06 17:26:04 -04:00
|
|
|
ui.Say("Set boot order...")
|
|
|
|
order := strings.Split(s.Config.BootOrder, ",")
|
|
|
|
if err := vm.SetBootOrder(order); err != nil {
|
|
|
|
state.Put("error", err)
|
2018-01-30 12:25:05 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-10-20 20:57:28 -04:00
|
|
|
} else {
|
|
|
|
if s.SetOrder {
|
|
|
|
ui.Say("Set boot order temporary...")
|
|
|
|
if err := vm.SetBootOrder([]string{"disk", "cdrom"}); err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 12:25:05 -05:00
|
|
|
}
|
|
|
|
|
2018-05-06 10:46:40 -04:00
|
|
|
ui.Say("Power on VM...")
|
2017-08-23 20:06:50 -04:00
|
|
|
err := vm.PowerOn()
|
2017-05-09 10:23:57 -04:00
|
|
|
if err != nil {
|
2018-10-31 17:42:24 -04:00
|
|
|
state.Put("error", err)
|
2017-05-09 10:23:57 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRun) Cleanup(state multistep.StateBag) {
|
2018-10-20 20:57:28 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-08-31 04:34:41 -04:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachineDriver)
|
2018-10-20 20:57:28 -04:00
|
|
|
|
|
|
|
if s.Config.BootOrder == "" && s.SetOrder {
|
|
|
|
ui.Say("Clear boot order...")
|
|
|
|
if err := vm.SetBootOrder([]string{"-"}); err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 10:23:57 -04:00
|
|
|
_, 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.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
|
|
|
}
|
|
|
|
}
|