Add tmp package that offers Dir & File funcs

this regroups the calls of tmp func calls into one package.
the tmp pkg allows to store tmp files under a common directory for easier managment

Squashed commit of the following:
commit 74b674c015
Merge: fc94b5714 66001525d
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Fri Nov 2 02:02:38 2018 -0400

    Merge branch 'remove_TMPDIR_docs' of github.com:tb3088/packer into remove_TMPDIR_docs

commit fc94b57142
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Fri Nov 2 01:42:38 2018 -0400

    update tests to honor configured temporary directory

commit 5a562359f4
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Thu Nov 1 18:52:38 2018 -0400

    sync straggler to refactor. implement tests at packer/configfile instead of buried in docker.

commit 9d5a4a0935
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Thu Nov 1 15:39:46 2018 -0400

    insert URL to GoLang API for os.TempDir()

commit fbae4c2e05
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Thu Nov 1 07:41:29 2018 -0400

    revise documentation to match code

commit 3590fae8bd
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Thu Nov 1 07:21:59 2018 -0400

    refacter config_file. replace all hard-coded os.TempDir with wrapper

commit d5c5306a97
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Wed Oct 31 20:11:36 2018 -0400

    close massive file overwrite hole with TempDir

commit 0a72297da8
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Wed Oct 31 01:06:00 2018 -0400

    adjust var declaration scope

commit 20f68228b6
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Wed Oct 31 00:54:35 2018 -0400

    use mktemp() equivalent to create temporary directory

commit c73ebe3d83
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Tue Oct 30 20:40:58 2018 -0400

    remove extraneous variable declaration, fix FOR loop

commit 63549b8bd7
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Tue Oct 30 19:30:44 2018 -0400

    match styistic convension with rest of docs

commit 9761010749
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Tue Oct 30 19:08:39 2018 -0400

    revert dangling config file change

commit 95159afbc0
Author: Matthew Patton <matthew.patton@itgfirm.com>
Date:   Fri Dec 29 23:53:43 2017 -0500

    replace invalid TMPDIR variable wth PACKER_TMP_DIR.
    update ConfigTmpDir() to try common temporary paths first and
    only write to configDir() as a last resort.

commit 66001525d7
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Fri Nov 2 01:42:38 2018 -0400

    update tests to honor configured temporary directory

commit e9b6adefea
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Thu Nov 1 18:52:38 2018 -0400

    sync straggler to refactor. implement tests at packer/configfile instead of buried in docker.

commit 852113ed07
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Thu Nov 1 15:39:46 2018 -0400

    insert URL to GoLang API for os.TempDir()

commit 54add38d1d
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Thu Nov 1 07:41:29 2018 -0400

    revise documentation to match code

commit 6b5b8f6d4e
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Thu Nov 1 07:21:59 2018 -0400

    refacter config_file. replace all hard-coded os.TempDir with wrapper

commit c22092c601
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Wed Oct 31 20:11:36 2018 -0400

    close massive file overwrite hole with TempDir

commit 7a73045091
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Wed Oct 31 01:06:00 2018 -0400

    adjust var declaration scope

commit 0f2933adb6
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Wed Oct 31 00:54:35 2018 -0400

    use mktemp() equivalent to create temporary directory

commit d74839ede0
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Tue Oct 30 20:40:58 2018 -0400

    remove extraneous variable declaration, fix FOR loop

commit eb65416619
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Tue Oct 30 19:30:44 2018 -0400

    match styistic convension with rest of docs

commit acaa2b31ed
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Tue Oct 30 19:08:39 2018 -0400

    revert dangling config file change

commit e573fde668
Author: Matthew Patton <matthew.patton@itgfirm.com>
Date:   Fri Dec 29 23:53:43 2017 -0500

    replace invalid TMPDIR variable wth PACKER_TMP_DIR.
    update ConfigTmpDir() to try common temporary paths first and
    only write to configDir() as a last resort.

commit 39a9874afc
Merge: 8a413cfe8 3315812c2
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Tue Oct 30 18:19:26 2018 -0400

    Merge branch 'master' of https://github.com/hashicorp/packer into prestine

