github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/vmware/step_prepare_output_dir.go (about)

     1  package vmware
     2  
     3  import (
     4  	"github.com/mitchellh/multistep"
     5  	"github.com/mitchellh/packer/packer"
     6  	"log"
     7  	"os"
     8  	"time"
     9  )
    10  
    11  type stepPrepareOutputDir struct{}
    12  
    13  func (stepPrepareOutputDir) Run(state map[string]interface{}) multistep.StepAction {
    14  	config := state["config"].(*config)
    15  	ui := state["ui"].(packer.Ui)
    16  
    17  	if _, err := os.Stat(config.OutputDir); err == nil && config.PackerForce {
    18  		ui.Say("Deleting previous output directory...")
    19  		os.RemoveAll(config.OutputDir)
    20  	}
    21  
    22  	if err := os.MkdirAll(config.OutputDir, 0755); err != nil {
    23  		state["error"] = err
    24  		return multistep.ActionHalt
    25  	}
    26  
    27  	return multistep.ActionContinue
    28  }
    29  
    30  func (stepPrepareOutputDir) Cleanup(state map[string]interface{}) {
    31  	_, cancelled := state[multistep.StateCancelled]
    32  	_, halted := state[multistep.StateHalted]
    33  
    34  	if cancelled || halted {
    35  		config := state["config"].(*config)
    36  		ui := state["ui"].(packer.Ui)
    37  
    38  		ui.Say("Deleting output directory...")
    39  		for i := 0; i < 5; i++ {
    40  			err := os.RemoveAll(config.OutputDir)
    41  			if err == nil {
    42  				break
    43  			}
    44  
    45  			log.Printf("Error removing output dir: %s", err)
    46  			time.Sleep(2 * time.Second)
    47  		}
    48  	}
    49  }