packer-cn/common/step_run.go

65 lines
1.4 KiB
Go
Raw Normal View History

package common
import (
2018-04-25 07:22:38 -04:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
2018-01-30 12:25:05 -05:00
"strings"
2018-04-25 07:22:38 -04:00
"context"
)
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"
2018-01-30 12:25:05 -05:00
}
func (c *RunConfig) Prepare() []error {
2018-02-12 08:07:43 -05:00
var errs []error
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
}
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)
2018-05-06 10:46:40 -04:00
ui.Say("Set boot order...")
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
}
}
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-01-30 12:25:05 -05:00
state.Put("error", fmt.Errorf("error powering on VM: %v", err))
return multistep.ActionHalt
}
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-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())
}
}