packer-cn/packer/rpc/command.go

85 lines
1.7 KiB
Go
Raw Normal View History

package rpc
import (
"github.com/mitchellh/packer/packer"
"net/rpc"
)
// A Command is an implementation of the packer.Command interface where the
// command is actually executed over an RPC connection.
type command struct {
client *rpc.Client
2013-12-11 14:19:36 -05:00
mux *MuxConn
}
// A CommandServer wraps a packer.Command and makes it exportable as part
// of a Golang RPC server.
type CommandServer struct {
command packer.Command
2013-12-11 14:19:36 -05:00
mux *MuxConn
}
type CommandRunArgs struct {
2013-12-11 14:19:36 -05:00
Args []string
StreamId uint32
}
type CommandSynopsisArgs byte
func (c *command) Help() (result string) {
err := c.client.Call("Command.Help", new(interface{}), &result)
if err != nil {
panic(err)
}
return
}
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)
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,
}
err := c.client.Call("Command.Run", rpcArgs, &result)
if err != nil {
panic(err)
}
return
}
func (c *command) Synopsis() (result string) {
err := c.client.Call("Command.Synopsis", CommandSynopsisArgs(0), &result)
if err != nil {
panic(err)
}
return
}
func (c *CommandServer) Help(args *interface{}, reply *string) error {
*reply = c.command.Help()
return nil
}
func (c *CommandServer) Run(args *CommandRunArgs, reply *int) error {
client, err := newClientWithMux(c.mux, args.StreamId)
if err != nil {
2013-12-10 16:23:07 -05:00
return NewBasicError(err)
}
2013-12-10 16:23:07 -05:00
defer client.Close()
2013-12-10 16:23:07 -05:00
*reply = c.command.Run(client.Environment(), args.Args)
return nil
}
func (c *CommandServer) Synopsis(args *CommandSynopsisArgs, reply *string) error {
*reply = c.command.Synopsis()
return nil
}