2011-10-25 60 views
4

如何通過某種形式的庫或通過SUDS聯繫vSphere(或VMWare)形式的Python來獲取vCPU數量或特定主機/ guest虛擬機/虛擬-機?Python - VMWare vSphere(WEB SDK) - SUDS

我目前正試圖:

from suds.client import Client 
from suds.sudsobject import Property 

client = Client("https://<server>/sdk/vimService?wsdl") 
queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo'] 
print queryCon 

而且這樣的作品,它給了我某種形式的輸出:

(Method){ 
    name = "QueryConnectionInfo" 
    location = "https://localhost/sdk/vimService" 
    binding = 
     (binding){ 
     input = <suds.bindings.document.Document instance at 0x0775C080> 
     output = <suds.bindings.document.Document instance at 0x0775C080> 
     } 
    soap = 
     (soap){ 
     action = ""urn:vim25/4.1"" 
     style = "document" 
     input = 
      (Input){ 
       body = 
        (Body){ 
        parts[] = 
         (Part){ 
          root = <part name="parameters" element="vim25:QueryConnectionInfo"/> 
          name = "parameters" 
          qname[] = 
           "parameters", 
           "urn:vim25", 
          element = "(u'QueryConnectionInfo', u'urn:vim25')" 
          type = "None" 
         }, 
        use = "literal" 
        namespace[] = 
         "vim25", 
         "urn:vim25", 
        wrapped = True 
        } 
       headers[] = <empty> 
      } 
     output = 
      (Output){ 
       body = 
        (Body){ 
        parts[] = 
         (Part){ 
          root = <part name="parameters" element="vim25:QueryConnectionInfoResponse"/> 
          name = "parameters" 
          qname[] = 
           "parameters", 
           "urn:vim25", 
          element = "(u'QueryConnectionInfoResponse', u'urn:vim25')" 
          type = "None" 
         }, 
        use = "literal" 
        namespace[] = 
         "vim25", 
         "urn:vim25", 
        wrapped = True 
        } 
       headers[] = <empty> 
      } 
     faults[] = 
      (Fault){ 
       name = "InvalidLoginFault" 
       use = "literal" 
       parts[] = 
        (Part){ 
        root = <part name="fault" element="vim25:InvalidLoginFault"/> 
        name = "fault" 
        qname[] = 
         "fault", 
         "urn:vim25", 
        element = "(u'InvalidLoginFault', u'urn:vim25')" 
        type = "None" 
        }, 
      }, 
      (Fault){ 
       name = "HostConnectFaultFault" 
       use = "literal" 
       parts[] = 
        (Part){ 
        root = <part name="fault" element="vim25:HostConnectFaultFault"/> 
        name = "fault" 
        qname[] = 
         "fault", 
         "urn:vim25", 
        element = "(u'HostConnectFaultFault', u'urn:vim25')" 
        type = "None" 
        }, 
      }, 
      (Fault){ 
       name = "RuntimeFault" 
       use = "literal" 
       parts[] = 
        (Part){ 
        root = <part name="fault" element="vim25:RuntimeFaultFault"/> 
        name = "fault" 
        qname[] = 
         "fault", 
         "urn:vim25", 
        element = "(u'RuntimeFaultFault', u'urn:vim25')" 
        type = "None" 
        }, 
      }, 
     } 
} 

我已試過下面的 「指南」:

SUDS - programmatic access to methods and types

http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.VirtualMachine.html#field_detail

http://communities.vmware.com/thread/273616

我知道所有的信息可能是在這裏,我只是不能看到整個畫面:/

一段時間的努力之後被我卡住:

client = Client("https://<server>/sdk/vimService?wsdl") 
#queryCon = client.wsdl.services[0].ports[0].methods['QueryConnectionInfo'] 
print client.service.QueryConnectionInfo("https://<server>/sdk", None, r'domain\user', 'Password') 

,輸出是:

urllib2.URLError: <urlopen error [Errno 1] _ssl.c:490: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol> 
+0

似乎服務器不理解ssl。你有沒有嘗試純HTTP請求? – jfs

回答

2

你可能想嘗試psphere,一個Python庫(基於泡沫),它可以讓您訪問整個vSphere Web Services SDK。

安裝:

$ pip install psphere 

找到一個虛擬機,並打印具有CPU數量:

>>> from psphere.client import Client 
>>> from psphere.managedobjects import VirtualMachine 
>>> client = Client(server="vcenter.mydomain.com", username="Administrator", password="strong") 
>>> vm = VirtualMachine.get(client, name="genesis") 
>>> vm 
<psphere.managedobjects.VirtualMachine object at 0xd3fbccc> 
>>> print("%s has %s CPUs" % (vm.name, vm.config.hardware.numCPU)) 
genesis has 2 CPUs 

您可以找到documentation更多的例子。

免責聲明:我是psphere的作者。

+0

抱歉,對於遲到的回答,我從來沒有得到psphere正常工作,不記得這個問題,但可能能夠重現它。 – Torxed

2

一個新的庫解決了這個問題:pysphere (easy_install的pysphere)

from pysphere import VIServer 
server = VIServer() 
server.connect("server", 'user', "pass") 
vm = server.get_vm_by_name("virtual_host_name") 
info = vm.get_properties() 

它使命名空間完好的vSphere變量,因爲我需要它,這意味着它是透明不改變任何容易找到的東西,它也支持信息的加載,所以我不必在一個緩慢的痛苦過程中逐個查詢所有1500個服務器,它需要幾秒鐘來收集關於任何主機的信息。

+0

你還在使用這個pysphere嗎?你是否將它用於VM操作以外的其他事情? – Litty

+0

有時,我已經開發了我自己的KVM周圍的包裝,所以我有種使用專有軟件停止使用的所有內容:) – Torxed