Files
Packer-Cn/builder/docker/step_temp_dir.go
T

48 lines
1.0 KiB
Go
Raw Normal View History

2013-11-08 23:43:41 -08:00
package docker
import (
2018-01-22 15:32:33 -08:00
"context"
2013-11-08 23:43:41 -08:00
"fmt"
"io/ioutil"
"os"
2018-01-22 17:21:10 -08:00
2018-01-22 15:32:33 -08:00
"github.com/hashicorp/packer/helper/multistep"
2018-01-22 17:21:10 -08:00
"github.com/hashicorp/packer/packer"
2013-11-08 23:43:41 -08: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
}
2018-01-22 15:31:41 -08:00
func (s *StepTempDir) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2013-11-08 23:43:41 -08:00
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating a temporary directory for sharing data...")
2015-10-16 17:36:29 -07:00
var err error
var tempdir string
configTmpDir, err := packer.ConfigTmpDir()
if err == nil {
tempdir, err = ioutil.TempDir(configTmpDir, "packer-docker")
}
2013-11-08 23:43:41 -08: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 17:36:29 -07:00
s.tempDir = tempdir
2013-11-08 23:43:41 -08:00
state.Put("temp_dir", s.tempDir)
return multistep.ActionContinue
}
func (s *StepTempDir) Cleanup(state multistep.StateBag) {
if s.tempDir != "" {
os.RemoveAll(s.tempDir)
}
}