2015-06-21 07:36:07 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-21 12:29:26 -04:00
|
|
|
"log"
|
|
|
|
"path/filepath"
|
2017-05-29 20:16:03 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-21 07:36:07 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates the actual virtual machine.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// VMName string - The name of the VM
|
|
|
|
type StepCreateVM struct {
|
2016-08-07 07:26:27 -04:00
|
|
|
VMName string
|
|
|
|
SwitchName string
|
2017-05-21 12:29:26 -04:00
|
|
|
HarddrivePath string
|
2016-11-06 09:07:46 -05:00
|
|
|
RamSize uint
|
2016-08-07 07:26:27 -04:00
|
|
|
DiskSize uint
|
|
|
|
Generation uint
|
|
|
|
Cpu uint
|
|
|
|
EnableMacSpoofing bool
|
|
|
|
EnableDynamicMemory bool
|
|
|
|
EnableSecureBoot bool
|
2016-08-06 02:09:33 -04:00
|
|
|
EnableVirtualizationExtensions bool
|
2015-06-21 07:36:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say("Creating virtual machine...")
|
|
|
|
|
2015-06-21 14:35:32 -04:00
|
|
|
path := state.Get("packerTempDir").(string)
|
2017-05-29 20:16:03 -04:00
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
// Determine if we even have an existing virtual harddrive to attach
|
|
|
|
harddrivePath := ""
|
|
|
|
if harddrivePathRaw, ok := state.GetOk("iso_path"); ok {
|
|
|
|
extension := strings.ToLower(filepath.Ext(harddrivePathRaw.(string)))
|
2017-05-28 21:36:01 -04:00
|
|
|
if extension == ".vhd" || extension == ".vhdx" {
|
2017-05-21 12:29:26 -04:00
|
|
|
harddrivePath = harddrivePathRaw.(string)
|
|
|
|
} else {
|
|
|
|
log.Println("No existing virtual harddrive, not attaching.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Println("No existing virtual harddrive, not attaching.")
|
|
|
|
}
|
2017-10-11 12:40:39 -04:00
|
|
|
|
|
|
|
vhdPath := state.Get("packerVhdTempDir").(string)
|
2015-06-21 07:36:07 -04:00
|
|
|
// convert the MB to bytes
|
2016-11-06 09:07:46 -05:00
|
|
|
ramSize := int64(s.RamSize * 1024 * 1024)
|
2015-10-30 04:23:30 -04:00
|
|
|
diskSize := int64(s.DiskSize * 1024 * 1024)
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2017-05-21 12:29:26 -04:00
|
|
|
err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, vhdPath, ramSize, diskSize, s.SwitchName, s.Generation)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating virtual machine: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-10-30 04:23:30 -04:00
|
|
|
|
2016-08-07 07:26:27 -04:00
|
|
|
err = driver.SetVirtualMachineCpuCount(s.VMName, s.Cpu)
|
2015-06-21 14:35:32 -04:00
|
|
|
if err != nil {
|
2017-09-20 08:53:37 -04:00
|
|
|
err := fmt.Errorf("Error setting virtual machine cpu count: %s", err)
|
2015-06-21 14:35:32 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-10-30 04:23:30 -04:00
|
|
|
|
2017-09-20 08:53:37 -04:00
|
|
|
err = driver.SetVirtualMachineDynamicMemory(s.VMName, s.EnableDynamicMemory)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error setting virtual machine dynamic memory: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2016-08-07 07:26:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.EnableMacSpoofing {
|
|
|
|
err = driver.SetVirtualMachineMacSpoofing(s.VMName, s.EnableMacSpoofing)
|
|
|
|
if err != nil {
|
2017-09-20 08:53:37 -04:00
|
|
|
err := fmt.Errorf("Error setting virtual machine mac spoofing: %s", err)
|
2016-08-07 07:26:27 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
if s.Generation == 2 {
|
2016-08-07 07:26:27 -04:00
|
|
|
err = driver.SetVirtualMachineSecureBoot(s.VMName, s.EnableSecureBoot)
|
2015-06-29 16:18:25 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error setting secure boot: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
2015-06-21 14:35:32 -04:00
|
|
|
|
2016-08-07 07:26:27 -04:00
|
|
|
if s.EnableVirtualizationExtensions {
|
|
|
|
//This is only supported on Windows 10 and Windows Server 2016 onwards
|
|
|
|
err = driver.SetVirtualMachineVirtualizationExtensions(s.VMName, s.EnableVirtualizationExtensions)
|
|
|
|
if err != nil {
|
2017-09-20 08:53:37 -04:00
|
|
|
err := fmt.Errorf("Error setting virtual machine virtualization extensions: %s", err)
|
2016-08-07 07:26:27 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
// Set the final name in the state bag so others can use it
|
|
|
|
state.Put("vmName", s.VMName)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateVM) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.VMName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2015-06-21 07:36:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say("Unregistering and deleting virtual machine...")
|
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
err := driver.DeleteVirtualMachine(s.VMName)
|
2015-06-21 07:36:07 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting virtual machine: %s", err))
|
|
|
|
}
|
|
|
|
}
|