[ install pysphere on Ubuntu 12.04 LTS ]
# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=12.04 DISTRIB_CODENAME=precise DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS" # uname -ri 3.2.0-37-virtual x86_64 |
the installation is very easy , just type
# easy_install -U pysphere |
[ sample usage ]
connect to the ESXi not vCenter
# python Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from pysphere import VIServer >>> server = VIServer() >>> server.connect("192.168.0.1","user",".Password.") |
get the ESXi info
>>> print server.get_server_type() VMware ESXi >>> print server.get_api_version() 4.1 |
>>> vmlist = server.get_registered_vms() >>> print vmlist[0] [datastore1] CentOS6-1-32bit/CentOS6-1-32bit.vmx |
retrieve VMs
>>> server.get_vm_by_name("CentOS6-1-32bit") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/pysphere-0.1.7-py2.7.egg/pysphere/vi_server.py", line 323, in get_vm_by_name FaultTypes.OBJECT_NOT_FOUND) pysphere.resources.vi_exception.VIException: [Object Not Found]: Could not find a VM named 'CentOS6-1-32bit' |
if you see the above error , connect to the ESXi again.
>>> server.connect("192.168.0.1","user",".Password.") >>> server.get_vm_by_path("[datastore1] CentOS6-1-32bit/CentOS6-1-32bit.vmx") <pysphere.vi_virtual_machine.VIVirtualMachine instance at 0x3cffd88> >>> vm1 = server.get_vm_by_path("[datastore1] CentOS6-1-32bit/CentOS6-1-32bit.vm x") |
>>> vmlist[1] '[datastore1] CentOS6-1-64bit/CentOS6-1-64bit.vmx' >>> vm2 = server.get_vm_by_name("CentOS6-1-64bit") |
check the status of the VM
>>> print vm1.get_status(basic_status=True) POWERED OFF >>> print vm1.is_powering_on() False |
>>> vm1.get_properties() {'files': {0: {'type': 'diskDescriptor', 'name': '[datastore1] CentOS6-1-32bit/CentOS6-1-32bit.vmdk', 'key': 0, 'size': 499}, 1: {'type': 'diskExtent', 'name': '[datastore1] CentOS6-1-32bit/CentOS6-1-32bit-flat.vmdk', 'key': 1, 'size': 10737418240}, 2: {'type': 'log', 'name': '[datastore1] CentOS6-1-32bit/vmware-1.log' <snip> >>> print vm1.get_property('guest_id') rhel6Guest |
power on the VM
>>> vm1.get_status() 'POWERED OFF' >>> vm1.power_on() |
>>> vm1.get_status() 'POWERED ON' |
suspend / shutdown the VM
>>> vm1.get_status() 'POWERED ON' >>> vm1.suspend() >>> vm1.get_status() 'SUSPENDED' >>> vm1.power_on() >>> vm1.get_status() 'POWERED ON' >>> vm1.power_off() >>> vm1.get_status() 'POWERED OFF' |
disconnect
>>> server.disconnect() |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.