packer-cn/builder/lxc/step_prepare_output_dir.go

53 lines
1.2 KiB
Go
Raw Normal View History

2015-12-22 09:56:33 -05:00
package lxc
import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
2015-12-22 09:56:33 -05:00
"log"
"os"
"time"
2018-01-22 20:21:10 -05:00
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
2015-12-22 09:56:33 -05:00
)
type stepPrepareOutputDir struct{}
func (stepPrepareOutputDir) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2015-12-22 09:56:33 -05:00
config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui)
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 {
config := state.Get("config").(*Config)
ui := state.Get("ui").(packer.Ui)
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)
}
}
}