Vmotion using pysphere

587 views
Skip to first unread message

Mabroor Ahmed

unread,
Jan 24, 2012, 5:50:56 AM1/24/12
to pysphere
Hi,

I have just had a quick look at pysphere and it is quite amazing!
Kudos to the Devs! I have been looking for something like this for a
while..

I was wondering is vmotion is currently supported or if it is on the
horizon to be supported in pysphere?

Thanks,
Mabroor

Seba

unread,
Jan 24, 2012, 10:19:42 AM1/24/12
to pysp...@googlegroups.com, Mabroor Ahmed
Hi Mabroor,

   You can access to all the vSphere sdk API from pysphere (so virtually, you should be able to do everything you can do from the vSphere Client or CLI).
   On the other hand I try to provide some simplified interfaces for some methods (in particular virtual machines related methods) so users do not need to deal with the complexity of the vSphere SDK.

   Regarding vmotion, I've just implemented a "migrate" method in virtual machine objects (if that is what you need), so you might want to try the current trunk version.
   If you want to use the last stable version (pysphere 0.1.5), you might still want to check the svn trunk to see how I wrote the "migrate" method and implement something similar yourself.

Here there's an example on how to migrate a vm with the current trunk version of pysphere:

>>> from pysphere import *
>>> s = VIServer()
>>> s.connect("vcenter", "user", "password")
>>>
>>> s.get_hosts()

{'host-1029': 'esx1.example.com',
 'host-120': 'esx2.example.com',
 'host-123': 'esx3.example.com',
 'host-1263': 'esx4.example.com',
  ...
 'host-720': 'esxN.example.com'}

>>> vm = s.get_vm_by_path("[datastore1] path/to/config/file.vmx")

>>>
>>> vm.migrate(host='host-1029')
>>> vm.properties._flush_cache()

>>> s.disconnect()


There are other parameters you can provide to the migrate method. For example to migrate from resource pool as well, or to run the task asynchronously. I'm copying here the method signature and its doc string.

def migrate(self, sync_run=True, priority='default', resource_pool=None,
                host=None, state=None):
        """
        Cold or Hot migrates this VM to a new host or resource pool.
        @sync_run: If True (default) waits for the task to finish, and returns 
            (raises an exception if the task didn't succeed). If False the task
            is started an a VITask instance is returned.
        @priority: either 'default', 'high', or 'low': priority of the task that
            moves the vm. Note this priority can affect both the source and 
            target hosts.
        @resource_pool: The target resource pool for the virtual machine. If the
            pool parameter is left unset, the virtual machine's current pool is
            used as the target pool.
        @host: The target host to which the virtual machine is intended to 
            migrate. The host parameter may be left unset if the compute 
            resource associated with the target pool represents a stand-alone
            host or a DRS-enabled cluster. In the former case the stand-alone
            host is used as the target host. In the latter case, the DRS system
            selects an appropriate target host from the cluster.
        @state: If specified, the virtual machine migrates only if its state
            matches the specified state. 
        """

Is that what you needed? Hope it helps,

Regards,

Sebastian.

2012/1/24 Mabroor Ahmed

Mabroor Ahmed

unread,
Jan 24, 2012, 2:09:47 PM1/24/12
to Seba, pysp...@googlegroups.com
It is great!

Thanks for the detailed answer..

Kind Regards,
Mabroor Ahmed

craig

unread,
Jan 25, 2012, 5:43:25 AM1/25/12
to pysphere
Hi Seba,

This works great when supplying a Host. However, when a Host is not
supplied, my VM is not migrated to a new Host with the Cluster. Is
this because I have load balancing on, or is it because the code is
not drilling down to provide a different Host within the Cluster
(which is not the current Host).

As a side note, I'm not sure how to get Hosts for a specific Cluster,
so I can work around this.

craig

On Jan 24, 7:09 pm, Mabroor Ahmed <mabr...@gmail.com> wrote:
> It is great!
>
> Thanks for the detailed answer..
>
> Kind Regards,
> Mabroor Ahmed
>

