packer/rpc: Remote environments
This commit is contained in:
parent
255b94761c
commit
5aec3f6745
|
@ -1,7 +1,6 @@
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"cgl.tideland.biz/asserts"
|
"cgl.tideland.biz/asserts"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
"net"
|
"net"
|
||||||
|
@ -26,21 +25,6 @@ func (tc *TestCommand) Synopsis() string {
|
||||||
return "foo"
|
return "foo"
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEnvironment() packer.Environment {
|
|
||||||
config := &packer.EnvironmentConfig{}
|
|
||||||
config.Ui = &packer.ReaderWriterUi{
|
|
||||||
new(bytes.Buffer),
|
|
||||||
new(bytes.Buffer),
|
|
||||||
}
|
|
||||||
|
|
||||||
env, err := packer.NewEnvironment(config)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return env
|
|
||||||
}
|
|
||||||
|
|
||||||
// This starts a RPC server for the given command listening on the
|
// This starts a RPC server for the given command listening on the
|
||||||
// given address. The RPC server is ready when "readyChan" receives a message
|
// given address. The RPC server is ready when "readyChan" receives a message
|
||||||
// and the RPC server will quit when "stopChan" receives a message.
|
// and the RPC server will quit when "stopChan" receives a message.
|
||||||
|
@ -102,7 +86,7 @@ func TestRPCCommand(t *testing.T) {
|
||||||
|
|
||||||
clientComm := &ClientCommand{client}
|
clientComm := &ClientCommand{client}
|
||||||
runArgs := []string{"foo", "bar"}
|
runArgs := []string{"foo", "bar"}
|
||||||
testEnv := testEnvironment()
|
testEnv := &testEnvironment{}
|
||||||
exitCode := clientComm.Run(testEnv, runArgs)
|
exitCode := clientComm.Run(testEnv, runArgs)
|
||||||
synopsis := clientComm.Synopsis()
|
synopsis := clientComm.Synopsis()
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"net/rpc"
|
"net/rpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A EnvironmentClient is an implementation of the packer.Environment interface
|
// A Environment is an implementation of the packer.Environment interface
|
||||||
// where the actual environment is executed over an RPC connection.
|
// where the actual environment is executed over an RPC connection.
|
||||||
type EnvironmentClient struct {
|
type Environment struct {
|
||||||
client *rpc.Client
|
client *rpc.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,3 +16,60 @@ type EnvironmentClient struct {
|
||||||
type EnvironmentServer struct {
|
type EnvironmentServer struct {
|
||||||
env packer.Environment
|
env packer.Environment
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EnvironmentCliArgs struct {
|
||||||
|
Args []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Environment) BuilderFactory() packer.BuilderFactory {
|
||||||
|
var reply string
|
||||||
|
e.client.Call("Environment.BuilderFactory", new(interface{}), &reply)
|
||||||
|
|
||||||
|
// TODO: error handling
|
||||||
|
client, _ := rpc.Dial("tcp", reply)
|
||||||
|
return &BuilderFactory{client}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Environment) Cli(args []string) (result int) {
|
||||||
|
rpcArgs := &EnvironmentCliArgs{args}
|
||||||
|
e.client.Call("Environment.Cli", rpcArgs, &result)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Environment) Ui() packer.Ui {
|
||||||
|
var reply string
|
||||||
|
e.client.Call("Environment.Ui", new(interface{}), &reply)
|
||||||
|
|
||||||
|
// TODO: error handling
|
||||||
|
client, _ := rpc.Dial("tcp", reply)
|
||||||
|
return &Ui{client}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EnvironmentServer) BuilderFactory(args *interface{}, reply *string) error {
|
||||||
|
bf := e.env.BuilderFactory()
|
||||||
|
|
||||||
|
// Wrap that up into a server, and server a single connection back
|
||||||
|
server := NewServer()
|
||||||
|
server.RegisterBuilderFactory(bf)
|
||||||
|
server.StartSingle()
|
||||||
|
|
||||||
|
*reply = server.Address()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EnvironmentServer) Cli(args *EnvironmentCliArgs, reply *int) error {
|
||||||
|
*reply = e.env.Cli(args.Args)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EnvironmentServer) Ui(args *interface{}, reply *string) error {
|
||||||
|
ui := e.env.Ui()
|
||||||
|
|
||||||
|
// Wrap it
|
||||||
|
server := NewServer()
|
||||||
|
server.RegisterUi(ui)
|
||||||
|
server.StartSingle()
|
||||||
|
|
||||||
|
*reply = server.Address()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,85 @@
|
||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cgl.tideland.biz/asserts"
|
||||||
|
"github.com/mitchellh/packer/packer"
|
||||||
|
"net/rpc"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testEnvBF = &testBuilderFactory{}
|
||||||
|
var testEnvUi = &testUi{}
|
||||||
|
|
||||||
|
type testEnvironment struct {
|
||||||
|
bfCalled bool
|
||||||
|
cliCalled bool
|
||||||
|
cliArgs []string
|
||||||
|
uiCalled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *testEnvironment) BuilderFactory() packer.BuilderFactory {
|
||||||
|
e.bfCalled = true
|
||||||
|
return testEnvBF
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *testEnvironment) Cli(args []string) int {
|
||||||
|
e.cliCalled = true
|
||||||
|
e.cliArgs = args
|
||||||
|
return 42
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *testEnvironment) Ui() packer.Ui {
|
||||||
|
e.uiCalled = true
|
||||||
|
return testEnvUi
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnvironmentRPC(t *testing.T) {
|
||||||
|
assert := asserts.NewTestingAsserts(t, true)
|
||||||
|
|
||||||
|
// Create the interface to test
|
||||||
|
e := &testEnvironment{}
|
||||||
|
|
||||||
|
// Start the server
|
||||||
|
server := NewServer()
|
||||||
|
server.RegisterEnvironment(e)
|
||||||
|
server.Start()
|
||||||
|
defer server.Stop()
|
||||||
|
|
||||||
|
// Create the client over RPC and run some methods to verify it works
|
||||||
|
client, err := rpc.Dial("tcp", server.Address())
|
||||||
|
assert.Nil(err, "should be able to connect")
|
||||||
|
|
||||||
|
// Test BuilderFactory
|
||||||
|
eClient := &Environment{client}
|
||||||
|
bf := eClient.BuilderFactory()
|
||||||
|
assert.True(e.bfCalled, "BuilderFactory should be called")
|
||||||
|
|
||||||
|
// Test calls on the factory
|
||||||
|
_ = bf.CreateBuilder("foo")
|
||||||
|
assert.True(testEnvBF.createCalled, "create should be called on BF")
|
||||||
|
|
||||||
|
// Test Cli
|
||||||
|
cliArgs := []string{"foo", "bar"}
|
||||||
|
result := eClient.Cli(cliArgs)
|
||||||
|
assert.True(e.cliCalled, "CLI should be called")
|
||||||
|
assert.Equal(e.cliArgs, cliArgs, "args should match")
|
||||||
|
assert.Equal(result, 42, "result shuld be 42")
|
||||||
|
|
||||||
|
// Test Ui
|
||||||
|
ui := eClient.Ui()
|
||||||
|
assert.True(e.uiCalled, "Ui should've been called")
|
||||||
|
|
||||||
|
// Test calls on the Ui
|
||||||
|
ui.Say("format")
|
||||||
|
assert.True(testEnvUi.sayCalled, "Say should be called")
|
||||||
|
assert.Equal(testEnvUi.sayFormat, "format", "format should match")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnvironment_ImplementsEnvironment(t *testing.T) {
|
||||||
|
assert := asserts.NewTestingAsserts(t, true)
|
||||||
|
|
||||||
|
var realVar packer.Environment
|
||||||
|
e := &Environment{nil}
|
||||||
|
|
||||||
|
assert.Implementor(e, &realVar, "should be an Environment")
|
||||||
|
}
|
|
@ -41,6 +41,10 @@ func (s *Server) RegisterBuilderFactory(b packer.BuilderFactory) {
|
||||||
s.server.RegisterName("BuilderFactory", &BuilderFactoryServer{b})
|
s.server.RegisterName("BuilderFactory", &BuilderFactoryServer{b})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) RegisterEnvironment(e packer.Environment) {
|
||||||
|
s.server.RegisterName("Environment", &EnvironmentServer{e})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) RegisterUi(ui packer.Ui) {
|
func (s *Server) RegisterUi(ui packer.Ui) {
|
||||||
s.server.RegisterName("Ui", &UiServer{ui})
|
s.server.RegisterName("Ui", &UiServer{ui})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue