github.com/alouche/packer@v0.3.7/builder/virtualbox/step_prepare_output_dir.go (about)

     1  package virtualbox
     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 multistep.StateBag) multistep.StepAction {
    14  	config := state.Get("config").(*config)
    15  	ui := state.Get("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.Put("error", err)
    24  		return multistep.ActionHalt
    25  	}
    26  
    27  	return multistep.ActionContinue
    28  }
    29  
    30  func (stepPrepareOutputDir) Cleanup(state multistep.StateBag) {
    31  	_, cancelled := state.GetOk(multistep.StateCancelled)
    32  	_, halted := state.GetOk(multistep.StateHalted)
    33  
    34  	if cancelled || halted {
    35  		config := state.Get("config").(*config)
    36  		ui := state.Get("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  }