packer-cn/step_run.go
Elizaveta Tretyakova b2c6e44c70 Builder (#2)
* the very initial builder: just clones the VM

* removed post-processor

* builder now waits for IP address

* more separated steps

* clean up

* added run and shutdown steps

* added step connecting via ssh

* changed the BuilderId to a proper one

* added shutdown command, only echo works

* added cleanup

* removed wainting for VM to stop, removed error on the non-zero exit status

* removed BuildSuccessFlag

* refactored config structures; fixed interpolation of json template

* the rest of configuration refactoring

* removed duplicated parameters from Config

* removed duplicated parameters drom Config

* changed BuilderId and Artifact

* create a dedicated step for VM hardware configuration

* merged StepSetupCloningEnv into StepCloneVM

* improved cleanup

* added proper handling of 'disconnected' command exit status; added guest os shutdown before halting the machine [in case when no shutdown command is given]

* refactored non-conventional variable and field names

* removed redundant fields from Artifact

* removed the success field at all

* removed ArtifactFiles

* removed unnecessary warnings

* minor change in parameters structure
2017-05-09 17:23:57 +03:00

73 lines
1.6 KiB
Go

package main
import (
"github.com/mitchellh/multistep"
"github.com/hashicorp/packer/packer"
"github.com/vmware/govmomi/object"
"context"
"fmt"
"github.com/vmware/govmomi/vim25/types"
)
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)
ctx := state.Get("ctx").(context.Context)
ui.Say("VM power on...")
task, err := vm.PowerOn(ctx)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
_, err = task.WaitForResult(ctx, nil)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
ui.Say("VM waiting for IP...")
ip, err := vm.WaitForIP(ctx)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("ip", ip)
ui.Say(fmt.Sprintf("VM ip %v", ip))
return multistep.ActionContinue
}
func (s *StepRun) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if cancelled || halted {
vm := state.Get("vm").(*object.VirtualMachine)
ctx := state.Get("ctx").(context.Context)
ui := state.Get("ui").(packer.Ui)
if state, err := vm.PowerState(ctx); state != types.VirtualMachinePowerStatePoweredOff && err == nil {
ui.Say("shutting down VM...")
task, err := vm.PowerOff(ctx)
if err != nil {
ui.Error(err.Error())
return
}
_, err = task.WaitForResult(ctx, nil)
if err != nil {
ui.Error(err.Error())
return
}
} else if err != nil {
ui.Error(err.Error())
return
}
}
}