command/build: Cancel builds when interrupted

This commit is contained in:
Mitchell Hashimoto 2013-06-03 16:14:10 -07:00
parent 94cfe39a76
commit 3956b3a515
1 changed files with 25 additions and 0 deletions

View File

@ -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