packer-cn/step_run.go

55 lines
1.2 KiB
Go
Raw Normal View History

package main
import (
"github.com/mitchellh/multistep"
"github.com/hashicorp/packer/packer"
"github.com/vmware/govmomi/object"
"fmt"
"github.com/jetbrains-infra/packer-builder-vsphere/driver"
)
2017-07-02 07:44:32 -04:00
type StepRun struct {
}
func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
2017-07-02 07:44:32 -04:00
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.Driver)
2017-07-02 16:29:50 -04:00
vm := state.Get("vm").(*object.VirtualMachine)
2017-07-02 07:44:32 -04:00
ui.Say("Power on VM...")
2017-07-02 16:29:50 -04:00
err := d.PowerOn(vm)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
2017-07-02 07:44:32 -04:00
ui.Say("Waiting for IP...")
ip, err := d.WaitForIP(vm)
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)
d := state.Get("driver").(*driver.Driver)
2017-07-02 16:29:50 -04:00
vm := state.Get("vm").(*object.VirtualMachine)
ui.Say("Power off VM...")
err := d.PowerOff(vm)
if err != nil {
ui.Error(err.Error())
}
}