packer: builds now have post processors as part of them

This commit is contained in:
Mitchell Hashimoto 2013-06-18 09:58:39 -07:00
parent 18f9677b54
commit 91a6a7797d
3 changed files with 59 additions and 12 deletions

View File

@ -48,6 +48,7 @@ type coreBuild struct {
builder Builder builder Builder
builderConfig interface{} builderConfig interface{}
hooks map[string][]Hook hooks map[string][]Hook
postProcessors [][]coreBuildPostProcessor
provisioners []coreBuildProvisioner provisioners []coreBuildProvisioner
debug bool debug bool
@ -55,6 +56,13 @@ type coreBuild struct {
prepareCalled bool prepareCalled bool
} }
// Keeps track of the post-processor and the configuration of the
// post-processor used within a build.
type coreBuildPostProcessor struct {
processor PostProcessor
config interface{}
}
// Keeps track of the provisioner and the configuration of the provisioner // Keeps track of the provisioner and the configuration of the provisioner
// within the build. // within the build.
type coreBuildProvisioner struct { type coreBuildProvisioner struct {

View File

@ -285,6 +285,29 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
hooks[tplEvent] = curHooks hooks[tplEvent] = curHooks
} }
// Prepare the post-processors
postProcessors := make([][]coreBuildPostProcessor, 0, len(t.PostProcessors))
for _, rawPPs := range t.PostProcessors {
current := make([]coreBuildPostProcessor, len(rawPPs))
for i, rawPP := range rawPPs {
pp, err := components.PostProcessor(rawPP.Type)
if err != nil {
return nil, err
}
if pp == nil {
return nil, fmt.Errorf("PostProcessor type not found: %s", rawPP.Type)
}
current[i] = coreBuildPostProcessor{
processor: pp,
config: rawPP.rawConfig,
}
}
postProcessors = append(postProcessors, current)
}
// Prepare the provisioners // Prepare the provisioners
provisioners := make([]coreBuildProvisioner, 0, len(t.Provisioners)) provisioners := make([]coreBuildProvisioner, 0, len(t.Provisioners))
for _, rawProvisioner := range t.Provisioners { for _, rawProvisioner := range t.Provisioners {
@ -317,6 +340,7 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
builder: builder, builder: builder,
builderConfig: builderConfig.rawConfig, builderConfig: builderConfig.rawConfig,
hooks: hooks, hooks: hooks,
postProcessors: postProcessors,
provisioners: provisioners, provisioners: provisioners,
} }

View File

@ -443,6 +443,11 @@ func TestTemplate_Build(t *testing.T) {
{ {
"type": "test-prov" "type": "test-prov"
} }
],
"post-processors": [
"simple",
["simple", "simple"]
] ]
} }
` `
@ -465,10 +470,17 @@ func TestTemplate_Build(t *testing.T) {
"test-prov": provisioner, "test-prov": provisioner,
} }
pp := new(TestPostProcessor)
ppMap := map[string]PostProcessor{
"simple": pp,
}
builderFactory := func(n string) (Builder, error) { return builderMap[n], nil } builderFactory := func(n string) (Builder, error) { return builderMap[n], nil }
ppFactory := func(n string) (PostProcessor, error) { return ppMap[n], nil }
provFactory := func(n string) (Provisioner, error) { return provisionerMap[n], nil } provFactory := func(n string) (Provisioner, error) { return provisionerMap[n], nil }
components := &ComponentFinder{ components := &ComponentFinder{
Builder: builderFactory, Builder: builderFactory,
PostProcessor: ppFactory,
Provisioner: provFactory, Provisioner: provFactory,
} }
@ -482,6 +494,9 @@ func TestTemplate_Build(t *testing.T) {
assert.Equal(coreBuild.builder, builder, "should have the same builder") assert.Equal(coreBuild.builder, builder, "should have the same builder")
assert.Equal(coreBuild.builderConfig, expectedConfig, "should have proper config") assert.Equal(coreBuild.builderConfig, expectedConfig, "should have proper config")
assert.Equal(len(coreBuild.provisioners), 1, "should have one provisioner") assert.Equal(len(coreBuild.provisioners), 1, "should have one provisioner")
assert.Equal(len(coreBuild.postProcessors), 2, "should have pps")
assert.Equal(len(coreBuild.postProcessors[0]), 1, "should have correct number")
assert.Equal(len(coreBuild.postProcessors[1]), 2, "should have correct number")
} }
func TestTemplate_Build_ProvisionerOverride(t *testing.T) { func TestTemplate_Build_ProvisionerOverride(t *testing.T) {