github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/builder/docker/step_temp_dir.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  	"io/ioutil"
     8  	"os"
     9  )
    10  
    11  // StepTempDir creates a temporary directory that we use in order to
    12  // share data with the docker container over the communicator.
    13  type StepTempDir struct {
    14  	tempDir string
    15  }
    16  
    17  func (s *StepTempDir) Run(state multistep.StateBag) multistep.StepAction {
    18  	ui := state.Get("ui").(packer.Ui)
    19  
    20  	ui.Say("Creating a temporary directory for sharing data...")
    21  
    22  	var err error
    23  	var tempdir string
    24  
    25  	configTmpDir, err := packer.ConfigTmpDir()
    26  	if err == nil {
    27  		tempdir, err = ioutil.TempDir(configTmpDir, "packer-docker")
    28  	}
    29  	if err != nil {
    30  		err := fmt.Errorf("Error making temp dir: %s", err)
    31  		state.Put("error", err)
    32  		ui.Error(err.Error())
    33  		return multistep.ActionHalt
    34  	}
    35  
    36  	s.tempDir = tempdir
    37  	state.Put("temp_dir", s.tempDir)
    38  	return multistep.ActionContinue
    39  }
    40  
    41  func (s *StepTempDir) Cleanup(state multistep.StateBag) {
    42  	if s.tempDir != "" {
    43  		os.RemoveAll(s.tempDir)
    44  	}
    45  }