github.com/mitchellh/packer@v1.3.2/builder/docker/step_temp_dir.go (about)

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