2013-05-05 00:26:30 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2013-05-08 14:14:21 -04:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"log"
|
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
|
|
|
|
var managedClients = make([]*client, 0, 5)
|
|
|
|
|
2013-05-08 14:14:21 -04:00
|
|
|
type client struct {
|
2013-06-01 22:35:19 -04:00
|
|
|
StartTimeout time.Duration
|
|
|
|
|
2013-05-10 20:01:24 -04:00
|
|
|
cmd *exec.Cmd
|
|
|
|
exited bool
|
2013-05-08 14:23:24 -04:00
|
|
|
doneLogging bool
|
2013-05-05 00:26:30 -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)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
client.Kill()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
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-05-08 14:14:21 -04:00
|
|
|
func NewClient(cmd *exec.Cmd) *client {
|
|
|
|
return &client{
|
2013-06-01 22:35:19 -04:00
|
|
|
1 * time.Minute,
|
2013-05-08 14:14:21 -04:00
|
|
|
cmd,
|
|
|
|
false,
|
2013-05-08 14:23:24 -04:00
|
|
|
false,
|
2013-05-08 14:14:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 17:27:20 -04:00
|
|
|
// Creates a new client that is managed, meaning it'll automatically be
|
|
|
|
// cleaned up when CleanupClients() is called at some point. Please see
|
|
|
|
// the documentation for CleanupClients() for more information on how
|
|
|
|
// managed clients work.
|
2013-05-08 14:54:59 -04:00
|
|
|
func NewManagedClient(cmd *exec.Cmd) (result *client) {
|
|
|
|
result = NewClient(cmd)
|
|
|
|
managedClients = append(managedClients, result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-09 17:27:20 -04:00
|
|
|
// Tells whether or not the underlying process has exited.
|
2013-05-08 14:14:21 -04:00
|
|
|
func (c *client) Exited() bool {
|
|
|
|
return c.exited
|
|
|
|
}
|
|
|
|
|
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-05-08 14:14:21 -04:00
|
|
|
func (c *client) Start() (address string, err error) {
|
2013-05-09 17:27:20 -04:00
|
|
|
// TODO: Make only run once
|
|
|
|
// TODO: Mutex
|
|
|
|
|
2013-05-08 14:14:21 -04:00
|
|
|
env := []string{
|
|
|
|
"PACKER_PLUGIN_MIN_PORT=10000",
|
|
|
|
"PACKER_PLUGIN_MAX_PORT=25000",
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
stderr := new(bytes.Buffer)
|
2013-06-05 23:39:39 -04:00
|
|
|
c.cmd.Env = append(c.cmd.Env, os.Environ()...)
|
2013-05-08 14:14:21 -04:00
|
|
|
c.cmd.Env = append(c.cmd.Env, env...)
|
|
|
|
c.cmd.Stderr = stderr
|
|
|
|
c.cmd.Stdout = stdout
|
|
|
|
err = c.cmd.Start()
|
|
|
|
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 {
|
|
|
|
c.cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
panic(r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start goroutine to wait for process to exit
|
|
|
|
go func() {
|
|
|
|
c.cmd.Wait()
|
2013-05-09 16:59:33 -04:00
|
|
|
log.Printf("%s: plugin process exited\n", c.cmd.Path)
|
2013-05-08 14:14:21 -04:00
|
|
|
c.exited = true
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start goroutine that logs the stderr
|
|
|
|
go c.logStderr(stderr)
|
|
|
|
|
|
|
|
// Some channels for the next step
|
2013-06-01 22:35:19 -04:00
|
|
|
timeout := time.After(c.StartTimeout)
|
2013-05-08 14:14:21 -04:00
|
|
|
|
|
|
|
// Start looking for the address
|
|
|
|
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.
|
|
|
|
address = strings.TrimSpace(string(line))
|
|
|
|
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-05-09 17:27:20 -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-05-08 14:14:21 -04:00
|
|
|
func (c *client) Kill() {
|
2013-05-08 17:51:14 -04:00
|
|
|
if c.cmd.Process == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-08 14:14:21 -04:00
|
|
|
c.cmd.Process.Kill()
|
2013-05-08 14:23:24 -04:00
|
|
|
|
|
|
|
// Wait for the client to finish logging so we have a complete log
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
for !c.doneLogging {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-done
|
2013-05-08 14:14:21 -04:00
|
|
|
}
|
|
|
|
|
2013-05-08 16:02:21 -04:00
|
|
|
func (c *client) logStderr(buf *bytes.Buffer) {
|
2013-05-08 14:14:21 -04:00
|
|
|
for done := false; !done; {
|
|
|
|
if c.Exited() {
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2013-05-08 15:43:41 -04:00
|
|
|
for err != io.EOF {
|
2013-05-08 14:14:21 -04:00
|
|
|
var line string
|
|
|
|
line, err = buf.ReadString('\n')
|
|
|
|
if line != "" {
|
2013-05-09 16:29:14 -04:00
|
|
|
log.Printf("%s: %s", c.cmd.Path, line)
|
2013-05-08 14:14:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
2013-05-08 14:23:24 -04:00
|
|
|
|
|
|
|
// Flag that we've completed logging for others
|
|
|
|
c.doneLogging = true
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|