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