2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-09-02 23:23:52 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepPrepareOutputDir struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (stepPrepareOutputDir) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-05-27 16:39:43 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
if _, err := os.Stat(config.OutputDir); err == nil && config.PackerForce {
|
|
|
|
ui.Say("Deleting previous output directory...")
|
|
|
|
os.RemoveAll(config.OutputDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(config.OutputDir, 0755); err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stepPrepareOutputDir) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
|
|
|
|
if cancelled || halted {
|
2015-05-27 16:39:43 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting output directory...")
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
err := os.RemoveAll(config.OutputDir)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Error removing output dir: %s", err)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|