Skip to content

Commit

Permalink
Merge pull request #50 from madhanrm/vhdcleanup
Browse files Browse the repository at this point in the history
Update to use timeoutSeconds properly
  • Loading branch information
madhanrm authored Apr 9, 2021
2 parents c55b969 + fc43bb2 commit c835dc3
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 110 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.13
require (
github.com/go-ole/go-ole v1.2.4
github.com/google/uuid v1.1.1
github.com/nwoodmsft/iso9660 v0.0.0-20191211094520-15de0aa41e99 // indirect
github.com/nwoodmsft/iso9660 v0.0.0-20191211094520-15de0aa41e99
github.com/pkg/errors v0.9.1
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5
)
12 changes: 8 additions & 4 deletions pkg/virtualization/core/job/virtualsystemjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (vmjob *VirtualSystemJob) JobType() (value v2.ConcreteJob_JobType, err erro
}

// WaitForPercentComplete waits for the percentComplete or timeout
func (vmjob *VirtualSystemJob) WaitForPercentComplete(percentComplete, timeoutSeconds uint16) error {
func (vmjob *VirtualSystemJob) WaitForPercentComplete(percentComplete uint16, timeoutSeconds int16) error {
start := time.Now()
// Run the loop, only if the job is actually running
for !vmjob.IsComplete() {
Expand All @@ -62,7 +62,11 @@ func (vmjob *VirtualSystemJob) WaitForPercentComplete(percentComplete, timeoutSe
if pComplete >= percentComplete {
break
}

time.Sleep(100 * time.Millisecond)
if timeoutSeconds < 0 {
continue
}
// If we have waited enough time, break
if time.Since(start) > (time.Duration(timeoutSeconds) * time.Second) {
state, err2 := vmjob.getPropertyJobStateFixed()
Expand All @@ -78,7 +82,7 @@ func (vmjob *VirtualSystemJob) WaitForPercentComplete(percentComplete, timeoutSe
}

// WaitForAction waits for the task based on the action type, percent complete and timeoutSeconds
func (vmjob *VirtualSystemJob) WaitForAction(action wmi.UserAction, percentComplete, timeoutSeconds uint16) error {
func (vmjob *VirtualSystemJob) WaitForAction(action wmi.UserAction, percentComplete uint16, timeoutSeconds int16) error {
switch action {
case wmi.Wait:
return vmjob.WaitForPercentComplete(percentComplete, timeoutSeconds)
Expand Down Expand Up @@ -182,7 +186,7 @@ func (vmjob *VirtualSystemJob) GetException() error {
return nil
}

func WaitForJobCompletionEx(result int32, currentJob *VirtualSystemJob, timeoutSeconds uint16) error {
func WaitForJobCompletionEx(result int32, currentJob *VirtualSystemJob, timeoutSeconds int16) error {
if result == 0 {
return nil
} else if result == 4096 {
Expand All @@ -197,7 +201,7 @@ func WaitForJobCompletionEx2(instance *wmi.WmiInstance, result int32, jobName st
return nil
}

func WaitForJobCompletion(instance *wmi.WmiInstance, result int32, jobType v2.ConcreteJob_JobType, timeoutSeconds uint16) error {
func WaitForJobCompletion(instance *wmi.WmiInstance, result int32, jobType v2.ConcreteJob_JobType, timeoutSeconds int16) error {
if result == 0 {
return nil
} else if result == 4096 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func getService(whost *host.WmiHost) (mgmt *ResourcePoolConfigurationService, er
func (rpcs *ResourcePoolConfigurationService) ModifyPoolResourcesEx(child resourcepool.ResourcePool,
parentPools resourcepool.ResourcePoolCollection,
rasds resourceallocation.ResourceAllocationSettingDataCollection,
timeoutSeconds uint16) error {
timeoutSeconds int16) error {
embeddedInstance, err := rasds.EmbeddedXMLInstances()
if err != nil {
return err
Expand All @@ -83,7 +83,7 @@ func (rpcs *ResourcePoolConfigurationService) ModifyPoolResourcesEx(child resour
return rpcs.WaitForJobCompletion(result, v2.ConcreteJob_JobType_Modify_Virtual_Machine_Resources, timeoutSeconds)
}

func (rpcs *ResourcePoolConfigurationService) WaitForJobCompletion(result int32, jobType v2.ConcreteJob_JobType, timeoutSeconds uint16) error {
func (rpcs *ResourcePoolConfigurationService) WaitForJobCompletion(result int32, jobType v2.ConcreteJob_JobType, timeoutSeconds int16) error {
if result == 0 {
return nil
} else if result == 4096 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/virtualization/core/security/service/securityservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func getService(whost *host.WmiHost) (mgmt *SecurityService, err error) {
}

// SetKey
func (ss *SecurityService) SetKey(settings *wmi.WmiInstance, timeoutSeconds uint16) (err error) {
func (ss *SecurityService) SetKey(settings *wmi.WmiInstance) (err error) {
method, err := ss.GetWmiMethod("SetKeyProtector")
if err != nil {
return
Expand Down Expand Up @@ -104,5 +104,5 @@ func (ss *SecurityService) SetKey(settings *wmi.WmiInstance, timeoutSeconds uint
return
}
defer job.Close()
return job.WaitForJobCompletion(result.ReturnValue, timeoutSeconds)
return job.WaitForJobCompletion(result.ReturnValue, -1)
}
22 changes: 11 additions & 11 deletions pkg/virtualization/core/service/dvddrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (
"github.com/microsoft/wmi/pkg/virtualization/core/virtualsystem"
)

func (vmms *VirtualSystemManagementService) AddISODisk(vm *virtualsystem.VirtualMachine, isoPath string, timeoutSeconds uint16) (
func (vmms *VirtualSystemManagementService) AddISODisk(vm *virtualsystem.VirtualMachine, isoPath string) (
ld *disk.LogicalDisk,
dvddrive *drive.DvdDrive,
err error) {

dvddrive, err = vmms.AddDvdDrive(vm, timeoutSeconds)
dvddrive, err = vmms.AddDvdDrive(vm)
if err != nil {
return
}

defer func() {
if err != nil {
log.Printf("[%+v]\n", err)
err1 := vmms.RemoveDvdDrive(dvddrive, timeoutSeconds)
err1 := vmms.RemoveDvdDrive(dvddrive)
log.Printf("RemoveDvdDrive [%+v]\n", err1)
dvddrive.Close()
dvddrive = nil
Expand Down Expand Up @@ -60,7 +60,7 @@ func (vmms *VirtualSystemManagementService) AddISODisk(vm *virtualsystem.Virtual
defer vmsetting.Close()

// apply the settings
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, tmpld.CIM_ResourceAllocationSettingData, timeoutSeconds)
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, tmpld.CIM_ResourceAllocationSettingData, -1)
if err != nil {
return
}
Expand All @@ -84,17 +84,17 @@ func (vmms *VirtualSystemManagementService) AddISODisk(vm *virtualsystem.Virtual

}

func (vmms *VirtualSystemManagementService) RemoveISODisk(ld *disk.LogicalDisk, timeoutSeconds uint16) (err error) {
func (vmms *VirtualSystemManagementService) RemoveISODisk(ld *disk.LogicalDisk) (err error) {
dvddrive, err := ld.GetDrive()
if err != nil {
return
}
defer dvddrive.Close()

// Remove Disk
err1 := vmms.RemoveVirtualSystemResource(ld.CIM_ResourceAllocationSettingData, timeoutSeconds)
err1 := vmms.RemoveVirtualSystemResource(ld.CIM_ResourceAllocationSettingData, -1)
// Remove Drive
err = vmms.RemoveVirtualSystemResource(dvddrive.CIM_ResourceAllocationSettingData, timeoutSeconds)
err = vmms.RemoveVirtualSystemResource(dvddrive.CIM_ResourceAllocationSettingData, -1)
if err != nil {
return
}
Expand All @@ -105,7 +105,7 @@ func (vmms *VirtualSystemManagementService) RemoveISODisk(ld *disk.LogicalDisk,
return
}

func (vmms *VirtualSystemManagementService) AddDvdDrive(vm *virtualsystem.VirtualMachine, timeoutSeconds uint16) (dvd *drive.DvdDrive, err error) {
func (vmms *VirtualSystemManagementService) AddDvdDrive(vm *virtualsystem.VirtualMachine) (dvd *drive.DvdDrive, err error) {
// Add the dvd drive
tmp, err := vm.NewDvdDrive()
if err != nil {
Expand All @@ -120,7 +120,7 @@ func (vmms *VirtualSystemManagementService) AddDvdDrive(vm *virtualsystem.Virtua
defer vmsetting.Close()

// apply the settings
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, tmp.CIM_ResourceAllocationSettingData, timeoutSeconds)
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, tmp.CIM_ResourceAllocationSettingData, -1)
if err != nil {
return
}
Expand All @@ -140,7 +140,7 @@ func (vmms *VirtualSystemManagementService) AddDvdDrive(vm *virtualsystem.Virtua
return
}

func (vmms *VirtualSystemManagementService) RemoveDvdDrive(dvd *drive.DvdDrive, timeoutSeconds uint16) (err error) {
err = vmms.RemoveVirtualSystemResource(dvd.CIM_ResourceAllocationSettingData, timeoutSeconds)
func (vmms *VirtualSystemManagementService) RemoveDvdDrive(dvd *drive.DvdDrive) (err error) {
err = vmms.RemoveVirtualSystemResource(dvd.CIM_ResourceAllocationSettingData, -1)
return
}
29 changes: 14 additions & 15 deletions pkg/virtualization/core/service/virtualharddisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/microsoft/wmi/pkg/virtualization/core/virtualsystem"
)

func (vmms *VirtualSystemManagementService) AddSCSIController(vm *virtualsystem.VirtualMachine, timeoutSeconds uint16) (err error) {
func (vmms *VirtualSystemManagementService) AddSCSIController(vm *virtualsystem.VirtualMachine) (err error) {
tmp, err := vm.NewSCSIController()
if err != nil {
return err
Expand All @@ -27,7 +27,7 @@ func (vmms *VirtualSystemManagementService) AddSCSIController(vm *virtualsystem.
defer vmsetting.Close()

// apply the settings
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, tmp.CIM_ResourceAllocationSettingData, timeoutSeconds)
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, tmp.CIM_ResourceAllocationSettingData, -1)
if err != nil {
return
}
Expand All @@ -47,13 +47,13 @@ func (vmms *VirtualSystemManagementService) AddSCSIController(vm *virtualsystem.
// * Add a drive to available first controller at available location
// * Connects the Disk to the Drive
// Returns Disk and Drive
func (vmms *VirtualSystemManagementService) AttachVirtualHardDisk(vm *virtualsystem.VirtualMachine, path string, timeoutSeconds uint16) (
func (vmms *VirtualSystemManagementService) AttachVirtualHardDisk(vm *virtualsystem.VirtualMachine, path string) (
vhd *disk.VirtualHardDisk,
vhddrive *drive.SyntheticDiskDrive,
err error) {

// Add a drive
vhddrive, err = vmms.AddSyntheticDiskDrive(vm, -1, -1, timeoutSeconds)
vhddrive, err = vmms.AddSyntheticDiskDrive(vm, -1, -1)
if err != nil {
return
}
Expand All @@ -62,7 +62,7 @@ func (vmms *VirtualSystemManagementService) AttachVirtualHardDisk(vm *virtualsys
if err != nil {
log.Printf("[%+v]\n", err)
// Remove the drive
err1 := vmms.RemoveSyntheticDiskDrive(vhddrive, timeoutSeconds)
err1 := vmms.RemoveSyntheticDiskDrive(vhddrive)
log.Printf("RemoveSyntheticDiskDrive [%+v]\n", err1)
vhddrive.Close()
vhddrive = nil
Expand All @@ -83,7 +83,7 @@ func (vmms *VirtualSystemManagementService) AttachVirtualHardDisk(vm *virtualsys
}

if !strings.Contains(vhdtmp.InstancePath(), "Definition") {
err = vmms.ModifyVirtualSystemResourceEx(vhdtmp.WmiInstance, timeoutSeconds)
err = vmms.ModifyVirtualSystemResourceEx(vhdtmp.WmiInstance, -1)
if err != nil {
return
}
Expand All @@ -96,7 +96,7 @@ func (vmms *VirtualSystemManagementService) AttachVirtualHardDisk(vm *virtualsys
defer vmsetting.Close()

// apply the settings
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, vhdtmp.CIM_ResourceAllocationSettingData, timeoutSeconds)
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, vhdtmp.CIM_ResourceAllocationSettingData, -1)
if err != nil {
return
}
Expand All @@ -120,17 +120,17 @@ func (vmms *VirtualSystemManagementService) AttachVirtualHardDisk(vm *virtualsys
return
}

func (vmms *VirtualSystemManagementService) DetachVirtualHardDisk(vhd *disk.VirtualHardDisk, timeoutSeconds uint16) (err error) {
func (vmms *VirtualSystemManagementService) DetachVirtualHardDisk(vhd *disk.VirtualHardDisk) (err error) {
drive, err := vhd.GetDrive()
if err != nil {
return
}
defer drive.Close()

// Remove Disk
err1 := vmms.RemoveVirtualSystemResource(vhd.CIM_ResourceAllocationSettingData, timeoutSeconds)
err1 := vmms.RemoveVirtualSystemResource(vhd.CIM_ResourceAllocationSettingData, -1)
// Remove Drive
err = vmms.RemoveVirtualSystemResource(drive.CIM_ResourceAllocationSettingData, timeoutSeconds)
err = vmms.RemoveVirtualSystemResource(drive.CIM_ResourceAllocationSettingData, -1)
if err != nil {
return
}
Expand All @@ -143,8 +143,7 @@ func (vmms *VirtualSystemManagementService) DetachVirtualHardDisk(vhd *disk.Virt

func (vmms *VirtualSystemManagementService) AddSyntheticDiskDrive(vm *virtualsystem.VirtualMachine,
controllernumber,
controllerlocation int32,
timeoutSeconds uint16) (vhddrive *drive.SyntheticDiskDrive, err error) {
controllerlocation int32) (vhddrive *drive.SyntheticDiskDrive, err error) {
vmsetting, err := vm.GetVirtualSystemSettingData()
if err != nil {
return
Expand All @@ -156,7 +155,7 @@ func (vmms *VirtualSystemManagementService) AddSyntheticDiskDrive(vm *virtualsys
return
}
defer vhddrivetmp.Close()
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, vhddrivetmp.CIM_ResourceAllocationSettingData, timeoutSeconds)
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, vhddrivetmp.CIM_ResourceAllocationSettingData, -1)
if err != nil {
return
}
Expand All @@ -176,7 +175,7 @@ func (vmms *VirtualSystemManagementService) AddSyntheticDiskDrive(vm *virtualsys
}

func (vmms *VirtualSystemManagementService) RemoveSyntheticDiskDrive(
vhddrive *drive.SyntheticDiskDrive, timeoutSeconds uint16) (err error) {
err = vmms.RemoveVirtualSystemResource(vhddrive.CIM_ResourceAllocationSettingData, timeoutSeconds)
vhddrive *drive.SyntheticDiskDrive) (err error) {
err = vmms.RemoveVirtualSystemResource(vhddrive.CIM_ResourceAllocationSettingData, -1)
return
}
26 changes: 14 additions & 12 deletions pkg/virtualization/core/service/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func (vmms *VirtualSystemManagementService) GetVirtualMachineByName(vmName strin
return nil, errors.Wrapf(errors.NotFound, "Unable to find a virtual system with name [%s]", vmName)
}

func (vmms *VirtualSystemManagementService) CreateVirtualMachine(settings *virtualsystem.VirtualSystemSettingData, memorySettings *memory.MemorySettingData, processorSettings *processor.ProcessorSettingData, timeoutSeconds uint16) (
func (vmms *VirtualSystemManagementService) CreateVirtualMachine(settings *virtualsystem.VirtualSystemSettingData,
memorySettings *memory.MemorySettingData, processorSettings *processor.ProcessorSettingData) (
vm *virtualsystem.VirtualMachine,
err error) {

Expand Down Expand Up @@ -128,10 +129,11 @@ func (vmms *VirtualSystemManagementService) CreateVirtualMachine(settings *virtu
return
}
defer job.Close()
err = job.WaitForJobCompletion(result.ReturnValue, timeoutSeconds)
err = job.WaitForJobCompletion(result.ReturnValue, -1)
return
}
func (vmms *VirtualSystemManagementService) DeleteVirtualMachine(vm *virtualsystem.VirtualMachine, timeoutSeconds uint16) (err error) {

func (vmms *VirtualSystemManagementService) DeleteVirtualMachine(vm *virtualsystem.VirtualMachine) (err error) {
method, err := vmms.GetWmiMethod("DestroySystem")
if err != nil {
return
Expand Down Expand Up @@ -176,12 +178,12 @@ func (vmms *VirtualSystemManagementService) DeleteVirtualMachine(vm *virtualsyst
return
}
defer job.Close()
return job.WaitForJobCompletion(result.ReturnValue, timeoutSeconds)
return job.WaitForJobCompletion(result.ReturnValue, -1)
}
return
}

func (vmms *VirtualSystemManagementService) AddTPM(vm *virtualsystem.VirtualMachine, timeoutSeconds uint16) (resource *resourceallocation.ResourceAllocationSettingData, err error) {
func (vmms *VirtualSystemManagementService) AddTPM(vm *virtualsystem.VirtualMachine) (resource *resourceallocation.ResourceAllocationSettingData, err error) {
tmp, err := vm.NewTPM()
if err != nil {
return
Expand All @@ -195,7 +197,7 @@ func (vmms *VirtualSystemManagementService) AddTPM(vm *virtualsystem.VirtualMach
defer vmsetting.Close()

// apply the settings
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, tmp.CIM_ResourceAllocationSettingData, timeoutSeconds)
resultcol, err := vmms.AddVirtualSystemResource(vmsetting, tmp.CIM_ResourceAllocationSettingData, -1)
if err != nil {
return
}
Expand All @@ -219,12 +221,12 @@ func (vmms *VirtualSystemManagementService) AddTPM(vm *virtualsystem.VirtualMach
return
}

func (vmms *VirtualSystemManagementService) RemoveTPM(resource *resourceallocation.ResourceAllocationSettingData, timeoutSeconds uint16) (err error) {
err = vmms.RemoveVirtualSystemResource(resource.CIM_ResourceAllocationSettingData, timeoutSeconds)
func (vmms *VirtualSystemManagementService) RemoveTPM(resource *resourceallocation.ResourceAllocationSettingData) (err error) {
err = vmms.RemoveVirtualSystemResource(resource.CIM_ResourceAllocationSettingData, -1)
return
}

func (vmms *VirtualSystemManagementService) SetProcessorCount(vm *virtualsystem.VirtualMachine, count uint64, timeoutSeconds uint16) (err error) {
func (vmms *VirtualSystemManagementService) SetProcessorCount(vm *virtualsystem.VirtualMachine, count uint64) (err error) {
proc, err := vm.GetProcessor()
if err != nil {
return
Expand All @@ -236,11 +238,11 @@ func (vmms *VirtualSystemManagementService) SetProcessorCount(vm *virtualsystem.
return
}

err = vmms.ModifyVirtualSystemResourceEx(proc.WmiInstance, timeoutSeconds)
err = vmms.ModifyVirtualSystemResourceEx(proc.WmiInstance, -1)
return
}

func (vmms *VirtualSystemManagementService) SetMemoryMB(vm *virtualsystem.VirtualMachine, sizeMB uint64, timeoutSeconds uint16) (err error) {
func (vmms *VirtualSystemManagementService) SetMemoryMB(vm *virtualsystem.VirtualMachine, sizeMB uint64) (err error) {
mem, err := vm.GetMemory()
if err != nil {
return
Expand All @@ -252,6 +254,6 @@ func (vmms *VirtualSystemManagementService) SetMemoryMB(vm *virtualsystem.Virtua
return
}

err = vmms.ModifyVirtualSystemResourceEx(mem.WmiInstance, timeoutSeconds)
err = vmms.ModifyVirtualSystemResourceEx(mem.WmiInstance, -1)
return
}
Loading

0 comments on commit c835dc3

Please sign in to comment.