common: detect ctrl-c in Provision

This commit is contained in:
Mitchell Hashimoto 2013-08-30 23:28:31 -07:00
parent 84001c7c76
commit 66f7f5aad5
1 changed files with 23 additions and 5 deletions

View File

@ -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{}) {}