packer-cn/builder/vsphere/common/step_run.go

81 lines
1.8 KiB
Go
Raw Normal View History

//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type RunConfig
package common
import (
2018-10-31 17:42:24 -04:00
"context"
"strings"
"github.com/hashicorp/packer/builder/vsphere/driver"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
)
2018-01-30 12:25:05 -05:00
type RunConfig struct {
// Priority of boot devices. Defaults to `disk,cdrom`
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
SetOrder bool
}
2018-04-25 07:22:38 -04:00
func (s *StepRun) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packersdk.Ui)
vm := state.Get("vm").(*driver.VirtualMachineDriver)
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
}
} 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()
if err != nil {
2018-10-31 17:42:24 -04:00
state.Put("error", err)
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *StepRun) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packersdk.Ui)
vm := state.Get("vm").(*driver.VirtualMachineDriver)
if s.Config.BootOrder == "" && s.SetOrder {
ui.Say("Clear boot order...")
if err := vm.SetBootOrder([]string{"-"}); err != nil {
state.Put("error", err)
return
}
}
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
2017-07-02 16:29:50 -04:00
if !cancelled && !halted {
return
}
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())
}
}