2013-05-07 22:50:34 -04:00
|
|
|
// The plugin package provides the functionality to both expose a Packer
|
|
|
|
// plugin binary and to connect to an existing Packer plugin binary.
|
|
|
|
//
|
|
|
|
// Packer supports plugins in the form of self-contained external static
|
|
|
|
// Go binaries. These binaries behave in a certain way (enforced by this
|
|
|
|
// package) and are connected to in a certain way (also enforced by this
|
|
|
|
// package).
|
2013-05-05 00:26:30 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-05-10 20:01:24 -04:00
|
|
|
packrpc "github.com/mitchellh/packer/packer/rpc"
|
2013-05-08 12:46:37 -04:00
|
|
|
"log"
|
2013-05-05 19:25:32 -04:00
|
|
|
"net"
|
|
|
|
"net/rpc"
|
2013-05-05 00:26:30 -04:00
|
|
|
"os"
|
2013-06-04 01:31:54 -04:00
|
|
|
"os/signal"
|
2013-05-06 18:27:44 -04:00
|
|
|
"strconv"
|
2013-05-09 01:25:47 -04:00
|
|
|
"strings"
|
2013-05-05 00:26:30 -04:00
|
|
|
)
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
// This serves a single RPC connection on the given RPC server on
|
|
|
|
// a random port.
|
|
|
|
func serve(server *rpc.Server) (err error) {
|
2013-05-06 18:27:44 -04:00
|
|
|
minPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MIN_PORT"), 10, 32)
|
2013-05-05 19:25:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-05-06 18:27:44 -04:00
|
|
|
|
|
|
|
maxPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MAX_PORT"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-08 12:46:37 -04:00
|
|
|
log.Printf("Plugin minimum port: %d\n", minPort)
|
2013-05-08 16:45:57 -04:00
|
|
|
log.Printf("Plugin maximum port: %d\n", maxPort)
|
2013-05-08 12:46:37 -04:00
|
|
|
|
2013-05-06 18:27:44 -04:00
|
|
|
var address string
|
|
|
|
var listener net.Listener
|
|
|
|
for port := minPort; port <= maxPort; port++ {
|
|
|
|
address = fmt.Sprintf(":%d", port)
|
|
|
|
listener, err = net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
2013-05-09 01:25:47 -04:00
|
|
|
if !strings.Contains(err.Error(), "address already in use") {
|
|
|
|
// Not an address already in use error, return.
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// Address is in use, just try another
|
|
|
|
err = nil
|
|
|
|
continue
|
|
|
|
}
|
2013-05-06 18:27:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
defer listener.Close()
|
2013-05-05 00:26:30 -04:00
|
|
|
|
|
|
|
// Output the address to stdout
|
2013-05-08 12:46:37 -04:00
|
|
|
log.Printf("Plugin address: %s\n", address)
|
2013-05-06 18:27:44 -04:00
|
|
|
fmt.Println(address)
|
2013-05-05 00:26:30 -04:00
|
|
|
os.Stdout.Sync()
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
// Accept a connection
|
2013-05-08 12:46:37 -04:00
|
|
|
log.Println("Waiting for connection...")
|
2013-05-05 19:25:32 -04:00
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2013-05-08 12:46:37 -04:00
|
|
|
log.Printf("Error accepting connection: %s\n", err.Error())
|
2013-05-05 19:25:32 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve a single connection
|
2013-05-08 12:46:37 -04:00
|
|
|
log.Println("Serving a plugin connection...")
|
2013-05-05 19:25:32 -04:00
|
|
|
server.ServeConn(conn)
|
|
|
|
return
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 01:31:54 -04:00
|
|
|
// 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.")
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-05-09 01:25:47 -04:00
|
|
|
// Serves a builder from a plugin.
|
|
|
|
func ServeBuilder(builder packer.Builder) {
|
|
|
|
log.Println("Preparing to serve a builder plugin...")
|
|
|
|
|
|
|
|
server := rpc.NewServer()
|
|
|
|
packrpc.RegisterBuilder(server, builder)
|
|
|
|
|
2013-06-04 01:31:54 -04:00
|
|
|
swallowInterrupts()
|
2013-05-09 01:25:47 -04:00
|
|
|
if err := serve(server); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
// Serves a command from a plugin.
|
2013-05-05 00:26:30 -04:00
|
|
|
func ServeCommand(command packer.Command) {
|
2013-05-08 12:46:37 -04:00
|
|
|
log.Println("Preparing to serve a command plugin...")
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
server := rpc.NewServer()
|
|
|
|
packrpc.RegisterCommand(server, command)
|
|
|
|
|
2013-06-04 01:31:54 -04:00
|
|
|
swallowInterrupts()
|
2013-05-05 19:25:32 -04:00
|
|
|
if err := serve(server); err != nil {
|
2013-05-08 12:46:37 -04:00
|
|
|
log.Panic(err)
|
2013-05-05 19:25:32 -04:00
|
|
|
}
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|
2013-05-11 13:46:17 -04:00
|
|
|
|
|
|
|
// Serves a hook from a plugin.
|
|
|
|
func ServeHook(hook packer.Hook) {
|
|
|
|
log.Println("Preparing to serve a hook plugin...")
|
|
|
|
|
|
|
|
server := rpc.NewServer()
|
|
|
|
packrpc.RegisterHook(server, hook)
|
|
|
|
|
2013-06-04 01:31:54 -04:00
|
|
|
swallowInterrupts()
|
2013-05-11 13:46:17 -04:00
|
|
|
if err := serve(server); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
2013-05-24 00:37:16 -04:00
|
|
|
|
|
|
|
// Serves a provisioner from a plugin.
|
|
|
|
func ServeProvisioner(p packer.Provisioner) {
|
|
|
|
log.Println("Preparing to serve a provisioner plugin...")
|
|
|
|
|
|
|
|
server := rpc.NewServer()
|
|
|
|
packrpc.RegisterProvisioner(server, p)
|
|
|
|
|
2013-06-04 01:31:54 -04:00
|
|
|
swallowInterrupts()
|
2013-05-24 00:37:16 -04:00
|
|
|
if err := serve(server); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|