Skip to content

Commit

Permalink
Use alternate temp directories for docker
Browse files Browse the repository at this point in the history
The temporary directories will be created under the packer config
directory. Setting PACKER_TMP_DIR will override this path.
  • Loading branch information
markpeek committed Oct 20, 2015
1 parent 38c81cf commit 3704f9e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
11 changes: 9 additions & 2 deletions builder/docker/step_temp_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ 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)
ui.Error(err.Error())
return multistep.ActionHalt
}

s.tempDir = td
s.tempDir = tempdir
state.Put("temp_dir", s.tempDir)
return multistep.ActionContinue
}
Expand Down
59 changes: 57 additions & 2 deletions builder/docker/step_temp_dir_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package docker

import (
"github.com/mitchellh/multistep"
"os"
"path/filepath"
"runtime"
"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)
Expand Down Expand Up @@ -41,4 +45,55 @@ 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 := "TMPDIR"
if runtime.GOOS == "windows" {
tempenv = "TMP"
}
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))
}
}
26 changes: 26 additions & 0 deletions packer/config_file.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

0 comments on commit 3704f9e

Please sign in to comment.