2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-21 07:36:07 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-06-30 19:19:27 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-06-30 19:19:27 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepCreateTempDir struct {
|
2018-03-13 03:37:32 -04:00
|
|
|
// The user-supplied root directories into which we create subdirectories.
|
2017-08-01 05:23:19 -04:00
|
|
|
TempPath string
|
|
|
|
VhdTempPath string
|
2017-08-01 05:48:17 -04:00
|
|
|
// The subdirectories with the randomly generated name.
|
|
|
|
dirPath string
|
|
|
|
vhdDirPath string
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepCreateTempDir) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Creating temporary directory...")
|
|
|
|
|
2017-06-30 19:19:27 -04:00
|
|
|
if s.TempPath == "" {
|
|
|
|
s.TempPath = os.TempDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
packerTempDir, err := ioutil.TempDir(s.TempPath, "packerhv")
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating temporary directory: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:02:00 -05:00
|
|
|
s.dirPath = packerTempDir
|
2015-06-21 07:36:07 -04:00
|
|
|
state.Put("packerTempDir", packerTempDir)
|
|
|
|
|
2017-08-01 05:23:19 -04:00
|
|
|
if s.VhdTempPath == "" {
|
|
|
|
// Fall back to regular temp dir if no separate VHD temp dir set.
|
|
|
|
state.Put("packerVhdTempDir", packerTempDir)
|
|
|
|
} else {
|
|
|
|
packerVhdTempDir, err := ioutil.TempDir(s.VhdTempPath, "packerhv-vhd")
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating temporary VHD directory: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-08-01 05:48:17 -04:00
|
|
|
s.vhdDirPath = packerVhdTempDir
|
2017-08-01 05:23:19 -04:00
|
|
|
state.Put("packerVhdTempDir", packerVhdTempDir)
|
|
|
|
}
|
|
|
|
|
2016-11-06 11:02:00 -05:00
|
|
|
// ui.Say("packerTempDir = '" + packerTempDir + "'")
|
2015-06-21 07:36:07 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateTempDir) Cleanup(state multistep.StateBag) {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2017-08-01 05:48:17 -04:00
|
|
|
if s.dirPath != "" {
|
|
|
|
ui.Say("Deleting temporary directory...")
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2017-08-01 05:48:17 -04:00
|
|
|
err := os.RemoveAll(s.dirPath)
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2017-08-01 05:48:17 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting temporary directory: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.vhdDirPath != "" && s.dirPath != s.vhdDirPath {
|
|
|
|
ui.Say("Deleting temporary VHD directory...")
|
|
|
|
|
|
|
|
err := os.RemoveAll(s.vhdDirPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting temporary VHD directory: %s", err))
|
|
|
|
}
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
}
|