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"
|
|
|
|
"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) {
|
|
|
|
if tmpdir := os.Getenv("PACKER_TMP_DIR"); tmpdir != "" {
|
|
|
|
return filepath.Abs(tmpdir)
|
|
|
|
}
|
|
|
|
configdir, err := packer.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
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepTempDir) Run(_ 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)
|
|
|
|
}
|
|
|
|
}
|