2013-05-05 00:26:30 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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 00:26:30 -04:00
|
|
|
"net/rpc"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2013-05-08 13:52:23 -04:00
|
|
|
type cmdCommand struct {
|
|
|
|
command packer.Command
|
2013-05-10 20:01:24 -04:00
|
|
|
client *client
|
2013-05-08 13:52:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdCommand) Run(e packer.Environment, args []string) (exitCode int) {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
c.checkExit(r, func() { exitCode = 1 })
|
|
|
|
}()
|
|
|
|
|
|
|
|
exitCode = c.command.Run(e, args)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdCommand) Synopsis() (result string) {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
c.checkExit(r, func() {
|
|
|
|
result = ""
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
result = c.command.Synopsis()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdCommand) checkExit(p interface{}, cb func()) {
|
2013-05-08 14:14:21 -04:00
|
|
|
if c.client.Exited() {
|
2013-05-08 13:52:23 -04:00
|
|
|
cb()
|
2013-05-08 14:14:21 -04:00
|
|
|
} else if p != nil {
|
|
|
|
log.Panic(p)
|
2013-05-08 13:52:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 22:47:32 -04:00
|
|
|
// Returns a valid packer.Command where the command is executed via RPC
|
|
|
|
// to a plugin that is within a subprocess.
|
|
|
|
//
|
|
|
|
// This method will start the given exec.Cmd, which should point to
|
|
|
|
// the plugin binary to execute. Some configuration will be done to
|
|
|
|
// the command, such as overriding Stdout and some environmental variables.
|
|
|
|
//
|
|
|
|
// This function guarantees the subprocess will end in a timely manner.
|
2013-05-07 17:05:51 -04:00
|
|
|
func Command(cmd *exec.Cmd) (result packer.Command, err error) {
|
2013-05-08 14:54:59 -04:00
|
|
|
cmdClient := NewManagedClient(cmd)
|
2013-05-08 14:14:21 -04:00
|
|
|
address, err := cmdClient.Start()
|
2013-05-07 17:05:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-05-05 00:26:30 -04:00
|
|
|
|
2013-05-07 22:48:14 -04:00
|
|
|
defer func() {
|
2013-05-08 12:46:37 -04:00
|
|
|
// Make sure the command is properly killed in the case of an error
|
2013-05-07 22:48:14 -04:00
|
|
|
if err != nil {
|
2013-05-08 14:14:21 -04:00
|
|
|
cmdClient.Kill()
|
2013-05-08 13:52:23 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-05-05 19:25:32 -04:00
|
|
|
client, err := rpc.Dial("tcp", address)
|
|
|
|
if err != nil {
|
2013-05-07 20:10:45 -04:00
|
|
|
return
|
2013-05-05 19:25:32 -04:00
|
|
|
}
|
2013-05-05 00:26:30 -04:00
|
|
|
|
2013-05-08 13:52:23 -04:00
|
|
|
result = &cmdCommand{
|
|
|
|
packrpc.Command(client),
|
2013-05-08 14:14:21 -04:00
|
|
|
cmdClient,
|
2013-05-08 13:52:23 -04:00
|
|
|
}
|
|
|
|
|
2013-05-07 17:05:51 -04:00
|
|
|
return
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|