Commit 8217e64a authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Set DebugConfigKey to true if debug is on

parent fd044982
...@@ -2,6 +2,10 @@ package packer ...@@ -2,6 +2,10 @@ package packer
import "log" import "log"
// This is the key in configurations that is set to "true" when Packer
// debugging is enabled.
const DebugConfigKey = "packer_config"
// A Build represents a single job within Packer that is responsible for // A Build represents a single job within Packer that is responsible for
// building some machine image artifact. Builds are meant to be parallelized. // building some machine image artifact. Builds are meant to be parallelized.
type Build interface { type Build interface {
...@@ -65,8 +69,12 @@ func (b *coreBuild) Prepare() (err error) { ...@@ -65,8 +69,12 @@ func (b *coreBuild) Prepare() (err error) {
// TODO: lock // TODO: lock
b.prepareCalled = true b.prepareCalled = true
debugConfig := map[string]interface{}{
DebugConfigKey: b.debug,
}
// Prepare the builder // Prepare the builder
err = b.builder.Prepare(b.builderConfig) err = b.builder.Prepare(b.builderConfig, debugConfig)
if err != nil { if err != nil {
log.Printf("Build '%s' prepare failure: %s\n", b.name, err) log.Printf("Build '%s' prepare failure: %s\n", b.name, err)
return return
...@@ -74,7 +82,11 @@ func (b *coreBuild) Prepare() (err error) { ...@@ -74,7 +82,11 @@ func (b *coreBuild) Prepare() (err error) {
// Prepare the provisioners // Prepare the provisioners
for _, coreProv := range b.provisioners { for _, coreProv := range b.provisioners {
if err = coreProv.provisioner.Prepare(coreProv.config...); err != nil { configs := make([]interface{}, len(coreProv.config), len(coreProv.config)+1)
copy(configs, coreProv.config)
configs = append(configs, debugConfig)
if err = coreProv.provisioner.Prepare(configs...); err != nil {
return return
} }
} }
......
...@@ -33,20 +33,40 @@ func TestBuild_Name(t *testing.T) { ...@@ -33,20 +33,40 @@ func TestBuild_Name(t *testing.T) {
func TestBuild_Prepare(t *testing.T) { func TestBuild_Prepare(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
debugFalseConfig := map[string]interface{}{DebugConfigKey: false}
build := testBuild() build := testBuild()
coreB := build.(*coreBuild)
builder := coreB.builder.(*TestBuilder)
build.Prepare()
assert.True(builder.prepareCalled, "prepare should be called")
assert.Equal(builder.prepareConfig, []interface{}{42, debugFalseConfig}, "prepare config should be 42")
coreProv := coreB.provisioners[0]
prov := coreProv.provisioner.(*TestProvisioner)
assert.True(prov.prepCalled, "prepare should be called")
assert.Equal(prov.prepConfigs, []interface{}{42, debugFalseConfig}, "prepare should be called with proper config")
}
func TestBuild_Prepare_Debug(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
debugConfig := map[string]interface{}{DebugConfigKey: true}
build := testBuild()
coreB := build.(*coreBuild) coreB := build.(*coreBuild)
builder := coreB.builder.(*TestBuilder) builder := coreB.builder.(*TestBuilder)
build.SetDebug(true)
build.Prepare() build.Prepare()
assert.True(builder.prepareCalled, "prepare should be called") assert.True(builder.prepareCalled, "prepare should be called")
assert.Equal(builder.prepareConfig, []interface{}{42}, "prepare config should be 42") assert.Equal(builder.prepareConfig, []interface{}{42, debugConfig}, "prepare config should be 42")
// Verify provisioners were prepared
coreProv := coreB.provisioners[0] coreProv := coreB.provisioners[0]
prov := coreProv.provisioner.(*TestProvisioner) prov := coreProv.provisioner.(*TestProvisioner)
assert.True(prov.prepCalled, "prepare should be called") assert.True(prov.prepCalled, "prepare should be called")
assert.Equal(prov.prepConfigs, []interface{}{42}, "prepare should be called with proper config") assert.Equal(prov.prepConfigs, []interface{}{42, debugConfig}, "prepare should be called with proper config")
} }
func TestBuild_Run(t *testing.T) { func TestBuild_Run(t *testing.T) {
......
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