2014-12-29 06:46:54 -06:00
|
|
|
package common
|
2014-11-10 12:16:02 -06:00
|
|
|
|
|
|
|
/* Taken from https://raw.githubusercontent.com/mitchellh/packer/master/builder/qemu/step_prepare_output_dir.go */
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
type StepPrepareOutputDir struct {
|
|
|
|
Force bool
|
|
|
|
Path string
|
|
|
|
}
|
2014-11-10 12:16:02 -06:00
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepAction {
|
2014-11-10 12:16:02 -06:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if _, err := os.Stat(self.Path); err == nil && self.Force {
|
2014-11-10 12:16:02 -06:00
|
|
|
ui.Say("Deleting previous output directory...")
|
2014-12-29 06:46:54 -06:00
|
|
|
os.RemoveAll(self.Path)
|
2014-11-10 12:16:02 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
if err := os.MkdirAll(self.Path, 0755); err != nil {
|
2014-11-10 12:16:02 -06:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:46:54 -06:00
|
|
|
func (self *StepPrepareOutputDir) Cleanup(state multistep.StateBag) {
|
2014-11-10 12:16:02 -06:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
|
|
|
|
if cancelled || halted {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Deleting output directory...")
|
|
|
|
for i := 0; i < 5; i++ {
|
2014-12-29 06:46:54 -06:00
|
|
|
err := os.RemoveAll(self.Path)
|
2014-11-10 12:16:02 -06:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Error removing output dir: %s", err)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|