Commit b25ae21e authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: run provisioners

parent b5f4ffa5
...@@ -42,6 +42,12 @@ func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) { ...@@ -42,6 +42,12 @@ func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) {
return nil, nil return nil, nil
} }
if h != nil {
if err := h.Run(HookProvision, ui, nil, nil); err != nil {
return nil, err
}
}
return &MockArtifact{ return &MockArtifact{
IdValue: tb.ArtifactId, IdValue: tb.ArtifactId,
}, nil }, nil
......
...@@ -118,13 +118,55 @@ func (c *Core) Build(n string) (Build, error) { ...@@ -118,13 +118,55 @@ func (c *Core) Build(n string) (Build, error) {
"builder type not found: %s", configBuilder.Type) "builder type not found: %s", configBuilder.Type)
} }
// TODO: template process name // rawName is the uninterpolated name that we use for various lookups
rawName := configBuilder.Name
// Setup the provisioners for this build
provisioners := make([]coreBuildProvisioner, 0, len(c.template.Provisioners))
for _, rawP := range c.template.Provisioners {
// If we're skipping this, then ignore it
if rawP.Skip(rawName) {
continue
}
// Get the provisioner
provisioner, err := c.components.Provisioner(rawP.Type)
if err != nil {
return nil, fmt.Errorf(
"error initializing provisioner '%s': %s",
rawP.Type, err)
}
if provisioner == nil {
return nil, fmt.Errorf(
"provisioner type not found: %s", rawP.Type)
}
// Get the configuration
config := make([]interface{}, 1, 2)
config[0] = rawP.Config
// TODO override
// If we're pausing, we wrap the provisioner in a special pauser.
if rawP.PauseBefore > 0 {
provisioner = &PausedProvisioner{
PauseBefore: rawP.PauseBefore,
Provisioner: provisioner,
}
}
provisioners = append(provisioners, coreBuildProvisioner{
provisioner: provisioner,
config: config,
})
}
return &coreBuild{ return &coreBuild{
name: n, name: n,
builder: builder, builder: builder,
builderConfig: configBuilder.Config, builderConfig: configBuilder.Config,
builderType: configBuilder.Type, builderType: configBuilder.Type,
provisioners: provisioners,
variables: c.variables, variables: c.variables,
}, nil }, nil
} }
......
...@@ -120,6 +120,40 @@ func TestCoreBuild_nonExist(t *testing.T) { ...@@ -120,6 +120,40 @@ func TestCoreBuild_nonExist(t *testing.T) {
} }
} }
func TestCoreBuild_prov(t *testing.T) {
config := TestCoreConfig(t)
testCoreTemplate(t, config, fixtureDir("build-prov.json"))
b := TestBuilder(t, config, "test")
p := TestProvisioner(t, config, "test")
core := TestCore(t, config)
b.ArtifactId = "hello"
build, err := core.Build("test")
if err != nil {
t.Fatalf("err: %s", err)
}
if _, err := build.Prepare(); err != nil {
t.Fatalf("err: %s", err)
}
artifact, err := build.Run(nil, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(artifact) != 1 {
t.Fatalf("bad: %#v", artifact)
}
if artifact[0].Id() != b.ArtifactId {
t.Fatalf("bad: %s", artifact[0].Id())
}
if !p.ProvCalled {
t.Fatal("provisioner not called")
}
}
func TestCoreValidate(t *testing.T) { func TestCoreValidate(t *testing.T) {
cases := []struct { cases := []struct {
File string File string
......
{
"builders": [{
"type": "test"
}],
"provisioners": [{
"type": "test"
}]
}
...@@ -58,3 +58,19 @@ func TestBuilder(t *testing.T, c *CoreConfig, n string) *MockBuilder { ...@@ -58,3 +58,19 @@ func TestBuilder(t *testing.T, c *CoreConfig, n string) *MockBuilder {
return &b return &b
} }
// TestProvisioner sets the prov. with the name n to the component finder
// and returns the mock.
func TestProvisioner(t *testing.T, c *CoreConfig, n string) *MockProvisioner {
var b MockProvisioner
c.Components.Provisioner = func(actual string) (Provisioner, error) {
if actual != n {
return nil, nil
}
return &b, nil
}
return &b
}
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