2013-05-05 00:26:30 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2013-07-30 13:38:48 -04:00
|
|
|
"bufio"
|
2013-05-08 14:14:21 -04:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2013-06-11 13:39:06 -04:00
|
|
|
"fmt"
|
2013-06-11 14:00:06 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
packrpc "github.com/mitchellh/packer/packer/rpc"
|
2013-05-08 14:14:21 -04:00
|
|
|
"io"
|
2013-07-30 13:41:02 -04:00
|
|
|
"io/ioutil"
|
2013-05-08 14:14:21 -04:00
|
|
|
"log"
|
2013-06-11 14:00:06 -04:00
|
|
|
"net/rpc"
|
2013-06-05 23:39:39 -04:00
|
|
|
"os"
|
2013-05-05 00:26:30 -04:00
|
|
|
"os/exec"
|
2013-05-08 14:14:21 -04:00
|
|
|
"strings"
|
2013-05-08 14:54:59 -04:00
|
|
|
"sync"
|
2013-05-08 14:14:21 -04:00
|
|
|
"time"
|
2013-05-05 00:26:30 -04:00
|
|
|
)
|
|
|
|
|
2013-05-08 14:54:59 -04:00
|
|
|
// This is a slice of the "managed" clients which are cleaned up when
|
|
|
|
// calling Cleanup
|
2013-06-11 14:03:36 -04:00
|
|
|
var managedClients = make([]*Client, 0, 5)
|
2013-05-08 14:54:59 -04:00
|
|
|
|
2013-06-11 14:03:36 -04:00
|
|
|
// Client handles the lifecycle of a plugin application, determining its
|
|
|
|
// RPC address, and returning various types of packer interface implementations
|
|
|
|
// across the multi-process communication layer.
|
|
|
|
type Client struct {
|
2013-06-11 17:09:31 -04:00
|
|
|
config *ClientConfig
|
2013-05-10 20:01:24 -04:00
|
|
|
exited bool
|
2013-07-30 13:43:43 -04:00
|
|
|
doneLogging chan struct{}
|
2013-06-11 14:00:06 -04:00
|
|
|
l sync.Mutex
|
|
|
|
address string
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 13:39:06 -04:00
|
|
|
// ClientConfig is the configuration used to initialize a new
|
|
|
|
// plugin client. After being used to initialize a plugin client,
|
|
|
|
// that configuration must not be modified again.
|
|
|
|
type ClientConfig struct {
|
|
|
|
// The unstarted subprocess for starting the plugin.
|
|
|
|
Cmd *exec.Cmd
|
|
|
|
|
|
|
|
// Managed represents if the client should be managed by the
|
|
|
|
// plugin package or not. If true, then by calling CleanupClients,
|
|
|
|
// it will automatically be cleaned up. Otherwise, the client
|
|
|
|
// user is fully responsible for making sure to Kill all plugin
|
2013-06-11 14:06:23 -04:00
|
|
|
// clients. By default the client is _not_ managed.
|
2013-06-11 13:39:06 -04:00
|
|
|
Managed bool
|
|
|
|
|
|
|
|
// The minimum and maximum port to use for communicating with
|
|
|
|
// the subprocess. If not set, this defaults to 10,000 and 25,000
|
|
|
|
// respectively.
|
|
|
|
MinPort, MaxPort uint
|
|
|
|
|
|
|
|
// StartTimeout is the timeout to wait for the plugin to say it
|
|
|
|
// has started successfully.
|
|
|
|
StartTimeout time.Duration
|
2013-07-30 13:38:48 -04:00
|
|
|
|
|
|
|
// If non-nil, then the stderr of the client will be written to here
|
|
|
|
// (as well as the log).
|
|
|
|
Stderr io.Writer
|
2013-06-11 13:39:06 -04:00
|
|
|
}
|
|
|
|
|
2013-05-08 14:54:59 -04:00
|
|
|
// This makes sure all the managed subprocesses are killed and properly
|
|
|
|
// logged. This should be called before the parent process running the
|
|
|
|
// plugins exits.
|
2013-05-09 17:27:20 -04:00
|
|
|
//
|
|
|
|
// This must only be called _once_.
|
2013-05-08 14:54:59 -04:00
|
|
|
func CleanupClients() {
|
|
|
|
// Kill all the managed clients in parallel and use a WaitGroup
|
|
|
|
// to wait for them all to finish up.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, client := range managedClients {
|
|
|
|
wg.Add(1)
|
|
|
|
|
2013-06-11 14:10:08 -04:00
|
|
|
go func(client *Client) {
|
2013-05-08 14:54:59 -04:00
|
|
|
client.Kill()
|
|
|
|
wg.Done()
|
2013-06-11 14:10:08 -04:00
|
|
|
}(client)
|
2013-05-08 14:54:59 -04:00
|
|
|
}
|
|
|
|
|
2013-05-08 15:43:41 -04:00
|
|
|
log.Println("waiting for all plugin processes to complete...")
|
2013-05-08 14:54:59 -04:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2013-05-09 17:27:20 -04:00
|
|
|
// Creates a new plugin client which manages the lifecycle of an external
|
|
|
|
// plugin and gets the address for the RPC connection.
|
|
|
|
//
|
|
|
|
// The client must be cleaned up at some point by calling Kill(). If
|
|
|
|
// the client is a managed client (created with NewManagedClient) you
|
|
|
|
// can just call CleanupClients at the end of your program and they will
|
|
|
|
// be properly cleaned.
|
2013-06-11 14:03:36 -04:00
|
|
|
func NewClient(config *ClientConfig) (c *Client) {
|
2013-06-11 13:39:06 -04:00
|
|
|
if config.MinPort == 0 && config.MaxPort == 0 {
|
|
|
|
config.MinPort = 10000
|
|
|
|
config.MaxPort = 25000
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.StartTimeout == 0 {
|
|
|
|
config.StartTimeout = 1 * time.Minute
|
|
|
|
}
|
|
|
|
|
2013-07-30 13:41:02 -04:00
|
|
|
if config.Stderr == nil {
|
|
|
|
config.Stderr = ioutil.Discard
|
|
|
|
}
|
|
|
|
|
2013-06-11 14:03:36 -04:00
|
|
|
c = &Client{config: config}
|
2013-06-11 13:39:06 -04:00
|
|
|
if config.Managed {
|
|
|
|
managedClients = append(managedClients, c)
|
|
|
|
}
|
|
|
|
|
2013-05-08 14:54:59 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-09 17:27:20 -04:00
|
|
|
// Tells whether or not the underlying process has exited.
|
2013-06-11 14:03:36 -04:00
|
|
|
func (c *Client) Exited() bool {
|
2013-05-08 14:14:21 -04:00
|
|
|
return c.exited
|
|
|
|
}
|
|
|
|
|
2013-06-11 14:00:06 -04:00
|
|
|
// Returns a builder implementation that is communicating over this
|
|
|
|
// client. If the client hasn't been started, this will start it.
|
2013-06-11 14:03:36 -04:00
|
|
|
func (c *Client) Builder() (packer.Builder, error) {
|
2013-06-11 14:00:06 -04:00
|
|
|
client, err := c.rpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cmdBuilder{packrpc.Builder(client), c}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a command implementation that is communicating over this
|
|
|
|
// client. If the client hasn't been started, this will start it.
|
2013-06-11 14:03:36 -04:00
|
|
|
func (c *Client) Command() (packer.Command, error) {
|
2013-06-11 14:00:06 -04:00
|
|
|
client, err := c.rpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cmdCommand{packrpc.Command(client), c}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a hook implementation that is communicating over this
|
|
|
|
// client. If the client hasn't been started, this will start it.
|
2013-06-11 14:03:36 -04:00
|
|
|
func (c *Client) Hook() (packer.Hook, error) {
|
2013-06-11 14:00:06 -04:00
|
|
|
client, err := c.rpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cmdHook{packrpc.Hook(client), c}, nil
|
|
|
|
}
|
|
|
|
|
2013-06-18 14:00:31 -04:00
|
|
|
// Returns a post-processor implementation that is communicating over
|
|
|
|
// this client. If the client hasn't been started, this will start it.
|
|
|
|
func (c *Client) PostProcessor() (packer.PostProcessor, error) {
|
2013-06-18 16:49:07 -04:00
|
|
|
client, err := c.rpcClient()
|
2013-06-18 14:00:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:49:07 -04:00
|
|
|
return &cmdPostProcessor{packrpc.PostProcessor(client), c}, nil
|
2013-06-18 14:00:31 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 14:00:06 -04:00
|
|
|
// Returns a provisioner implementation that is communicating over this
|
|
|
|
// client. If the client hasn't been started, this will start it.
|
2013-06-11 14:03:36 -04:00
|
|
|
func (c *Client) Provisioner() (packer.Provisioner, error) {
|
2013-06-11 14:00:06 -04:00
|
|
|
client, err := c.rpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cmdProvisioner{packrpc.Provisioner(client), c}, nil
|
|
|
|
}
|
|
|
|
|
2013-06-11 13:39:06 -04:00
|
|
|
// End the executing subprocess (if it is running) and perform any cleanup
|
|
|
|
// tasks necessary such as capturing any remaining logs and so on.
|
|
|
|
//
|
|
|
|
// This method blocks until the process successfully exits.
|
|
|
|
//
|
|
|
|
// This method can safely be called multiple times.
|
2013-06-11 14:03:36 -04:00
|
|
|
func (c *Client) Kill() {
|
2013-06-11 13:39:06 -04:00
|
|
|
cmd := c.config.Cmd
|
|
|
|
|
|
|
|
if cmd.Process == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Process.Kill()
|
|
|
|
|
|
|
|
// Wait for the client to finish logging so we have a complete log
|
2013-07-30 13:43:43 -04:00
|
|
|
<-c.doneLogging
|
2013-06-11 13:39:06 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 17:27:20 -04:00
|
|
|
// Starts the underlying subprocess, communicating with it to negotiate
|
|
|
|
// a port for RPC connections, and returning the address to connect via RPC.
|
|
|
|
//
|
|
|
|
// This method is safe to call multiple times. Subsequent calls have no effect.
|
|
|
|
// Once a client has been started once, it cannot be started again, even if
|
|
|
|
// it was killed.
|
2013-06-11 14:03:36 -04:00
|
|
|
func (c *Client) Start() (address string, err error) {
|
2013-06-11 14:00:06 -04:00
|
|
|
c.l.Lock()
|
|
|
|
defer c.l.Unlock()
|
2013-06-11 13:43:55 -04:00
|
|
|
|
2013-06-11 14:00:06 -04:00
|
|
|
if c.address != "" {
|
|
|
|
return c.address, nil
|
2013-06-11 13:43:55 -04:00
|
|
|
}
|
2013-05-09 17:27:20 -04:00
|
|
|
|
2013-07-30 13:43:43 -04:00
|
|
|
c.doneLogging = make(chan struct{})
|
|
|
|
|
2013-05-08 14:14:21 -04:00
|
|
|
env := []string{
|
2013-06-25 15:27:12 -04:00
|
|
|
fmt.Sprintf("%s=%s", MagicCookieKey, MagicCookieValue),
|
2013-06-11 13:39:06 -04:00
|
|
|
fmt.Sprintf("PACKER_PLUGIN_MIN_PORT=%d", c.config.MinPort),
|
|
|
|
fmt.Sprintf("PACKER_PLUGIN_MAX_PORT=%d", c.config.MaxPort),
|
2013-05-08 14:14:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
stdout := new(bytes.Buffer)
|
2013-07-30 13:38:48 -04:00
|
|
|
stderr_r, stderr_w := io.Pipe()
|
2013-06-11 13:39:06 -04:00
|
|
|
|
|
|
|
cmd := c.config.Cmd
|
|
|
|
cmd.Env = append(cmd.Env, os.Environ()...)
|
|
|
|
cmd.Env = append(cmd.Env, env...)
|
2013-07-25 22:24:49 -04:00
|
|
|
cmd.Stdin = os.Stdin
|
2013-07-30 13:38:48 -04:00
|
|
|
cmd.Stderr = stderr_w
|
2013-06-11 13:39:06 -04:00
|
|
|
cmd.Stdout = stdout
|
2013-06-28 15:51:31 -04:00
|
|
|
|
|
|
|
log.Printf("Starting plugin: %s %#v", cmd.Path, cmd.Args)
|
2013-06-11 13:39:06 -04:00
|
|
|
err = cmd.Start()
|
2013-05-08 14:14:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the command is properly cleaned up if there is an error
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
|
|
|
|
if err != nil || r != nil {
|
2013-06-11 13:39:06 -04:00
|
|
|
cmd.Process.Kill()
|
2013-05-08 14:14:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start goroutine to wait for process to exit
|
|
|
|
go func() {
|
2013-07-30 13:48:59 -04:00
|
|
|
// Make sure we close the write end of our stderr listener so
|
|
|
|
// that the log goroutine ends properly.
|
2013-07-30 13:38:48 -04:00
|
|
|
defer stderr_w.Close()
|
2013-07-30 13:48:59 -04:00
|
|
|
|
|
|
|
// Wait for the command to end.
|
2013-06-11 13:39:06 -04:00
|
|
|
cmd.Wait()
|
2013-07-30 13:48:59 -04:00
|
|
|
|
|
|
|
// Log and make sure to flush the logs write away
|
2013-06-11 13:39:06 -04:00
|
|
|
log.Printf("%s: plugin process exited\n", cmd.Path)
|
2013-07-30 13:48:59 -04:00
|
|
|
os.Stderr.Sync()
|
|
|
|
|
|
|
|
// Mark that we exited
|
2013-05-08 14:14:21 -04:00
|
|
|
c.exited = true
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start goroutine that logs the stderr
|
2013-07-30 13:38:48 -04:00
|
|
|
go c.logStderr(stderr_r)
|
2013-05-08 14:14:21 -04:00
|
|
|
|
|
|
|
// Some channels for the next step
|
2013-06-11 13:39:06 -04:00
|
|
|
timeout := time.After(c.config.StartTimeout)
|
2013-05-08 14:14:21 -04:00
|
|
|
|
|
|
|
// Start looking for the address
|
2013-07-01 14:01:23 -04:00
|
|
|
log.Printf("Waiting for RPC address for: %s", cmd.Path)
|
2013-05-08 14:14:21 -04:00
|
|
|
for done := false; !done; {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
err = errors.New("timeout while waiting for plugin to start")
|
|
|
|
done = true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && c.Exited() {
|
|
|
|
err = errors.New("plugin exited before we could connect")
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if line, lerr := stdout.ReadBytes('\n'); lerr == nil {
|
|
|
|
// Trim the address and reset the err since we were able
|
|
|
|
// to read some sort of address.
|
2013-06-11 14:00:06 -04:00
|
|
|
c.address = strings.TrimSpace(string(line))
|
|
|
|
address = c.address
|
2013-05-08 14:14:21 -04:00
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If error is nil from previously, return now
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait a bit
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-07-30 13:38:48 -04:00
|
|
|
func (c *Client) logStderr(r io.Reader) {
|
|
|
|
bufR := bufio.NewReader(r)
|
|
|
|
for {
|
|
|
|
line, err := bufR.ReadString('\n')
|
|
|
|
if line != "" {
|
|
|
|
log.Printf("%s: %s", c.config.Cmd.Path, line)
|
2013-07-30 13:41:02 -04:00
|
|
|
c.config.Stderr.Write([]byte(line))
|
2013-05-08 14:14:21 -04:00
|
|
|
}
|
|
|
|
|
2013-07-30 13:38:48 -04:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2013-05-08 14:14:21 -04:00
|
|
|
}
|
2013-05-08 14:23:24 -04:00
|
|
|
|
|
|
|
// Flag that we've completed logging for others
|
2013-07-30 13:43:43 -04:00
|
|
|
close(c.doneLogging)
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|
2013-06-11 14:00:06 -04:00
|
|
|
|
2013-06-11 14:03:36 -04:00
|
|
|
func (c *Client) rpcClient() (*rpc.Client, error) {
|
2013-06-11 14:00:06 -04:00
|
|
|
address, err := c.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := rpc.Dial("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|