[PATCH] [wip-v1] Create template from guest (BACKEND)

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@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 + # ToDo: + # Get Disks + # Get StoragePool + # Get Networks + return ret + def get_list(self): with self.objstore as session: return session.get_list('template') -- 1.9.3

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@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')

On 09/01/2014 02:23 PM, Aline Manera wrote:
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@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.
True
In this code you always override the value as it just accepts one cdrom drive.
What value am I overwriting ? Not sure if I got your point =) What I am trying to do is get the first cdrom found, because Templates can only have one cdrom configured.
+ # ToDo: + # Get Disks + # Get StoragePool + # Get Networks + return ret + def get_list(self): with self.objstore as session: return session.get_list('template')
_______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel

On 26-08-2014 11:23, Rodrigo Trujillo wrote:
+ def _get_vm_params(self, vm): + from kimchi.model.vms import VMModel
I'd suggest importing modules at the top of the file, not where they're used. There are other occurrences of this in the rest of the code. And what will happen if the user sets the parameter 'vm' as well as other values (e.g. 'cdrom', 'cpus', etc)? According to this code, the parameter 'vm' will override whatever has been set before. Is that expected? I think this should be documented somewhere, probably at src/kimchi/API.json.

On 09/09/2014 02:28 PM, Crístian Viana wrote:
On 26-08-2014 11:23, Rodrigo Trujillo wrote:
+ def _get_vm_params(self, vm): + from kimchi.model.vms import VMModel
I'd suggest importing modules at the top of the file, not where they're used. There are other occurrences of this in the rest of the code.
Definitively! I had this discussion with Aline, but if I import in the begging, as the module is loaded, python raises an error, due to cyclic import. Importing in the middle, at runtime, I do not see the the problem.
And what will happen if the user sets the parameter 'vm' as well as other values (e.g. 'cdrom', 'cpus', etc)? According to this code, the parameter 'vm' will override whatever has been set before. Is that expected? I think this should be documented somewhere, probably at src/kimchi/API.json.
Did not get your point Cristian. "vm" is the name of the guest which parameters will be copied to create a new Template. User should not pass any other parameter. What will be overwritten ??
_______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel

On 22-09-2014 15:47, Rodrigo Trujillo wrote:
User should not pass any other parameter. What will be overwritten ??
They *should not* but they *can*. Let's suppose Kimchi receives the following command: POST /templates/ {'name': 'template-foo', 'vm': 'existing-vm', 'cpus': 8} What will happen here? How many CPUs will the new template have: the number of CPUs on the guest 'existing-vm' or 8? This should be clear to the user when creating a template from a VM: if they can also specify other values to overwrite from the base VM or if they cannot set anything else.
participants (3)
-
Aline Manera
-
Crístian Viana
-
Rodrigo Trujillo