github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/hyperv/common/step_create_vm.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/packer"
     7  	"github.com/mitchellh/multistep"
     8  )
     9  
    10  // This step creates the actual virtual machine.
    11  //
    12  // Produces:
    13  //   VMName string - The name of the VM
    14  type StepCreateVM struct {
    15  	VMName                         string
    16  	SwitchName                     string
    17  	RamSize                        uint
    18  	DiskSize                       uint
    19  	Generation                     uint
    20  	Cpu                            uint
    21  	EnableMacSpoofing              bool
    22  	EnableDynamicMemory            bool
    23  	EnableSecureBoot               bool
    24  	EnableVirtualizationExtensions bool
    25  }
    26  
    27  func (s *StepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
    28  	driver := state.Get("driver").(Driver)
    29  	ui := state.Get("ui").(packer.Ui)
    30  	ui.Say("Creating virtual machine...")
    31  
    32  	path := state.Get("packerTempDir").(string)
    33  	vhdPath := state.Get("packerVhdTempDir").(string)
    34  
    35  	// convert the MB to bytes
    36  	ramSize := int64(s.RamSize * 1024 * 1024)
    37  	diskSize := int64(s.DiskSize * 1024 * 1024)
    38  
    39  	err := driver.CreateVirtualMachine(s.VMName, path, vhdPath, ramSize, diskSize, s.SwitchName, s.Generation)
    40  	if err != nil {
    41  		err := fmt.Errorf("Error creating virtual machine: %s", err)
    42  		state.Put("error", err)
    43  		ui.Error(err.Error())
    44  		return multistep.ActionHalt
    45  	}
    46  
    47  	err = driver.SetVirtualMachineCpuCount(s.VMName, s.Cpu)
    48  	if err != nil {
    49  		err := fmt.Errorf("Error setting virtual machine cpu count: %s", err)
    50  		state.Put("error", err)
    51  		ui.Error(err.Error())
    52  		return multistep.ActionHalt
    53  	}
    54  
    55  	err = driver.SetVirtualMachineDynamicMemory(s.VMName, s.EnableDynamicMemory)
    56  	if err != nil {
    57  		err := fmt.Errorf("Error setting virtual machine dynamic memory: %s", err)
    58  		state.Put("error", err)
    59  		ui.Error(err.Error())
    60  		return multistep.ActionHalt
    61  	}
    62  
    63  	if s.EnableMacSpoofing {
    64  		err = driver.SetVirtualMachineMacSpoofing(s.VMName, s.EnableMacSpoofing)
    65  		if err != nil {
    66  			err := fmt.Errorf("Error setting virtual machine mac spoofing: %s", err)
    67  			state.Put("error", err)
    68  			ui.Error(err.Error())
    69  			return multistep.ActionHalt
    70  		}
    71  	}
    72  
    73  	if s.Generation == 2 {
    74  		err = driver.SetVirtualMachineSecureBoot(s.VMName, s.EnableSecureBoot)
    75  		if err != nil {
    76  			err := fmt.Errorf("Error setting secure boot: %s", err)
    77  			state.Put("error", err)
    78  			ui.Error(err.Error())
    79  			return multistep.ActionHalt
    80  		}
    81  	}
    82  
    83  	if s.EnableVirtualizationExtensions {
    84  		//This is only supported on Windows 10 and Windows Server 2016 onwards
    85  		err = driver.SetVirtualMachineVirtualizationExtensions(s.VMName, s.EnableVirtualizationExtensions)
    86  		if err != nil {
    87  			err := fmt.Errorf("Error setting virtual machine virtualization extensions: %s", err)
    88  			state.Put("error", err)
    89  			ui.Error(err.Error())
    90  			return multistep.ActionHalt
    91  		}
    92  	}
    93  
    94  	// Set the final name in the state bag so others can use it
    95  	state.Put("vmName", s.VMName)
    96  
    97  	return multistep.ActionContinue
    98  }
    99  
   100  func (s *StepCreateVM) Cleanup(state multistep.StateBag) {
   101  	if s.VMName == "" {
   102  		return
   103  	}
   104  
   105  	driver := state.Get("driver").(Driver)
   106  	ui := state.Get("ui").(packer.Ui)
   107  	ui.Say("Unregistering and deleting virtual machine...")
   108  
   109  	err := driver.DeleteVirtualMachine(s.VMName)
   110  	if err != nil {
   111  		ui.Error(fmt.Sprintf("Error deleting virtual machine: %s", err))
   112  	}
   113  }