Commit ce7ea006 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: use the msgpack codec

parent 130c0b1c
...@@ -33,7 +33,7 @@ const MagicCookieValue = "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d69 ...@@ -33,7 +33,7 @@ const MagicCookieValue = "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d69
// The APIVersion is outputted along with the RPC address. The plugin // 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 // client validates this API version and will show an error if it doesn't
// know how to speak it. // know how to speak it.
const APIVersion = "2" const APIVersion = "3"
// Server waits for a connection to this plugin and returns a Packer // Server waits for a connection to this plugin and returns a Packer
// RPC server that you can use to register components and serve them. // RPC server that you can use to register components and serve them.
......
...@@ -26,7 +26,7 @@ type BuilderPrepareArgs struct { ...@@ -26,7 +26,7 @@ type BuilderPrepareArgs struct {
type BuilderPrepareResponse struct { type BuilderPrepareResponse struct {
Warnings []string Warnings []string
Error error Error *BasicError
} }
func (b *builder) Prepare(config ...interface{}) ([]string, error) { func (b *builder) Prepare(config ...interface{}) ([]string, error) {
...@@ -35,8 +35,12 @@ func (b *builder) Prepare(config ...interface{}) ([]string, error) { ...@@ -35,8 +35,12 @@ func (b *builder) Prepare(config ...interface{}) ([]string, error) {
if cerr != nil { if cerr != nil {
return nil, cerr 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) { func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
...@@ -72,13 +76,9 @@ func (b *builder) Cancel() { ...@@ -72,13 +76,9 @@ func (b *builder) Cancel() {
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error { func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error {
warnings, err := b.builder.Prepare(args.Configs...) warnings, err := b.builder.Prepare(args.Configs...)
if err != nil {
err = NewBasicError(err)
}
*reply = BuilderPrepareResponse{ *reply = BuilderPrepareResponse{
Warnings: warnings, Warnings: warnings,
Error: err, Error: NewBasicError(err),
} }
return nil return nil
} }
......
...@@ -30,8 +30,9 @@ func TestBuilderPrepare(t *testing.T) { ...@@ -30,8 +30,9 @@ func TestBuilderPrepare(t *testing.T) {
t.Fatal("should be called") t.Fatal("should be called")
} }
if !reflect.DeepEqual(b.PrepareConfig, []interface{}{42}) { expected := []interface{}{int64(42)}
t.Fatalf("bad: %#v", b.PrepareConfig) if !reflect.DeepEqual(b.PrepareConfig, expected) {
t.Fatalf("bad: %#v != %#v", b.PrepareConfig, expected)
} }
} }
......
...@@ -2,6 +2,7 @@ package rpc ...@@ -2,6 +2,7 @@ package rpc
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/ugorji/go/codec"
"io" "io"
"log" "log"
"net/rpc" "net/rpc"
...@@ -32,9 +33,12 @@ func newClientWithMux(mux *MuxConn, streamId uint32) (*Client, error) { ...@@ -32,9 +33,12 @@ func newClientWithMux(mux *MuxConn, streamId uint32) (*Client, error) {
return nil, err return nil, err
} }
var h codec.MsgpackHandle
clientCodec := codec.GoRpc.ClientCodec(clientConn, &h)
return &Client{ return &Client{
mux: mux, mux: mux,
client: rpc.NewClient(clientConn), client: rpc.NewClientWithCodec(clientCodec),
closeMux: false, closeMux: false,
}, nil }, nil
} }
......
...@@ -9,6 +9,10 @@ type BasicError struct { ...@@ -9,6 +9,10 @@ type BasicError struct {
} }
func NewBasicError(err error) *BasicError { func NewBasicError(err error) *BasicError {
if err == nil {
return nil
}
return &BasicError{err.Error()} return &BasicError{err.Error()}
} }
......
...@@ -54,7 +54,8 @@ func TestPostProcessorRPC(t *testing.T) { ...@@ -54,7 +54,8 @@ func TestPostProcessorRPC(t *testing.T) {
t.Fatal("config should be called") 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) t.Fatalf("unknown config value: %#v", p.configVal)
} }
......
...@@ -23,7 +23,8 @@ func TestProvisionerRPC(t *testing.T) { ...@@ -23,7 +23,8 @@ func TestProvisionerRPC(t *testing.T) {
if !p.PrepCalled { if !p.PrepCalled {
t.Fatal("should be called") 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) t.Fatalf("bad: %#v", p.PrepConfigs)
} }
......
...@@ -3,6 +3,7 @@ package rpc ...@@ -3,6 +3,7 @@ package rpc
import ( import (
"fmt" "fmt"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/ugorji/go/codec"
"io" "io"
"log" "log"
"net/rpc" "net/rpc"
...@@ -145,7 +146,9 @@ func (s *Server) Serve() { ...@@ -145,7 +146,9 @@ func (s *Server) Serve() {
return 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 // registerComponent registers a single Packer RPC component onto
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment