run step uses vSphere driver
This commit is contained in:
parent
b05ba0a9a2
commit
0e20a77e1c
39
driver.go
39
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
|
||||
}
|
||||
|
|
39
step_run.go
39
step_run.go
|
@ -5,7 +5,6 @@ import (
|
|||
"github.com/hashicorp/packer/packer"
|
||||
"github.com/vmware/govmomi/object"
|
||||
"fmt"
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
type StepRun struct {
|
||||
|
@ -13,31 +12,26 @@ type StepRun struct{
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue