packer/rpc: use the msgpack codec

This commit is contained in:
Mitchell Hashimoto 2014-04-26 13:31:22 -07:00
parent 130c0b1ce5
commit ce7ea006c7
8 changed files with 28 additions and 14 deletions

View File

@ -33,7 +33,7 @@ const MagicCookieValue = "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d69
// The APIVersion is outputted along with the RPC address. The plugin
// client validates this API version and will show an error if it doesn't
// know how to speak it.
const APIVersion = "2"
const APIVersion = "3"
// Server waits for a connection to this plugin and returns a Packer
// RPC server that you can use to register components and serve them.

View File

@ -26,7 +26,7 @@ type BuilderPrepareArgs struct {
type BuilderPrepareResponse struct {
Warnings []string
Error error
Error *BasicError
}
func (b *builder) Prepare(config ...interface{}) ([]string, error) {
@ -35,8 +35,12 @@ func (b *builder) Prepare(config ...interface{}) ([]string, error) {
if cerr != nil {
return nil, cerr
}
var err error = nil
if resp.Error != nil {
err = resp.Error
}
return resp.Warnings, resp.Error
return resp.Warnings, err
}
func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
@ -72,13 +76,9 @@ func (b *builder) Cancel() {
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error {
warnings, err := b.builder.Prepare(args.Configs...)
if err != nil {
err = NewBasicError(err)
}
*reply = BuilderPrepareResponse{
Warnings: warnings,
Error: err,
Error: NewBasicError(err),
}
return nil
}

View File

@ -30,8 +30,9 @@ func TestBuilderPrepare(t *testing.T) {
t.Fatal("should be called")
}
if !reflect.DeepEqual(b.PrepareConfig, []interface{}{42}) {
t.Fatalf("bad: %#v", b.PrepareConfig)
expected := []interface{}{int64(42)}
if !reflect.DeepEqual(b.PrepareConfig, expected) {
t.Fatalf("bad: %#v != %#v", b.PrepareConfig, expected)
}
}

View File

@ -2,6 +2,7 @@ package rpc
import (
"github.com/mitchellh/packer/packer"
"github.com/ugorji/go/codec"
"io"
"log"
"net/rpc"
@ -32,9 +33,12 @@ func newClientWithMux(mux *MuxConn, streamId uint32) (*Client, error) {
return nil, err
}
var h codec.MsgpackHandle
clientCodec := codec.GoRpc.ClientCodec(clientConn, &h)
return &Client{
mux: mux,
client: rpc.NewClient(clientConn),
client: rpc.NewClientWithCodec(clientCodec),
closeMux: false,
}, nil
}

View File

@ -9,6 +9,10 @@ type BasicError struct {
}
func NewBasicError(err error) *BasicError {
if err == nil {
return nil
}
return &BasicError{err.Error()}
}

View File

@ -54,7 +54,8 @@ func TestPostProcessorRPC(t *testing.T) {
t.Fatal("config should be called")
}
if !reflect.DeepEqual(p.configVal, []interface{}{42}) {
expected := []interface{}{int64(42)}
if !reflect.DeepEqual(p.configVal, expected) {
t.Fatalf("unknown config value: %#v", p.configVal)
}

View File

@ -23,7 +23,8 @@ func TestProvisionerRPC(t *testing.T) {
if !p.PrepCalled {
t.Fatal("should be called")
}
if !reflect.DeepEqual(p.PrepConfigs, []interface{}{42}) {
expected := []interface{}{int64(42)}
if !reflect.DeepEqual(p.PrepConfigs, expected) {
t.Fatalf("bad: %#v", p.PrepConfigs)
}

View File

@ -3,6 +3,7 @@ package rpc
import (
"fmt"
"github.com/mitchellh/packer/packer"
"github.com/ugorji/go/codec"
"io"
"log"
"net/rpc"
@ -145,7 +146,9 @@ func (s *Server) Serve() {
return
}
s.server.ServeConn(stream)
var h codec.MsgpackHandle
rpcCodec := codec.GoRpc.ServerCodec(stream, &h)
s.server.ServeCodec(rpcCodec)
}
// registerComponent registers a single Packer RPC component onto