2015-06-21 07:36:07 -04:00
|
|
|
// Copyright (c) Microsoft Open Technologies, Inc.
|
|
|
|
// All Rights Reserved.
|
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
// See License.txt in the project root for license information.
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-07 07:26:27 -04:00
|
|
|
|
2015-06-21 07:36:07 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
RamSizeMB uint
|
|
|
|
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)
|
2015-06-21 07:36:07 -04:00
|
|
|
|
|
|
|
// convert the MB to bytes
|
2015-10-30 04:23:30 -04:00
|
|
|
ram := int64(s.RamSizeMB * 1024 * 1024)
|
|
|
|
diskSize := int64(s.DiskSize * 1024 * 1024)
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2016-08-07 07:26:27 -04:00
|
|
|
err := driver.CreateVirtualMachine(s.VMName, path, ram, 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 {
|
|
|
|
err := fmt.Errorf("Error creating setting virtual machine cpu: %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
|
|
|
if s.EnableDynamicMemory {
|
|
|
|
err = driver.SetVirtualMachineDynamicMemory(s.VMName, s.EnableDynamicMemory)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating setting virtual machine dynamic memory: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.EnableMacSpoofing {
|
|
|
|
err = driver.SetVirtualMachineMacSpoofing(s.VMName, s.EnableMacSpoofing)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating setting virtual machine mac spoofing: %s", err)
|
|
|
|
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 {
|
|
|
|
err := fmt.Errorf("Error creating setting virtual machine virtualization extensions: %s", err)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|