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