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