Commit fbcc6cb2 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Build now has provisioners, template parses and builds

parent 11d46a3a
......@@ -15,14 +15,22 @@ type Build interface {
// multiple files, of course, but it should be for only a single provider
// (such as VirtualBox, EC2, etc.).
type coreBuild struct {
name string
builder Builder
hooks map[string][]Hook
rawConfig interface{}
name string
builder Builder
builderConfig interface{}
hooks map[string][]Hook
provisioners []coreBuildProvisioner
prepareCalled bool
}
// Keeps track of the provisioner and the configuration of the provisioner
// within the build.
type coreBuildProvisioner struct {
provisioner Provisioner
config interface{}
}
// Returns the name of the build.
func (b *coreBuild) Name() string {
return b.name
......@@ -32,7 +40,7 @@ func (b *coreBuild) Name() string {
// and any hooks. This _must_ be called prior to Run.
func (b *coreBuild) Prepare() (err error) {
b.prepareCalled = true
err = b.builder.Prepare(b.rawConfig)
err = b.builder.Prepare(b.builderConfig)
if err != nil {
log.Printf("Build '%s' prepare failure: %s\n", b.name, err)
}
......
......@@ -28,9 +28,9 @@ func (tb *TestBuilder) Run(ui Ui, h Hook) Artifact {
func testBuild() Build {
return &coreBuild{
name: "test",
builder: &TestBuilder{},
rawConfig: 42,
name: "test",
builder: &TestBuilder{},
builderConfig: 42,
}
}
......
......@@ -149,6 +149,7 @@ func (t *Template) BuildNames() []string {
// If the build does not exist as part of this template, an error is
// returned.
func (t *Template) Build(name string, components *ComponentFinder) (b Build, err error) {
// Setup the Builder
builderConfig, ok := t.Builders[name]
if !ok {
err = fmt.Errorf("No such build found in template: %s", name)
......@@ -165,6 +166,7 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
return
}
// Gather the Hooks
hooks := make(map[string][]Hook)
for tplEvent, tplHooks := range t.Hooks {
curHooks := make([]Hook, 0, len(tplHooks))
......@@ -187,11 +189,30 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
hooks[tplEvent] = curHooks
}
// Prepare the provisioners
provisioners := make([]coreBuildProvisioner, 0, len(t.Provisioners))
for _, rawProvisioner := range t.Provisioners {
var provisioner Provisioner
provisioner, err = components.Provisioner(rawProvisioner.pType)
if err != nil {
return
}
if provisioner == nil {
err = fmt.Errorf("Provisioner type not found: %s", rawProvisioner.pType)
return
}
coreProv := coreBuildProvisioner{provisioner, rawProvisioner.rawConfig}
provisioners = append(provisioners, coreProv)
}
b = &coreBuild{
name: name,
builder: builder,
hooks: hooks,
rawConfig: builderConfig.rawConfig,
name: name,
builder: builder,
builderConfig: builderConfig.rawConfig,
hooks: hooks,
provisioners: provisioners,
}
return
......
......@@ -302,6 +302,12 @@ func TestTemplate_Build(t *testing.T) {
"name": "test1",
"type": "test-builder"
}
],
"provisioners": [
{
"type": "test-prov"
}
]
}
`
......@@ -319,18 +325,26 @@ func TestTemplate_Build(t *testing.T) {
"test-builder": builder,
}
provisioner := &TestProvisioner{}
provisionerMap := map[string]Provisioner{
"test-prov": provisioner,
}
builderFactory := func(n string) (Builder, error) { return builderMap[n], nil }
components := &ComponentFinder{Builder: builderFactory}
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")
build.Prepare()
build.Run(testUi())
assert.True(builder.prepareCalled, "prepare should be called")
assert.Equal(builder.prepareConfig, expectedConfig, "prepare config should be correct")
assert.True(builder.runCalled, "run should be called")
coreBuild, ok := build.(*coreBuild)
assert.True(ok, "should be a core build")
assert.Equal(coreBuild.builder, builder, "should have the same builder")
assert.Equal(coreBuild.builderConfig, expectedConfig, "should have proper config")
assert.Equal(len(coreBuild.provisioners), 1, "should have one 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