forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.go
39 lines (31 loc) · 980 Bytes
/
signal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer/plugin"
)
// Prepares the signal handlers so that we handle interrupts properly.
// The signal handler exists in a goroutine.
func setupSignalHandlers(ui packer.Ui) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, syscall.SIGTERM)
go func() {
// First interrupt. We mostly ignore this because it allows the
// plugins time to cleanup.
<-ch
log.Println("First interrupt. Ignoring to allow plugins to clean up.")
ui.Error("Interrupt signal received. Cleaning up...")
// Second interrupt. Go down hard.
<-ch
log.Println("Second interrupt. Exiting now.")
ui.Error("Interrupt signal received twice. Forcefully exiting now.")
// Force kill all the plugins, but mark that we're killing them
// first so that we don't get panics everywhere.
plugin.CleanupClients()
os.Exit(1)
}()
}