run step uses vSphere driver

This commit is contained in:
Michael Kuzmin 2017-07-02 14:44:32 +03:00
parent b05ba0a9a2
commit 0e20a77e1c
2 changed files with 52 additions and 30 deletions

View File

@ -157,3 +157,42 @@ func (d *Driver) configureVM(vm *object.VirtualMachine, config *HardwareConfig)
return nil
}
func (d *Driver) powerOn(vm *object.VirtualMachine) error {
task, err := vm.PowerOn(d.ctx)
if err != nil {
return err
}
_, err = task.WaitForResult(d.ctx, nil)
if err != nil {
return err
}
return nil
}
func (d *Driver) WaitForIP(vm *object.VirtualMachine) (string, error) {
ip, err := vm.WaitForIP(d.ctx)
if err != nil {
return "", err
}
return ip, nil
}
func (d *Driver) powerOff(vm *object.VirtualMachine) error {
state, err := vm.PowerState(d.ctx)
if err != nil {
return err
}
if state != types.VirtualMachinePowerStatePoweredOff {
task, err := vm.PowerOff(d.ctx)
if err != nil {
return err
}
_, err = task.WaitForResult(d.ctx, nil)
if err != nil {
return err
}
}
return nil
}

View File

@ -5,39 +5,33 @@ import (
"github.com/hashicorp/packer/packer"
"github.com/vmware/govmomi/object"
"fmt"
"github.com/vmware/govmomi/vim25/types"
)
type StepRun struct{
type StepRun struct {
// TODO: add boot time to provide a proper timeout during cleanup
}
func (s *StepRun) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*object.VirtualMachine)
d := state.Get("driver").(Driver)
vm := state.Get("vm").(*object.VirtualMachine)
ui := state.Get("ui").(packer.Ui)
ui.Say("VM power on...")
task, err := vm.PowerOn(d.ctx)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
_, err = task.WaitForResult(d.ctx, nil)
ui.Say("Power on VM...")
err := d.powerOn(vm)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
ui.Say("VM waiting for IP...")
ip, err := vm.WaitForIP(d.ctx)
ui.Say("Waiting for IP...")
ip, err := d.WaitForIP(vm)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("ip", ip)
ui.Say(fmt.Sprintf("VM ip %v", ip))
ui.Say(fmt.Sprintf("IP address: %v", ip))
return multistep.ActionContinue
}
@ -46,26 +40,15 @@ func (s *StepRun) Cleanup(state multistep.StateBag) {
_, halted := state.GetOk(multistep.StateHalted)
if cancelled || halted {
vm := state.Get("vm").(*object.VirtualMachine)
d := state.Get("driver").(Driver)
vm := state.Get("vm").(*object.VirtualMachine)
ui := state.Get("ui").(packer.Ui)
if state, err := vm.PowerState(d.ctx); state != types.VirtualMachinePowerStatePoweredOff && err == nil {
ui.Say("shutting down VM...")
task, err := vm.PowerOff(d.ctx)
ui.Say("Power off VM...")
err := d.powerOff(vm)
if err != nil {
ui.Error(err.Error())
return
}
_, err = task.WaitForResult(d.ctx, nil)
if err != nil {
ui.Error(err.Error())
return
}
} else if err != nil {
ui.Error(err.Error())
return
}
}
}