diff --git a/builder/docker/step_temp_dir.go b/builder/docker/step_temp_dir.go index a5b1d6ade..622a2150c 100644 --- a/builder/docker/step_temp_dir.go +++ b/builder/docker/step_temp_dir.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) // StepTempDir creates a temporary directory that we use in order to @@ -24,10 +25,8 @@ func (s *StepTempDir) Run(_ context.Context, state multistep.StateBag) multistep var err error var tempdir string - configTmpDir, err := packer.ConfigTmpDir() - if err == nil { - tempdir, err = ioutil.TempDir(configTmpDir, "packer-docker") - } + prefix, _ := configfile.ConfigTmpDir() + tempdir, err = ioutil.TempDir(prefix, "docker") if err != nil { err := fmt.Errorf("Error making temp dir: %s", err) state.Put("error", err) diff --git a/builder/docker/step_temp_dir_test.go b/builder/docker/step_temp_dir_test.go index e36d4fcff..b15154353 100644 --- a/builder/docker/step_temp_dir_test.go +++ b/builder/docker/step_temp_dir_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/hashicorp/packer/helper/multistep" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func TestStepTempDir_impl(t *testing.T) { @@ -54,20 +54,18 @@ func TestStepTempDir(t *testing.T) { } func TestStepTempDir_notmpdir(t *testing.T) { - tempenv := "PACKER_TMP_DIR" - - oldenv := os.Getenv(tempenv) - defer os.Setenv(tempenv, oldenv) - os.Setenv(tempenv, "") + oldenv := os.Getenv(configfile.EnvPackerTmpDir) + defer os.Setenv(configfile.EnvPackerTmpDir, oldenv) + os.Setenv(configfile.EnvPackerTmpDir, "") dir1 := testStepTempDir_impl(t) - cd, err := packer.ConfigDir() + cd, err := configfile.ConfigDir() if err != nil { t.Fatalf("bad ConfigDir") } td := filepath.Join(cd, "tmp") - os.Setenv(tempenv, td) + os.Setenv(configfile.EnvPackerTmpDir, td) dir2 := testStepTempDir_impl(t) @@ -77,11 +75,9 @@ func TestStepTempDir_notmpdir(t *testing.T) { } func TestStepTempDir_packertmpdir(t *testing.T) { - tempenv := "PACKER_TMP_DIR" - - oldenv := os.Getenv(tempenv) - defer os.Setenv(tempenv, oldenv) - os.Setenv(tempenv, ".") + oldenv := os.Getenv(configfile.EnvPackerTmpDir) + defer os.Setenv(configfile.EnvPackerTmpDir, oldenv) + os.Setenv(configfile.EnvPackerTmpDir, ".") dir1 := testStepTempDir_impl(t) diff --git a/builder/hyperv/common/step_create_build_dir.go b/builder/hyperv/common/step_create_build_dir.go index 405a3407c..53c04b492 100644 --- a/builder/hyperv/common/step_create_build_dir.go +++ b/builder/hyperv/common/step_create_build_dir.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) type StepCreateBuildDir struct { @@ -30,11 +31,11 @@ func (s *StepCreateBuildDir) Run(_ context.Context, state multistep.StateBag) mu ui.Say("Creating build directory...") if s.TempPath == "" { - s.TempPath = os.TempDir() + s.TempPath, _ = configfile.ConfigTmpDir() } var err error - s.buildDir, err = ioutil.TempDir(s.TempPath, "packerhv") + s.buildDir, err = ioutil.TempDir(s.TempPath, "hyperv") if err != nil { err = fmt.Errorf("Error creating build directory: %s", err) state.Put("error", err) diff --git a/builder/hyperv/common/step_create_build_dir_test.go b/builder/hyperv/common/step_create_build_dir_test.go index 3f0fe57cc..5b12b16b6 100644 --- a/builder/hyperv/common/step_create_build_dir_test.go +++ b/builder/hyperv/common/step_create_build_dir_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer/configfile" ) func TestStepCreateBuildDir_imp(t *testing.T) { @@ -37,8 +38,9 @@ func TestStepCreateBuildDir_Defaults(t *testing.T) { // On windows convert everything to forward slash separated paths // This prevents the regexp interpreting backslashes as escape sequences stateBuildDir := filepath.ToSlash(v.(string)) + configTmpDir, _ := configfile.ConfigTmpDir() expectedBuildDirRe := regexp.MustCompile( - filepath.ToSlash(filepath.Join(os.TempDir(), "packerhv") + `[[:digit:]]{9}$`)) + filepath.ToSlash(filepath.Join(configTmpDir, "hyperv") + `[[:digit:]]{9}$`)) match := expectedBuildDirRe.MatchString(stateBuildDir) if !match { t.Fatalf("Got path that doesn't match expected format in 'build_dir': %s", stateBuildDir) @@ -79,7 +81,7 @@ func TestStepCreateBuildDir_UserDefinedTempPath(t *testing.T) { // This prevents the regexp interpreting backslashes as escape sequences stateBuildDir := filepath.ToSlash(v.(string)) expectedBuildDirRe := regexp.MustCompile( - filepath.ToSlash(filepath.Join(step.TempPath, "packerhv") + `[[:digit:]]{9}$`)) + filepath.ToSlash(filepath.Join(step.TempPath, "hyperv") + `[[:digit:]]{9}$`)) match := expectedBuildDirRe.MatchString(stateBuildDir) if !match { t.Fatalf("Got path that doesn't match expected format in 'build_dir': %s", stateBuildDir) diff --git a/builder/hyperv/common/step_mount_floppydrive.go b/builder/hyperv/common/step_mount_floppydrive.go index 1ae03d1db..483938c9b 100644 --- a/builder/hyperv/common/step_mount_floppydrive.go +++ b/builder/hyperv/common/step_mount_floppydrive.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) const ( @@ -93,7 +94,8 @@ func (s *StepMountFloppydrive) Cleanup(state multistep.StateBag) { } func (s *StepMountFloppydrive) copyFloppy(path string) (string, error) { - tempdir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tempdir, err := ioutil.TempDir(prefix, "hyperv") if err != nil { return "", err } diff --git a/builder/virtualbox/common/step_attach_floppy.go b/builder/virtualbox/common/step_attach_floppy.go index 6c73aa4b4..7cf1534a7 100644 --- a/builder/virtualbox/common/step_attach_floppy.go +++ b/builder/virtualbox/common/step_attach_floppy.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) // This step attaches the ISO to the virtual machine. @@ -106,7 +107,8 @@ func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) { } func (s *StepAttachFloppy) copyFloppy(path string) (string, error) { - tempdir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + tempdir, err := ioutil.TempDir(prefix, "virtualbox") if err != nil { return "", err } diff --git a/builder/vmware/iso/step_create_vmx.go b/builder/vmware/iso/step_create_vmx.go index 432d543fe..529af33a7 100644 --- a/builder/vmware/iso/step_create_vmx.go +++ b/builder/vmware/iso/step_create_vmx.go @@ -12,6 +12,7 @@ import ( vmwcommon "github.com/hashicorp/packer/builder/vmware/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" ) @@ -614,7 +615,8 @@ func (s *stepCreateVMX) Run(_ context.Context, state multistep.StateBag) multist if config.RemoteType != "" { // For remote builds, we just put the VMX in a temporary // directory since it just gets uploaded anyways. - vmxDir, err = ioutil.TempDir("", "packer-vmx") + prefix, _ := configfile.ConfigTmpDir() + vmxDir, err = ioutil.TempDir(prefix, "vmware") if err != nil { err := fmt.Errorf("Error preparing VMX template: %s", err) state.Put("error", err) diff --git a/checkpoint.go b/checkpoint.go index bbefeef5e..570a18f43 100644 --- a/checkpoint.go +++ b/checkpoint.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/go-checkpoint" "github.com/hashicorp/packer/command" - "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" packerVersion "github.com/hashicorp/packer/version" ) @@ -27,7 +27,7 @@ func runCheckpoint(c *config) { return } - configDir, err := packer.ConfigDir() + configDir, err := configfile.ConfigDir() if err != nil { log.Printf("[ERR] Checkpoint setup error: %s", err) checkpointResult <- nil diff --git a/common/powershell/powershell.go b/common/powershell/powershell.go index 4c3dd80d1..5765aec3c 100644 --- a/common/powershell/powershell.go +++ b/common/powershell/powershell.go @@ -10,6 +10,8 @@ import ( "os/exec" "strconv" "strings" + + "github.com/hashicorp/packer/packer/configfile" ) const ( @@ -121,7 +123,8 @@ func (ps *PowerShellCmd) getPowerShellPath() (string, error) { } func saveScript(fileContents string) (string, error) { - file, err := ioutil.TempFile(os.TempDir(), "ps") + prefix, _ := configfile.ConfigTmpDir() + file, err := ioutil.TempFile(prefix, "powershell") if err != nil { return "", err } diff --git a/config.go b/config.go index 409b8f12d..c5866e2a1 100644 --- a/config.go +++ b/config.go @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/packer/command" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/packer/plugin" "github.com/kardianos/osext" ) @@ -64,7 +65,7 @@ func (c *config) Discover() error { } // Next, look in the plugins directory. - dir, err := packer.ConfigDir() + dir, err := configfile.ConfigDir() if err != nil { log.Printf("[ERR] Error loading config directory: %s", err) } else { diff --git a/helper/common/shared_state.go b/helper/common/shared_state.go index 88ddf0aaa..c1316e9f4 100644 --- a/helper/common/shared_state.go +++ b/helper/common/shared_state.go @@ -5,16 +5,29 @@ import ( "io/ioutil" "os" "path/filepath" + + "github.com/hashicorp/packer/packer/configfile" ) -var sharedStateDir := ioutil.TempDir(packer.ConfigTmpDir(), "state") +var sharedStateDir string // Used to set variables which we need to access later in the build, where // state bag and config information won't work func sharedStateFilename(suffix string, buildName string) string { - uuid := os.Getenv("PACKER_RUN_UUID") + var uuid string + + if sharedStateDir == "" { + prefix, _ := configfile.ConfigTmpDir() + sharedStateDir, err := ioutil.TempDir(prefix, "state") + if err != nil { + return "" + } + defer os.RemoveAll(sharedStateDir) + } + + uuid = os.Getenv("PACKER_RUN_UUID") if uuid == "" { - uuid="none" + uuid = "none" } return filepath.Join(sharedStateDir, fmt.Sprintf("%s-%s-%s", uuid, suffix, buildName)) } diff --git a/main.go b/main.go index 16ec61b41..0ad7fe94e 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "github.com/hashicorp/go-uuid" "github.com/hashicorp/packer/command" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/packer/plugin" "github.com/hashicorp/packer/version" "github.com/mitchellh/cli" @@ -294,7 +295,7 @@ func loadConfig() (*config, error) { log.Printf("'PACKER_CONFIG' set, loading config from environment.") } else { var err error - configFilePath, err = packer.ConfigFile() + configFilePath, err = configfile.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 deleted file mode 100644 index 2b68074cf..000000000 --- a/packer/config_file.go +++ /dev/null @@ -1,41 +0,0 @@ -package packer - -import ( - "io/ioutil" - "log" - "os" -) - -// 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() -} - -// ConfigTmpDir returns the configuration tmp directory for Packer -func ConfigTmpDir() (string, error) { - var tmpdir, td, cd string - var err error - - cd, _ = ConfigDir() - for _, tmpdir = range []string{os.Getenv("PACKER_TMP_DIR"), os.TempDir(), cd} { - if tmpdir != "" { - break - } - } - - if td, err = ioutil.TempDir(tmpdir, "packer"); err != nil { - log.Fatal(err) - } - - defer os.RemoveAll(td) - - return td, nil -} diff --git a/packer/configfile/configfile.go b/packer/configfile/configfile.go new file mode 100644 index 000000000..33c641602 --- /dev/null +++ b/packer/configfile/configfile.go @@ -0,0 +1,47 @@ +package configfile + +import ( + "os" + "path/filepath" +) + +const EnvPackerTmpDir = "PACKER_TMP_DIR" + +// 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() +} + +// ConfigTmpDir returns a "deterministic" (based on environment or Packer config) +// path intended as the root of subsequent temporary items, to minimize scatter. +// +// The caller must ensure safe tempfile practice via ioutil.TempDir() and friends. +func ConfigTmpDir() (string, error) { + var tmpdir, td string + var err error + + if tmpdir = os.Getenv(EnvPackerTmpDir); tmpdir != "" { + td, err = filepath.Abs(tmpdir) + } else if tmpdir, err = configDir(); err == nil { + td = filepath.Join(tmpdir, "tmp") + } else if tmpdir = os.TempDir(); tmpdir != "" { + td = filepath.Join(tmpdir, "packer") + } + + if _, err = os.Stat(td); os.IsNotExist(err) { + err = os.MkdirAll(td, 0700) + } + if err != nil { + return "", err + } + + return td, nil +} diff --git a/packer/configfile/configfile_test.go b/packer/configfile/configfile_test.go new file mode 100644 index 000000000..137627040 --- /dev/null +++ b/packer/configfile/configfile_test.go @@ -0,0 +1,65 @@ +package configfile + +import ( + "os" + "path/filepath" + "io/ioutil" + "fmt" + "testing" +) + +func testConfigTmpDir_impl(t *testing.T) string { + var dir string + + prefix, _ := ConfigTmpDir() + if dir, err := ioutil.TempDir(prefix, ""); err == nil { + defer os.RemoveAll(dir) + } else { + _ := fmt.Errorf("Error making directory: %s", err) + } + + return dir +} + +func TestConfigTmpDir(t *testing.T) { + testConfigTmpDir_impl(t) +} + +func TestConfigTmpDir_noenv_PackerTmpDir(t *testing.T) { + oldenv := os.Getenv(EnvPackerTmpDir) + defer os.Setenv(EnvPackerTmpDir, oldenv) + os.Setenv(EnvPackerTmpDir, "") + + dir1 := testConfigTmpDir_impl(t) + + cd, err := ConfigDir() + if err != nil { + t.Fatalf("bad ConfigDir") + } + td := filepath.Join(cd, "tmp") + os.Setenv(EnvPackerTmpDir, td) + + dir2 := testConfigTmpDir_impl(t) + + if filepath.Dir(dir1) != filepath.Dir(dir2) { + t.Fatalf("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) + } +} + +func TestConfigTmpDir_PackerTmpDir(t *testing.T) { + oldenv := os.Getenv(EnvPackerTmpDir) + defer os.Setenv(EnvPackerTmpDir, oldenv) + os.Setenv(EnvPackerTmpDir, ".") + + dir1 := testConfigTmpDir_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("base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2)) + } +} diff --git a/packer/config_file_unix.go b/packer/configfile/unix.go similarity index 91% rename from packer/config_file_unix.go rename to packer/configfile/unix.go index 512ac42ed..fd3e95147 100644 --- a/packer/config_file_unix.go +++ b/packer/configfile/unix.go @@ -1,11 +1,10 @@ // +build darwin freebsd linux netbsd openbsd solaris -package packer +package configfile import ( "bytes" "errors" - "log" "os" "os/exec" "path/filepath" @@ -33,7 +32,6 @@ func configDir() (string, error) { func homeDir() (string, error) { // First prefer the HOME environmental variable if home := os.Getenv("HOME"); home != "" { - log.Printf("Detected home directory from env var: %s", home) return home, nil } diff --git a/packer/config_file_windows.go b/packer/configfile/windows.go similarity index 97% rename from packer/config_file_windows.go rename to packer/configfile/windows.go index d0bcc1c50..37370c1f6 100644 --- a/packer/config_file_windows.go +++ b/packer/configfile/windows.go @@ -1,6 +1,6 @@ // +build windows -package packer +package configfile import ( "path/filepath" diff --git a/packer/telemetry.go b/packer/telemetry.go index 0f493a83a..9f69ff9a9 100644 --- a/packer/telemetry.go +++ b/packer/telemetry.go @@ -9,6 +9,7 @@ import ( "time" checkpoint "github.com/hashicorp/go-checkpoint" + "github.com/hashicorp/packer/packer/configfile" packerVersion "github.com/hashicorp/packer/version" ) @@ -35,7 +36,7 @@ func NewCheckpointReporter(disableSignature bool) *CheckpointTelemetry { return nil } - configDir, err := ConfigDir() + configDir, err := configfile.ConfigDir() if err != nil { log.Printf("[WARN] (telemetry) setup error: %s", err) return nil diff --git a/post-processor/vagrant/post-processor.go b/post-processor/vagrant/post-processor.go index 5ea07f3a3..39cab9681 100644 --- a/post-processor/vagrant/post-processor.go +++ b/post-processor/vagrant/post-processor.go @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" "github.com/mitchellh/mapstructure" ) @@ -95,7 +96,8 @@ func (p *PostProcessor) PostProcessProvider(name string, provider Provider, ui p } // Create a temporary directory for us to build the contents of the box in - dir, err := ioutil.TempDir("", "packer") + prefix, _ := configfile.ConfigTmpDir() + dir, err := ioutil.TempDir(prefix, "vagrant") if err != nil { return nil, false, err } diff --git a/provisioner/ansible/scp.go b/provisioner/ansible/scp.go index 0d951c728..037273b9f 100644 --- a/provisioner/ansible/scp.go +++ b/provisioner/ansible/scp.go @@ -14,6 +14,7 @@ import ( "time" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) const ( @@ -43,7 +44,8 @@ func scpUploadSession(opts []byte, rest string, in io.Reader, out io.Writer, com return errors.New("no scp target specified") } - d, err := ioutil.TempDir("", "packer-ansible-upload") + prefix, _ := configfile.ConfigTmpDir() + d, err := ioutil.TempDir(prefix, "ansible-upload") if err != nil { fmt.Fprintf(out, scpEmptyError) return err @@ -68,7 +70,8 @@ func scpDownloadSession(opts []byte, rest string, in io.Reader, out io.Writer, c return errors.New("no scp source specified") } - d, err := ioutil.TempDir("", "packer-ansible-download") + prefix, _ := configfile.ConfigTmpDir() + d, err := ioutil.TempDir(prefix, "ansible-download") if err != nil { fmt.Fprintf(out, scpEmptyError) return err diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index c5724021b..f6aade45e 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -20,6 +20,7 @@ import ( commonhelper "github.com/hashicorp/packer/helper/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" ) @@ -238,7 +239,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { // Takes the inline scripts, concatenates them into a temporary file and // returns a string containing the location of said file. func extractScript(p *Provisioner) (string, error) { - temp, err := ioutil.TempFile(os.TempDir(), "packer-powershell-provisioner") + prefix, _ := configfile.ConfigTmpDir() + temp, err := ioutil.TempFile(prefix, "powershell-provisioner") if err != nil { return "", err } diff --git a/provisioner/windows-shell/provisioner.go b/provisioner/windows-shell/provisioner.go index f9b105121..bd02b98de 100644 --- a/provisioner/windows-shell/provisioner.go +++ b/provisioner/windows-shell/provisioner.go @@ -16,9 +16,11 @@ import ( "github.com/hashicorp/packer/common" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" "github.com/hashicorp/packer/template/interpolate" ) +//FIXME query remote host or use %SYSTEMROOT%, %TEMP% and more creative filename const DefaultRemotePath = "c:/Windows/Temp/script.bat" var retryableSleep = 2 * time.Second @@ -157,7 +159,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { // into a temporary file and returns a string containing the location // of said file. func extractScript(p *Provisioner) (string, error) { - temp, err := ioutil.TempFile(os.TempDir(), "packer-windows-shell-provisioner") + prefix, _ := configfile.ConfigTmpDir() + temp, err := ioutil.TempFile(prefix, "windows-shell-provisioner") if err != nil { log.Printf("Unable to create temporary file for inline scripts: %s", err) return "", err diff --git a/provisioner/windows-shell/provisioner_test.go b/provisioner/windows-shell/provisioner_test.go index 5f4149478..21595fa8d 100644 --- a/provisioner/windows-shell/provisioner_test.go +++ b/provisioner/windows-shell/provisioner_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/packer/configfile" ) func testConfig() map[string]interface{} { @@ -30,8 +31,9 @@ func TestProvisionerPrepare_extractScript(t *testing.T) { t.Fatalf("Should not be error: %s", err) } log.Printf("File: %s", file) - if strings.Index(file, os.TempDir()) != 0 { - t.Fatalf("Temp file should reside in %s. File location: %s", os.TempDir(), file) + tmpdir, _ := configfile.ConfigTmpDir() + if strings.Index(file, tmpdir) != 0 { + t.Fatalf("Temp file should reside in %s. File location: %s", tmpdir, file) } // File contents should contain 2 lines concatenated by newlines: foo\nbar diff --git a/template/parse.go b/template/parse.go index e0d06a2b4..0b5880d90 100644 --- a/template/parse.go +++ b/template/parse.go @@ -13,6 +13,7 @@ import ( "strings" "github.com/hashicorp/go-multierror" + "github.com/hashicorp/packer/packer/configfile" "github.com/mitchellh/mapstructure" ) @@ -328,7 +329,8 @@ func ParseFile(path string) (*Template, error) { var err error if path == "-" { // Create a temp file for stdin in case of errors - f, err = ioutil.TempFile(os.TempDir(), "packer") + prefix, _ := configfile.ConfigTmpDir() + f, err = ioutil.TempFile(prefix, "parse") if err != nil { return nil, err }