您好,我正在使用pyvmomi,在DRS设置为手动模式时对群集执行vmotions。我正在通过vcenter查询群集并获得建议,然后使用该建议执行Vmotions。代码是这样的。
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(我找不到它的文档),当我使用它时,我得到了这个错误
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) 任何帮助都是非常感谢的。谢谢
发布于 2016-10-27 15:37:12
看起来像是文档中的错误。该方法称为Relocate (而不是RelocateVM)。
请注意,在您的第一个示例中,您没有将目标主机传递给对Relocate的调用,因此这里肯定缺少一些东西。
您可以在https://gist.github.com/rgerganov/12fdd2ded8d80f36230f或https://github.com/sijis/pyvmomi-examples/blob/master/migrate-vm.py上看到一些示例。
最后,认识到您使用了错误名称的一种方法是在VirtualMachine对象上调用Python的dir方法。这将列出该对象的所有属性,以便您可以查看它具有哪些方法:
>>> vm = vim.VirtualMachine('vm-1234', None)
>>> dir(vm)
['AcquireMksTicket', [...] 'Relocate', 'RelocateVM_Task', [...] ](缩写输出)
https://stackoverflow.com/questions/40273250
复制相似问题