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"
|
2013-05-08 12:46:37 -04:00
|
|
|
"log"
|
2015-10-11 15:35:13 -04:00
|
|
|
"math/rand"
|
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"
|
2017-09-08 14:31:19 -04:00
|
|
|
"syscall"
|
2015-10-11 15:35:13 -04:00
|
|
|
"time"
|
2017-09-08 14:31:19 -04:00
|
|
|
|
2020-12-09 06:39:54 -05:00
|
|
|
packrpc "github.com/hashicorp/packer/packer-plugin-sdk/rpc"
|
2020-11-16 18:13:08 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/tmp"
|
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.
|
2019-12-16 14:46:46 -05:00
|
|
|
const APIVersion = "5"
|
2013-09-22 12:51:14 -04:00
|
|
|
|
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-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())
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:21:45 -05:00
|
|
|
listener, err := serverListener()
|
2013-12-11 15:24:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-05-06 18:27:44 -04:00
|
|
|
}
|
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-12-11 15:24:45 -05:00
|
|
|
log.Printf("Plugin address: %s %s\n",
|
|
|
|
listener.Addr().Network(), listener.Addr().String())
|
|
|
|
fmt.Printf("%s|%s|%s\n",
|
|
|
|
APIVersion,
|
|
|
|
listener.Addr().Network(),
|
|
|
|
listener.Addr().String())
|
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
|
|
|
}
|
|
|
|
|
2013-12-10 17:12:00 -05:00
|
|
|
// Eat the interrupts
|
2013-06-04 01:31:54 -04:00
|
|
|
ch := make(chan os.Signal, 1)
|
2017-09-08 14:31:19 -04:00
|
|
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
2013-06-04 01:31:54 -04:00
|
|
|
go func() {
|
2013-12-10 17:12:00 -05:00
|
|
|
var count int32 = 0
|
2013-08-23 17:22:32 -04:00
|
|
|
for {
|
|
|
|
<-ch
|
2013-12-10 17:12:00 -05:00
|
|
|
newCount := atomic.AddInt32(&count, 1)
|
2013-08-30 19:29:21 -04:00
|
|
|
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-12-10 17:12:00 -05:00
|
|
|
// Serve a single connection
|
|
|
|
log.Println("Serving a plugin connection...")
|
2019-03-27 07:29:09 -04:00
|
|
|
return packrpc.NewServer(conn)
|
2013-08-30 19:39:28 -04:00
|
|
|
}
|
2013-12-11 15:24:45 -05:00
|
|
|
|
2019-12-09 15:21:45 -05:00
|
|
|
func serverListener() (net.Listener, error) {
|
2013-12-11 15:24:45 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
2019-12-09 15:21:45 -05:00
|
|
|
return serverListener_tcp()
|
2013-12-11 15:24:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return serverListener_unix()
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:21:45 -05:00
|
|
|
func serverListener_tcp() (net.Listener, error) {
|
|
|
|
minPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MIN_PORT"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
maxPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MAX_PORT"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
log.Printf("Plugin port range: [%d,%d]", minPort, maxPort)
|
2019-12-09 15:21:45 -05:00
|
|
|
|
2013-12-11 15:24:45 -05:00
|
|
|
for port := minPort; port <= maxPort; port++ {
|
|
|
|
address := fmt.Sprintf("127.0.0.1:%d", port)
|
|
|
|
listener, err := net.Listen("tcp", address)
|
|
|
|
if err == nil {
|
|
|
|
return listener, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("Couldn't bind plugin TCP listener")
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverListener_unix() (net.Listener, error) {
|
2018-12-12 09:45:00 -05:00
|
|
|
tf, err := tmp.File("packer-plugin")
|
2013-12-11 15:24:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path := tf.Name()
|
|
|
|
|
|
|
|
// Close the file and remove it because it has to not exist for
|
|
|
|
// the domain socket.
|
|
|
|
if err := tf.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.Listen("unix", path)
|
|
|
|
}
|
2015-10-11 15:35:13 -04:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Seed the random number generator
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
}
|