Commit edaf1919 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Environment has Cache method, RPC implements

parent 2c8b1980
......@@ -39,6 +39,7 @@ type ComponentFinder struct {
// list of available builders, and more.
type Environment interface {
Builder(string) (Builder, error)
Cache() Cache
Cli([]string) (int, error)
Hook(string) (Hook, error)
Provisioner(string) (Provisioner, error)
......@@ -48,6 +49,7 @@ type Environment interface {
// An implementation of an Environment that represents the Packer core
// environment.
type coreEnvironment struct {
cache Cache
commands []string
components ComponentFinder
ui Ui
......@@ -55,6 +57,7 @@ type coreEnvironment struct {
// This struct configures new environments.
type EnvironmentConfig struct {
Cache Cache
Commands []string
Components ComponentFinder
Ui Ui
......@@ -77,6 +80,7 @@ func NewEnvironment(config *EnvironmentConfig) (resultEnv Environment, err error
}
env := &coreEnvironment{}
env.cache = config.Cache
env.commands = config.Commands
env.components = config.Components
env.ui = config.Ui
......@@ -119,6 +123,11 @@ func (e *coreEnvironment) Builder(name string) (b Builder, err error) {
return
}
// Returns the cache for this environment
func (e *coreEnvironment) Cache() Cache {
return e.cache
}
// Returns a hook of the given name that is registered with this
// environment.
func (e *coreEnvironment) Hook(name string) (h Hook, err error) {
......
......@@ -37,6 +37,20 @@ func (e *Environment) Builder(name string) (b packer.Builder, err error) {
return
}
func (e *Environment) Cache() packer.Cache {
var reply string
if err := e.client.Call("Environment.Cache", new(interface{}), &reply); err != nil{
panic(err)
}
client, err := rpc.Dial("tcp", reply)
if err != nil {
panic(err)
}
return Cache(client)
}
func (e *Environment) Cli(args []string) (result int, err error) {
rpcArgs := &EnvironmentCliArgs{args}
err = e.client.Call("Environment.Cli", rpcArgs, &result)
......@@ -98,6 +112,15 @@ func (e *EnvironmentServer) Builder(name *string, reply *string) error {
return nil
}
func (e *EnvironmentServer) Cache(args *interface{}, reply *string) error {
cache := e.env.Cache()
server := rpc.NewServer()
RegisterCache(server, cache)
*reply = serveSingleConn(server)
return nil
}
func (e *EnvironmentServer) Cli(args *EnvironmentCliArgs, reply *int) (err error) {
*reply, err = e.env.Cli(args.Args)
return
......
......@@ -8,6 +8,7 @@ import (
)
var testEnvBuilder = &testBuilder{}
var testEnvCache = &testCache{}
var testEnvUi = &testUi{}
type testEnvironment struct {
......@@ -28,6 +29,10 @@ func (e *testEnvironment) Builder(name string) (packer.Builder, error) {
return testEnvBuilder, nil
}
func (e *testEnvironment) Cache() packer.Cache {
return testEnvCache
}
func (e *testEnvironment) Cli(args []string) (int, error) {
e.cliCalled = true
e.cliArgs = args
......@@ -75,6 +80,11 @@ func TestEnvironmentRPC(t *testing.T) {
builder.Prepare(nil)
assert.True(testEnvBuilder.prepareCalled, "Prepare should be called")
// Test Cache
cache := eClient.Cache()
cache.Lock("foo")
assert.True(testEnvCache.lockCalled, "lock should be called")
// Test Cli
cliArgs := []string{"foo", "bar"}
result, _ := eClient.Cli(cliArgs)
......
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