2013-06-04 18:00:58 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
2013-09-19 20:07:04 -04:00
|
|
|
"fmt"
|
2013-06-04 18:00:58 -04:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
type OutputDir interface {
|
|
|
|
FileExists(path string) bool
|
|
|
|
MkdirAll(path string) error
|
|
|
|
RemoveAll(path string) error
|
|
|
|
DirType() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type localOutputDir struct{}
|
|
|
|
|
|
|
|
func (localOutputDir) FileExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (localOutputDir) MkdirAll(path string) error {
|
|
|
|
return os.MkdirAll(path, 0755)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (localOutputDir) RemoveAll(path string) error {
|
|
|
|
return os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (localOutputDir) DirType() string {
|
|
|
|
return "local"
|
|
|
|
}
|
|
|
|
|
2013-06-04 18:00:58 -04:00
|
|
|
type stepPrepareOutputDir struct{}
|
|
|
|
|
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-09-19 20:07:04 -04:00
|
|
|
for _, dir := range s.outputDirs(state) {
|
|
|
|
if dir.FileExists(config.OutputDir) && config.PackerForce {
|
|
|
|
ui.Say(fmt.Sprintf("Deleting previous %s output directory...", dir.DirType()))
|
|
|
|
dir.RemoveAll(config.OutputDir)
|
|
|
|
}
|
2013-06-04 18:00:58 -04:00
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
if err := dir.MkdirAll(config.OutputDir); err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
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
|
|
|
config := state.Get("config").(*config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-27 22:23:40 -04:00
|
|
|
|
2013-09-19 20:07:04 -04:00
|
|
|
for _, dir := range s.outputDirs(state) {
|
|
|
|
ui.Say(fmt.Sprintf("Deleting %s output directory...", dir.DirType()))
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
err := dir.RemoveAll(config.OutputDir)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2013-07-31 18:16:39 -04:00
|
|
|
|
2013-09-19 20:07:04 -04: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
|
|
|
|
|
|
|
func (s *stepPrepareOutputDir) outputDirs(state multistep.StateBag) []OutputDir {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
dirs := []OutputDir{
|
|
|
|
localOutputDir{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if dir, ok := driver.(OutputDir); ok {
|
|
|
|
dirs = append(dirs, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dirs
|
|
|
|
}
|