From 3956b3a5153daae807e922e0b004f6fcddfe7bf8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 3 Jun 2013 16:14:10 -0700 Subject: [PATCH] command/build: Cancel builds when interrupted --- command/build/command.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/command/build/command.go b/command/build/command.go index c6371aa0d..1b95fbba0 100644 --- a/command/build/command.go +++ b/command/build/command.go @@ -5,6 +5,8 @@ import ( "github.com/mitchellh/packer/packer" "io/ioutil" "log" + "os" + "os/signal" "strings" "sync" ) @@ -112,6 +114,29 @@ func (c Command) Run(env packer.Environment, args []string) int { }() } + // Handle signals + sigCh := make(chan os.Signal, 1) + signal.Notify(sigCh, os.Interrupt) + + go func() { + <-sigCh + log.Println("Interrupted! Cancelling builds...") + + var wg sync.WaitGroup + for _, b := range builds { + wg.Add(1) + + go func() { + defer wg.Done() + + log.Printf("Stopping build: %s", b.Name()) + b.Cancel() + }() + } + + wg.Wait() + }() + wg.Wait() // Output all the artifacts