Fix build cancellation (#101)
This commit is contained in:
parent
689f1789eb
commit
23c918d00a
|
@ -34,7 +34,7 @@ type StepCloneVM struct {
|
|||
Location *common.LocationConfig
|
||||
}
|
||||
|
||||
func (s *StepCloneVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
d := state.Get("driver").(*driver.Driver)
|
||||
|
||||
|
@ -46,7 +46,7 @@ func (s *StepCloneVM) Run(_ context.Context, state multistep.StateBag) multistep
|
|||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
vm, err := template.Clone(&driver.CloneConfig{
|
||||
vm, err := template.Clone(ctx, &driver.CloneConfig{
|
||||
Name: s.Location.VMName,
|
||||
Folder: s.Location.Folder,
|
||||
Cluster: s.Location.Cluster,
|
||||
|
@ -59,6 +59,9 @@ func (s *StepCloneVM) Run(_ context.Context, state multistep.StateBag) multistep
|
|||
state.Put("error", err)
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
if vm == nil {
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
state.Put("vm", vm)
|
||||
|
||||
if s.Config.DiskSize > 0 {
|
||||
|
|
|
@ -35,8 +35,8 @@ type StepConnect struct {
|
|||
Config *ConnectConfig
|
||||
}
|
||||
|
||||
func (s *StepConnect) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
d, err := driver.NewDriver(ctx, &driver.ConnectConfig{
|
||||
func (s *StepConnect) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
d, err := driver.NewDriver(&driver.ConnectConfig{
|
||||
VCenterServer: s.Config.VCenterServer,
|
||||
Username: s.Config.Username,
|
||||
Password: s.Config.Password,
|
||||
|
|
|
@ -39,7 +39,7 @@ type StepShutdown struct {
|
|||
Config *ShutdownConfig
|
||||
}
|
||||
|
||||
func (s *StepShutdown) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
func (s *StepShutdown) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
comm := state.Get("communicator").(packer.Communicator)
|
||||
vm := state.Get("vm").(*driver.VirtualMachine)
|
||||
|
@ -70,7 +70,7 @@ func (s *StepShutdown) Run(_ context.Context, state multistep.StateBag) multiste
|
|||
}
|
||||
|
||||
log.Printf("Waiting max %s for shutdown to complete", s.Config.Timeout)
|
||||
err := vm.WaitForShutdown(s.Config.Timeout)
|
||||
err := vm.WaitForShutdown(ctx, s.Config.Timeout)
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
return multistep.ActionHalt
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
type StepWaitForIp struct{}
|
||||
|
||||
func (s *StepWaitForIp) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
func (s *StepWaitForIp) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||
ui := state.Get("ui").(packer.Ui)
|
||||
vm := state.Get("vm").(*driver.VirtualMachine)
|
||||
|
||||
|
@ -20,7 +20,7 @@ func (s *StepWaitForIp) Run(_ context.Context, state multistep.StateBag) multist
|
|||
ipChan := make(chan string)
|
||||
errChan := make(chan error)
|
||||
go func() {
|
||||
ip, err := vm.WaitForIP()
|
||||
ip, err := vm.WaitForIP(ctx)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
} else {
|
||||
|
@ -33,6 +33,8 @@ func (s *StepWaitForIp) Run(_ context.Context, state multistep.StateBag) multist
|
|||
case err := <-errChan:
|
||||
state.Put("error", err)
|
||||
return multistep.ActionHalt
|
||||
case <-ctx.Done():
|
||||
return multistep.ActionHalt
|
||||
case ip := <-ipChan:
|
||||
state.Put("ip", ip)
|
||||
ui.Say(fmt.Sprintf("IP address: %v", ip))
|
||||
|
|
|
@ -28,7 +28,8 @@ type ConnectConfig struct {
|
|||
Datacenter string
|
||||
}
|
||||
|
||||
func NewDriver(ctx context.Context, config *ConnectConfig) (*Driver, error) {
|
||||
func NewDriver(config *ConnectConfig) (*Driver, error) {
|
||||
ctx := context.TODO()
|
||||
|
||||
vcenter_url, err := url.Parse(fmt.Sprintf("https://%v/sdk", config.VCenterServer))
|
||||
if err != nil {
|
||||
|
|
18
driver/vm.go
18
driver/vm.go
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/vmware/govmomi/vim25/types"
|
||||
"time"
|
||||
"strings"
|
||||
"context"
|
||||
)
|
||||
|
||||
type VirtualMachine struct {
|
||||
|
@ -178,7 +179,7 @@ func (vm *VirtualMachine) Devices() (object.VirtualDeviceList, error) {
|
|||
return vmInfo.Config.Hardware.Device, nil
|
||||
}
|
||||
|
||||
func (template *VirtualMachine) Clone(config *CloneConfig) (*VirtualMachine, error) {
|
||||
func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig) (*VirtualMachine, error) {
|
||||
folder, err := template.driver.FindFolder(config.Folder)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -223,8 +224,13 @@ func (template *VirtualMachine) Clone(config *CloneConfig) (*VirtualMachine, err
|
|||
return nil, err
|
||||
}
|
||||
|
||||
info, err := task.WaitForResult(template.driver.ctx, nil)
|
||||
info, err := task.WaitForResult(ctx, nil)
|
||||
if err != nil {
|
||||
if ctx.Err() == context.Canceled {
|
||||
err = task.Cancel(context.TODO())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -329,8 +335,8 @@ func (vm *VirtualMachine) PowerOn() error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (vm *VirtualMachine) WaitForIP() (string, error) {
|
||||
return vm.vm.WaitForIP(vm.driver.ctx)
|
||||
func (vm *VirtualMachine) WaitForIP(ctx context.Context) (string, error) {
|
||||
return vm.vm.WaitForIP(ctx)
|
||||
}
|
||||
|
||||
func (vm *VirtualMachine) PowerOff() error {
|
||||
|
@ -356,7 +362,7 @@ func (vm *VirtualMachine) StartShutdown() error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (vm *VirtualMachine) WaitForShutdown(timeout time.Duration) error {
|
||||
func (vm *VirtualMachine) WaitForShutdown(ctx context.Context, timeout time.Duration) error {
|
||||
shutdownTimer := time.After(timeout)
|
||||
for {
|
||||
powerState, err := vm.vm.PowerState(vm.driver.ctx)
|
||||
|
@ -371,6 +377,8 @@ func (vm *VirtualMachine) WaitForShutdown(timeout time.Duration) error {
|
|||
case <-shutdownTimer:
|
||||
err := errors.New("Timeout while waiting for machine to shut down.")
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue