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

pull/6942/head
Matthew Patton 7 years ago
parent d5c5306a97
commit 3590fae8bd

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@ -1,6 +1,6 @@
// +build windows
package packer
package configfile
import (
"path/filepath"

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

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

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

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

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

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

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

Loading…
Cancel
Save