Merge pull request #7913 from hashicorp/red_herring_EOF_abort

[WIP] Red herring eof abort
This commit is contained in:
Megan Marsh 2019-07-26 14:36:14 -07:00 committed by GitHub
commit 7ac17126a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 13 deletions

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"os"
"reflect"
"strings"
"time"
@ -71,17 +70,9 @@ func (s abortStep) Run(ctx context.Context, state multistep.StateBag) multistep.
}
func (s abortStep) Cleanup(state multistep.StateBag) {
err, ok := state.GetOk("error")
if ok {
s.ui.Error(fmt.Sprintf("%s", err))
}
if _, ok := state.GetOk(multistep.StateCancelled); ok {
s.ui.Error("Interrupted, aborting...")
os.Exit(1)
}
if _, ok := state.GetOk(multistep.StateHalted); ok {
s.ui.Error(fmt.Sprintf("Step %q failed, aborting...", typeName(s.step)))
os.Exit(1)
shouldCleanup := handleAbortsAndInterupts(state, s.ui, typeName(s.step))
if !shouldCleanup {
return
}
s.step.Cleanup(state)
}
@ -112,7 +103,8 @@ func (s askStep) Run(ctx context.Context, state multistep.StateBag) (action mult
case askCleanup:
return
case askAbort:
os.Exit(1)
state.Put("aborted", true)
return
case askRetry:
continue
}
@ -120,6 +112,12 @@ func (s askStep) Run(ctx context.Context, state multistep.StateBag) (action mult
}
func (s askStep) Cleanup(state multistep.StateBag) {
if _, ok := state.GetOk("aborted"); ok {
shouldCleanup := handleAbortsAndInterupts(state, s.ui, typeName(s.step))
if !shouldCleanup {
return
}
}
s.step.Cleanup(state)
}
@ -170,3 +168,33 @@ func askPrompt(ui packer.Ui) askResponse {
ui.Say(fmt.Sprintf("Incorrect input: %#v", line))
}
}
func handleAbortsAndInterupts(state multistep.StateBag, ui packer.Ui, stepName string) bool {
// if returns false, don't run cleanup. If true, do run cleanup.
_, alreadyLogged := state.GetOk("abort_step_logged")
err, ok := state.GetOk("error")
if ok && !alreadyLogged {
ui.Error(fmt.Sprintf("%s", err))
state.Put("abort_step_logged", true)
}
if _, ok := state.GetOk(multistep.StateCancelled); ok {
if !alreadyLogged {
ui.Error("Interrupted, aborting...")
state.Put("abort_step_logged", true)
} else {
ui.Error(fmt.Sprintf("aborted: skipping cleanup of step %q", stepName))
}
return false
}
if _, ok := state.GetOk(multistep.StateHalted); ok {
if !alreadyLogged {
ui.Error(fmt.Sprintf("Step %q failed, aborting...", stepName))
state.Put("abort_step_logged", true)
} else {
ui.Error(fmt.Sprintf("aborted: skipping cleanup of step %q", stepName))
}
return false
}
return true
}

3
log.go
View File

@ -35,6 +35,9 @@ func logOutput() (logOutput io.Writer, err error) {
if strings.Contains(scanner.Text(), "ui:") {
continue
}
if strings.Contains(scanner.Text(), "ui error:") {
continue
}
os.Stderr.WriteString(fmt.Sprint(scanner.Text() + "\n"))
}
}(scanner)