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
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
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
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
2013-05-03 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type CommandRunArgs struct {
|
2013-12-11 14:19:36 -05:00
|
|
|
Args []string
|
|
|
|
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()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(c.mux, nextId)
|
2013-12-10 16:23:07 -05:00
|
|
|
server.RegisterEnvironment(env)
|
|
|
|
go server.Serve()
|
|
|
|
|
|
|
|
rpcArgs := &CommandRunArgs{
|
2013-12-11 14:19:36 -05:00
|
|
|
Args: args,
|
2013-12-10 16:23:07 -05:00
|
|
|
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-19 20:35:32 -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
|
|
|
|
}
|