packer-cn/step_run.go

54 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"github.com/mitchellh/multistep"
"github.com/hashicorp/packer/packer"
"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)
2017-08-23 20:06:50 -04:00
vm := state.Get("vm").(*driver.VirtualMachine)
2017-07-02 07:44:32 -04:00
ui.Say("Power on VM...")
2017-08-23 20:06:50 -04:00
err := vm.PowerOn()
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
2017-07-02 07:44:32 -04:00
ui.Say("Waiting for IP...")
2017-08-23 20:06:50 -04:00
ip, err := vm.WaitForIP()
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)
2017-08-23 20:06:50 -04:00
vm := state.Get("vm").(*driver.VirtualMachine)
2017-07-02 16:29:50 -04:00
ui.Say("Power off VM...")
2017-08-23 20:06:50 -04:00
err := vm.PowerOff()
2017-07-02 16:29:50 -04:00
if err != nil {
ui.Error(err.Error())
}
}