2020-01-07 16:59:31 -08:00
|
|
|
//go:generate struct-markdown
|
|
|
|
//go:generate mapstructure-to-hcl2 -type RunConfig
|
|
|
|
|
2018-01-24 14:04:39 +03:00
|
|
|
package common
|
2017-05-09 17:23:57 +03:00
|
|
|
|
|
|
|
import (
|
2018-11-01 00:42:24 +03:00
|
|
|
"context"
|
2020-02-14 11:42:29 -05:00
|
|
|
"strings"
|
|
|
|
|
2020-01-07 16:59:31 -08:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2018-04-25 14:22:38 +03:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-09 17:23:57 +03:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
2018-01-30 20:25:05 +03:00
|
|
|
type RunConfig struct {
|
2020-01-07 16:59:31 -08:00
|
|
|
// Priority of boot devices. Defaults to `disk,cdrom`
|
2018-05-06 19:04:51 +03:00
|
|
|
BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk"
|
2018-01-30 20:25:05 +03:00
|
|
|
}
|
|
|
|
|
2017-07-02 14:44:32 +03:00
|
|
|
type StepRun struct {
|
2018-11-01 00:42:24 +03:00
|
|
|
Config *RunConfig
|
2018-10-21 03:57:28 +03:00
|
|
|
SetOrder bool
|
2017-05-09 17:23:57 +03:00
|
|
|
}
|
|
|
|
|
2018-04-25 14:22:38 +03:00
|
|
|
func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2017-07-02 14:44:32 +03:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-08-24 03:06:50 +03:00
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
2017-05-09 17:23:57 +03:00
|
|
|
|
2018-01-30 20:25:05 +03:00
|
|
|
if s.Config.BootOrder != "" {
|
2018-05-07 00:26:04 +03: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 20:25:05 +03:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2018-10-21 03:57:28 +03: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 20:25:05 +03:00
|
|
|
}
|
|
|
|
|
2018-05-06 17:46:40 +03:00
|
|
|
ui.Say("Power on VM...")
|
2017-08-24 03:06:50 +03:00
|
|
|
err := vm.PowerOn()
|
2017-05-09 17:23:57 +03:00
|
|
|
if err != nil {
|
2018-11-01 00:42:24 +03:00
|
|
|
state.Put("error", err)
|
2017-05-09 17:23:57 +03:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRun) Cleanup(state multistep.StateBag) {
|
2018-10-21 03:57:28 +03:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vm := state.Get("vm").(*driver.VirtualMachine)
|
|
|
|
|
|
|
|
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 17:23:57 +03:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2017-07-02 23:29:50 +03:00
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
2017-05-09 17:23:57 +03:00
|
|
|
|
2017-07-02 23:29:50 +03:00
|
|
|
ui.Say("Power off VM...")
|
2017-08-24 03:06:50 +03:00
|
|
|
|
|
|
|
err := vm.PowerOff()
|
2017-07-02 23:29:50 +03:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
2017-05-09 17:23:57 +03:00
|
|
|
}
|
|
|
|
}
|