diff --git a/packer/rpc/client.go b/packer/rpc/client.go index 83783d79b..e0729c009 100644 --- a/packer/rpc/client.go +++ b/packer/rpc/client.go @@ -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, diff --git a/packer/rpc/command.go b/packer/rpc/command.go index 3e2b48b2f..4d45a2ec1 100644 --- a/packer/rpc/command.go +++ b/packer/rpc/command.go @@ -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) { diff --git a/packer/rpc/command_test.go b/packer/rpc/command_test.go index 086e0eeed..976b70e6f 100644 --- a/packer/rpc/command_test.go +++ b/packer/rpc/command_test.go @@ -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) } diff --git a/packer/rpc/server.go b/packer/rpc/server.go index 904a1b1aa..177479e44 100644 --- a/packer/rpc/server.go +++ b/packer/rpc/server.go @@ -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 diff --git a/packer/rpc/server_new.go b/packer/rpc/server_new.go index b80f6e441..83ece27ee 100644 --- a/packer/rpc/server_new.go +++ b/packer/rpc/server_new.go @@ -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,