From 0f51ab5c084c36a915e0b19d8e52f0143b180215 Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Mon, 25 Feb 2019 15:57:15 -0600 Subject: [PATCH] feature: add early unflock step for chroot builder --- builder/osc/chroot/builder.go | 1 + builder/osc/chroot/cleanup.go | 10 ++++++++ builder/osc/chroot/step_early_unflock.go | 30 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 builder/osc/chroot/cleanup.go create mode 100644 builder/osc/chroot/step_early_unflock.go diff --git a/builder/osc/chroot/builder.go b/builder/osc/chroot/builder.go index 6f545ee98..b5bcb907e 100644 --- a/builder/osc/chroot/builder.go +++ b/builder/osc/chroot/builder.go @@ -248,6 +248,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Ctx: b.config.ctx, }, &StepLinkVolume{}, + &StepEarlyUnflock{}, ) // Run! diff --git a/builder/osc/chroot/cleanup.go b/builder/osc/chroot/cleanup.go new file mode 100644 index 000000000..0befac174 --- /dev/null +++ b/builder/osc/chroot/cleanup.go @@ -0,0 +1,10 @@ +package chroot + +import ( + "github.com/hashicorp/packer/helper/multistep" +) + +// Cleanup is an interface that some steps implement for early cleanup. +type Cleanup interface { + CleanupFunc(multistep.StateBag) error +} diff --git a/builder/osc/chroot/step_early_unflock.go b/builder/osc/chroot/step_early_unflock.go new file mode 100644 index 000000000..225e91fb9 --- /dev/null +++ b/builder/osc/chroot/step_early_unflock.go @@ -0,0 +1,30 @@ +package chroot + +import ( + "context" + "fmt" + "log" + + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +// StepEarlyUnflock unlocks the flock. +type StepEarlyUnflock struct{} + +func (s *StepEarlyUnflock) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { + cleanup := state.Get("flock_cleanup").(Cleanup) + ui := state.Get("ui").(packer.Ui) + + log.Println("Unlocking file lock...") + if err := cleanup.CleanupFunc(state); err != nil { + err := fmt.Errorf("Error unlocking file lock: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + return multistep.ActionContinue +} + +func (s *StepEarlyUnflock) Cleanup(state multistep.StateBag) {}