From 9f559cb25cf50774314f89e6ed1d7e93870ed6af Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 30 Aug 2013 23:28:31 -0700 Subject: [PATCH] common: detect ctrl-c in Provision --- common/step_provision.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/common/step_provision.go b/common/step_provision.go index 893f13431..b51896208 100644 --- a/common/step_provision.go +++ b/common/step_provision.go @@ -4,6 +4,7 @@ import ( "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" "log" + "time" ) // StepProvision runs the provisioners. @@ -22,13 +23,30 @@ func (*StepProvision) Run(state map[string]interface{}) multistep.StepAction { hook := state["hook"].(packer.Hook) ui := state["ui"].(packer.Ui) + // Run the provisioner in a goroutine so we can continually check + // for cancellations... log.Println("Running the provision hook") - if err := hook.Run(packer.HookProvision, ui, comm, nil); err != nil { - state["error"] = err - return multistep.ActionHalt - } + errCh := make(chan error, 1) + go func() { + errCh <- hook.Run(packer.HookProvision, ui, comm, nil) + }() - return multistep.ActionContinue + for { + select { + case err := <-errCh: + if err != nil { + state["error"] = err + return multistep.ActionHalt + } + + return multistep.ActionContinue + case <-time.After(1 * time.Second): + if _, ok := state[multistep.StateCancelled]; ok { + hook.Cancel() + return multistep.ActionHalt + } + } + } } func (*StepProvision) Cleanup(map[string]interface{}) {}