clone_vm_from_template method ? help

232 views
Skip to first unread message

centerpoint

unread,
Jan 18, 2012, 6:53:21 AM1/18/12
to pysphere
Hello Seba:
def clone_vm_from_template(self,vm_template,new_vm_name):

request = VI.CloneVM_TaskRequestMsg()



template_mor=self.get_vm_mor_by_name(vm_template)
#paste below
template_mor.set_attribute_type('VirtualMachine')

request.set_element__this(template_mor)


request.set_element_name(new_vm_name)


request.set_element_folder(???)

Q: how to get a folder object (data)?


new_spec=???

Q: how to get a new_spec object ?
new_spec.set_element_location(???)

Q: how to set this parameter ?
new_spec.set_element_powerOn(True)

new_spec.set_element_template(False)

request.set_element_spec(new_spec)



self._server._proxy.CloneVM_Task(request)



#a method
def get_vm_mor_by_name(self, name, datacenter=None):

if not self.__logged:

raise VIException("Must call 'connect' before invoking
this method",

FaultTypes.NOT_CONNECTED)



try:

node = self._do_service_content.RootFolder

if datacenter is not None:

dc = self._get_datacenters()

node = dc[datacenter]

do_object_content =
self._retrieve_properties_traversal(property_names=['name'],from_node=node,obj_type='VirtualMachine')

if do_object_content is not None:

for oc in do_object_content:

mor = oc.get_element_obj()

properties = oc.PropSet

if properties is not None:

for prop in properties:

if prop.Name == 'name' and prop.Val ==
name:

return mor

except (VI.ZSI.FaultException), e:

raise VIApiException(e)



raise VIException("Could not find a VM named '%s'"

% name,
FaultTypes.VM_NOT_FOUND_ERROR)



Thanks in advance.

Seba

unread,
Jan 18, 2012, 12:43:02 PM1/18/12
to pysphere, ubunt...@gmail.com
Hi there!

The clone VM task has already been implemented in pysphere 0.1.5,
make sure you have the last version of pysphere.

> vm = server.get_vm_by_path(vmpath)
>
> vm2 = vm.clone("The new vm name")
> vm2.get_status()

You might pass some optional additional parameters. Here's the method
docuentation:
def clone(self, name, sync_run=True, folder=None, power_on=True):
"""Clones this Virtual Machine
@name: name of the new virtual machine
@folder: name of the folder that will contain the new VM, if
not set
the vm will be added to the folder the original VM
belongs to
@power_on: If the new VM will be powered on after being
created
@sync_run: if True (default) waits for the task to finish, and
returns
a VIVirtualMachine instance with the new VM (raises an
exception if the
task didn't succeed). If sync_run is set to False the task is
started an
a VITask instance is returned
"""

Regards,

Sebastian
> self._retrieve_properties_traversal(property_names=['name'],from_node=node, obj_type='VirtualMachine')

centerpoint

unread,
Jan 20, 2012, 9:46:06 AM1/20/12
to pysphere
Hi Seba :
For a clone operation from a template to a virtual machine,"
pool " argument of " location " is required.
So should add these code to clone() method:

pool=location.new_pool(xxx')

pool.set_attribute_type('ResourcePool')

location.set_element_pool(pool)


Regards
centerpoint

Seba

unread,
Jan 20, 2012, 10:56:30 AM1/20/12
to pysp...@googlegroups.com, centerpoint
Thanks! I didn't know that a specifying a resourcepool was mandatory when cloning a template.
I added 'resourcepool' as an optional parameter, already committed in trunk.

By the way, I was mistaken when I said that the clone method was available in pysphere 0.1.5. I added it after that release, it's in trunk now and will be available in the next release.

Thanks again.

Sebastian.

2012/1/20 
Reply all
Reply to author
Forward
0 new messages