[Kimchi-devel] [PATCH] [wip-v1] Create template from guest (BACKEND)
Aline Manera
alinefm at linux.vnet.ibm.com
Mon Sep 1 17:23:48 UTC 2014
On 08/26/2014 11:23 AM, Rodrigo Trujillo wrote:
> This patch implements new functionality that allows user to create a
> template based on a givem virtual machine. Template will have same
> number of CPUS, amount of Memory, CDROM attached, disks, etc.
>
> Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
> ---
> src/kimchi/API.json | 6 ++++++
> src/kimchi/i18n.py | 1 +
> src/kimchi/model/templates.py | 34 ++++++++++++++++++++++++++++++++++
> 3 files changed, 41 insertions(+)
>
> diff --git a/src/kimchi/API.json b/src/kimchi/API.json
> index c3fc5e3..e1e0218 100644
> --- a/src/kimchi/API.json
> +++ b/src/kimchi/API.json
> @@ -385,6 +385,12 @@
> "minimum": 512,
> "error": "KCHTMPL0013E"
> },
> + "vm": {
> + "description": "Virtual Machine which template will be based in",
> + "type": "string",
> + "minimum": 1,
> + "error": "KCHTMPL0025E"
> + },
> "cdrom": {
> "description": "Path for cdrom",
> "type": "string",
> diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
> index 2eae7e8..8b9c713 100644
> --- a/src/kimchi/i18n.py
> +++ b/src/kimchi/i18n.py
> @@ -130,6 +130,7 @@ messages = {
> "KCHTMPL0022E": _("Disk size must be greater than 1GB."),
> "KCHTMPL0023E": _("Template base image must be a valid local image file"),
> "KCHTMPL0024E": _("Cannot identify base image %(path)s format"),
> + "KCHTMPL0025E": _("Virtual machine name must be a string"),
>
> "KCHPOOL0001E": _("Storage pool %(name)s already exists"),
> "KCHPOOL0002E": _("Storage pool %(name)s does not exist"),
> diff --git a/src/kimchi/model/templates.py b/src/kimchi/model/templates.py
> index bf04304..15296b0 100644
> --- a/src/kimchi/model/templates.py
> +++ b/src/kimchi/model/templates.py
> @@ -36,6 +36,7 @@ class TemplatesModel(object):
> def __init__(self, **kargs):
> self.objstore = kargs['objstore']
> self.conn = kargs['conn']
> + self.kargs = kargs
>
> def create(self, params):
> name = params.get('name', '').strip()
> @@ -68,6 +69,12 @@ class TemplatesModel(object):
> except Exception:
> raise InvalidParameter("KCHTMPL0003E", {'network': net_name,
> 'template': name})
> +
> + # Template based in a given VM
> + vm = params.get('vm')
> + if vm:
> + params.update(self._get_vm_params(vm))
> +
> # Creates the template class with necessary information
> # Checkings will be done while creating this class, so any exception
> # will be raised here
> @@ -83,6 +90,33 @@ class TemplatesModel(object):
>
> return name
>
> + def _get_vm_params(self, vm):
> + from kimchi.model.vms import VMModel
> + vm_info = VMModel(**self.kargs).lookup(vm)
> + ret = {}
> +
> + # Get CPUs, Memory, Graphics
> + ret['cpus'] = vm_info['cpus']
> + ret['memory'] = vm_info['memory']
> + ret['graphics'] = vm_info['graphics']
> +
> + # CDROM
> + from kimchi.model.vmstorages import VMStoragesModel, VMStorageModel
> + vmStorages = VMStoragesModel(**self.kargs)
> + disks = vmStorages.get_list(vm)
> + vmStorage = VMStorageModel(**self.kargs)
> + for disk in disks:
> + disk_info = vmStorage.lookup(vm, disk)
> + if disk_info['type'] == 'cdrom':
> + # get first cdrom found
> + ret['cdrom'] = disk_info['path']
> + break
The VM can have multiples cdrom drives.
In this code you always override the value as it just accepts one cdrom
drive.
> + # ToDo:
> + # Get Disks
> + # Get StoragePool
> + # Get Networks
> + return ret
> +
> def get_list(self):
> with self.objstore as session:
> return session.get_list('template')
More information about the Kimchi-devel
mailing list