2013-06-04 18:00:58 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/multistep"
|
2013-06-27 22:23:40 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-07-31 18:16:39 -04:00
|
|
|
"log"
|
2013-06-04 18:00:58 -04:00
|
|
|
"os"
|
2013-07-31 18:16:39 -04:00
|
|
|
"time"
|
2013-06-04 18:00:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepPrepareOutputDir struct{}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func (stepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-12 22:23:00 -04:00
|
|
|
|
|
|
|
if _, err := os.Stat(config.OutputDir); err == nil && config.PackerForce {
|
|
|
|
ui.Say("Deleting previous output directory...")
|
|
|
|
os.RemoveAll(config.OutputDir)
|
2013-07-13 01:47:08 -04:00
|
|
|
}
|
2013-06-04 18:00:58 -04:00
|
|
|
|
|
|
|
if err := os.MkdirAll(config.OutputDir, 0755); err != nil {
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-06-04 18:00:58 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func (stepPrepareOutputDir) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2013-06-27 22:23:40 -04:00
|
|
|
|
|
|
|
if cancelled || halted {
|
2013-08-31 15:50:25 -04:00
|
|
|
config := state.Get("config").(*config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-27 22:23:40 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting output directory...")
|
2013-07-31 18:16:39 -04:00
|
|
|
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)
|
|
|
|
}
|
2013-06-27 22:23:40 -04:00
|
|
|
}
|
|
|
|
}
|