When using docker-machine on a Mac only the /Users folder is shared with the VM. Uploads fail since the normal tmpdir is not shared. This change uses the local packer directory (usually when run in the users home folders) allowing it to work without setting TMPDIR explicitly. A better fix would be to use the docker API directly but that would force users to use docker API version 20+. - fixes #901, fixes #1752, fixes #2436, fixes #2675, fixes #2697.
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package docker
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/mitchellh/multistep"
|
|
"github.com/mitchellh/packer/packer"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// StepTempDir creates a temporary directory that we use in order to
|
|
// share data with the docker container over the communicator.
|
|
type StepTempDir struct {
|
|
tempDir string
|
|
}
|
|
|
|
func (s *StepTempDir) Run(state multistep.StateBag) multistep.StepAction {
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say("Creating a temporary directory for sharing data...")
|
|
// Create the docker temp files in the current working directory
|
|
// to work around an issue when running with docker-machine
|
|
// using vm's needing access to shared folder content. This assumes
|
|
// the current working directory is mapped as a share folder.
|
|
// Allow TMPDIR to override this location.
|
|
path := ""
|
|
if tmpdir := os.Getenv("TMPDIR"); tmpdir == "" {
|
|
abspath, err := filepath.Abs(".")
|
|
if err == nil {
|
|
path = abspath
|
|
}
|
|
}
|
|
td, err := ioutil.TempDir(path, "packer-docker")
|
|
if err != nil {
|
|
err := fmt.Errorf("Error making temp dir: %s", err)
|
|
state.Put("error", err)
|
|
ui.Error(err.Error())
|
|
return multistep.ActionHalt
|
|
}
|
|
|
|
s.tempDir = td
|
|
state.Put("temp_dir", s.tempDir)
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
func (s *StepTempDir) Cleanup(state multistep.StateBag) {
|
|
if s.tempDir != "" {
|
|
os.RemoveAll(s.tempDir)
|
|
}
|
|
}
|