commit 8a413cfe83
Merge: e07491de5 4e14710a6
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Mon Oct 1 20:18:10 2018 -0400

    Merge remote-tracking branch 'upstream/master' into prestine

commit e07491de59
Merge: 42610a35d a1fa35dff
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Tue Aug 21 13:26:19 2018 -0400

    Merge remote-tracking branch 'upstream/master' into prestine

commit 42610a35d5
Merge: 5298142da 0d63cf7bc
Author: Matthew Patton <matthew.patton@itgfirm.com>
Date:   Tue Jun 19 22:45:05 2018 -0400

    Merge remote-tracking branch 'upstream/master' into prestine

commit 5298142da6
Merge: 7bb110bc7 9d9736552
Author: Matthew Patton <mpatton@enquizit.com>
Date:   Mon Jun 11 15:10:09 2018 -0400

    Merge remote-tracking branch 'upstream/master' into prestine

commit 7bb110bc74
Merge: a61c869ca 6189d66e7
Author: Matthew Patton <matthew.patton@itgfirm.com>
Date:   Wed May 9 23:41:22 2018 -0400

    Merge remote-tracking branch 'upstream/master' into prestine

commit a61c869ca7
Merge: 098101dd9 4be5f072c
Author: Matthew Patton <matthew.patton@itgfirm.com>
Date:   Fri May 4 10:13:04 2018 -0400

    Merge remote-tracking branch 'upstream/master' into prestine

commit 098101dd99
Merge: fefaf0fa6 554b2b4a5
Author: Matthew Patton <matthew.patton@itgfirm.com>
Date:   Tue May 1 20:52:30 2018 -0400

    Merge branch 'EOL-handling' into prestine

