0

Hi I am using pyvmomi API, to perform vmotions against a cluster when DRS is set to manual mode. I am going through a vcenter and querying a cluster and getting recommendation and using that to perform the Vmotions. The code is something like this.

    content=getVCContent(thisHost,    {'user':username,'pwd':decoded_password},logger)
        allClusterObj = content.viewManager.CreateContainerView(content.rootFolder, [pyVmomi.vim.ClusterComputeResource], True)

        allCluster = allClusterObj.view



        for thisDrsRecommendation in thisCluster.drsRecommendation:
            print thisDrsRecommendation.reason
        for thisMigration in thisDrsRecommendation.migrationList:
            print ' vm:', thisMigration.vm.name 
     while True:
            relocate_vm_to_host(thisMigration.vm.name,thisMigration.destination.name, allClusterObj.view)

#FUNCTION definition
    def relocate_vm_to_host(vm, host , allCluster):
        for thisCluster in allCluster:
            for thisHost in thisCluster.host:
                if thisHost.name == host:
                    for thisVm in thisHost.vm:
                        print 'Relocating vm:%s to host:%s on cluster:%s' %(thisVm.name,thisHost.name,thisCluster.name)
                        task = thisVm.RelocateVM(priority='defaultpriority')

I am getting an error saying the attribute doesn't exist. AttributeError: 'vim.VirtualMachine' object has no attribute 'RelocateVM'

But the pyvmomi documentaion here https://github.com/vmware/pyvmomi/blob/master/docs/vim/VirtualMachine.rst has a detailed explanation for the method RelocateVM(spec, priority):

Anyone know what's the reason the method is missing? I also tried checking the available methods of the object ,that has RelocateVM_Task ,instead of RelocateVM(for which I couldn't find documentation) When I used that I get this error

TypeError: For "spec" expected type vim.vm.RelocateSpec, but got str

I checked the documentation for vim.vm.RelocateSpec, I am calling it in a function , but still throws an error.

def relocate_vm(VmToRelocate,destination_host,content):
    allvmObj = content.viewManager.CreateContainerView(content.rootFolder, [pyVmomi.vim.VirtualMachine], True)  
    allvms = allvmObj.view
    for vm in allvms:
        if vm.name == VmToRelocate:
        print 'vm:%s to relocate %s' %(vm.name , VmToRelocate)
        task = vm.RelocateVM_Task(spec = destination_host)  

Any help is appreciated. Thanks

jramacha
  • 117
  • 2
  • 13

1 Answers1

0

Looks like a mistake in the documentation. The method is called Relocate (and not RelocateVM).

Note, BTW, that in your first sample you're not passing the destination host to the call to Relocate so something is definitely missing there.

You can see some samples at https://gist.github.com/rgerganov/12fdd2ded8d80f36230f or at https://github.com/sijis/pyvmomi-examples/blob/master/migrate-vm.py.

Lastly, one way to realize you're using the wrong name is by calling Python's dir method on a VirtualMachine object. This will list all properties of the object so you can see which methods it has:

>>> vm = vim.VirtualMachine('vm-1234', None)
>>> dir(vm)
['AcquireMksTicket', [...] 'Relocate', 'RelocateVM_Task', [...] ]

(abbreviated output)

YSK
  • 1,572
  • 10
  • 19
  • I am getting an error when I call the Relocate/RelocateVM_Task, any ideas? I set the spec with the host. But it throws this error. pyVmomi.VmomiSupport.ManagedObjectNotFound: (vmodl.fault.ManagedObjectNotFound) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = '', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], obj = 'vim.VirtualMachine:xxxx' } – jramacha Oct 27 '16 at 18:10
  • @jramacha Can you post your updated code in the question? Note that you have to create a new `vim.vm.RelocateSpec` object (you can't pass `spec=host`). See the example at https://github.com/sijis/pyvmomi-examples/blob/master/migrate-vm.py. – YSK Oct 27 '16 at 20:11
  • Actually it works now, I had a typo. I did spec.host = host obj. Thanks a lot – jramacha Oct 27 '16 at 20:42