Commit bd6f176b authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: Builds use their own UI [GH-21]

parent a7a51f1d
...@@ -128,19 +128,11 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -128,19 +128,11 @@ func (c Command) Run(env packer.Environment, args []string) int {
buildUis := make(map[string]packer.Ui) buildUis := make(map[string]packer.Ui)
for i, b := range builds { for i, b := range builds {
var ui packer.Ui ui := &packer.ColoredUi{
ui = &packer.ColoredUi{
Color: colors[i%len(colors)], Color: colors[i%len(colors)],
Ui: env.Ui(), Ui: env.Ui(),
} }
ui = &packer.PrefixedUi{
fmt.Sprintf("==> %s", b.Name()),
fmt.Sprintf(" %s", b.Name()),
ui,
}
buildUis[b.Name()] = ui buildUis[b.Name()] = ui
ui.Say(fmt.Sprintf("%s output will be in this color.", b.Name())) ui.Say(fmt.Sprintf("%s output will be in this color.", b.Name()))
} }
......
...@@ -126,7 +126,7 @@ func (b *coreBuild) Prepare() (err error) { ...@@ -126,7 +126,7 @@ func (b *coreBuild) Prepare() (err error) {
} }
// Runs the actual build. Prepare must be called prior to running this. // Runs the actual build. Prepare must be called prior to running this.
func (b *coreBuild) Run(ui Ui, cache Cache) ([]Artifact, error) { func (b *coreBuild) Run(originalUi Ui, cache Cache) ([]Artifact, error) {
if !b.prepareCalled { if !b.prepareCalled {
panic("Prepare must be called first") panic("Prepare must be called first")
} }
...@@ -155,8 +155,15 @@ func (b *coreBuild) Run(ui Ui, cache Cache) ([]Artifact, error) { ...@@ -155,8 +155,15 @@ func (b *coreBuild) Run(ui Ui, cache Cache) ([]Artifact, error) {
hook := &DispatchHook{hooks} hook := &DispatchHook{hooks}
artifacts := make([]Artifact, 0, 1) artifacts := make([]Artifact, 0, 1)
// The builder just has a normal Ui, but prefixed
builderUi := &PrefixedUi{
fmt.Sprintf("==> %s", b.Name()),
fmt.Sprintf(" %s", b.Name()),
originalUi,
}
log.Printf("Running builder: %s", b.builderType) log.Printf("Running builder: %s", b.builderType)
builderArtifact, err := b.builder.Run(ui, hook, cache) builderArtifact, err := b.builder.Run(builderUi, hook, cache)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -171,12 +178,18 @@ func (b *coreBuild) Run(ui Ui, cache Cache) ([]Artifact, error) { ...@@ -171,12 +178,18 @@ func (b *coreBuild) Run(ui Ui, cache Cache) ([]Artifact, error) {
keepOriginalArtifact := len(b.postProcessors) == 0 keepOriginalArtifact := len(b.postProcessors) == 0
// Run the post-processors // Run the post-processors
PostProcessorRunSeqLoop: PostProcessorRunSeqLoop:
for _, ppSeq := range b.postProcessors { for _, ppSeq := range b.postProcessors {
priorArtifact := builderArtifact priorArtifact := builderArtifact
for i, corePP := range ppSeq { for i, corePP := range ppSeq {
ui.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType)) ppUi := &PrefixedUi{
artifact, err := corePP.processor.PostProcess(ui, priorArtifact) fmt.Sprintf("==> %s (%s)", b.Name(), corePP.processorType),
fmt.Sprintf(" %s (%s)", b.Name(), corePP.processorType),
originalUi,
}
builderUi.Say(fmt.Sprintf("Running post-processor: %s", corePP.processorType))
artifact, err := corePP.processor.PostProcess(ppUi, priorArtifact)
if err != nil { if err != nil {
errors = append(errors, fmt.Errorf("Post-processor failed: %s", err)) errors = append(errors, fmt.Errorf("Post-processor failed: %s", err))
continue PostProcessorRunSeqLoop continue PostProcessorRunSeqLoop
...@@ -195,57 +208,57 @@ PostProcessorRunSeqLoop: ...@@ -195,57 +208,57 @@ PostProcessorRunSeqLoop:
log.Printf( log.Printf(
"Flagging to keep original artifact from post-processor '%s'", "Flagging to keep original artifact from post-processor '%s'",
corePP.processorType) corePP.processorType)
keepOriginalArtifact = true keepOriginalArtifact = true
} }
} else {
// We have a prior artifact. If we want to keep it, we append
// it to the results list. Otherwise, we destroy it.
if corePP.keepInputArtifact {
artifacts = append(artifacts, priorArtifact)
} else { } else {
log.Printf("Deleting prior artifact from post-processor '%s'", corePP.processorType) // We have a prior artifact. If we want to keep it, we append
if err := priorArtifact.Destroy(); err != nil { // it to the results list. Otherwise, we destroy it.
errors = append(errors, fmt.Errorf("Failed cleaning up prior artifact: %s", err)) if corePP.keepInputArtifact {
artifacts = append(artifacts, priorArtifact)
} else {
log.Printf("Deleting prior artifact from post-processor '%s'", corePP.processorType)
if err := priorArtifact.Destroy(); err != nil {
errors = append(errors, fmt.Errorf("Failed cleaning up prior artifact: %s", err))
}
} }
} }
priorArtifact = artifact
} }
priorArtifact = artifact // Add on the last artifact to the results
if priorArtifact != nil {
artifacts = append(artifacts, priorArtifact)
}
} }
// Add on the last artifact to the results if keepOriginalArtifact {
if priorArtifact != nil { artifacts = append(artifacts, nil)
artifacts = append(artifacts, priorArtifact) copy(artifacts[1:], artifacts)
artifacts[0] = builderArtifact
} else {
log.Printf("Deleting original artifact for build '%s'", b.name)
if err := builderArtifact.Destroy(); err != nil {
errors = append(errors, fmt.Errorf("Error destroying builder artifact: %s", err))
}
} }
}
if keepOriginalArtifact { if len(errors) > 0 {
artifacts = append(artifacts, nil) err = &MultiError{errors}
copy(artifacts[1:], artifacts)
artifacts[0] = builderArtifact
} else {
log.Printf("Deleting original artifact for build '%s'", b.name)
if err := builderArtifact.Destroy(); err != nil {
errors = append(errors, fmt.Errorf("Error destroying builder artifact: %s", err))
} }
}
if len(errors) > 0 { return artifacts, err
err = &MultiError{errors}
} }
return artifacts, err func (b *coreBuild) SetDebug(val bool) {
} if b.prepareCalled {
panic("prepare has already been called")
}
func (b *coreBuild) SetDebug(val bool) { b.debug = val
if b.prepareCalled {
panic("prepare has already been called")
} }
b.debug = val // Cancels the build if it is running.
} func (b *coreBuild) Cancel() {
b.builder.Cancel()
// Cancels the build if it is running. }
func (b *coreBuild) Cancel() {
b.builder.Cancel()
}
...@@ -113,7 +113,6 @@ func TestBuild_Run(t *testing.T) { ...@@ -113,7 +113,6 @@ func TestBuild_Run(t *testing.T) {
// Verify builder was run // Verify builder was run
builder := build.builder.(*TestBuilder) builder := build.builder.(*TestBuilder)
assert.True(builder.runCalled, "run should be called") assert.True(builder.runCalled, "run should be called")
assert.Equal(builder.runUi, ui, "run should be called with ui")
// Verify hooks are disapatchable // Verify hooks are disapatchable
dispatchHook := builder.runHook dispatchHook := builder.runHook
......
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