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"
|
|
|
|
"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 {
|
2015-10-30 04:23:30 -04:00
|
|
|
VMName string
|
|
|
|
SwitchName string
|
|
|
|
RamSizeMB uint
|
|
|
|
DiskSize uint
|
|
|
|
Generation uint
|
|
|
|
Cpu uint
|
2016-08-05 21:03:30 -04:00
|
|
|
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
|
|
|
|
|
|
|
switchName := s.SwitchName
|
2016-08-05 21:03:30 -04:00
|
|
|
enableSecureBoot := s.EnableSecureBoot
|
2015-06-21 07:36:07 -04:00
|
|
|
|
2015-10-30 04:23:30 -04:00
|
|
|
err := driver.CreateVirtualMachine(s.VMName, path, ram, diskSize, 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-06 02:09:33 -04:00
|
|
|
err = driver.SetVirtualMachineCpu(s.VMName, s.Cpu, s.EnableVirtualizationExtensions)
|
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
|
|
|
|
|
|
|
if s.Generation == 2 {
|
2016-08-05 21:03:30 -04:00
|
|
|
err = driver.SetSecureBoot(s.VMName, 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
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|