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