Add StepEarlyCleanup

This commit is contained in:
Paul Meyer 2019-05-31 19:01:47 +00:00
parent 977ca5be1c
commit 77b782c5b5
2 changed files with 11 additions and 1 deletions

View File

@ -165,6 +165,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
Files: b.config.CopyFiles,
},
&chroot.StepChrootProvision{},
&chroot.StepEarlyCleanup{},
)
// Run!

View File

@ -44,10 +44,18 @@ func (s StepAttachDisk) Run(ctx context.Context, state multistep.StateBag) multi
ui.Say(fmt.Sprintf("Disk available at %q", device))
state.Put("device", device)
state.Put("attach_cleanup", s)
return multistep.ActionContinue
}
func (s StepAttachDisk) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
if err := s.CleanupFunc(state); err != nil {
ui.Error(err.Error())
}
}
func (s *StepAttachDisk) CleanupFunc(state multistep.StateBag) error {
azcli := state.Get("azureclient").(client.AzureClientSet)
ui := state.Get("ui").(packer.Ui)
@ -60,6 +68,7 @@ func (s StepAttachDisk) Cleanup(state multistep.StateBag) {
da := NewDiskAttacher(azcli)
err := da.DetachDisk(context.Background(), diskResourceID)
if err != nil {
ui.Error(fmt.Sprintf("error detaching %q: %v", diskResourceID, err))
return fmt.Errorf("error detaching %q: %v", diskResourceID, err)
}
return nil
}