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 (
|
2013-06-25 15:27:12 -04:00
|
|
|
"errors"
|
2013-05-05 00:26:30 -04:00
|
|
|
"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"
|
2013-05-05 00:26:30 -04:00
|
|
|
"os"
|
2013-06-04 01:31:54 -04:00
|
|
|
"os/signal"
|
2013-06-05 20:39:27 -04:00
|
|
|
"runtime"
|
2013-05-06 18:27:44 -04:00
|
|
|
"strconv"
|
2013-08-30 19:29:21 -04:00
|
|
|
"sync/atomic"
|
2013-05-05 00:26:30 -04:00
|
|
|
)
|
|
|
|
|
2013-08-30 19:29:21 -04:00
|
|
|
// This is a count of the number of interrupts the process has received.
|
|
|
|
// This is updated with sync/atomic whenever a SIGINT is received and can
|
|
|
|
// be checked by the plugin safely to take action.
|
|
|
|
var Interrupts int32 = 0
|
|
|
|
|
2013-06-25 15:27:12 -04:00
|
|
|
const MagicCookieKey = "PACKER_PLUGIN_MAGIC_COOKIE"
|
|
|
|
const MagicCookieValue = "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2"
|
|
|
|
|
2013-09-22 12:51:14 -04:00
|
|
|
// The APIVersion is outputted along with the RPC address. The plugin
|
|
|
|
// client validates this API version and will show an error if it doesn't
|
|
|
|
// know how to speak it.
|
|
|
|
const APIVersion = "1"
|
|
|
|
|
2013-12-10 16:47:18 -05:00
|
|
|
// Server waits for a connection to this plugin and returns a Packer
|
|
|
|
// RPC server that you can use to register components and serve them.
|
|
|
|
func Server() (*packrpc.Server, error) {
|
2013-09-22 12:39:27 -04:00
|
|
|
log.Printf("Plugin build against Packer '%s'", packer.GitCommit)
|
|
|
|
|
2013-06-25 15:27:12 -04:00
|
|
|
if os.Getenv(MagicCookieKey) != MagicCookieValue {
|
2013-12-10 16:47:18 -05:00
|
|
|
return nil, errors.New(
|
|
|
|
"Please do not execute plugins directly. Packer will execute these for you.")
|
2013-06-25 15:27:12 -04:00
|
|
|
}
|
|
|
|
|
2013-06-05 20:39:27 -04:00
|
|
|
// If there is no explicit number of Go threads to use, then set it
|
|
|
|
if os.Getenv("GOMAXPROCS") == "" {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-12-10 16:47:18 -05:00
|
|
|
return nil, err
|
2013-05-05 19:25:32 -04:00
|
|
|
}
|
2013-05-06 18:27:44 -04:00
|
|
|
|
|
|
|
maxPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MAX_PORT"), 10, 32)
|
|
|
|
if err != nil {
|
2013-12-10 16:47:18 -05:00
|
|
|
return nil, err
|
2013-05-06 18:27:44 -04:00
|
|
|
}
|
|
|
|
|
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-06-11 14:14:36 -04:00
|
|
|
// Set the RPC port range
|
|
|
|
packrpc.PortRange(int(minPort), int(maxPort))
|
|
|
|
|
2013-05-06 18:27:44 -04:00
|
|
|
var address string
|
|
|
|
var listener net.Listener
|
|
|
|
for port := minPort; port <= maxPort; port++ {
|
2013-06-28 21:45:18 -04:00
|
|
|
address = fmt.Sprintf("127.0.0.1:%d", port)
|
2013-05-06 18:27:44 -04:00
|
|
|
listener, err = net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
2013-06-29 16:35:24 -04:00
|
|
|
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-09-22 12:51:14 -04:00
|
|
|
fmt.Printf("%s|%s\n", APIVersion, 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-12-10 16:47:18 -05:00
|
|
|
return nil, err
|
2013-05-05 19:25:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve a single connection
|
2013-05-08 12:46:37 -04:00
|
|
|
log.Println("Serving a plugin connection...")
|
2013-12-10 16:47:18 -05:00
|
|
|
return packrpc.NewServer(conn), nil
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|
|
|
|
|
2013-08-30 19:29:21 -04:00
|
|
|
// Registers a signal handler to swallow and count interrupts so that the
|
2013-06-04 01:31:54 -04:00
|
|
|
// plugin isn't killed. The main host Packer process is responsible
|
|
|
|
// for killing the plugins when interrupted.
|
2013-08-30 19:29:21 -04:00
|
|
|
func countInterrupts() {
|
2013-06-04 01:31:54 -04:00
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
2013-08-23 17:22:32 -04:00
|
|
|
for {
|
|
|
|
<-ch
|
2013-08-30 19:29:21 -04:00
|
|
|
newCount := atomic.AddInt32(&Interrupts, 1)
|
|
|
|
log.Printf("Received interrupt signal (count: %d). Ignoring.", newCount)
|
2013-08-23 17:22:32 -04:00
|
|
|
}
|
2013-06-04 01:31:54 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-08-30 19:39:28 -04:00
|
|
|
// Tests whether or not the plugin was interrupted or not.
|
|
|
|
func Interrupted() bool {
|
|
|
|
return atomic.LoadInt32(&Interrupts) > 0
|
|
|
|
}
|