packer-cn/common/step_run.go

71 lines
1.5 KiB
Go
Raw Normal View History

package common
import (
"github.com/mitchellh/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-01-30 12:25:05 -05:00
type RunConfig struct {
BootOrder string `mapstructure:"boot_order"` // example: "floppy,cdrom,ethernet,disk"
}
func (c *RunConfig) Prepare() []error {
return nil
}
2017-07-02 07:44:32 -04:00
type StepRun struct {
2018-01-30 12:25:05 -05:00
Config *RunConfig
}
func (s *StepRun) Run(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-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()
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
}
2017-07-02 07:44:32 -04:00
ui.Say("Waiting for IP...")
2017-08-23 20:06:50 -04:00
ip, err := vm.WaitForIP()
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("ip", ip)
2017-07-02 07:44:32 -04:00
ui.Say(fmt.Sprintf("IP address: %v", ip))
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())
}
}