2011-10-25 52 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