packer: run provisioners
This commit is contained in:
parent
b5f4ffa56c
commit
b25ae21e13
|
@ -42,6 +42,12 @@ func (tb *MockBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) {
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
if h != nil {
|
||||
if err := h.Run(HookProvision, ui, nil, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &MockArtifact{
|
||||
IdValue: tb.ArtifactId,
|
||||
}, nil
|
||||
|
|
|
@ -118,13 +118,55 @@ func (c *Core) Build(n string) (Build, error) {
|
|||
"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{
|
||||
name: n,
|
||||
builder: builder,
|
||||
builderConfig: configBuilder.Config,
|
||||
builderType: configBuilder.Type,
|
||||
provisioners: provisioners,
|
||||
variables: c.variables,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
cases := []struct {
|
||||
File string
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"builders": [{
|
||||
"type": "test"
|
||||
}],
|
||||
|
||||
"provisioners": [{
|
||||
"type": "test"
|
||||
}]
|
||||
}
|
|
@ -58,3 +58,19 @@ func TestBuilder(t *testing.T, c *CoreConfig, n string) *MockBuilder {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue