Merge pull request #2846 from markpeek/packer-tmp

Create docker temp files under packer.d when TMPDIR is not set
pull/2876/head
Mark Peek 11 years ago
commit ba7814b0ed

@ -18,7 +18,14 @@ func (s *StepTempDir) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Creating a temporary directory for sharing data...") 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 { if err != nil {
err := fmt.Errorf("Error making temp dir: %s", err) err := fmt.Errorf("Error making temp dir: %s", err)
state.Put("error", err) state.Put("error", err)
@ -26,7 +33,7 @@ func (s *StepTempDir) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt return multistep.ActionHalt
} }
s.tempDir = td s.tempDir = tempdir
state.Put("temp_dir", s.tempDir) state.Put("temp_dir", s.tempDir)
return multistep.ActionContinue return multistep.ActionContinue
} }

@ -1,16 +1,19 @@
package docker package docker
import ( import (
"github.com/mitchellh/multistep"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
) )
func TestStepTempDir_impl(t *testing.T) { func TestStepTempDir_impl(t *testing.T) {
var _ multistep.Step = new(StepTempDir) var _ multistep.Step = new(StepTempDir)
} }
func TestStepTempDir(t *testing.T) { func testStepTempDir_impl(t *testing.T) string {
state := testState(t) state := testState(t)
step := new(StepTempDir) step := new(StepTempDir)
defer step.Cleanup(state) defer step.Cleanup(state)
@ -41,4 +44,53 @@ func TestStepTempDir(t *testing.T) {
if _, err := os.Stat(dir); err == nil { if _, err := os.Stat(dir); err == nil {
t.Fatalf("dir should be gone") 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))
}
} }

@ -7,6 +7,7 @@ import (
"github.com/hashicorp/go-checkpoint" "github.com/hashicorp/go-checkpoint"
"github.com/mitchellh/packer/command" "github.com/mitchellh/packer/command"
"github.com/mitchellh/packer/packer"
) )
func init() { func init() {
@ -25,7 +26,7 @@ func runCheckpoint(c *config) {
return return
} }
configDir, err := ConfigDir() configDir, err := packer.ConfigDir()
if err != nil { if err != nil {
log.Printf("[ERR] Checkpoint setup error: %s", err) log.Printf("[ERR] Checkpoint setup error: %s", err)
checkpointResult <- nil checkpointResult <- nil

@ -31,19 +31,6 @@ type config struct {
Provisioners map[string]string 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 // Decodes configuration in JSON format from the given io.Reader into
// the config object pointed to. // the config object pointed to.
func decodeConfig(r io.Reader, c *config) error { func decodeConfig(r io.Reader, c *config) error {
@ -70,7 +57,7 @@ func (c *config) Discover() error {
} }
// Next, look in the plugins directory. // Next, look in the plugins directory.
dir, err := ConfigDir() dir, err := packer.ConfigDir()
if err != nil { if err != nil {
log.Printf("[ERR] Error loading config directory: %s", err) log.Printf("[ERR] Error loading config directory: %s", err)
} else { } else {

@ -244,7 +244,7 @@ func loadConfig() (*config, error) {
configFilePath := os.Getenv("PACKER_CONFIG") configFilePath := os.Getenv("PACKER_CONFIG")
if configFilePath == "" { if configFilePath == "" {
var err error var err error
configFilePath, err = configFile() configFilePath, err = packer.ConfigFile()
if err != nil { if err != nil {
log.Printf("Error detecting default config file path: %s", err) log.Printf("Error detecting default config file path: %s", err)

@ -0,0 +1,40 @@
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
// 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) {
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
}

@ -1,6 +1,6 @@
// +build darwin freebsd linux netbsd openbsd // +build darwin freebsd linux netbsd openbsd
package main package packer
import ( import (
"bytes" "bytes"

@ -1,6 +1,6 @@
// +build windows // +build windows
package main package packer
import ( import (
"path/filepath" "path/filepath"
Loading…
Cancel
Save