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"
|
2018-07-08 18:27:37 -04:00
|
|
|
"log"
|
2015-06-21 07:36:07 -04:00
|
|
|
"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"
|
2020-11-16 18:13:08 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/tmp"
|
2015-06-21 07:36:07 -04:00
|
|
|
)
|
|
|
|
|
2018-07-08 17:24:06 -04:00
|
|
|
type StepCreateBuildDir struct {
|
2018-07-08 06:16:02 -04:00
|
|
|
// User supplied directory under which we create the main build
|
2018-07-08 17:24:06 -04:00
|
|
|
// directory. The build directory is used to house the VM files and
|
2018-07-08 06:16:02 -04:00
|
|
|
// folders during the build. If unspecified the default temp directory
|
|
|
|
// for the OS is used
|
|
|
|
TempPath string
|
2018-07-08 17:24:06 -04:00
|
|
|
// The full path to the build directory. This is the concatenation of
|
2018-07-08 06:16:02 -04:00
|
|
|
// TempPath plus a directory uniquely named for the build
|
2018-07-08 18:27:37 -04:00
|
|
|
buildDir string
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
2018-07-08 06:16:02 -04:00
|
|
|
// Creates the main directory used to house the VMs files and folders
|
|
|
|
// during the build
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepCreateBuildDir) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2018-07-08 17:24:06 -04:00
|
|
|
ui.Say("Creating build directory...")
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2018-12-12 09:45:00 -05:00
|
|
|
var err error
|
2017-06-30 19:19:27 -04:00
|
|
|
if s.TempPath == "" {
|
2018-12-12 09:45:00 -05:00
|
|
|
s.buildDir, err = tmp.Dir("hyperv")
|
|
|
|
} else {
|
|
|
|
s.buildDir, err = ioutil.TempDir(s.TempPath, "hyperv")
|
2017-06-30 19:19:27 -04:00
|
|
|
}
|
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
2018-07-08 18:27:37 -04:00
|
|
|
err = fmt.Errorf("Error creating build directory: %s", err)
|
2015-06-21 07:36:07 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-07-08 18:27:37 -04:00
|
|
|
log.Printf("Created build directory: %s", s.buildDir)
|
|
|
|
|
|
|
|
// Record the build directory location for later steps
|
|
|
|
state.Put("build_dir", s.buildDir)
|
2015-06-21 07:36:07 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-07-08 06:16:02 -04:00
|
|
|
// Cleanup removes the build directory
|
2018-07-08 17:24:06 -04:00
|
|
|
func (s *StepCreateBuildDir) Cleanup(state multistep.StateBag) {
|
2018-07-08 18:27:37 -04:00
|
|
|
if s.buildDir == "" {
|
2018-07-08 06:16:02 -04:00
|
|
|
return
|
2017-08-01 05:48:17 -04:00
|
|
|
}
|
|
|
|
|
2018-07-08 06:16:02 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-07-08 17:24:06 -04:00
|
|
|
ui.Say("Deleting build directory...")
|
2017-08-01 05:48:17 -04:00
|
|
|
|
2018-07-08 18:27:37 -04:00
|
|
|
err := os.RemoveAll(s.buildDir)
|
2018-07-08 06:16:02 -04:00
|
|
|
if err != nil {
|
2018-07-08 17:24:06 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error deleting build directory: %s", err))
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
}
|