Commit 6b87c42e authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Change provisioner interface to take array of configs

parent a7896e7c
...@@ -52,7 +52,7 @@ func (b *coreBuild) Prepare(ui Ui) (err error) { ...@@ -52,7 +52,7 @@ func (b *coreBuild) Prepare(ui Ui) (err error) {
// Prepare the provisioners // Prepare the provisioners
// TODO: error handling // TODO: error handling
for _, coreProv := range b.provisioners { for _, coreProv := range b.provisioners {
coreProv.provisioner.Prepare(coreProv.config, ui) coreProv.provisioner.Prepare(coreProv.config)
} }
return return
......
...@@ -47,8 +47,7 @@ func TestBuild_Prepare(t *testing.T) { ...@@ -47,8 +47,7 @@ func TestBuild_Prepare(t *testing.T) {
coreProv := coreB.provisioners[0] coreProv := coreB.provisioners[0]
prov := coreProv.provisioner.(*TestProvisioner) prov := coreProv.provisioner.(*TestProvisioner)
assert.True(prov.prepCalled, "prepare should be called") assert.True(prov.prepCalled, "prepare should be called")
assert.Equal(prov.prepConfig, 42, "prepare should be called with proper config") assert.Equal(prov.prepConfigs, []interface{}{42}, "prepare should be called with proper config")
assert.Equal(prov.prepUi, ui, "prepare should be called with proper ui")
} }
func TestBuild_Run(t *testing.T) { func TestBuild_Run(t *testing.T) {
......
...@@ -13,13 +13,13 @@ type cmdProvisioner struct { ...@@ -13,13 +13,13 @@ type cmdProvisioner struct {
client *client client *client
} }
func (c *cmdProvisioner) Prepare(config interface{}, ui packer.Ui) { func (c *cmdProvisioner) Prepare(configs ...interface{}) {
defer func() { defer func() {
r := recover() r := recover()
c.checkExit(r, nil) c.checkExit(r, nil)
}() }()
c.p.Prepare(config, ui) c.p.Prepare(configs...)
} }
func (c *cmdProvisioner) Provision(ui packer.Ui, comm packer.Communicator) { func (c *cmdProvisioner) Provision(ui packer.Ui, comm packer.Communicator) {
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
type helperProvisioner byte type helperProvisioner byte
func (helperProvisioner) Prepare(interface{}, packer.Ui) {} func (helperProvisioner) Prepare(...interface{}) {}
func (helperProvisioner) Provision(packer.Ui, packer.Communicator) {} func (helperProvisioner) Provision(packer.Ui, packer.Communicator) {}
......
...@@ -6,7 +6,7 @@ type Provisioner interface { ...@@ -6,7 +6,7 @@ type Provisioner interface {
// Prepare is called with the raw configuration and a UI element in // Prepare is called with the raw configuration and a UI element in
// order to setup the internal state of the provisioner and perform // order to setup the internal state of the provisioner and perform
// any validation necessary for the provisioner. // any validation necessary for the provisioner.
Prepare(interface{}, Ui) Prepare(...interface{})
// Provision is called to actually provision the machine. A UI is // Provision is called to actually provision the machine. A UI is
// given to communicate with the user, and a communicator is given that // given to communicate with the user, and a communicator is given that
......
...@@ -4,15 +4,13 @@ import "testing" ...@@ -4,15 +4,13 @@ import "testing"
type TestProvisioner struct { type TestProvisioner struct {
prepCalled bool prepCalled bool
prepConfig interface{} prepConfigs []interface{}
prepUi Ui
provCalled bool provCalled bool
} }
func (t *TestProvisioner) Prepare(config interface{}, ui Ui) { func (t *TestProvisioner) Prepare(configs ...interface{}) {
t.prepCalled = true t.prepCalled = true
t.prepConfig = config t.prepConfigs = configs
t.prepUi = ui
} }
func (t *TestProvisioner) Provision(Ui, Communicator) { func (t *TestProvisioner) Provision(Ui, Communicator) {
......
...@@ -18,8 +18,7 @@ type ProvisionerServer struct { ...@@ -18,8 +18,7 @@ type ProvisionerServer struct {
} }
type ProvisionerPrepareArgs struct { type ProvisionerPrepareArgs struct {
Config interface{} Configs []interface{}
RPCAddress string
} }
type ProvisionerProvisionArgs struct { type ProvisionerProvisionArgs struct {
...@@ -29,12 +28,8 @@ type ProvisionerProvisionArgs struct { ...@@ -29,12 +28,8 @@ type ProvisionerProvisionArgs struct {
func Provisioner(client *rpc.Client) *provisioner { func Provisioner(client *rpc.Client) *provisioner {
return &provisioner{client} return &provisioner{client}
} }
func (p *provisioner) Prepare(config interface{}, ui packer.Ui) { func (p *provisioner) Prepare(configs ...interface{}) {
// TODO: Error handling args := &ProvisionerPrepareArgs{configs}
server := rpc.NewServer()
RegisterUi(server, ui)
args := &ProvisionerPrepareArgs{config, serveSingleConn(server)}
p.client.Call("Provisioner.Prepare", args, new(interface{})) p.client.Call("Provisioner.Prepare", args, new(interface{}))
} }
...@@ -49,14 +44,7 @@ func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) { ...@@ -49,14 +44,7 @@ func (p *provisioner) Provision(ui packer.Ui, comm packer.Communicator) {
} }
func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *interface{}) error { func (p *ProvisionerServer) Prepare(args *ProvisionerPrepareArgs, reply *interface{}) error {
client, err := rpc.Dial("tcp", args.RPCAddress) p.p.Prepare(args.Configs...)
if err != nil {
return err
}
ui := &Ui{client}
p.p.Prepare(args.Config, ui)
return nil return nil
} }
......
...@@ -9,17 +9,15 @@ import ( ...@@ -9,17 +9,15 @@ import (
type testProvisioner struct { type testProvisioner struct {
prepareCalled bool prepareCalled bool
prepareConfig interface{} prepareConfigs []interface{}
prepareUi packer.Ui
provCalled bool provCalled bool
provComm packer.Communicator provComm packer.Communicator
provUi packer.Ui provUi packer.Ui
} }
func (p *testProvisioner) Prepare(config interface{}, ui packer.Ui) { func (p *testProvisioner) Prepare(configs ...interface{}) {
p.prepareCalled = true p.prepareCalled = true
p.prepareConfig = config p.prepareConfigs = configs
p.prepareUi = ui
} }
func (p *testProvisioner) Provision(ui packer.Ui, comm packer.Communicator) { func (p *testProvisioner) Provision(ui packer.Ui, comm packer.Communicator) {
...@@ -45,17 +43,13 @@ func TestProvisionerRPC(t *testing.T) { ...@@ -45,17 +43,13 @@ func TestProvisionerRPC(t *testing.T) {
// Test Prepare // Test Prepare
config := 42 config := 42
ui := &testUi{}
pClient := Provisioner(client) pClient := Provisioner(client)
pClient.Prepare(config, ui) pClient.Prepare(config)
assert.True(p.prepareCalled, "prepare should be called") assert.True(p.prepareCalled, "prepare should be called")
assert.Equal(p.prepareConfig, 42, "prepare should be called with right arg") assert.Equal(p.prepareConfigs, []interface{}{42}, "prepare should be called with right arg")
p.prepareUi.Say("foo")
assert.True(ui.sayCalled, "say should be called")
// Test Provision // Test Provision
ui = &testUi{} ui := &testUi{}
comm := &testCommunicator{} comm := &testCommunicator{}
pClient.Provision(ui, comm) pClient.Provision(ui, comm)
assert.True(p.provCalled, "provision should be called") assert.True(p.provCalled, "provision should be called")
......
...@@ -29,9 +29,11 @@ type Provisioner struct { ...@@ -29,9 +29,11 @@ type Provisioner struct {
config config config config
} }
func (p *Provisioner) Prepare(raw interface{}, ui packer.Ui) { func (p *Provisioner) Prepare(raws ...interface{}) {
// TODO: errors // TODO: errors
_ = mapstructure.Decode(raw, &p.config) for _, raw := range raws {
_ = mapstructure.Decode(raw, &p.config)
}
if p.config.RemotePath == "" { if p.config.RemotePath == "" {
p.config.RemotePath = DefaultRemotePath p.config.RemotePath = DefaultRemotePath
......
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