add on-error flag option to run cleanup provisioner

This commit is contained in:
Moss 2020-06-16 15:29:56 +02:00
parent 415a9c47b6
commit 4f26534072
4 changed files with 30 additions and 10 deletions

View File

@ -361,7 +361,7 @@ Options:
-only=foo,bar,baz Build only the specified builds. -only=foo,bar,baz Build only the specified builds.
-force Force a build to continue if artifacts exist, deletes existing artifacts. -force Force a build to continue if artifacts exist, deletes existing artifacts.
-machine-readable Produce machine-readable output. -machine-readable Produce machine-readable output.
-on-error=[cleanup|abort|ask] If the build fails do: clean up (default), abort, or ask. -on-error=[cleanup|abort|ask|run-cleanup-provisioner] If the build fails do: clean up (default), abort, ask, or run-cleanup-provisioner.
-parallel-builds=1 Number of builds to run in parallel. 1 disables parallelization. 0 means no limit (Default: 0) -parallel-builds=1 Number of builds to run in parallel. 1 disables parallelization. 0 means no limit (Default: 0)
-timestamp-ui Enable prefixing of each ui output with an RFC3339 timestamp. -timestamp-ui Enable prefixing of each ui output with an RFC3339 timestamp.
-var 'key=value' Variable for templates, can be used multiple times. -var 'key=value' Variable for templates, can be used multiple times.

View File

@ -79,7 +79,7 @@ func (ba *BuildArgs) AddFlagSets(flags *flag.FlagSet) {
flags.Int64Var(&ba.ParallelBuilds, "parallel-builds", 0, "") flags.Int64Var(&ba.ParallelBuilds, "parallel-builds", 0, "")
flagOnError := enumflag.New(&ba.OnError, "cleanup", "abort", "ask") flagOnError := enumflag.New(&ba.OnError, "cleanup", "abort", "ask", "run-cleanup-provisioner")
flags.Var(flagOnError, "on-error", "") flags.Var(flagOnError, "on-error", "")
ba.MetaArgs.AddFlagSets(flags) ba.MetaArgs.AddFlagSets(flags)

View File

@ -17,12 +17,24 @@ func newRunner(steps []multistep.Step, config PackerConfig, ui packer.Ui) (multi
case "", "cleanup": case "", "cleanup":
case "abort": case "abort":
for i, step := range steps { for i, step := range steps {
steps[i] = abortStep{step, ui} steps[i] = abortStep{
step: step,
cleanupProv: false,
ui: ui,
}
} }
case "ask": case "ask":
for i, step := range steps { for i, step := range steps {
steps[i] = askStep{step, ui} steps[i] = askStep{step, ui}
} }
case "run-cleanup-provisioner":
for i, step := range steps {
steps[i] = abortStep{
step: step,
cleanupProv: true,
ui: ui,
}
}
} }
if config.PackerDebug { if config.PackerDebug {
@ -57,8 +69,9 @@ func typeName(i interface{}) string {
} }
type abortStep struct { type abortStep struct {
step multistep.Step step multistep.Step
ui packer.Ui cleanupProv bool
ui packer.Ui
} }
func (s abortStep) InnerStepName() string { func (s abortStep) InnerStepName() string {
@ -70,6 +83,11 @@ func (s abortStep) Run(ctx context.Context, state multistep.StateBag) multistep.
} }
func (s abortStep) Cleanup(state multistep.StateBag) { func (s abortStep) Cleanup(state multistep.StateBag) {
if s.InnerStepName() == typeName(StepProvision{}) && s.cleanupProv {
s.step.Cleanup(state)
return
}
shouldCleanup := handleAbortsAndInterupts(state, s.ui, typeName(s.step)) shouldCleanup := handleAbortsAndInterupts(state, s.ui, typeName(s.step))
if !shouldCleanup { if !shouldCleanup {
return return

View File

@ -40,12 +40,14 @@ artifacts that are created will be outputted at the end of the build.
remove the artifacts from the previous build. This will allow the user to remove the artifacts from the previous build. This will allow the user to
repeat a build without having to manually clean these artifacts beforehand. repeat a build without having to manually clean these artifacts beforehand.
- `-on-error=cleanup` (default), `-on-error=abort`, `-on-error=ask` - Selects - `-on-error=cleanup` (default), `-on-error=abort`, `-on-error=ask`, `-on-error=run-cleanup-provisioner` -
what to do when the build fails. `cleanup` cleans up after the previous Selects what to do when the build fails.
steps, deleting temporary files and virtual machines. `abort` exits without - `cleanup` cleans up after the previous steps, deleting temporary files and virtual machines.
any cleanup, which might require the next build to use `-force`. `ask` - `abort` exits without any cleanup, which might require the next build to use `-force`.
presents a prompt and waits for you to decide to clean up, abort, or retry - `ask` presents a prompt and waits for you to decide to clean up, abort, or retry
the failed step. the failed step.
- `run-cleanup-provisioner` aborts and exits without any cleanup besides
the [error-cleanup-provisioner](/docs/templates/provisioners#on-error-provisioner) if one is defined.
- `-only=foo,bar,baz` - Only run the builds with the given comma-separated - `-only=foo,bar,baz` - Only run the builds with the given comma-separated
names. Build names by default are their type, unless a specific `name` names. Build names by default are their type, unless a specific `name`