2016-10-26 47 views
0

您好,我正在使用pyvmomi API,在DRS設置爲手動模式時對羣集執行vmotions。我正在瀏覽一箇中心,查詢羣集並獲得推薦信息,並使用它來執行Vmotions。代碼是這樣的。pyvmomi:調用RelocateVM時出錯

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的詳細說明(規格,優先級):

任何人都知道是什麼原因該方法缺失?我也試過檢查對象,有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) 

任何幫助表示讚賞。 謝謝

回答

0

看起來像文件中的錯誤。該方法被稱爲Relocate(而不是RelocateVM)。

請注意,順便說一下,在您的第一個示例中,您未將目標主機傳遞給Relocate的調用,所以肯定會丟失某些內容。

您可以在https://gist.github.com/rgerganov/12fdd2ded8d80f36230fhttps://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', [...] ] 

(簡稱輸出)

+0

我得到一個錯誤,當我撥打移居/ RelocateVM_Task,任何想法?我和主持人一起設定了規格。但它會拋出這個錯誤。 pyVmomi.VmomiSupport.ManagedObjectNotFound:(vmodl.fault.ManagedObjectNotFound){ dynamicType = , dynamicProperty =(vmodl.DynamicProperty)[], MSG = '', faultCause = , faultMessage =(vmodl.LocalizableMessage) [], obj ='vim.VirtualMachine:xxxx' } – jramacha

+0

@jramacha你可以在問題中發佈你的更新代碼嗎?請注意,您必須創建一個新的'vim.vm.RelocateSpec'對象(您不能傳遞'spec = host')。請參閱https://github.com/sijis/pyvmomi-examples/blob/master/migrate-vm.py上的示例。 – YSK

+0

其實現在起作用了,我有一個錯字。我做了spec.host = host obj。非常感謝 – jramacha