2013-05-03 17:26:21 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
)
|
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
// A Command is an implementation of the packer.Command interface where the
|
2013-05-03 17:26:21 -04:00
|
|
|
// command is actually executed over an RPC connection.
|
2013-05-08 16:42:25 -04:00
|
|
|
type command struct {
|
2013-05-03 17:26:21 -04:00
|
|
|
client *rpc.Client
|
|
|
|
}
|
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
// A CommandServer wraps a packer.Command and makes it exportable as part
|
2013-05-03 17:26:21 -04:00
|
|
|
// of a Golang RPC server.
|
2013-05-08 16:42:25 -04:00
|
|
|
type CommandServer struct {
|
2013-05-03 17:26:21 -04:00
|
|
|
command packer.Command
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommandRunArgs struct {
|
2013-05-08 16:11:47 -04:00
|
|
|
RPCAddress string
|
2013-05-10 20:01:24 -04:00
|
|
|
Args []string
|
2013-05-03 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommandSynopsisArgs byte
|
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
func Command(client *rpc.Client) *command {
|
|
|
|
return &command{client}
|
2013-05-05 00:26:30 -04:00
|
|
|
}
|
|
|
|
|
2013-06-02 14:41:12 -04:00
|
|
|
func (c *command) Help() (result string) {
|
|
|
|
err := c.client.Call("Command.Help", new(interface{}), &result)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
func (c *command) Run(env packer.Environment, args []string) (result int) {
|
2013-05-08 16:11:47 -04:00
|
|
|
// Create and start the server for the Environment
|
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterEnvironment(server, env)
|
|
|
|
|
|
|
|
rpcArgs := &CommandRunArgs{serveSingleConn(server), args}
|
2013-05-05 19:25:32 -04:00
|
|
|
err := c.client.Call("Command.Run", rpcArgs, &result)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2013-05-03 17:26:21 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
func (c *command) Synopsis() (result string) {
|
2013-05-05 19:25:32 -04:00
|
|
|
err := c.client.Call("Command.Synopsis", CommandSynopsisArgs(0), &result)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2013-05-03 17:26:21 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-06-02 14:41:12 -04:00
|
|
|
func (c *CommandServer) Help(args *interface{}, reply *string) error {
|
|
|
|
*reply = c.command.Help()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
func (c *CommandServer) Run(args *CommandRunArgs, reply *int) error {
|
2013-09-18 20:15:48 -04:00
|
|
|
client, err := rpcDial(args.RPCAddress)
|
2013-05-08 16:11:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
env := &Environment{client}
|
|
|
|
|
|
|
|
*reply = c.command.Run(env, args.Args)
|
2013-05-03 17:26:21 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
func (c *CommandServer) Synopsis(args *CommandSynopsisArgs, reply *string) error {
|
2013-05-03 17:26:21 -04:00
|
|
|
*reply = c.command.Synopsis()
|
|
|
|
return nil
|
|
|
|
}
|