github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/signal.go (about) 1 package main 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "github.com/mitchellh/packer/packer/plugin" 6 "log" 7 "os" 8 "os/signal" 9 ) 10 11 // Prepares the signal handlers so that we handle interrupts properly. 12 // The signal handler exists in a goroutine. 13 func setupSignalHandlers(env packer.Environment) { 14 ch := make(chan os.Signal, 1) 15 signal.Notify(ch, os.Interrupt) 16 17 go func() { 18 // First interrupt. We mostly ignore this because it allows the 19 // plugins time to cleanup. 20 <-ch 21 log.Println("First interrupt. Ignoring to allow plugins to clean up.") 22 23 env.Ui().Error("Interrupt signal received. Cleaning up...") 24 25 // Second interrupt. Go down hard. 26 <-ch 27 log.Println("Second interrupt. Exiting now.") 28 29 env.Ui().Error("Interrupt signal received twice. Forcefully exiting now.") 30 31 // Force kill all the plugins, but mark that we're killing them 32 // first so that we don't get panics everywhere. 33 plugin.CleanupClients() 34 os.Exit(1) 35 }() 36 }