From 38c81cf3e3ad8830a7588e8318070ebccb2872ae Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Fri, 16 Oct 2015 17:32:36 -0700 Subject: [PATCH 1/2] Move ConfigFile() and ConfigDir() from package main to packer --- checkpoint.go | 3 ++- config.go | 15 +-------------- main.go | 2 +- packer/config_file.go | 14 ++++++++++++++ config_unix.go => packer/config_file_unix.go | 2 +- .../config_file_windows.go | 2 +- 6 files changed, 20 insertions(+), 18 deletions(-) create mode 100644 packer/config_file.go rename config_unix.go => packer/config_file_unix.go (98%) rename config_windows.go => packer/config_file_windows.go (98%) diff --git a/checkpoint.go b/checkpoint.go index f8db2be28..87ef594c2 100644 --- a/checkpoint.go +++ b/checkpoint.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/go-checkpoint" "github.com/mitchellh/packer/command" + "github.com/mitchellh/packer/packer" ) func init() { @@ -25,7 +26,7 @@ func runCheckpoint(c *config) { return } - configDir, err := ConfigDir() + configDir, err := packer.ConfigDir() if err != nil { log.Printf("[ERR] Checkpoint setup error: %s", err) checkpointResult <- nil diff --git a/config.go b/config.go index efb4e7d31..62b92d9f5 100644 --- a/config.go +++ b/config.go @@ -25,19 +25,6 @@ type config struct { Provisioners map[string]string } -// 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 -// directory. -func ConfigFile() (string, error) { - return configFile() -} - -// ConfigDir returns the configuration directory for Packer. -func ConfigDir() (string, error) { - return configDir() -} - // Decodes configuration in JSON format from the given io.Reader into // the config object pointed to. func decodeConfig(r io.Reader, c *config) error { @@ -64,7 +51,7 @@ func (c *config) Discover() error { } // Next, look in the plugins directory. - dir, err := ConfigDir() + dir, err := packer.ConfigDir() if err != nil { log.Printf("[ERR] Error loading config directory: %s", err) } else { diff --git a/main.go b/main.go index a0d3190d1..69bec4a8d 100644 --- a/main.go +++ b/main.go @@ -223,7 +223,7 @@ func loadConfig() (*config, error) { configFilePath := os.Getenv("PACKER_CONFIG") if configFilePath == "" { var err error - configFilePath, err = configFile() + configFilePath, err = packer.ConfigFile() if err != nil { log.Printf("Error detecting default config file path: %s", err) diff --git a/packer/config_file.go b/packer/config_file.go new file mode 100644 index 000000000..f5d36e9e4 --- /dev/null +++ b/packer/config_file.go @@ -0,0 +1,14 @@ +package packer + +// 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 +// directory. +func ConfigFile() (string, error) { + return configFile() +} + +// ConfigDir returns the configuration directory for Packer. +func ConfigDir() (string, error) { + return configDir() +} diff --git a/config_unix.go b/packer/config_file_unix.go similarity index 98% rename from config_unix.go rename to packer/config_file_unix.go index 2c8a7a304..82260c2a2 100644 --- a/config_unix.go +++ b/packer/config_file_unix.go @@ -1,6 +1,6 @@ // +build darwin freebsd linux netbsd openbsd -package main +package packer import ( "bytes" diff --git a/config_windows.go b/packer/config_file_windows.go similarity index 98% rename from config_windows.go rename to packer/config_file_windows.go index fa3ab94b7..d0bcc1c50 100644 --- a/config_windows.go +++ b/packer/config_file_windows.go @@ -1,6 +1,6 @@ // +build windows -package main +package packer import ( "path/filepath" From bc0f438db0b77717994420076f8505d780dc3ca0 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Fri, 16 Oct 2015 17:36:29 -0700 Subject: [PATCH 2/2] 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. --- builder/docker/step_temp_dir.go | 11 +++++- builder/docker/step_temp_dir_test.go | 56 +++++++++++++++++++++++++++- packer/config_file.go | 26 +++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) 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 +}