diff --git a/builder/docker/step_temp_dir.go b/builder/docker/step_temp_dir.go index c8b2fa7e6..58b264a4d 100644 --- a/builder/docker/step_temp_dir.go +++ b/builder/docker/step_temp_dir.go @@ -18,7 +18,14 @@ func (s *StepTempDir) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) ui.Say("Creating a temporary directory for sharing data...") - td, err := ioutil.TempDir("", "packer-docker") + + var err error + var tempdir string + + configTmpDir, err := packer.ConfigTmpDir() + if err == nil { + tempdir, err = ioutil.TempDir(configTmpDir, "packer-docker") + } if err != nil { err := fmt.Errorf("Error making temp dir: %s", err) state.Put("error", err) @@ -26,7 +33,7 @@ func (s *StepTempDir) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - s.tempDir = td + s.tempDir = tempdir state.Put("temp_dir", s.tempDir) return multistep.ActionContinue } diff --git a/builder/docker/step_temp_dir_test.go b/builder/docker/step_temp_dir_test.go index a7d495f65..5cf851f77 100644 --- a/builder/docker/step_temp_dir_test.go +++ b/builder/docker/step_temp_dir_test.go @@ -1,16 +1,19 @@ package docker import ( - "github.com/mitchellh/multistep" "os" + "path/filepath" "testing" + + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" ) func TestStepTempDir_impl(t *testing.T) { var _ multistep.Step = new(StepTempDir) } -func TestStepTempDir(t *testing.T) { +func testStepTempDir_impl(t *testing.T) string { state := testState(t) step := new(StepTempDir) defer step.Cleanup(state) @@ -41,4 +44,53 @@ func TestStepTempDir(t *testing.T) { if _, err := os.Stat(dir); err == nil { t.Fatalf("dir should be gone") } + + return dir +} + +func TestStepTempDir(t *testing.T) { + testStepTempDir_impl(t) +} + +func TestStepTempDir_notmpdir(t *testing.T) { + tempenv := "PACKER_TMP_DIR" + + oldenv := os.Getenv(tempenv) + defer os.Setenv(tempenv, oldenv) + os.Setenv(tempenv, "") + + dir1 := testStepTempDir_impl(t) + + cd, err := packer.ConfigDir() + if err != nil { + t.Fatalf("bad ConfigDir") + } + td := filepath.Join(cd, "tmp") + os.Setenv(tempenv, td) + + dir2 := testStepTempDir_impl(t) + + if filepath.Dir(dir1) != filepath.Dir(dir2) { + t.Fatalf("temp base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) + } +} + +func TestStepTempDir_packertmpdir(t *testing.T) { + tempenv := "PACKER_TMP_DIR" + + oldenv := os.Getenv(tempenv) + defer os.Setenv(tempenv, oldenv) + os.Setenv(tempenv, ".") + + dir1 := testStepTempDir_impl(t) + + abspath, err := filepath.Abs(".") + if err != nil { + t.Fatalf("bad absolute path") + } + dir2 := filepath.Join(abspath, "tmp") + + if filepath.Dir(dir1) != filepath.Dir(dir2) { + t.Fatalf("temp base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) + } } diff --git a/packer/config_file.go b/packer/config_file.go index f5d36e9e4..edd10edee 100644 --- a/packer/config_file.go +++ b/packer/config_file.go @@ -1,5 +1,10 @@ package packer +import ( + "os" + "path/filepath" +) + // ConfigFile returns the default path to the configuration file. On // Unix-like systems this is the ".packerconfig" file in the home directory. // On Windows, this is the "packer.config" file in the application data @@ -12,3 +17,24 @@ func ConfigFile() (string, error) { func ConfigDir() (string, error) { return configDir() } + +// ConfigTmpDir returns the configuration tmp directory for Packer +func ConfigTmpDir() (string, error) { + if tmpdir := os.Getenv("PACKER_TMP_DIR"); tmpdir != "" { + return filepath.Abs(tmpdir) + } + configdir, err := configDir() + if err != nil { + return "", err + } + td := filepath.Join(configdir, "tmp") + _, err = os.Stat(td) + if os.IsNotExist(err) { + if err = os.MkdirAll(td, 0755); err != nil { + return "", err + } + } else if err != nil { + return "", err + } + return td, nil +}