Commit 12a9362f authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/plugin: Swallow interrupts in plugins

parent 3956b3a5
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"net" "net"
"net/rpc" "net/rpc"
"os" "os"
"os/signal"
"strconv" "strconv"
"strings" "strings"
) )
...@@ -75,6 +76,19 @@ func serve(server *rpc.Server) (err error) { ...@@ -75,6 +76,19 @@ func serve(server *rpc.Server) (err error) {
return return
} }
// Registers a signal handler to "swallow" interrupts so that the
// plugin isn't killed. The main host Packer process is responsible
// for killing the plugins when interrupted.
func swallowInterrupts() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
<-ch
log.Println("Received interrupt signal. Ignoring.")
}()
}
// Serves a builder from a plugin. // Serves a builder from a plugin.
func ServeBuilder(builder packer.Builder) { func ServeBuilder(builder packer.Builder) {
log.Println("Preparing to serve a builder plugin...") log.Println("Preparing to serve a builder plugin...")
...@@ -82,6 +96,7 @@ func ServeBuilder(builder packer.Builder) { ...@@ -82,6 +96,7 @@ func ServeBuilder(builder packer.Builder) {
server := rpc.NewServer() server := rpc.NewServer()
packrpc.RegisterBuilder(server, builder) packrpc.RegisterBuilder(server, builder)
swallowInterrupts()
if err := serve(server); err != nil { if err := serve(server); err != nil {
log.Panic(err) log.Panic(err)
} }
...@@ -94,6 +109,7 @@ func ServeCommand(command packer.Command) { ...@@ -94,6 +109,7 @@ func ServeCommand(command packer.Command) {
server := rpc.NewServer() server := rpc.NewServer()
packrpc.RegisterCommand(server, command) packrpc.RegisterCommand(server, command)
swallowInterrupts()
if err := serve(server); err != nil { if err := serve(server); err != nil {
log.Panic(err) log.Panic(err)
} }
...@@ -106,6 +122,7 @@ func ServeHook(hook packer.Hook) { ...@@ -106,6 +122,7 @@ func ServeHook(hook packer.Hook) {
server := rpc.NewServer() server := rpc.NewServer()
packrpc.RegisterHook(server, hook) packrpc.RegisterHook(server, hook)
swallowInterrupts()
if err := serve(server); err != nil { if err := serve(server); err != nil {
log.Panic(err) log.Panic(err)
} }
...@@ -118,6 +135,7 @@ func ServeProvisioner(p packer.Provisioner) { ...@@ -118,6 +135,7 @@ func ServeProvisioner(p packer.Provisioner) {
server := rpc.NewServer() server := rpc.NewServer()
packrpc.RegisterProvisioner(server, p) packrpc.RegisterProvisioner(server, p)
swallowInterrupts()
if err := serve(server); err != nil { if err := serve(server); err != nil {
log.Panic(err) log.Panic(err)
} }
......
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