Use alternate temp directories for docker

The temporary directories will be created under the packer config
directory. Setting PACKER_TMP_DIR will override this path.
This commit is contained in:
Mark Peek 2015-10-16 17:36:29 -07:00
parent 38c81cf3e3
commit bc0f438db0
3 changed files with 89 additions and 4 deletions

View file

@ -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
}

View file

@ -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))
}
}

View file

@ -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
}