2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
2019-05-31 08:27:41 -04:00
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
package vmcx
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2017-03-12 07:31:31 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-05-21 12:29:26 -04:00
|
|
|
hypervcommon "github.com/hashicorp/packer/builder/hyperv/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
2018-04-18 19:26:48 -04:00
|
|
|
"github.com/hashicorp/packer/common/bootcommand"
|
2017-05-21 12:29:26 -04:00
|
|
|
powershell "github.com/hashicorp/packer/common/powershell"
|
2019-06-13 11:23:21 -04:00
|
|
|
"github.com/hashicorp/packer/common/shutdowncommand"
|
2017-05-21 12:29:26 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-05-21 12:29:26 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2017-03-12 07:31:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-06-24 19:39:27 -04:00
|
|
|
DefaultRamSize = 1 * 1024 // 1GB
|
|
|
|
MinRamSize = 32 // 32MB
|
|
|
|
MaxRamSize = 1024 * 1024 // 1TB
|
|
|
|
MinNestedVirtualizationRamSize = 4 * 1024 // 4GB
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
LowRam = 256 // 256MB
|
|
|
|
|
|
|
|
DefaultUsername = ""
|
|
|
|
DefaultPassword = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
// Builder implements packer.Builder and builds the actual Hyperv
|
|
|
|
// images.
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2019-06-13 11:23:21 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
common.HTTPConfig `mapstructure:",squash"`
|
|
|
|
common.ISOConfig `mapstructure:",squash"`
|
|
|
|
bootcommand.BootConfig `mapstructure:",squash"`
|
|
|
|
hypervcommon.OutputConfig `mapstructure:",squash"`
|
|
|
|
hypervcommon.SSHConfig `mapstructure:",squash"`
|
2019-10-18 16:01:22 -04:00
|
|
|
hypervcommon.CommonConfig `mapstructure:",squash"`
|
2019-06-13 11:23:21 -04:00
|
|
|
shutdowncommand.ShutdownConfig `mapstructure:",squash"`
|
2017-03-12 07:31:31 -04:00
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
// This is the path to a directory containing an exported virtual machine.
|
2018-07-19 13:53:42 -04:00
|
|
|
CloneFromVMCXPath string `mapstructure:"clone_from_vmcx_path"`
|
2017-03-12 07:31:31 -04:00
|
|
|
// This is the name of the virtual machine to clone from.
|
|
|
|
CloneFromVMName string `mapstructure:"clone_from_vm_name"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of a snapshot in the
|
2019-06-06 10:29:25 -04:00
|
|
|
// source machine to use as a starting point for the clone. If the value
|
|
|
|
// given is an empty string, the last snapshot present in the source will
|
|
|
|
// be chosen as the starting point for the new VM.
|
2019-05-28 11:50:58 -04:00
|
|
|
CloneFromSnapshotName string `mapstructure:"clone_from_snapshot_name" required:"false"`
|
|
|
|
// If set to true all snapshots
|
2019-06-06 10:29:25 -04:00
|
|
|
// present in the source machine will be copied when the machine is
|
|
|
|
// cloned. The final result of the build will be an exported virtual
|
|
|
|
// machine that contains all the snapshots of the parent.
|
2019-05-28 11:50:58 -04:00
|
|
|
CloneAllSnapshots bool `mapstructure:"clone_all_snapshots" required:"false"`
|
|
|
|
// If true enables differencing disks. Only
|
2019-06-06 10:29:25 -04:00
|
|
|
// the changes will be written to the new disk. This is especially useful if
|
|
|
|
// your source is a VHD/VHDX. This defaults to false.
|
2019-05-28 11:50:58 -04:00
|
|
|
DifferencingDisk bool `mapstructure:"differencing_disk" required:"false"`
|
|
|
|
// When cloning a vm to build from, we run a powershell
|
2019-06-06 10:29:25 -04:00
|
|
|
// Compare-VM command, which, depending on your version of Windows, may need
|
|
|
|
// the "Copy" flag to be set to true or false. Defaults to "false". Command:
|
|
|
|
CompareCopy bool `mapstructure:"copy_in_compare" required:"false"`
|
2018-05-05 17:54:58 -04:00
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2017-03-12 07:31:31 -04:00
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2017-11-05 09:16:47 -05:00
|
|
|
Interpolate: true,
|
2017-11-05 08:55:56 -05:00
|
|
|
InterpolateContext: &b.config.ctx,
|
2017-03-12 07:31:31 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, nil, err
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate any errors and warnings
|
|
|
|
var errs *packer.MultiError
|
|
|
|
warnings := make([]string, 0)
|
|
|
|
|
2017-03-12 13:31:56 -04:00
|
|
|
if b.config.RawSingleISOUrl != "" || len(b.config.ISOUrls) > 0 {
|
|
|
|
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
|
|
|
|
warnings = append(warnings, isoWarnings...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, isoErrs...)
|
|
|
|
}
|
2017-03-12 07:31:31 -04:00
|
|
|
|
2018-04-18 19:26:48 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...)
|
2017-03-12 07:31:31 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...)
|
|
|
|
|
2019-10-18 16:01:22 -04:00
|
|
|
commonErrs, commonWarns := b.config.CommonConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)
|
|
|
|
packer.MultiErrorAppend(errs, commonErrs...)
|
|
|
|
warnings = append(warnings, commonWarns...)
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
if b.config.Cpu < 1 {
|
|
|
|
b.config.Cpu = 1
|
|
|
|
}
|
|
|
|
|
2017-03-12 13:31:56 -04:00
|
|
|
if b.config.CloneFromVMName == "" {
|
2018-07-19 13:53:42 -04:00
|
|
|
if b.config.CloneFromVMCXPath == "" {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("The clone_from_vm_name must be specified if "+
|
2018-07-19 13:53:42 -04:00
|
|
|
"clone_from_vmcx_path is not specified."))
|
2017-05-21 12:29:26 -04:00
|
|
|
}
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
virtualMachineExists, err := powershell.DoesVirtualMachineExist(b.config.CloneFromVMName)
|
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine to clone "+
|
|
|
|
"from exists: %s", err))
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
if !virtualMachineExists {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Virtual machine '%s' to clone from does not "+
|
|
|
|
"exist.", b.config.CloneFromVMName))
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
b.config.Generation, err = powershell.GetVirtualMachineGeneration(b.config.CloneFromVMName)
|
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting virtual machine to clone "+
|
|
|
|
"from generation: %s", err))
|
2017-03-12 13:31:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.CloneFromSnapshotName != "" {
|
2018-07-09 12:20:38 -04:00
|
|
|
virtualMachineSnapshotExists, err := powershell.DoesVirtualMachineSnapshotExist(
|
|
|
|
b.config.CloneFromVMName, b.config.CloneFromSnapshotName)
|
2017-03-12 13:31:56 -04:00
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine "+
|
|
|
|
"snapshot to clone from exists: %s", err))
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
if !virtualMachineSnapshotExists {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Virtual machine snapshot '%s' on "+
|
|
|
|
"virtual machine '%s' to clone from does not exist.",
|
|
|
|
b.config.CloneFromSnapshotName, b.config.CloneFromVMName))
|
2017-03-12 13:31:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtualMachineOn, err := powershell.IsVirtualMachineOn(b.config.CloneFromVMName)
|
|
|
|
if err != nil {
|
2018-07-09 12:20:38 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Failed detecting if virtual machine to "+
|
|
|
|
"clone is running: %s", err))
|
2017-03-12 13:31:56 -04:00
|
|
|
} else {
|
|
|
|
if virtualMachineOn {
|
|
|
|
warning := fmt.Sprintf("Cloning from a virtual machine that is running.")
|
2019-10-18 16:01:22 -04:00
|
|
|
warnings = hypervcommon.Appendwarns(warnings, warning)
|
2017-03-12 13:31:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-29 20:16:03 -04:00
|
|
|
|
2018-07-19 13:53:42 -04:00
|
|
|
if b.config.CloneFromVMCXPath == "" {
|
2017-05-21 12:29:26 -04:00
|
|
|
if b.config.CloneFromVMName == "" {
|
2018-07-19 13:53:42 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("The clone_from_vmcx_path be specified if "+
|
2018-07-09 12:20:38 -04:00
|
|
|
"clone_from_vm_name must is not specified."))
|
2017-05-21 12:29:26 -04:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-19 13:53:42 -04:00
|
|
|
if _, err := os.Stat(b.config.CloneFromVMCXPath); os.IsNotExist(err) {
|
2017-05-21 12:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
2018-07-19 13:53:42 -04:00
|
|
|
errs, fmt.Errorf("CloneFromVMCXPath does not exist: %s", err))
|
2017-05-21 12:29:26 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:56:32 -04:00
|
|
|
if strings.HasSuffix(strings.ToLower(b.config.CloneFromVMCXPath), ".vmcx") {
|
|
|
|
// User has provided the vmcx file itself rather than the containing
|
|
|
|
// folder.
|
|
|
|
if strings.Contains(b.config.CloneFromVMCXPath, "Virtual Machines") {
|
|
|
|
keep := strings.Split(b.config.CloneFromVMCXPath, "Virtual Machines")
|
|
|
|
b.config.CloneFromVMCXPath = keep[0]
|
|
|
|
} else {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Unable to "+
|
|
|
|
"parse the clone_from_vmcx_path to find the vm directory. "+
|
|
|
|
"Please provide the path to the folder containing the "+
|
|
|
|
"vmcx file, not the file itself. Example: instead of "+
|
|
|
|
"C:\\path\\to\\output-hyperv-iso\\Virtual Machines\\filename.vmcx"+
|
|
|
|
", provide C:\\path\\to\\output-hyperv-iso\\."))
|
|
|
|
}
|
|
|
|
}
|
2017-05-21 12:29:26 -04:00
|
|
|
}
|
2017-03-12 13:31:56 -04:00
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
// Warnings
|
|
|
|
|
|
|
|
if b.config.ShutdownCommand == "" {
|
2019-10-18 16:01:22 -04:00
|
|
|
warnings = hypervcommon.Appendwarns(warnings,
|
2017-03-12 07:31:31 -04:00
|
|
|
"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
|
|
|
|
"will forcibly halt the virtual machine, which may result in data loss.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes a Packer build and returns a packer.Artifact representing
|
|
|
|
// a Hyperv appliance.
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2017-03-12 07:31:31 -04:00
|
|
|
// Create the driver that we'll use to communicate with Hyperv
|
|
|
|
driver, err := hypervcommon.NewHypervPS4Driver()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating Hyper-V driver: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the state.
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("debug", b.config.PackerDebug)
|
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
steps := []multistep.Step{
|
2018-07-09 07:06:45 -04:00
|
|
|
&hypervcommon.StepCreateBuildDir{
|
|
|
|
TempPath: b.config.TempPath,
|
|
|
|
},
|
2019-01-11 18:06:36 -05:00
|
|
|
&common.StepOutputDir{
|
2017-03-12 07:31:31 -04:00
|
|
|
Force: b.config.PackerForce,
|
|
|
|
Path: b.config.OutputDir,
|
|
|
|
},
|
2020-01-07 13:45:24 -05:00
|
|
|
&common.StepDownload{
|
2020-05-28 05:02:09 -04:00
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
Description: "ISO",
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
Url: b.config.ISOUrls,
|
|
|
|
Extension: b.config.TargetExtension,
|
|
|
|
TargetPath: b.config.TargetPath,
|
2020-01-07 13:45:24 -05:00
|
|
|
},
|
2017-03-12 07:31:31 -04:00
|
|
|
&common.StepCreateFloppy{
|
2017-09-11 13:11:45 -04:00
|
|
|
Files: b.config.FloppyFiles,
|
2017-06-19 16:22:55 -04:00
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2019-09-12 08:25:22 -04:00
|
|
|
Label: b.config.FloppyConfig.FloppyLabel,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
&common.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
|
|
|
},
|
|
|
|
&hypervcommon.StepCreateSwitch{
|
|
|
|
SwitchName: b.config.SwitchName,
|
|
|
|
},
|
|
|
|
&hypervcommon.StepCloneVM{
|
2018-07-19 13:53:42 -04:00
|
|
|
CloneFromVMCXPath: b.config.CloneFromVMCXPath,
|
2017-03-12 07:31:31 -04:00
|
|
|
CloneFromVMName: b.config.CloneFromVMName,
|
|
|
|
CloneFromSnapshotName: b.config.CloneFromSnapshotName,
|
|
|
|
CloneAllSnapshots: b.config.CloneAllSnapshots,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
SwitchName: b.config.SwitchName,
|
2018-12-10 17:46:16 -05:00
|
|
|
CompareCopy: b.config.CompareCopy,
|
2017-03-12 07:31:31 -04:00
|
|
|
RamSize: b.config.RamSize,
|
|
|
|
Cpu: b.config.Cpu,
|
|
|
|
EnableMacSpoofing: b.config.EnableMacSpoofing,
|
|
|
|
EnableDynamicMemory: b.config.EnableDynamicMemory,
|
|
|
|
EnableSecureBoot: b.config.EnableSecureBoot,
|
2018-05-10 13:00:35 -04:00
|
|
|
SecureBootTemplate: b.config.SecureBootTemplate,
|
2017-03-12 07:31:31 -04:00
|
|
|
EnableVirtualizationExtensions: b.config.EnableVirtualizationExtensions,
|
2017-12-14 21:24:15 -05:00
|
|
|
MacAddress: b.config.MacAddress,
|
2019-04-12 18:59:09 -04:00
|
|
|
KeepRegistered: b.config.KeepRegistered,
|
2019-10-18 14:43:11 -04:00
|
|
|
AdditionalDiskSize: b.config.AdditionalDiskSize,
|
2019-10-18 16:01:22 -04:00
|
|
|
DiskBlockSize: b.config.DiskBlockSize,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
&hypervcommon.StepEnableIntegrationService{},
|
|
|
|
|
|
|
|
&hypervcommon.StepMountDvdDrive{
|
2020-02-21 02:07:58 -05:00
|
|
|
Generation: b.config.Generation,
|
|
|
|
FirstBootDevice: b.config.FirstBootDevice,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
&hypervcommon.StepMountFloppydrive{
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
|
|
|
|
|
|
|
&hypervcommon.StepMountGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsPath: b.config.GuestAdditionsPath,
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
|
|
|
|
|
|
|
&hypervcommon.StepMountSecondaryDvdImages{
|
|
|
|
IsoPaths: b.config.SecondaryDvdImages,
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
|
|
|
|
|
|
|
&hypervcommon.StepConfigureVlan{
|
|
|
|
VlanId: b.config.VlanId,
|
|
|
|
SwitchVlanId: b.config.SwitchVlanId,
|
|
|
|
},
|
|
|
|
|
2020-04-30 07:31:41 -04:00
|
|
|
&hypervcommon.StepSetBootOrder{
|
|
|
|
BootOrder: b.config.BootOrder,
|
|
|
|
},
|
2020-02-21 02:01:09 -05:00
|
|
|
&hypervcommon.StepSetFirstBootDevice{
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
FirstBootDevice: b.config.FirstBootDevice,
|
|
|
|
},
|
|
|
|
|
2018-05-05 17:54:58 -04:00
|
|
|
&hypervcommon.StepRun{
|
2019-08-10 15:03:08 -04:00
|
|
|
Headless: b.config.Headless,
|
|
|
|
SwitchName: b.config.SwitchName,
|
2018-05-05 17:54:58 -04:00
|
|
|
},
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
&hypervcommon.StepTypeBootCommand{
|
2018-08-22 14:25:43 -04:00
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
|
|
|
BootWait: b.config.BootWait,
|
|
|
|
SwitchName: b.config.SwitchName,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
GroupInterval: b.config.BootConfig.BootGroupInterval,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// configure the communicator ssh, winrm
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.SSHConfig.Comm,
|
2018-12-27 03:33:58 -05:00
|
|
|
Host: hypervcommon.CommHost(b.config.SSHConfig.Comm.SSHHost),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.SSHConfig.Comm.SSHConfigFunc(),
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// provision requires communicator to be setup
|
|
|
|
&common.StepProvision{},
|
|
|
|
|
2018-09-14 14:03:23 -04:00
|
|
|
// Remove ephemeral SSH keys, if using
|
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.SSHConfig.Comm,
|
|
|
|
},
|
|
|
|
|
2017-03-12 07:31:31 -04:00
|
|
|
&hypervcommon.StepShutdown{
|
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
|
|
|
},
|
|
|
|
|
|
|
|
// wait for the vm to be powered off
|
|
|
|
&hypervcommon.StepWaitForPowerOff{},
|
|
|
|
|
|
|
|
// remove the secondary dvd images
|
|
|
|
// after we power down
|
|
|
|
&hypervcommon.StepUnmountSecondaryDvdImages{},
|
|
|
|
&hypervcommon.StepUnmountGuestAdditions{},
|
|
|
|
&hypervcommon.StepUnmountDvdDrive{},
|
|
|
|
&hypervcommon.StepUnmountFloppyDrive{
|
|
|
|
Generation: b.config.Generation,
|
|
|
|
},
|
2018-07-07 07:42:22 -04:00
|
|
|
&hypervcommon.StepCompactDisk{
|
2017-03-12 07:31:31 -04:00
|
|
|
SkipCompaction: b.config.SkipCompaction,
|
2018-07-07 07:42:22 -04:00
|
|
|
},
|
2017-03-12 07:31:31 -04:00
|
|
|
&hypervcommon.StepExportVm{
|
2018-07-07 07:27:09 -04:00
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
SkipExport: b.config.SkipExport,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
2018-07-08 09:12:32 -04:00
|
|
|
&hypervcommon.StepCollateArtifacts{
|
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
SkipExport: b.config.SkipExport,
|
2017-03-12 07:31:31 -04:00
|
|
|
},
|
2020-01-07 13:45:24 -05:00
|
|
|
}
|
2017-03-12 07:31:31 -04:00
|
|
|
|
2020-01-07 13:45:24 -05:00
|
|
|
// the clean up actions for each step will be executed reverse order
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
// Run the steps.
|
2017-09-11 13:38:47 -04:00
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2017-03-12 07:31:31 -04:00
|
|
|
|
|
|
|
// Report any errors.
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
|
|
|
|
return hypervcommon.NewArtifact(b.config.OutputDir, generatedData)
|
2017-03-12 07:31:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel.
|