diff --git a/driver.go b/driver.go index 79a692750..464372fc2 100644 --- a/driver.go +++ b/driver.go @@ -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 +} diff --git a/step_run.go b/step_run.go index f0d20e23a..8787926b1 100644 --- a/step_run.go +++ b/step_run.go @@ -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,24 +40,13 @@ 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) - 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.Say("Power off VM...") + err := d.powerOff(vm) + if err != nil { ui.Error(err.Error()) return }