2013-11-09 02:43:41 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-11-09 02:43:41 -05:00
|
|
|
"fmt"
|
2019-10-03 17:50:53 -04:00
|
|
|
"io/ioutil"
|
2019-07-24 19:04:55 -04:00
|
|
|
"log"
|
2013-11-09 02:43:41 -05:00
|
|
|
"os"
|
2019-01-18 10:42:04 -05:00
|
|
|
"path/filepath"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/pathing"
|
2013-11-09 02:43:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-01-18 10:42:04 -05:00
|
|
|
// ConfigTmpDir returns the configuration tmp directory for Docker
|
|
|
|
func ConfigTmpDir() (string, error) {
|
2020-12-02 16:19:45 -05:00
|
|
|
configdir, err := pathing.ConfigDir()
|
2019-01-18 10:42:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-07-24 19:04:55 -04:00
|
|
|
if tmpdir := os.Getenv("PACKER_TMP_DIR"); tmpdir != "" {
|
|
|
|
// override the config dir with tmp dir. Still stat it and mkdirall if
|
|
|
|
// necessary.
|
|
|
|
fp, err := filepath.Abs(tmpdir)
|
|
|
|
log.Printf("found PACKER_TMP_DIR env variable; setting tmpdir to %s", fp)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
configdir = fp
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:16:27 -04:00
|
|
|
_, err = os.Stat(configdir)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Printf("Config dir %s does not exist; creating...", configdir)
|
|
|
|
if err = os.MkdirAll(configdir, 0755); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-10-03 17:50:53 -04:00
|
|
|
td, err := ioutil.TempDir(configdir, "tmp")
|
2019-10-03 18:06:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error creating temp dir: %s", err)
|
|
|
|
|
|
|
|
}
|
2019-07-24 19:04:55 -04:00
|
|
|
log.Printf("Set Packer temp dir to %s", td)
|
2019-01-18 10:42:04 -05:00
|
|
|
return td, nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepTempDir) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-11-09 02:43:41 -05:00
|
|
|
|
|
|
|
ui.Say("Creating a temporary directory for sharing data...")
|
2015-10-16 20:36:29 -04:00
|
|
|
|
2019-01-18 10:42:04 -05:00
|
|
|
tempdir, err := ConfigTmpDir()
|
2013-11-09 02:43:41 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error making temp dir: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:36:29 -04:00
|
|
|
s.tempDir = tempdir
|
2013-11-09 02:43:41 -05:00
|
|
|
state.Put("temp_dir", s.tempDir)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepTempDir) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.tempDir != "" {
|
|
|
|
os.RemoveAll(s.tempDir)
|
|
|
|
}
|
|
|
|
}
|