2013-06-04 01:40:05 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2015-06-09 00:28:36 -04:00
|
|
|
"syscall"
|
2015-06-09 00:26:28 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/mitchellh/packer/packer/plugin"
|
2013-06-04 01:40:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Prepares the signal handlers so that we handle interrupts properly.
|
|
|
|
// The signal handler exists in a goroutine.
|
2015-05-25 20:29:10 -04:00
|
|
|
func setupSignalHandlers(ui packer.Ui) {
|
2013-06-04 01:40:05 -04:00
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
2015-06-09 00:28:36 -04:00
|
|
|
signal.Notify(ch, syscall.SIGTERM)
|
2013-06-04 01:40:05 -04:00
|
|
|
|
|
|
|
go func() {
|
2013-07-25 22:36:46 -04:00
|
|
|
// First interrupt. We mostly ignore this because it allows the
|
|
|
|
// plugins time to cleanup.
|
2013-06-04 01:40:05 -04:00
|
|
|
<-ch
|
2013-07-26 02:27:13 -04:00
|
|
|
log.Println("First interrupt. Ignoring to allow plugins to clean up.")
|
2013-07-25 22:36:46 -04:00
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
ui.Error("Interrupt signal received. Cleaning up...")
|
2013-08-24 06:50:49 -04:00
|
|
|
|
2013-07-25 22:36:46 -04:00
|
|
|
// Second interrupt. Go down hard.
|
2013-06-04 01:40:05 -04:00
|
|
|
<-ch
|
|
|
|
log.Println("Second interrupt. Exiting now.")
|
|
|
|
|
2015-05-25 20:29:10 -04:00
|
|
|
ui.Error("Interrupt signal received twice. Forcefully exiting now.")
|
2013-06-04 01:40:05 -04:00
|
|
|
|
2013-08-20 02:38:22 -04:00
|
|
|
// Force kill all the plugins, but mark that we're killing them
|
|
|
|
// first so that we don't get panics everywhere.
|
2013-06-04 01:40:05 -04:00
|
|
|
plugin.CleanupClients()
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
}
|