Commit d55bf3f3 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Support overrides in provisioners for each build

parent 5638cecd
......@@ -29,7 +29,7 @@ type coreBuild struct {
// within the build.
type coreBuildProvisioner struct {
provisioner Provisioner
config interface{}
config []interface{}
}
// Returns the name of the build.
......@@ -51,7 +51,7 @@ func (b *coreBuild) Prepare(ui Ui) (err error) {
// Prepare the provisioners
for _, coreProv := range b.provisioners {
if err = coreProv.provisioner.Prepare(coreProv.config); err != nil {
if err = coreProv.provisioner.Prepare(coreProv.config...); err != nil {
return
}
}
......
......@@ -14,7 +14,7 @@ func testBuild() Build {
"foo": []Hook{&TestHook{}},
},
provisioners: []coreBuildProvisioner{
coreBuildProvisioner{&TestProvisioner{}, 42},
coreBuildProvisioner{&TestProvisioner{}, []interface{}{42}},
},
}
}
......
......@@ -220,7 +220,16 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
return
}
coreProv := coreBuildProvisioner{provisioner, rawProvisioner.rawConfig}
configs := make([]interface{}, 1, 2)
configs[0] = rawProvisioner.rawConfig
if rawProvisioner.Override != nil {
if override, ok := rawProvisioner.Override[name]; ok {
configs = append(configs, override)
}
}
coreProv := coreBuildProvisioner{provisioner, configs}
provisioners = append(provisioners, coreProv)
}
......
......@@ -448,3 +448,59 @@ func TestTemplate_Build(t *testing.T) {
assert.Equal(coreBuild.builderConfig, expectedConfig, "should have proper config")
assert.Equal(len(coreBuild.provisioners), 1, "should have one provisioner")
}
func TestTemplate_Build_ProvisionerOverride(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := `
{
"name": "my-image",
"builders": [
{
"name": "test1",
"type": "test-builder"
}
],
"provisioners": [
{
"type": "test-prov",
"override": {
"test1": {}
}
}
]
}
`
template, err := ParseTemplate([]byte(data))
assert.Nil(err, "should not error")
builder := testBuilder()
builderMap := map[string]Builder{
"test-builder": builder,
}
provisioner := &TestProvisioner{}
provisionerMap := map[string]Provisioner{
"test-prov": provisioner,
}
builderFactory := func(n string) (Builder, error) { return builderMap[n], nil }
provFactory := func(n string) (Provisioner, error) { return provisionerMap[n], nil }
components := &ComponentFinder{
Builder: builderFactory,
Provisioner: provFactory,
}
// Get the build, verifying we can get it without issue, but also
// that the proper builder was looked up and used for the build.
build, err := template.Build("test1", components)
assert.Nil(err, "should not error")
coreBuild, ok := build.(*coreBuild)
assert.True(ok, "should be a core build")
assert.Equal(len(coreBuild.provisioners), 1, "should have one provisioner")
assert.Equal(len(coreBuild.provisioners[0].config), 2, "should have two configs on the provisioner")
}
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