2017-08-23 15:40:57 -04:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2017-11-14 16:19:32 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-08-23 15:40:57 -04:00
|
|
|
"github.com/vmware/govmomi/object"
|
|
|
|
"github.com/vmware/govmomi/vim25/mo"
|
2017-08-23 20:06:50 -04:00
|
|
|
"github.com/vmware/govmomi/vim25/types"
|
|
|
|
"time"
|
2018-02-01 06:47:09 -05:00
|
|
|
"strings"
|
2018-05-16 09:05:08 -04:00
|
|
|
"context"
|
2017-08-23 15:40:57 -04:00
|
|
|
)
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
type VirtualMachine struct {
|
|
|
|
vm *object.VirtualMachine
|
|
|
|
driver *Driver
|
2017-08-23 15:40:57 -04:00
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
type CloneConfig struct {
|
|
|
|
Name string
|
|
|
|
Folder string
|
2018-05-05 17:41:14 -04:00
|
|
|
Cluster string
|
2017-08-23 20:06:50 -04:00
|
|
|
Host string
|
|
|
|
ResourcePool string
|
|
|
|
Datastore string
|
|
|
|
LinkedClone bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type HardwareConfig struct {
|
2018-03-23 04:56:03 -04:00
|
|
|
CPUs int32
|
|
|
|
CPUReservation int64
|
|
|
|
CPULimit int64
|
|
|
|
RAM int64
|
|
|
|
RAMReservation int64
|
|
|
|
RAMReserveAll bool
|
|
|
|
NestedHV bool
|
|
|
|
CpuHotAddEnabled bool
|
|
|
|
MemoryHotAddEnabled bool
|
2017-08-23 20:06:50 -04:00
|
|
|
}
|
|
|
|
|
2018-01-24 08:35:04 -05:00
|
|
|
type CreateConfig struct {
|
|
|
|
DiskThinProvisioned bool
|
|
|
|
DiskControllerType string // example: "scsi", "pvscsi"
|
2018-05-05 17:41:14 -04:00
|
|
|
DiskSize int64
|
2018-01-24 08:35:04 -05:00
|
|
|
|
2018-02-26 19:22:52 -05:00
|
|
|
Annotation string
|
|
|
|
Name string
|
|
|
|
Folder string
|
2018-05-05 17:41:14 -04:00
|
|
|
Cluster string
|
2018-02-26 19:22:52 -05:00
|
|
|
Host string
|
|
|
|
ResourcePool string
|
|
|
|
Datastore string
|
|
|
|
GuestOS string // example: otherGuest
|
|
|
|
Network string // "" for default network
|
|
|
|
NetworkCard string // example: vmxnet3
|
|
|
|
USBController bool
|
2018-05-06 17:26:04 -04:00
|
|
|
Version uint // example: 10
|
2018-10-16 19:42:02 -04:00
|
|
|
Firmware string // efi or bios
|
2018-01-24 08:35:04 -05:00
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
func (d *Driver) NewVM(ref *types.ManagedObjectReference) *VirtualMachine {
|
|
|
|
return &VirtualMachine{
|
|
|
|
vm: object.NewVirtualMachine(d.client.Client, *ref),
|
|
|
|
driver: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) FindVM(name string) (*VirtualMachine, error) {
|
|
|
|
vm, err := d.finder.VirtualMachine(d.ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &VirtualMachine{
|
2017-08-24 16:18:14 -04:00
|
|
|
vm: vm,
|
2017-08-23 20:06:50 -04:00
|
|
|
driver: d,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-24 08:35:04 -05:00
|
|
|
func (d *Driver) CreateVM(config *CreateConfig) (*VirtualMachine, error) {
|
2018-05-05 17:41:14 -04:00
|
|
|
createSpec := types.VirtualMachineConfigSpec{
|
|
|
|
Name: config.Name,
|
|
|
|
Annotation: config.Annotation,
|
|
|
|
GuestId: config.GuestOS,
|
2018-10-16 19:42:02 -04:00
|
|
|
Firmware: config.Firmware,
|
2018-05-05 17:41:14 -04:00
|
|
|
}
|
|
|
|
if config.Version != 0 {
|
|
|
|
createSpec.Version = fmt.Sprintf("%s%d", "vmx-", config.Version)
|
|
|
|
}
|
2018-01-24 08:35:04 -05:00
|
|
|
|
|
|
|
folder, err := d.FindFolder(config.Folder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:53:25 -04:00
|
|
|
resourcePool, err := d.FindResourcePool(config.Cluster, config.Host, config.ResourcePool)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:53:25 -04:00
|
|
|
var host *object.HostSystem
|
|
|
|
if config.Host != "" {
|
|
|
|
h, err := d.FindHost(config.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
host = h.host
|
2018-01-24 08:35:04 -05:00
|
|
|
}
|
|
|
|
|
2018-03-20 19:03:47 -04:00
|
|
|
datastore, err := d.FindDatastore(config.Datastore, config.Host)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
devices := object.VirtualDeviceList{}
|
|
|
|
|
|
|
|
devices, err = addIDE(devices)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
devices, err = addDisk(d, devices, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-30 15:15:30 -05:00
|
|
|
devices, err = addNetwork(d, devices, config)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-26 19:22:52 -05:00
|
|
|
if config.USBController {
|
|
|
|
t := true
|
|
|
|
usb := &types.VirtualUSBController{
|
|
|
|
EhciEnabled: &t,
|
|
|
|
}
|
|
|
|
devices = append(devices, usb)
|
|
|
|
}
|
|
|
|
|
2018-01-24 08:35:04 -05:00
|
|
|
createSpec.DeviceChange, err = devices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
createSpec.Files = &types.VirtualMachineFileInfo{
|
|
|
|
VmPathName: fmt.Sprintf("[%s]", datastore.Name()),
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:53:25 -04:00
|
|
|
task, err := folder.folder.CreateVM(d.ctx, createSpec, resourcePool.pool, host)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
taskInfo, err := task.WaitForResult(d.ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vmRef := taskInfo.Result.(types.ManagedObjectReference)
|
|
|
|
|
|
|
|
return d.NewVM(&vmRef), nil
|
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
func (vm *VirtualMachine) Info(params ...string) (*mo.VirtualMachine, error) {
|
2017-08-23 15:40:57 -04:00
|
|
|
var p []string
|
|
|
|
if len(params) == 0 {
|
|
|
|
p = []string{"*"}
|
|
|
|
} else {
|
|
|
|
p = params
|
|
|
|
}
|
2017-08-23 20:06:50 -04:00
|
|
|
var info mo.VirtualMachine
|
|
|
|
err := vm.vm.Properties(vm.driver.ctx, vm.vm.Reference(), p, &info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &info, nil
|
|
|
|
}
|
|
|
|
|
2018-01-31 06:31:11 -05:00
|
|
|
func (vm *VirtualMachine) Devices() (object.VirtualDeviceList, error) {
|
|
|
|
vmInfo, err := vm.Info("config.hardware.device")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return vmInfo.Config.Hardware.Device, nil
|
|
|
|
}
|
|
|
|
|
2018-05-16 09:05:08 -04:00
|
|
|
func (template *VirtualMachine) Clone(ctx context.Context, config *CloneConfig) (*VirtualMachine, error) {
|
2017-08-23 20:06:50 -04:00
|
|
|
folder, err := template.driver.FindFolder(config.Folder)
|
2017-08-23 15:40:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-23 20:06:50 -04:00
|
|
|
|
|
|
|
var relocateSpec types.VirtualMachineRelocateSpec
|
|
|
|
|
2018-03-15 17:53:25 -04:00
|
|
|
pool, err := template.driver.FindResourcePool(config.Cluster, config.Host, config.ResourcePool)
|
2017-08-23 20:06:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
poolRef := pool.pool.Reference()
|
|
|
|
relocateSpec.Pool = &poolRef
|
|
|
|
|
2018-03-20 19:03:47 -04:00
|
|
|
datastore, err := template.driver.FindDatastore(config.Datastore, config.Host)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
2017-11-14 16:19:32 -05:00
|
|
|
return nil, err
|
2017-08-23 20:06:50 -04:00
|
|
|
}
|
2018-01-24 08:35:04 -05:00
|
|
|
datastoreRef := datastore.ds.Reference()
|
|
|
|
relocateSpec.Datastore = &datastoreRef
|
2017-08-23 20:06:50 -04:00
|
|
|
|
|
|
|
var cloneSpec types.VirtualMachineCloneSpec
|
|
|
|
cloneSpec.Location = relocateSpec
|
|
|
|
cloneSpec.PowerOn = false
|
|
|
|
|
|
|
|
if config.LinkedClone == true {
|
|
|
|
cloneSpec.Location.DiskMoveType = "createNewChildDiskBacking"
|
|
|
|
|
|
|
|
tpl, err := template.Info("snapshot")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tpl.Snapshot == nil {
|
|
|
|
err = errors.New("`linked_clone=true`, but template has no snapshots")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cloneSpec.Snapshot = tpl.Snapshot.CurrentSnapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
task, err := template.vm.Clone(template.driver.ctx, folder.folder, config.Name, cloneSpec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-16 09:05:08 -04:00
|
|
|
info, err := task.WaitForResult(ctx, nil)
|
2017-08-23 20:06:50 -04:00
|
|
|
if err != nil {
|
2018-05-16 09:05:08 -04:00
|
|
|
if ctx.Err() == context.Canceled {
|
|
|
|
err = task.Cancel(context.TODO())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-24 08:35:04 -05:00
|
|
|
vmRef := info.Result.(types.ManagedObjectReference)
|
|
|
|
vm := template.driver.NewVM(&vmRef)
|
2017-08-23 20:06:50 -04:00
|
|
|
return vm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) Destroy() error {
|
|
|
|
task, err := vm.vm.Destroy(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = task.WaitForResult(vm.driver.ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) Configure(config *HardwareConfig) error {
|
2018-05-05 17:41:14 -04:00
|
|
|
var confSpec types.VirtualMachineConfigSpec
|
|
|
|
confSpec.NumCPUs = config.CPUs
|
|
|
|
confSpec.MemoryMB = config.RAM
|
2017-08-23 20:06:50 -04:00
|
|
|
|
2018-05-05 17:41:14 -04:00
|
|
|
var cpuSpec types.ResourceAllocationInfo
|
2018-05-12 07:19:17 -04:00
|
|
|
cpuSpec.Reservation = &config.CPUReservation
|
2018-10-16 11:59:51 -04:00
|
|
|
if config.CPULimit != 0 {
|
|
|
|
cpuSpec.Limit = &config.CPULimit
|
|
|
|
}
|
2018-05-05 17:41:14 -04:00
|
|
|
confSpec.CpuAllocation = &cpuSpec
|
2017-12-01 06:09:47 -05:00
|
|
|
|
2018-05-05 17:41:14 -04:00
|
|
|
var ramSpec types.ResourceAllocationInfo
|
2018-05-12 07:19:17 -04:00
|
|
|
ramSpec.Reservation = &config.RAMReservation
|
2018-05-05 17:41:14 -04:00
|
|
|
confSpec.MemoryAllocation = &ramSpec
|
2017-12-01 06:09:47 -05:00
|
|
|
|
2018-05-05 17:41:14 -04:00
|
|
|
confSpec.MemoryReservationLockedToMax = &config.RAMReserveAll
|
|
|
|
confSpec.NestedHVEnabled = &config.NestedHV
|
2017-12-01 06:09:47 -05:00
|
|
|
|
2018-05-05 17:41:14 -04:00
|
|
|
confSpec.CpuHotAddEnabled = &config.CpuHotAddEnabled
|
|
|
|
confSpec.MemoryHotAddEnabled = &config.MemoryHotAddEnabled
|
|
|
|
|
|
|
|
task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = task.WaitForResult(vm.driver.ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) ResizeDisk(diskSize int64) error {
|
|
|
|
var confSpec types.VirtualMachineConfigSpec
|
|
|
|
|
|
|
|
devices, err := vm.vm.Device(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
disk, err := findDisk(devices)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
disk.CapacityInKB = diskSize * 1024
|
|
|
|
|
|
|
|
confSpec.DeviceChange = []types.BaseVirtualDeviceConfigSpec{
|
|
|
|
&types.VirtualDeviceConfigSpec{
|
|
|
|
Device: disk,
|
|
|
|
Operation: types.VirtualDeviceConfigSpecOperationEdit,
|
|
|
|
},
|
2017-12-01 06:09:47 -05:00
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-24 08:35:04 -05:00
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
_, err = task.WaitForResult(vm.driver.ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-01 06:09:47 -05:00
|
|
|
func findDisk(devices object.VirtualDeviceList) (*types.VirtualDisk, error) {
|
|
|
|
var disks []*types.VirtualDisk
|
|
|
|
for _, device := range devices {
|
|
|
|
switch d := device.(type) {
|
|
|
|
case *types.VirtualDisk:
|
|
|
|
disks = append(disks, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(disks) {
|
|
|
|
case 0:
|
|
|
|
return nil, errors.New("VM has no disks")
|
|
|
|
case 1:
|
|
|
|
return disks[0], nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("VM has multiple disks")
|
|
|
|
}
|
|
|
|
|
2017-08-23 20:06:50 -04:00
|
|
|
func (vm *VirtualMachine) PowerOn() error {
|
|
|
|
task, err := vm.vm.PowerOn(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = task.WaitForResult(vm.driver.ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-16 09:05:08 -04:00
|
|
|
func (vm *VirtualMachine) WaitForIP(ctx context.Context) (string, error) {
|
|
|
|
return vm.vm.WaitForIP(ctx)
|
2017-08-23 20:06:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) PowerOff() error {
|
|
|
|
state, err := vm.vm.PowerState(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if state == types.VirtualMachinePowerStatePoweredOff {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
task, err := vm.vm.PowerOff(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = task.WaitForResult(vm.driver.ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) StartShutdown() error {
|
|
|
|
err := vm.vm.ShutdownGuest(vm.driver.ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-16 09:05:08 -04:00
|
|
|
func (vm *VirtualMachine) WaitForShutdown(ctx context.Context, timeout time.Duration) error {
|
2017-08-23 20:06:50 -04:00
|
|
|
shutdownTimer := time.After(timeout)
|
|
|
|
for {
|
|
|
|
powerState, err := vm.vm.PowerState(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if powerState == "poweredOff" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-shutdownTimer:
|
|
|
|
err := errors.New("Timeout while waiting for machine to shut down.")
|
|
|
|
return err
|
2018-05-16 09:05:08 -04:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
2017-08-23 20:06:50 -04:00
|
|
|
default:
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) CreateSnapshot(name string) error {
|
|
|
|
task, err := vm.vm.CreateSnapshot(vm.driver.ctx, name, "", false, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = task.WaitForResult(vm.driver.ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) ConvertToTemplate() error {
|
2017-10-25 16:20:59 -04:00
|
|
|
return vm.vm.MarkAsTemplate(vm.driver.ctx)
|
2017-08-23 15:40:57 -04:00
|
|
|
}
|
2018-01-24 08:35:04 -05:00
|
|
|
|
2018-02-01 06:47:09 -05:00
|
|
|
func (vm *VirtualMachine) GetDir() (string, error) {
|
|
|
|
vmInfo, err := vm.Info("name", "layoutEx.file")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxName := fmt.Sprintf("/%s.vmx", vmInfo.Name)
|
|
|
|
for _, file := range vmInfo.LayoutEx.File {
|
|
|
|
if strings.HasSuffix(file.Name, vmxName) {
|
2018-02-01 07:48:30 -05:00
|
|
|
return RemoveDatastorePrefix(file.Name[:len(file.Name)-len(vmxName)]), nil
|
2018-02-01 06:47:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("cannot find '%s'", vmxName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addDisk(_ *Driver, devices object.VirtualDeviceList, config *CreateConfig) (object.VirtualDeviceList, error) {
|
2018-01-24 08:35:04 -05:00
|
|
|
device, err := devices.CreateSCSIController(config.DiskControllerType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
devices = append(devices, device)
|
|
|
|
controller, err := devices.FindDiskController(devices.Name(device))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
disk := &types.VirtualDisk{
|
|
|
|
VirtualDevice: types.VirtualDevice{
|
|
|
|
Key: devices.NewKey(),
|
|
|
|
Backing: &types.VirtualDiskFlatVer2BackingInfo{
|
|
|
|
DiskMode: string(types.VirtualDiskModePersistent),
|
|
|
|
ThinProvisioned: types.NewBool(config.DiskThinProvisioned),
|
|
|
|
},
|
|
|
|
},
|
2018-05-02 22:45:41 -04:00
|
|
|
CapacityInKB: config.DiskSize * 1024,
|
2018-01-24 08:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
devices.AssignController(disk, controller)
|
|
|
|
devices = append(devices, disk)
|
|
|
|
|
|
|
|
return devices, nil
|
|
|
|
}
|
|
|
|
|
2018-01-30 15:15:30 -05:00
|
|
|
func addNetwork(d *Driver, devices object.VirtualDeviceList, config *CreateConfig) (object.VirtualDeviceList, error) {
|
|
|
|
network, err := d.finder.NetworkOrDefault(d.ctx, config.Network)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backing, err := network.EthernetCardBackingInfo(d.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-31 06:31:11 -05:00
|
|
|
device, err := object.EthernetCardTypes().CreateEthernetCard(config.NetworkCard, backing)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(devices, device), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addIDE(devices object.VirtualDeviceList) (object.VirtualDeviceList, error) {
|
|
|
|
ideDevice, err := devices.CreateIDEController()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
devices = append(devices, ideDevice)
|
|
|
|
|
|
|
|
return devices, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) AddCdrom(isoPath string) error {
|
|
|
|
devices, err := vm.vm.Device(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-26 16:17:48 -05:00
|
|
|
sata, err := vm.FindSATAController()
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-26 16:17:48 -05:00
|
|
|
cdrom, err := vm.CreateCdrom(sata)
|
2018-01-24 08:35:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isoPath != "" {
|
|
|
|
cdrom = devices.InsertIso(cdrom, isoPath)
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:45:56 -05:00
|
|
|
return vm.addDevice(cdrom)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *VirtualMachine) AddFloppy(imgPath string) error {
|
|
|
|
devices, err := vm.vm.Device(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
floppy, err := devices.CreateFloppy()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if imgPath != "" {
|
|
|
|
floppy = devices.InsertImg(floppy, imgPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm.addDevice(floppy)
|
|
|
|
}
|
|
|
|
|
2018-01-30 12:25:05 -05:00
|
|
|
func (vm *VirtualMachine) SetBootOrder(order []string) error {
|
|
|
|
devices, err := vm.vm.Device(vm.driver.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bootOptions := types.VirtualMachineBootOptions{
|
|
|
|
BootOrder: devices.BootOrder(order),
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm.vm.SetBootOptions(vm.driver.ctx, &bootOptions)
|
|
|
|
}
|
|
|
|
|
2018-02-02 13:04:32 -05:00
|
|
|
func (vm *VirtualMachine) RemoveDevice(keepFiles bool, device ...types.BaseVirtualDevice) error {
|
|
|
|
return vm.vm.RemoveDevice(vm.driver.ctx, keepFiles, device...)
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:45:56 -05:00
|
|
|
func (vm *VirtualMachine) addDevice(device types.BaseVirtualDevice) error {
|
|
|
|
newDevices := object.VirtualDeviceList{device}
|
2018-01-24 08:35:04 -05:00
|
|
|
confSpec := types.VirtualMachineConfigSpec{}
|
2018-01-30 11:45:56 -05:00
|
|
|
var err error
|
2018-01-24 08:35:04 -05:00
|
|
|
confSpec.DeviceChange, err = newDevices.ConfigSpec(types.VirtualDeviceConfigSpecOperationAdd)
|
2018-01-30 11:45:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-24 08:35:04 -05:00
|
|
|
|
|
|
|
task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = task.WaitForResult(vm.driver.ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-26 16:18:06 -05:00
|
|
|
func (vm *VirtualMachine) AddConfigParams(params map[string]string) error {
|
|
|
|
var confSpec types.VirtualMachineConfigSpec
|
|
|
|
|
|
|
|
var ov []types.BaseOptionValue
|
|
|
|
for k, v := range params {
|
|
|
|
o := types.OptionValue{
|
|
|
|
Key: k,
|
|
|
|
Value: v,
|
|
|
|
}
|
|
|
|
ov = append(ov, &o)
|
|
|
|
}
|
|
|
|
confSpec.ExtraConfig = ov
|
|
|
|
|
|
|
|
task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = task.WaitForResult(vm.driver.ctx, nil)
|
|
|
|
return err
|
|
|
|
}
|