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-12-10 15:02:01 -05:00
|
|
|
mux *MuxConn
|
2013-05-03 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
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
|
2013-12-10 15:02:01 -05:00
|
|
|
mux *MuxConn
|
2013-05-03 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommandRunArgs struct {
|
2013-05-10 20:01:24 -04:00
|
|
|
Args []string
|
2013-12-10 16:23:07 -05:00
|
|
|
StreamId uint32
|
2013-05-03 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommandSynopsisArgs byte
|
|
|
|
|
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-12-10 16:23:07 -05:00
|
|
|
nextId := c.mux.NextId()
|
|
|
|
server := NewServerWithMux(c.mux, nextId)
|
|
|
|
server.RegisterEnvironment(env)
|
|
|
|
go server.Serve()
|
|
|
|
|
|
|
|
rpcArgs := &CommandRunArgs{
|
|
|
|
Args: args,
|
|
|
|
StreamId: nextId,
|
|
|
|
}
|
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-12-10 16:23:07 -05:00
|
|
|
client, err := NewClientWithMux(c.mux, args.StreamId)
|
2013-05-08 16:11:47 -04:00
|
|
|
if err != nil {
|
2013-12-10 16:23:07 -05:00
|
|
|
return NewBasicError(err)
|
2013-05-08 16:11:47 -04:00
|
|
|
}
|
2013-12-10 16:23:07 -05:00
|
|
|
defer client.Close()
|
2013-05-08 16:11:47 -04:00
|
|
|
|
2013-12-10 16:23:07 -05:00
|
|
|
*reply = c.command.Run(client.Environment(), 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
|
|
|
|
}
|