首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pyvmomi:调用RelocateVM时出错

pyvmomi:调用RelocateVM时出错
EN

Stack Overflow用户
提问于 2016-10-27 06:40:00
回答 1查看 825关注 0票数 0

您好,我正在使用pyvmomi,在DRS设置为手动模式时对群集执行vmotions。我正在通过vcenter查询群集并获得建议,然后使用该建议执行Vmotions。代码是这样的。

代码语言:javascript
复制
    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文档对RelocateVM方法(规范,优先级)有详细的解释:

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

代码语言:javascript
复制
TypeError: For "spec" expected type vim.vm.RelocateSpec, but got str

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

代码语言:javascript
复制
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)  

任何帮助都是非常感谢的。谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-10-27 15:37:12

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

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

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

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

代码语言:javascript
复制
>>> vm = vim.VirtualMachine('vm-1234', None)
>>> dir(vm)
['AcquireMksTicket', [...] 'Relocate', 'RelocateVM_Task', [...] ]

(缩写输出)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40273250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档