0

您好我正在使用 pyvmomi API,在 DRS 设置为手动模式时对集群执行 vmotions。我正在通过一个中心并查询一个集群并获得推荐并使用它来执行 Vmotion。代码是这样的。

    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')

我收到一条错误消息,说该属性不存在。AttributeError: 'vim.VirtualMachine' 对象没有属性 'RelocateVM'

但是这里的 pyvmomi 文档https://github.com/vmware/pyvmomi/blob/master/docs/vim/VirtualMachine.rst 对 RelocateVM(spec, priority) 方法有详细的解释:

任何人都知道该方法丢失的原因是什么?我还尝试检查具有 RelocateVM_Task 的对象的可用方法,而不是 RelocateVM(我找不到文档)当我使用它时,我得到了这个错误

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

我检查了 vim.vm.RelocateSpec 的文档,我在函数中调用它,但仍然抛出错误。

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)  

任何帮助表示赞赏。谢谢

4

1 回答 1

0

看起来像文档中的错误。该方法被调用Relocate(而不是RelocateVM)。

请注意,顺便说一句,在您的第一个示例中,您没有将目标主机传递给调用,Relocate因此那里肯定缺少某些东西。

您可以在https://gist.github.com/rgerganov/12fdd2ded8d80f36230fhttps://github.com/sijis/pyvmomi-examples/blob/master/migrate-vm.py查看一些示例。

最后,意识到您使用错误名称的dir一种方法是在 VirtualMachine 对象上调用 Python 的方法。这将列出对象的所有属性,以便您查看它具有哪些方法:

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

(缩写输出)

于 2016-10-27T07:37:12.253 回答