Co-Authored-By: Matthew Patton <pattonme@yahoo.com>
pull/7102/head
Adrien Delorme 8 years ago
parent a17f33e494
commit b7222d1f56

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
@ -14,6 +13,7 @@ import (
"syscall"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
)
// Communicator is a special communicator that works by executing
@ -67,7 +67,7 @@ func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
func (c *Communicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error {
dst = filepath.Join(c.Chroot, dst)
log.Printf("Uploading to chroot dir: %s", dst)
tf, err := ioutil.TempFile("", "packer-amazon-chroot")
tf, err := tmp.File("packer-amazon-chroot")
if err != nil {
return fmt.Errorf("Error preparing shell script: %s", err)
}

@ -3,11 +3,11 @@ package docker
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
)
// StepTempDir creates a temporary directory that we use in order to
@ -21,13 +21,7 @@ func (s *StepTempDir) Run(_ context.Context, state multistep.StateBag) multistep
ui.Say("Creating a temporary directory for sharing data...")
var err error
var tempdir string
configTmpDir, err := packer.ConfigTmpDir()
if err == nil {
tempdir, err = ioutil.TempDir(configTmpDir, "packer-docker")
}
tempdir, err := tmp.Dir("packer-docker")
if err != nil {
err := fmt.Errorf("Error making temp dir: %s", err)
state.Put("error", err)

@ -3,11 +3,9 @@ package docker
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
func TestStepTempDir_impl(t *testing.T) {
@ -52,46 +50,3 @@ func testStepTempDir_impl(t *testing.T) string {
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))
}
}

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
)
type StepCreateBuildDir struct {
@ -29,12 +30,13 @@ func (s *StepCreateBuildDir) Run(_ context.Context, state multistep.StateBag) mu
ui.Say("Creating build directory...")
var err error
if s.TempPath == "" {
s.TempPath = os.TempDir()
s.buildDir, err = tmp.Dir("hyperv")
} else {
s.buildDir, err = ioutil.TempDir(s.TempPath, "hyperv")
}
var err error
s.buildDir, err = ioutil.TempDir(s.TempPath, "packerhv")
if err != nil {
err = fmt.Errorf("Error creating build directory: %s", err)
state.Put("error", err)

@ -38,7 +38,7 @@ func TestStepCreateBuildDir_Defaults(t *testing.T) {
// This prevents the regexp interpreting backslashes as escape sequences
stateBuildDir := filepath.ToSlash(v.(string))
expectedBuildDirRe := regexp.MustCompile(
filepath.ToSlash(filepath.Join(os.TempDir(), "packerhv") + `[[:digit:]]{9}$`))
filepath.ToSlash(filepath.Join(os.TempDir(), "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 +79,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)

@ -4,13 +4,13 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
)
const (
@ -93,7 +93,7 @@ func (s *StepMountFloppydrive) Cleanup(state multistep.StateBag) {
}
func (s *StepMountFloppydrive) copyFloppy(path string) (string, error) {
tempdir, err := ioutil.TempDir("", "packer")
tempdir, err := tmp.Dir("hyperv")
if err != nil {
return "", err
}

@ -5,12 +5,11 @@ package iso
import (
"context"
"fmt"
"os"
"reflect"
"strconv"
"testing"
"os"
hypervcommon "github.com/hashicorp/packer/builder/hyperv/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"

@ -12,6 +12,7 @@ import (
"syscall"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
)
type LxcAttachCommunicator struct {
@ -60,7 +61,7 @@ func (c *LxcAttachCommunicator) Start(cmd *packer.RemoteCmd) error {
func (c *LxcAttachCommunicator) Upload(dst string, r io.Reader, fi *os.FileInfo) error {
log.Printf("Uploading to rootfs: %s", dst)
tf, err := ioutil.TempFile("", "packer-lxc-attach")
tf, err := tmp.File("packer-lxc-attach")
if err != nil {
return fmt.Errorf("Error uploading file to rootfs: %s", err)
}

@ -13,6 +13,7 @@ import (
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"golang.org/x/crypto/ssh"
)
@ -137,13 +138,13 @@ func berToDer(ber []byte, ui packer.Ui) []byte {
return ber
}
berKey, err := ioutil.TempFile("", "packer-ber-privatekey-")
berKey, err := tmp.File("packer-ber-privatekey-")
defer os.Remove(berKey.Name())
if err != nil {
return ber
}
ioutil.WriteFile(berKey.Name(), ber, os.ModeAppend)
derKey, err := ioutil.TempFile("", "packer-der-privatekey-")
derKey, err := tmp.File("packer-der-privatekey-")
defer os.Remove(derKey.Name())
if err != nil {
return ber

@ -15,6 +15,7 @@ import (
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
"github.com/hashicorp/packer/packer/tmp"
)
// Parallels9Driver is a base type for Parallels builders.
@ -288,7 +289,7 @@ func (d *Parallels9Driver) SendKeyScanCodes(vmName string, codes ...string) erro
return nil
}
f, err := ioutil.TempFile("", "prltype")
f, err := tmp.File("prltype")
if err != nil {
return err
}

@ -4,13 +4,13 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
)
// This step attaches the ISO to the virtual machine.
@ -106,7 +106,7 @@ func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) {
}
func (s *StepAttachFloppy) copyFloppy(path string) (string, error) {
tempdir, err := ioutil.TempDir("", "packer")
tempdir, err := tmp.Dir("virtualbox")
if err != nil {
return "", err
}

@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
@ -13,6 +12,7 @@ import (
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"github.com/hashicorp/packer/template/interpolate"
)
@ -153,7 +153,7 @@ func (s *StepDownloadGuestAdditions) downloadAdditionsSHA256(ctx context.Context
"https://download.virtualbox.org/virtualbox/%s/SHA256SUMS",
additionsVersion)
checksumsFile, err := ioutil.TempFile("", "packer")
checksumsFile, err := tmp.File("packer")
if err != nil {
state.Put("error", fmt.Errorf(
"Failed creating temporary file to store guest addition checksums: %s",

@ -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/tmp"
"github.com/hashicorp/packer/template/interpolate"
)
@ -417,7 +418,7 @@ 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")
vmxDir, err = tmp.Dir("vmw-iso")
if err != nil {
err := fmt.Errorf("Error preparing VMX template: %s", err)
state.Put("error", err)

@ -47,6 +47,7 @@ func tmpnam(prefix string) string {
dir := os.TempDir()
max := int(math.Pow(2, float64(length)))
// FIXME use ioutil.TempFile() or at least mimic implementation, this could loop forever
n, err := rand.Intn(max), nil
for path = filepath.Join(dir, prefix+strconv.Itoa(n)); err == nil; _, err = os.Stat(path) {
n = rand.Intn(max)

@ -3,7 +3,6 @@ package vmx
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -12,6 +11,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/tmp"
)
// StepCloneVMX takes a VMX file and clones the VM into the output directory.
@ -50,7 +50,7 @@ func (s *StepCloneVMX) Run(_ context.Context, state multistep.StateBag) multiste
// * The disk compaction step needs the paths to all attached disks
if remoteDriver, ok := driver.(vmwcommon.RemoteDriver); ok {
remoteVmxPath := vmxPath
tempDir, err := ioutil.TempDir("", "packer-vmx")
tempDir, err := tmp.Dir("packer-vmx")
if err != nil {
return halt(err)
}

@ -4,12 +4,13 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"github.com/hashicorp/packer/packer/tmp"
)
const (
@ -121,7 +122,7 @@ func (ps *PowerShellCmd) getPowerShellPath() (string, error) {
}
func saveScript(fileContents string) (string, error) {
file, err := ioutil.TempFile(os.TempDir(), "ps")
file, err := tmp.File("powershell")
if err != nil {
return "", err
}

@ -3,7 +3,6 @@ package shell_local
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
@ -13,6 +12,7 @@ import (
"github.com/hashicorp/packer/common"
commonhelper "github.com/hashicorp/packer/helper/common"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"github.com/hashicorp/packer/template/interpolate"
)
@ -107,7 +107,7 @@ func Run(ui packer.Ui, config *Config) (bool, error) {
}
func createInlineScriptFile(config *Config) (string, error) {
tf, err := ioutil.TempFile("", "packer-shell")
tf, err := tmp.File("packer-shell")
if err != nil {
return "", fmt.Errorf("Error preparing shell script: %s", err)
}

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
@ -13,6 +12,7 @@ import (
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"github.com/mitchellh/go-fs"
"github.com/mitchellh/go-fs/fat"
)
@ -39,7 +39,7 @@ func (s *StepCreateFloppy) Run(_ context.Context, state multistep.StateBag) mult
ui.Say("Creating floppy disk...")
// Create a temporary file to be our floppy drive
floppyF, err := ioutil.TempFile("", "packer")
floppyF, err := tmp.File("packer")
if err != nil {
state.Put("error",
fmt.Errorf("Error creating temporary file for floppy: %s", err))

@ -16,6 +16,7 @@ import (
"time"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
@ -793,7 +794,7 @@ func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader, fi *
} else {
// Create a temporary file where we can copy the contents of the src
// so that we can determine the length, since SCP is length-prefixed.
tf, err := ioutil.TempFile("", "packer-upload")
tf, err := tmp.File("packer-upload")
if err != nil {
return fmt.Errorf("Error creating temporary file for upload: %s", err)
}

@ -1,12 +1,13 @@
package communicator
import (
"io/ioutil"
"testing"
"github.com/hashicorp/packer/packer/tmp"
)
func TestPEM(t *testing.T) string {
tf, err := ioutil.TempFile("", "packer")
tf, err := tmp.File("packer")
if err != nil {
t.Fatalf("err: %s", err)
}

@ -21,6 +21,7 @@ import (
"github.com/hashicorp/packer/command"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/plugin"
"github.com/hashicorp/packer/packer/tmp"
"github.com/hashicorp/packer/version"
"github.com/mitchellh/cli"
"github.com/mitchellh/panicwrap"
@ -69,7 +70,7 @@ func realMain() int {
// We always send logs to a temporary file that we use in case
// there is a panic. Otherwise, we delete it.
logTempFile, err := ioutil.TempFile("", "packer-log")
logTempFile, err := tmp.File("packer-log")
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't setup logging tempfile: %s", err)
return 1

@ -5,6 +5,8 @@ import (
"os"
"strings"
"testing"
"github.com/hashicorp/packer/packer/tmp"
)
type TestCache struct{}
@ -30,7 +32,7 @@ func TestFileCache_Implements(t *testing.T) {
}
func TestFileCache(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "packer")
cacheDir, err := tmp.Dir("packer")
if err != nil {
t.Fatalf("error creating temporary dir: %s", err)
}

@ -21,29 +21,7 @@ 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
}
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)

@ -10,7 +10,6 @@ package plugin
import (
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
@ -23,6 +22,7 @@ import (
"time"
packrpc "github.com/hashicorp/packer/packer/rpc"
"github.com/hashicorp/packer/packer/tmp"
)
// This is a count of the number of interrupts the process has received.
@ -125,7 +125,7 @@ func serverListener_tcp(minPort, maxPort int64) (net.Listener, error) {
}
func serverListener_unix() (net.Listener, error) {
tf, err := ioutil.TempFile("", "packer-plugin")
tf, err := tmp.File("packer-plugin")
if err != nil {
return nil, err
}

@ -0,0 +1,49 @@
// Package tmp provides temporary directory helpers.
//
// tmp stores temporary items in the system's
// temporary directory ( see os.TempDir ).
//
// When the $TMPDIR_SUFFIX environment variable
// is set, tmp will return paths under $TMPDIR_SUFFIX
// inside the system temporary directory.
//
package tmp
import (
"io/ioutil"
"os"
"path/filepath"
)
var tmpDir = os.TempDir()
func init() {
if d := os.Getenv("TMPDIR_SUFFIX"); d != "" {
tmpDir = filepath.Join(tmpDir, d)
}
}
// Dir creates a new temporary directory in the system temporary
// directory with a name beginning with prefix and returns the path
// of the new directory.
// Multiple programs calling Dir simultaneously
// will not choose the same directory.
// It is the caller's responsibility
// to remove the file when no longer needed.
func Dir(prefix string) (string, error) {
return ioutil.TempDir(tmpDir, prefix)
}
// File creates a new temporary file in the system temporary
// directory, opens the file for reading and writing, and
// returns the resulting *os.File.
// The filename is generated by taking pattern and adding a random
// string to the end. If pattern includes a "*", the random string
// replaces the last "*".
// Multiple programs calling File simultaneously
// will not choose the same file. The caller can use f.Name()
// to find the pathname of the file. It is the caller's responsibility
// to remove the file when no longer needed.
func File(pattern string) (*os.File, error) {
return ioutil.TempFile(tmpDir, pattern)
}

@ -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/tmp"
"github.com/hashicorp/packer/template/interpolate"
"github.com/mitchellh/mapstructure"
)
@ -95,7 +96,7 @@ 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")
dir, err := tmp.Dir("packer")
if err != nil {
return nil, false, err
}

@ -1,11 +1,11 @@
package vagrant
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/hashicorp/packer/packer/tmp"
"github.com/stretchr/testify/assert"
)
@ -14,8 +14,10 @@ func TestVBoxProvider_impl(t *testing.T) {
}
func TestDecomressOVA(t *testing.T) {
td, err := ioutil.TempDir("", "pp-vagrant-virtualbox")
td, err := tmp.Dir("pp-vagrant-virtualbox")
assert.NoError(t, err)
defer os.RemoveAll(td)
fixture := "../../common/test-fixtures/decompress-tar/outside_parent.tar"
err = DecompressOva(td, fixture)
assert.NoError(t, err)
@ -23,5 +25,4 @@ func TestDecomressOVA(t *testing.T) {
assert.Error(t, err)
_, err = os.Stat(filepath.Join(td, "demo.poc"))
assert.NoError(t, err)
os.RemoveAll(td)
}

@ -2,7 +2,6 @@ package ansiblelocal
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -11,6 +10,7 @@ import (
"github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"github.com/hashicorp/packer/template/interpolate"
)
@ -212,7 +212,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
}
if len(p.config.InventoryFile) == 0 {
tf, err := ioutil.TempFile("", "packer-provisioner-ansible-local")
tf, err := tmp.File("packer-provisioner-ansible-local")
if err != nil {
return fmt.Errorf("Error preparing inventory file: %s", err)
}

@ -29,6 +29,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/tmp"
"github.com/hashicorp/packer/template/interpolate"
)
@ -499,7 +500,7 @@ func newUserKey(pubKeyFile string) (*userKey, error) {
Headers: nil,
Bytes: privateKeyDer,
}
tf, err := ioutil.TempFile("", "ansible-key")
tf, err := tmp.File("ansible-key")
if err != nil {
return nil, errors.New("failed to create temp file for generated key")
}

@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -14,6 +13,7 @@ import (
"time"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
)
const (
@ -43,7 +43,7 @@ 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")
d, err := tmp.Dir("ansible-upload")
if err != nil {
fmt.Fprintf(out, scpEmptyError)
return err
@ -68,7 +68,7 @@ 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")
d, err := tmp.Dir("ansible-download")
if err != nil {
fmt.Fprintf(out, scpEmptyError)
return err

@ -8,7 +8,6 @@ import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
@ -20,6 +19,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/tmp"
"github.com/hashicorp/packer/template/interpolate"
)
@ -233,7 +233,7 @@ 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")
temp, err := tmp.File("powershell-provisioner")
if err != nil {
return "", err
}

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
@ -18,6 +17,7 @@ import (
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"github.com/hashicorp/packer/template/interpolate"
)
@ -216,7 +216,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
// If we have an inline script, then turn that into a temporary
// shell script and use that.
if p.config.Inline != nil {
tf, err := ioutil.TempFile("", "packer-shell")
tf, err := tmp.File("packer-shell")
if err != nil {
return fmt.Errorf("Error preparing shell script: %s", err)
}
@ -242,7 +242,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
}
if p.config.UseEnvVarFile == true {
tf, err := ioutil.TempFile("", "packer-shell-vars")
tf, err := tmp.File("packer-shell-vars")
if err != nil {
return fmt.Errorf("Error preparing shell script: %s", err)
}

@ -6,7 +6,6 @@ import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
@ -16,9 +15,11 @@ import (
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/packer/tmp"
"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 +158,7 @@ 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")
temp, err := tmp.File("windows-shell-provisioner")
if err != nil {
log.Printf("Unable to create temporary file for inline scripts: %s", err)
return "", err

@ -6,13 +6,13 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/packer/packer/tmp"
"github.com/mitchellh/mapstructure"
)
@ -328,7 +328,7 @@ 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")
f, err = tmp.File("parse")
if err != nil {
return nil, err
}

@ -380,8 +380,8 @@ portable provisioning scripts.
## Overriding the host directory
By default, Packer creates a temporary folder under your home directory, and
uses that to stage files for uploading into the container. If you would like to
change the path to this temporary folder, you can set the `PACKER_TMP_DIR`
By default, Packer creates a temporary folder under your system temporary directory,
and uses that to stage files for uploading into the container. If you would like to
change the path to this temporary folder, you can set the `TMP_DIR`
environment variable. This can be useful, for example, if you have your home
directory permissions set up to disallow access from the docker daemon.

@ -42,9 +42,13 @@ each can be found below:
new versions of Packer. If you want to disable this for security or privacy
reasons, you can set this environment variable to `1`.
- `TMPDIR` (Unix) / `TMP` (Windows) - The location of the directory used for
temporary files (defaults to `/tmp` on Linux/Unix and
`%USERPROFILE%\AppData\Local\Temp` on Windows Vista and above). It might be
necessary to customize it when working with large files since `/tmp` is a
memory-backed filesystem in some Linux distributions in which case
`/var/tmp` might be preferred.
- `TMPDIR` (Unix) / `TMP` `TEMP` `USERPROFILE` (Windows) - The location of
the directory used for temporary files (defaults to `/tmp` on Linux/Unix
and `%USERPROFILE%\AppData\Local\Temp` on Windows Vista and above). It
might be necessary to customize it when working with large files since
`/tmp` is a memory-backed filesystem in some Linux distributions in which
case `/var/tmp` might be preferred.
- `TMPDIR_SUFFIX` - When set, packer will put temporary files under system
temporary directory + `TMPDIR_SUFFIX`. This allows to create & to give
specific rights to a directory ie: `chmod 777 $TMPDIR/$TMPDIR_SUFFIX/`

Loading…
Cancel
Save