packer/rpc: start command
This commit is contained in:
parent
13b8bef58f
commit
2dab0cab07
@ -51,6 +51,13 @@ func (c *Client) Cache() packer.Cache {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Command() packer.Command {
|
||||
return &command{
|
||||
client: c.client,
|
||||
mux: c.mux,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Communicator() packer.Communicator {
|
||||
return &communicator{
|
||||
client: c.client,
|
||||
|
@ -9,12 +9,14 @@ import (
|
||||
// command is actually executed over an RPC connection.
|
||||
type command struct {
|
||||
client *rpc.Client
|
||||
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
|
||||
mux *MuxConn
|
||||
}
|
||||
|
||||
type CommandRunArgs struct {
|
||||
@ -25,7 +27,7 @@ type CommandRunArgs struct {
|
||||
type CommandSynopsisArgs byte
|
||||
|
||||
func Command(client *rpc.Client) *command {
|
||||
return &command{client}
|
||||
return &command{client: client}
|
||||
}
|
||||
|
||||
func (c *command) Help() (result string) {
|
||||
|
@ -2,7 +2,6 @@ package rpc
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"net/rpc"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
@ -33,21 +32,14 @@ func TestRPCCommand(t *testing.T) {
|
||||
command := new(TestCommand)
|
||||
|
||||
// Start the server
|
||||
server := rpc.NewServer()
|
||||
RegisterCommand(server, command)
|
||||
address := serveSingleConn(server)
|
||||
|
||||
// Create the command client over RPC and run some methods to verify
|
||||
// we get the proper behavior.
|
||||
client, err := rpc.Dial("tcp", address)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
clientComm := Command(client)
|
||||
client, server := testClientServer(t)
|
||||
defer client.Close()
|
||||
defer server.Close()
|
||||
server.RegisterCommand(command)
|
||||
commClient := client.Command()
|
||||
|
||||
//Test Help
|
||||
help := clientComm.Help()
|
||||
help := commClient.Help()
|
||||
if help != "bar" {
|
||||
t.Fatalf("bad: %s", help)
|
||||
}
|
||||
@ -55,7 +47,7 @@ func TestRPCCommand(t *testing.T) {
|
||||
// Test run
|
||||
runArgs := []string{"foo", "bar"}
|
||||
testEnv := &testEnvironment{}
|
||||
exitCode := clientComm.Run(testEnv, runArgs)
|
||||
exitCode := commClient.Run(testEnv, runArgs)
|
||||
if !reflect.DeepEqual(command.runArgs, runArgs) {
|
||||
t.Fatalf("bad: %#v", command.runArgs)
|
||||
}
|
||||
@ -73,7 +65,7 @@ func TestRPCCommand(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test Synopsis
|
||||
synopsis := clientComm.Synopsis()
|
||||
synopsis := commClient.Synopsis()
|
||||
if synopsis != "foo" {
|
||||
t.Fatalf("bad: %#v", synopsis)
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ func RegisterCache(s *rpc.Server, c packer.Cache) {
|
||||
// Registers the appropriate endpoint on an RPC server to serve a
|
||||
// Packer Command.
|
||||
func RegisterCommand(s *rpc.Server, c packer.Command) {
|
||||
registerComponent(s, "Command", &CommandServer{c}, false)
|
||||
registerComponent(s, "Command", &CommandServer{command: c}, false)
|
||||
}
|
||||
|
||||
// Registers the appropriate endpoint on an RPC server to serve a
|
||||
|
@ -14,6 +14,7 @@ var endpointId uint64
|
||||
const (
|
||||
DefaultArtifactEndpoint string = "Artifact"
|
||||
DefaultCacheEndpoint = "Cache"
|
||||
DefaultCommandEndpoint = "Command"
|
||||
DefaultCommunicatorEndpoint = "Communicator"
|
||||
DefaultHookEndpoint = "Hook"
|
||||
DefaultPostProcessorEndpoint = "PostProcessor"
|
||||
@ -58,6 +59,13 @@ func (s *Server) RegisterCache(c packer.Cache) {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) RegisterCommand(c packer.Command) {
|
||||
s.server.RegisterName(DefaultCommandEndpoint, &CommandServer{
|
||||
command: c,
|
||||
mux: s.mux,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) RegisterCommunicator(c packer.Communicator) {
|
||||
s.server.RegisterName(DefaultCommunicatorEndpoint, &CommunicatorServer{
|
||||
c: c,
|
||||
|
Loading…
x
Reference in New Issue
Block a user