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-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
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
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) {
|
|
|
|
configdir, err := packer.ConfigDir()
|
|
|
|
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-01-18 10:42:04 -05:00
|
|
|
td := filepath.Join(configdir, "tmp")
|
|
|
|
_, err = os.Stat(td)
|
|
|
|
if os.IsNotExist(err) {
|
2019-07-24 19:04:55 -04:00
|
|
|
log.Printf("Creating tempdir in %s", td)
|
2019-01-18 10:42:04 -05:00
|
|
|
if err = os.MkdirAll(td, 0755); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return "", 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 {
|
2013-11-09 02:43:41 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|