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
|
2015-06-29 16:18:25 -04:00
|
|
|
EnabeSecureBoot 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
|
2015-06-29 16:18:25 -04:00
|
|
|
enabeSecureBoot := s.EnabeSecureBoot
|
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
|
|
|
|
|
|
|
err = driver.SetVirtualMachineCpu(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
|
|
|
|
|
|
|
if s.Generation == 2 {
|
|
|
|
err = driver.SetSecureBoot(s.VMName, enabeSecureBoot)
|
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))
|
|
|
|
}
|
|
|
|
}
|