Seba

unread,
Jan 27, 2012, 12:27:15 PM1/27/12
to pysp...@googlegroups.com, craig

Hi Craig,

   This is the vSphere documentation regarding the 'host' parameter:

"""
host: The target host to which the virtual machine is intended to migrate. The host parameter may be left unset if the compute resource associated with the target pool represents a stand-alone host or a DRS-enabled cluster. In the former case the stand-alone host is used as the target host. In the latter case, the DRS system selects an appropriate target host from the cluster. 
"""
   So, if you don't set a host, pysphere does not pick a different one, it is left unset and vCenter decides what to do.

Regarding your other question, I copy below some examples on how you can get hosts from a specific cluster (this works since trunk revision r55):

Regards,

Seba.

>>> from pysphere import VIServer, VIMor, MORTypes
>>>
>>> s = VIServer()
>>> s.connect("vcenter", "root", "secret")
>>>

Get ALL hosts:
--------------

>>> s.get_hosts()
{'host-444': 'dev-esx1.example.com',
 'host-120': 'dev-esx2.example.com',
 'host-67': 'dev-esx3.example.com',
 'host-1029': 'qa-esx1.example.com', 
 'host-1263': 'qa-esx2.example.com'
}

Get All clusters
-----------------

>>> s.get_clusters()
{'domain-c716': 'QA', 
 'domain-c441': 'DEV'}

Get Hosts by cluster
--------------------
you can provide an additional paramter 'from_mor' to the get_hosts() method to indicate from which object should start looking for hosts
Notice that the dictionary keys returned by get_clusters() or get_hosts() seem to be strings, but they actually are a subclass of str, with an additional attribute which indicate what type of managed object reference is.
So the following won't work

>>> s.get_hosts(from_mor='domain-c716')

Instead you should get the actual key from the clusters dictionary:

>>>
>>> qa_cluster = [k for k,v in s.get_clusters().items() if v =='QA'][0]
>>>
>>> s.get_hosts(from_mor=qa_cluster)
{'host-1029': 'qa-esx1.es.core-sdi.com',
 'host-1263': 'qa-esx2.es.core-sdi.com'}

Alternatively, if you don't want to iterate through the dictionary you can provide a tuple (mor_value, mor type) indicating the managed object reference type. E.g

>>>
>>> s.get_hosts(from_mor=('domain-c716', 'ClusterComputeResource'))
{'host-1029': 'qa-esx1.es.core-sdi.com',
 'host-1263': 'qa-esx2.es.core-sdi.com'}

If you wish, all Managed Object types are defined as contants in a pysphere class called MORTypes. E.g.

>>>
>>> s.get_hosts(from_mor=('domain-c716', MORTypes.ClusterComputeResource))
{'host-1029': 'qa-esx1.es.core-sdi.com',
 'host-1263': 'qa-esx2.es.core-sdi.com'}

Alternatively, instead of providing a tuple you can construct your mor object this way:

>>>
>>> qa_cluster = VIMor('domain-c716', MORTypes.ClusterComputeResource)
>>>
>>> s.get_hosts(from_mor=mor)
{'host-1029': 'qa-esx1.es.core-sdi.com',
 'host-1263': 'qa-esx2.es.core-sdi.com'}


2012/1/25 craig

craig

unread,
Jan 27, 2012, 12:37:01 PM1/27/12
to pysphere
Hi Seba,

Thank you for the great examples and quick reply. They make perfect
sense!! Will give them a try in my current scripts.

craig

Ravi raja

unread,
Apr 4, 2019, 11:22:21 AM4/4/19
to pysphere
Hi Seba,

I am very new to PySphere. past 4 days I am going through in google.

with the help of your and other people reply, i am learning pysphere. how it is interactiving with Vspheere GUI.

I tried to change memory_mb and macAddress, but change is not happened.

could you please provide me sample code for modifying number of CPUs, memory_mb, hard disk size and macaddress.

Thanks in advance.

Ravi
Reply all
Reply to author
Forward
0 new messages