packer/rpc: Provisioner

This commit is contained in:
Mitchell Hashimoto 2013-12-10 11:56:15 -08:00
parent a036bec96e
commit 5966a6e905
5 changed files with 36 additions and 36 deletions

View File

@ -72,6 +72,13 @@ func (c *Client) PostProcessor() packer.PostProcessor {
} }
} }
func (c *Client) Provisioner() packer.Provisioner {
return &provisioner{
client: c.client,
mux: c.mux,
}
}
func (c *Client) Ui() packer.Ui { func (c *Client) Ui() packer.Ui {
return &Ui{ return &Ui{
client: c.client, client: c.client,

View File

@ -10,24 +10,22 @@ import (
// executed over an RPC connection. // executed over an RPC connection.
type provisioner struct { type provisioner struct {
client *rpc.Client client *rpc.Client
mux *MuxConn
} }
// ProvisionerServer wraps a packer.Provisioner implementation and makes it // ProvisionerServer wraps a packer.Provisioner implementation and makes it
// exportable as part of a Golang RPC server. // exportable as part of a Golang RPC server.
type ProvisionerServer struct { type ProvisionerServer struct {
p packer.Provisioner p packer.Provisioner
mux *MuxConn
} }
type ProvisionerPrepareArgs struct { type ProvisionerPrepareArgs struct {
Configs []interface{} Configs []interface{}
} }
type ProvisionerProvisionArgs struct {
RPCAddress string
}
func Provisioner(client *rpc.Client) *provisioner { func Provisioner(client *rpc.Client) *provisioner {
return &provisioner{client} return &provisioner{client: client}
} }
func (p *provisioner) Prepare(configs ...interface{}) (err error) { func (p *provisioner) Prepare(configs ...interface{}) (err error) {
args := &ProvisionerPrepareArgs{configs} args := &ProvisionerPrepareArgs{configs}
@ -39,13 +37,13 @@ func (p *provisioner) Prepare(configs ...interface{}) (err error) {
} }
func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
// TODO: Error handling nextId := p.mux.NextId()
server := rpc.NewServer() server := NewServerWithMux(p.mux, nextId)
RegisterCommunicator(server, comm) server.RegisterCommunicator(comm)
RegisterUi(server, ui) server.RegisterUi(ui)
go server.Serve()
args := &ProvisionerProvisionArgs{serveSingleConn(server)} return p.client.Call("Provisioner.Provision", nextId, new(interface{}))
return p.client.Call("Provisioner.Provision", args, new(interface{}))
} }
func (p *provisioner) Cancel() { func (p *provisioner) Cancel() {
@ -64,16 +62,14 @@ func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *error)
return nil return nil
} }
func (p *ProvisionerServer) Provision(args *ProvisionerProvisionArgs, reply *interface{}) error { func (p *ProvisionerServer) Provision(streamId uint32, reply *interface{}) error {
client, err := rpcDial(args.RPCAddress) client, err := NewClientWithMux(p.mux, streamId)
if err != nil { if err != nil {
return err return NewBasicError(err)
} }
defer client.Close()
comm := Communicator(client) if err := p.p.Provision(client.Ui(), client.Communicator()); err != nil {
ui := &Ui{client: client}
if err := p.p.Provision(ui, comm); err != nil {
return NewBasicError(err) return NewBasicError(err)
} }

View File

@ -2,7 +2,6 @@ package rpc
import ( import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"net/rpc"
"reflect" "reflect"
"testing" "testing"
) )
@ -12,19 +11,14 @@ func TestProvisionerRPC(t *testing.T) {
p := new(packer.MockProvisioner) p := new(packer.MockProvisioner)
// Start the server // Start the server
server := rpc.NewServer() client, server := testClientServer(t)
RegisterProvisioner(server, p) defer client.Close()
address := serveSingleConn(server) defer server.Close()
server.RegisterProvisioner(p)
// Create the client over RPC and run some methods to verify it works pClient := client.Provisioner()
client, err := rpc.Dial("tcp", address)
if err != nil {
t.Fatalf("err: %s", err)
}
// Test Prepare // Test Prepare
config := 42 config := 42
pClient := Provisioner(client)
pClient.Prepare(config) pClient.Prepare(config)
if !p.PrepCalled { if !p.PrepCalled {
t.Fatal("should be called") t.Fatal("should be called")
@ -41,11 +35,6 @@ func TestProvisionerRPC(t *testing.T) {
t.Fatal("should be called") t.Fatal("should be called")
} }
p.ProvUi.Say("foo")
if !ui.sayCalled {
t.Fatal("should be called")
}
// Test Cancel // Test Cancel
pClient.Cancel() pClient.Cancel()
if !p.CancelCalled { if !p.CancelCalled {

View File

@ -61,7 +61,7 @@ func RegisterPostProcessor(s *rpc.Server, p packer.PostProcessor) {
// Registers the appropriate endpoint on an RPC server to serve a packer.Provisioner // Registers the appropriate endpoint on an RPC server to serve a packer.Provisioner
func RegisterProvisioner(s *rpc.Server, p packer.Provisioner) { func RegisterProvisioner(s *rpc.Server, p packer.Provisioner) {
registerComponent(s, "Provisioner", &ProvisionerServer{p}, false) registerComponent(s, "Provisioner", &ProvisionerServer{p: p}, false)
} }
// Registers the appropriate endpoint on an RPC server to serve a // Registers the appropriate endpoint on an RPC server to serve a

View File

@ -17,6 +17,7 @@ const (
DefaultCommunicatorEndpoint = "Communicator" DefaultCommunicatorEndpoint = "Communicator"
DefaultHookEndpoint = "Hook" DefaultHookEndpoint = "Hook"
DefaultPostProcessorEndpoint = "PostProcessor" DefaultPostProcessorEndpoint = "PostProcessor"
DefaultProvisionerEndpoint = "Provisioner"
DefaultUiEndpoint = "Ui" DefaultUiEndpoint = "Ui"
) )
@ -78,6 +79,13 @@ func (s *Server) RegisterPostProcessor(p packer.PostProcessor) {
}) })
} }
func (s *Server) RegisterProvisioner(p packer.Provisioner) {
s.server.RegisterName(DefaultProvisionerEndpoint, &ProvisionerServer{
mux: s.mux,
p: p,
})
}
func (s *Server) RegisterUi(ui packer.Ui) { func (s *Server) RegisterUi(ui packer.Ui) {
s.server.RegisterName(DefaultUiEndpoint, &UiServer{ s.server.RegisterName(DefaultUiEndpoint, &UiServer{
ui: ui, ui: ui,