diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index dcf6fea70..9186c868c 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -37,10 +37,6 @@ type StepRunSourceInstance struct { } func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction { - return s.RunWithContext(context.Background(), state) -} - -func (s *StepRunSourceInstance) RunWithContext(ctx context.Context, state multistep.StateBag) multistep.StepAction { ec2conn := state.Get("ec2").(*ec2.EC2) var keyName string if name, ok := state.GetOk("keyPair"); ok { @@ -183,6 +179,15 @@ func (s *StepRunSourceInstance) RunWithContext(ctx context.Context, state multis describeInstance := &ec2.DescribeInstancesInput{ InstanceIds: []*string{aws.String(instanceId)}, } + ctx, cancel := context.WithCancel(context.Background()) + + go func() { + for { + if _, ok := state.GetOk(multistep.StateCancelled); ok { + cancel() + } + } + }() if err := ec2conn.WaitUntilInstanceRunningWithContext(ctx, describeInstance); err != nil { err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", instanceId, err) diff --git a/helper/multistep/basic_runner.go b/helper/multistep/basic_runner.go index f152178bc..eb5018d36 100644 --- a/helper/multistep/basic_runner.go +++ b/helper/multistep/basic_runner.go @@ -70,13 +70,7 @@ func (b *BasicRunner) Run(state StateBag) { break } - var action StepAction - - if stepCtx, ok := step.(StepRunnableWithContext); ok { - action = stepCtx.RunWithContext(ctx, state) - } else { - action = step.Run(state) - } + action := step.Run(ctx, state) defer step.Cleanup(state) if _, ok := state.GetOk(StateCancelled); ok { diff --git a/helper/multistep/debug_runner.go b/helper/multistep/debug_runner.go index 882009494..88af01c54 100644 --- a/helper/multistep/debug_runner.go +++ b/helper/multistep/debug_runner.go @@ -1,6 +1,7 @@ package multistep import ( + "context" "fmt" "reflect" "sync" @@ -113,7 +114,7 @@ type debugStepPause struct { PauseFn DebugPauseFn } -func (s *debugStepPause) Run(state StateBag) StepAction { +func (s *debugStepPause) Run(_ context.Context, state StateBag) StepAction { s.PauseFn(DebugLocationAfterRun, s.StepName, state) return ActionContinue } diff --git a/helper/multistep/multistep.go b/helper/multistep/multistep.go index 9b8a40b6d..7b4e0801f 100644 --- a/helper/multistep/multistep.go +++ b/helper/multistep/multistep.go @@ -19,10 +19,6 @@ const StateCancelled = "cancelled" // This is the key set in the state bag when a step halted the sequence. const StateHalted = "halted" -type StepRunnableWithContext interface { - RunWithContext(context.Context, StateBag) StepAction -} - // Step is a single step that is part of a potentially large sequence // of other steps, responsible for performing some specific action. type Step interface { @@ -32,7 +28,7 @@ type Step interface { // // The return value determines whether multi-step sequences continue // or should halt. - Run(StateBag) StepAction + Run(context.Context, StateBag) StepAction // Cleanup is called in reverse order of the steps that have run // and allow steps to clean up after themselves. Do not assume if this