[ovirt-users] create a cloned virtual machine based on a template with SDK API python

Fred Rolland frolland at redhat.com
Thu Mar 22 09:02:32 UTC 2018


Hi Nicolas,

You can find an example here:

https://github.com/oVirt/ovirt-engine-sdk/blob/21f637345597729240f217cfe84fe2a2cf39a655/sdk/examples/add_independet_vm.py#L56

Regards,
Fred

On Thu, Mar 22, 2018 at 9:09 AM, Nicolas Vaye <nicolas.vaye at province-sud.nc>
wrote:

> Hi,
>
> I want to create a cloned virtual machine based on a template with SDK API
> python and i don't find the parameter to indicate the clone action for the
> disk
>
> here is my code :
>
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> #
> # Copyright (c) 2016 Red Hat, Inc.
> #
> # Licensed under the Apache License, Version 2.0 (the "License");
> # you may not use this file except in compliance with the License.
> # You may obtain a copy of the License at
> #
> #   http://www.apache.org/licenses/LICENSE-2.0
> #
> # Unless required by applicable law or agreed to in writing, software
> # distributed under the License is distributed on an "AS IS" BASIS,
> # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> # See the License for the specific language governing permissions and
> # limitations under the License.
> #
> from pprint import pprint
> import logging
> import time
>
> import ovirtsdk4 as sdk
> import ovirtsdk4.types as types
>
>
>
> template_name='test_debian_9.4'
> template_version=1
> cluster_name='nico-cluster'
> data_domain_name='OVIRT-TEST2'
>
>
>
> logging.basicConfig(level=logging.DEBUG, filename='example.log')
>
> # This example will connect to the server and start a virtual machine
> # with cloud-init, in order to automatically configure the network and
> # the password of the `root` user.
>
> # Create the connection to the server:
> connection = sdk.Connection(
>     url='https://ocenter.province-sud.prod/ovirt-engine/api',
>     username='admin at internal<mailto:username='admin at internal>',
>     password='admin',
>     ca_file='CA_ocenter.pem',
>     debug=True,
>     log=logging.getLogger(),
> )
>
>
>
> ##################################
> ############ TEMPLATE ############
> ##################################
>
> # Get the reference to the root of the tree of services:
> system_service = connection.system_service()
>
> # Get the reference to the service that manages the storage domains:
> storage_domains_service = system_service.storage_domains_service()
>
> # Find the storage domain we want to be used for virtual machine disks:
> storage_domain = storage_domains_service.list(search='name='+data_domain_
> name)[0]
>
>
> # Get the reference to the service that manages the templates:
> templates_service = system_service.templates_service()
>
> # When a template has multiple versions they all have the same name, so
> # we need to explicitly find the one that has the version name or
> # version number that we want to use. In this case we want to use
> # version 1 of the template.
> templates = templates_service.list(search='name='+template_name)
> template_id = None
> for template in templates:
>     if template.version.version_number == template_version:
>         template_id = template.id
>         break
>
> if template_id == None:
>     print "ERREUR le template "+template_name+"en version
> "+str(template_version)+" n'a pas été trouvé!!"
>
> # Find the template disk we want be created on specific storage domain
> # for our virtual machine:
> template_service = templates_service.template_service(template_id)
> disk_attachments = connection.follow_link(template_service.get().disk_
> attachments)
>
> print "disk_attachments=" + str(len(disk_attachments))
>
> template_disk_attachments = []
> for disk in disk_attachments:
>     template_disk_attachments.append(types.DiskAttachment(
>                 disk=types.Disk(
>                     id=disk.id,
>                     format=types.DiskFormat.COW,
>                     storage_domains=[
>                         types.StorageDomain(
>                             id=storage_domain.id,
>                         ),
>                     ],
>                 ),
>             )
>     )
>
>
>
> # Get the reference to the service that manages the virtual machines:
> vms_service = system_service.vms_service()
>
> # Add a new virtual machine explicitly indicating the identifier of the
> # template version that we want to use and indicating that template disk
> # should be created on specific storage domain for the virtual machine:
> vm = vms_service.add(
>     types.Vm(
>         name='myvm',
>         cluster=types.Cluster(
>             name=cluster_name
>         ),
>         stateless=False,
>         type=types.VmType('server'),
>         comment='based on template '+template_name+'en version
> '+str(template_version),
>         template=types.Template(
>             id=template_id
>         ),
>         disk_attachments=template_disk_attachments,
>     )
> )
>
>
>
> # Get a reference to the service that manages the virtual machine that
> # was created in the previous step:
> vm_service = vms_service.vm_service(vm.id)
>
> # Wait till the virtual machine is down, which indicats that all the
> # disks have been created:
> while True:
>     time.sleep(1)
>     vm = vm_service.get()
>     if vm.status == types.VmStatus.DOWN:
>         break
>
>
> # Close the connection to the server:
> connection.close()
>
>
>
>
> If the data_domain_name is the same as the template data domain, then this
> script seem to create a vm but not with cloned disk.
>
> If the data_domain_name is NOT the same as the template data domain, then
> this script produce an error
> ovirtsdk4.Error: Fault reason is "Operation Failed". Fault detail is
> "[Cannot add VM. The selected Storage Domain does not contain the VM
> Template.]". HTTP response code is 400.
>
> So how to create a cloned virtual machine based on a template ?
>
> I think i'm looking for the same parameter on the web ui, "Storage
> Allocation" => Thin/Clone
>
> [cid:1521702568.14216.1.camel at province-sud.nc]
>
>
> Thanks.
>
> Nicolas VAYE
>
>
> _______________________________________________
> Users mailing list
> Users at ovirt.org
> http://lists.ovirt.org/mailman/listinfo/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ovirt.org/pipermail/users/attachments/20180322/06d0eafb/attachment.html>


More information about the Users mailing list