Skip to content

Commit

Permalink
Add -on-error command line argument to allow preserving artifacts on …
Browse files Browse the repository at this point in the history
…builder errors

Resolves hashicorp#409
  • Loading branch information
orivej committed Sep 16, 2016
1 parent d310de8 commit 6762965
Show file tree
Hide file tree
Showing 23 changed files with 231 additions and 170 deletions.
10 changes: 1 addition & 9 deletions builder/amazon/chroot/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
)

// Run!
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)

// If there was an error, return that
Expand Down
10 changes: 1 addition & 9 deletions builder/amazon/ebs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run!
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)

// If there was an error, return that
Expand Down
10 changes: 1 addition & 9 deletions builder/amazon/instance/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run!
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)

// If there was an error, return that
Expand Down
15 changes: 1 addition & 14 deletions builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
ui.Message(fmt.Sprintf("temp admin password: '%s'", b.config.Password))
}

b.runner = b.createRunner(&steps, ui)
b.runner = packerCommon.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(b.stateBag)

// Report any errors.
Expand Down Expand Up @@ -198,19 +198,6 @@ func (b *Builder) Cancel() {
}
}

func (b *Builder) createRunner(steps *[]multistep.Step, ui packer.Ui) multistep.Runner {
if b.config.PackerDebug {
return &multistep.DebugRunner{
Steps: *steps,
PauseFn: packerCommon.MultistepDebugFn(ui),
}
}

return &multistep.BasicRunner{
Steps: *steps,
}
}

func (b *Builder) getBlobEndpoint(client *AzureClient, resourceGroupName string, storageAccountName string) (string, error) {
account, err := client.AccountsClient.GetProperties(resourceGroupName, storageAccountName)
if err != nil {
Expand Down
10 changes: 1 addition & 9 deletions builder/digitalocean/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run the steps
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)

// If there was an error, return that
Expand Down
10 changes: 1 addition & 9 deletions builder/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state.Put("driver", driver)

// Run!
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)

// If there was an error, return that
Expand Down
9 changes: 1 addition & 8 deletions builder/googlecompute/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run the steps.
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)

// Report any errors.
Expand Down
10 changes: 1 addition & 9 deletions builder/null/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state.Put("ui", ui)

// Run!
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)

// If there was an error, return that
Expand Down
10 changes: 1 addition & 9 deletions builder/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run!
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
b.runner.Run(state)

// If there was an error, return that
Expand Down
12 changes: 1 addition & 11 deletions builder/parallels/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state.Put("ui", ui)

// Run
if b.config.PackerDebug {
pauseFn := common.MultistepDebugFn(ui)
state.Put("pauseFn", pauseFn)
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: pauseFn,
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)

// If there was an error, return that
Expand Down
11 changes: 1 addition & 10 deletions builder/parallels/pvm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run the steps.
if b.config.PackerDebug {
pauseFn := common.MultistepDebugFn(ui)
state.Put("pauseFn", pauseFn)
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: pauseFn,
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)

// Report any errors.
Expand Down
12 changes: 1 addition & 11 deletions builder/qemu/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,17 +422,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state.Put("ui", ui)

// Run
if b.config.PackerDebug {
pauseFn := common.MultistepDebugFn(ui)
state.Put("pauseFn", pauseFn)
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: pauseFn,
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)

// If there was an error, return that
Expand Down
12 changes: 1 addition & 11 deletions builder/virtualbox/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state.Put("ui", ui)

// Run
if b.config.PackerDebug {
pauseFn := common.MultistepDebugFn(ui)
state.Put("pauseFn", pauseFn)
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: pauseFn,
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)

// If there was an error, return that
Expand Down
11 changes: 1 addition & 10 deletions builder/virtualbox/ovf/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run the steps.
if b.config.PackerDebug {
pauseFn := common.MultistepDebugFn(ui)
state.Put("pauseFn", pauseFn)
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: pauseFn,
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)

// Report any errors.
Expand Down
12 changes: 1 addition & 11 deletions builder/vmware/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run!
if b.config.PackerDebug {
pauseFn := common.MultistepDebugFn(ui)
state.Put("pauseFn", pauseFn)
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: pauseFn,
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}

b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)

// If there was an error, return that
Expand Down
11 changes: 1 addition & 10 deletions builder/vmware/vmx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}

// Run the steps.
if b.config.PackerDebug {
pauseFn := common.MultistepDebugFn(ui)
state.Put("pauseFn", pauseFn)
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: pauseFn,
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
b.runner.Run(state)

// Report any errors.
Expand Down
7 changes: 7 additions & 0 deletions command/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ type BuildCommand struct {

func (c BuildCommand) Run(args []string) int {
var cfgColor, cfgDebug, cfgForce, cfgParallel bool
var cfgOnError string
flags := c.Meta.FlagSet("build", FlagSetBuildFilter|FlagSetVars)
flags.Usage = func() { c.Ui.Say(c.Help()) }
flags.BoolVar(&cfgColor, "color", true, "")
flags.BoolVar(&cfgDebug, "debug", false, "")
flags.BoolVar(&cfgForce, "force", false, "")
flags.StringVar(&cfgOnError, "on-error", "cleanup", "")
flags.BoolVar(&cfgParallel, "parallel", true, "")
if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -99,12 +101,14 @@ func (c BuildCommand) Run(args []string) int {

log.Printf("Build debug mode: %v", cfgDebug)
log.Printf("Force build: %v", cfgForce)
log.Printf("On error: %v", cfgOnError)

// Set the debug and force mode and prepare all the builds
for _, b := range builds {
log.Printf("Preparing build: %s", b.Name())
b.SetDebug(cfgDebug)
b.SetForce(cfgForce)
b.SetOnError(cfgOnError)

warnings, err := b.Prepare()
if err != nil {
Expand Down Expand Up @@ -284,6 +288,9 @@ Options:
-except=foo,bar,baz Build all builds other than these
-force Force a build to continue if artifacts exist, deletes existing artifacts
-machine-readable Machine-readable output
-on-error=abort When a builder fails, abort without cleanup
-on-error=ask When a builder fails, prompt for action
-on-error=cleanup When a builder fails, clean up and exit (the default)
-only=foo,bar,baz Only build the given builds by name
-parallel=false Disable parallelization (on by default)
-var 'key=value' Variable for templates, can be used multiple times.
Expand Down
Loading

0 comments on commit 6762965

Please sign in to comment.