Commit 2dd5a982 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

command/build: Cleanly exit after being interrupted

parent 256b97ce
...@@ -115,11 +115,17 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -115,11 +115,17 @@ func (c Command) Run(env packer.Environment, args []string) int {
} }
// Handle signals // Handle signals
var interruptWg sync.WaitGroup
interrupted := false
sigCh := make(chan os.Signal, 1) sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt) signal.Notify(sigCh, os.Interrupt)
go func() { go func() {
<-sigCh <-sigCh
interruptWg.Add(1)
defer interruptWg.Done()
interrupted = true
log.Println("Interrupted! Cancelling builds...") log.Println("Interrupted! Cancelling builds...")
var wg sync.WaitGroup var wg sync.WaitGroup
...@@ -137,7 +143,15 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -137,7 +143,15 @@ func (c Command) Run(env packer.Environment, args []string) int {
wg.Wait() wg.Wait()
}() }()
// Wait for both the builds to complete and the interrupt handler,
// if it is interrupted.
wg.Wait() wg.Wait()
interruptWg.Wait()
if interrupted {
env.Ui().Say("Cleanly cancelled builds after being interrupted.")
return 1
}
// Output all the artifacts // Output all the artifacts
env.Ui().Say("\n==> The build completed! The artifacts created were:") env.Ui().Say("\n==> The build completed! The artifacts created were:")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment