packer-cn/builder/hyperv/common/step_create_tempdir.go

85 lines
2.0 KiB
Go
Raw Normal View History

package common
import (
"fmt"
"io/ioutil"
"os"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type StepCreateTempDir struct {
2017-08-01 05:48:17 -04:00
// The user-supplied root directores into which we create subdirectories.
TempPath string
VhdTempPath string
2017-08-01 05:48:17 -04:00
// The subdirectories with the randomly generated name.
dirPath string
vhdDirPath string
}
func (s *StepCreateTempDir) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating temporary directory...")
if s.TempPath == "" {
s.TempPath = os.TempDir()
}
packerTempDir, err := ioutil.TempDir(s.TempPath, "packerhv")
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
state.Put("packerTempDir", packerTempDir)
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
state.Put("packerVhdTempDir", packerVhdTempDir)
}
2016-11-06 11:02:00 -05:00
// ui.Say("packerTempDir = '" + packerTempDir + "'")
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...")
2017-08-01 05:48:17 -04:00
err := os.RemoveAll(s.dirPath)
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))
}
}
}