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"
|
|
|
|
"time"
|
2013-06-04 18:00:58 -04:00
|
|
|
)
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
type stepPrepareOutputDir struct {
|
|
|
|
dir OutputDir
|
|
|
|
}
|
2013-06-04 18:00:58 -04:00
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
func (s *stepPrepareOutputDir) Run(state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:50:25 -04:00
|
|
|
config := state.Get("config").(*config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-12 22:23:00 -04:00
|
|
|
|
2013-11-07 15:00:27 -05:00
|
|
|
dir := s.outputDir(state)
|
2013-11-08 00:18:25 -05:00
|
|
|
dir.SetOutputDir(config.OutputDir)
|
|
|
|
|
|
|
|
exists, err := dir.DirExists()
|
2013-11-07 15:00:27 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-04 18:00:58 -04:00
|
|
|
|
2013-11-07 15:00:27 -05:00
|
|
|
if exists && config.PackerForce {
|
|
|
|
ui.Say("Deleting previous output directory...")
|
2013-11-08 00:18:25 -05:00
|
|
|
dir.RemoveAll()
|
2013-11-07 15:00:27 -05:00
|
|
|
}
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
if err := dir.MkdirAll(); err != nil {
|
2013-11-07 15:00:27 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2013-06-04 18:00:58 -04:00
|
|
|
}
|
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
s.dir = dir
|
|
|
|
|
2013-06-04 18:00:58 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
func (s *stepPrepareOutputDir) Cleanup(state multistep.StateBag) {
|
2013-08-31 15:50:25 -04:00
|
|
|
_, 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
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-27 22:23:40 -04:00
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
if s.dir != nil {
|
|
|
|
ui.Say("Deleting output directory...")
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
err := s.dir.RemoveAll()
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2013-11-07 15:00:27 -05:00
|
|
|
|
2013-11-08 00:18:25 -05:00
|
|
|
log.Printf("Error removing output dir: %s", err)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
2013-07-31 18:16:39 -04:00
|
|
|
}
|
2013-06-27 22:23:40 -04:00
|
|
|
}
|
|
|
|
}
|
2013-09-19 20:07:04 -04:00
|
|
|
|
2013-11-07 15:00:27 -05:00
|
|
|
func (s *stepPrepareOutputDir) outputDir(state multistep.StateBag) (dir OutputDir) {
|
2013-09-19 20:07:04 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
|
2013-11-07 15:00:27 -05:00
|
|
|
switch d := driver.(type) {
|
|
|
|
case OutputDir:
|
2013-11-08 14:48:04 -05:00
|
|
|
log.Printf("Using driver as the OutputDir implementation")
|
2013-11-07 15:00:27 -05:00
|
|
|
dir = d
|
|
|
|
default:
|
2013-11-08 14:48:04 -05:00
|
|
|
log.Printf("Using localOutputDir implementation")
|
2013-11-07 15:00:27 -05:00
|
|
|
dir = new(localOutputDir)
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|
|
|
|
|
2013-11-07 15:00:27 -05:00
|
|
|
return
|
2013-09-19 20:07:04 -04:00
|
|
|
}
|