[Kimchi 0/4 v2] Use single field to add installation media

Changes: v2: Fix pep8 issues This patch changes how templates are created. Instead of specifying cdrom and disks, add them into a single field and kimchi will identify it. Points to take noteL 1) UI support. Another task will need to change UI for a single entry. 2) Old support for cdrom and disk installation are still available. All tests are running under tests/test*. Ramon Medeiros (4): Create new field to create VM Method to retrieve stored templates at object store Fix checking duplicate template before creating it Identify installation media while creating template API.json | 5 +++ model/templates.py | 9 ++-- utils.py | 30 +++++++++++++ vmtemplate.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 149 insertions(+), 16 deletions(-) -- 2.1.0

Create a single field to pass the installation media. Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- API.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/API.json b/API.json index 2b64d07..13fcfd3 100644 --- a/API.json +++ b/API.json @@ -460,6 +460,11 @@ "minimum": 512, "error": "KCHTMPL0013E" }, + "installation_media": { + "description": "Path for installation media (ISO, disk, remote ISO)", + "type" : "string", + "pattern" : "^[^ ]+( +[^ ]+)*$" + }, "cdrom": { "description": "Path for cdrom", "type": "string", -- 2.1.0

Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- utils.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/utils.py b/utils.py index 5a3f209..ae8954f 100644 --- a/utils.py +++ b/utils.py @@ -35,6 +35,36 @@ from wok.xmlutils.utils import xpath_get_text MAX_REDIRECTION_ALLOWED = 5 +def get_template_by_name(name): + + conn = sqlite3.connect(config.get_object_store(), timeout=10) + cursor = conn.cursor() + + # if * is passed: select all fields + sql = "SELECT json from objects where type=='template' and id=='%s'" % name + if name == "*": + sql = "SELECT json from objects where type == 'template'" + + # execute and fetch results + cursor.execute(sql) + content = cursor.fetchall() + + # no record: return nothing + if len(content) == 0: + return {} + + # sqllite returns a tuple of strings + # iterate over it and return a list of dictonaries + if len(content[0]) == 1: + return eval(content[0][0]) + + result = [] + for dictonary in content[0]: + result.append(eval(dictonary)) + + return result + + def _uri_to_name(collection, uri): expr = '/plugins/kimchi/%s/(.*?)$' % collection m = re.match(expr, uri) -- 2.1.0

Template was being check if duplicated after calling for VMTemplate, which can make unecessary work. Check it before creating the class. Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- model/templates.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/model/templates.py b/model/templates.py index c9b11c3..9c3f6d6 100644 --- a/model/templates.py +++ b/model/templates.py @@ -78,8 +78,11 @@ class TemplatesModel(object): raise InvalidParameter("KCHTMPL0003E", {'network': net_name, 'template': name}) # Creates the template class with necessary information - # Checkings will be done while creating this class, so any exception - # will be raised here + # template with the same name already exists: raise exception + with self.objstore as session: + if name in session.get_list('template'): + raise InvalidOperation("KCHTMPL0001E", {'name': name}) + t = LibvirtVMTemplate(params, scan=True, conn=self.conn) # Validate max memory @@ -98,8 +101,6 @@ class TemplatesModel(object): name = params['name'] try: with self.objstore as session: - if name in session.get_list('template'): - raise InvalidOperation("KCHTMPL0001E", {'name': name}) session.store('template', name, t.info, get_kimchi_version()) except InvalidOperation: -- 2.1.0

First, improve functions inside class. This will improve code reading, also, make more functions to split procedures like: validating disks, graphics xml and distro checking. Add separate function to identify the installation media. Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- vmtemplate.py | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 109 insertions(+), 12 deletions(-) diff --git a/vmtemplate.py b/vmtemplate.py index d629226..ee8be34 100644 --- a/vmtemplate.py +++ b/vmtemplate.py @@ -17,6 +17,7 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +import magic import os import platform import psutil @@ -30,10 +31,13 @@ from lxml.builder import E from wok.exception import InvalidParameter, ImageFormatError, IsoFormatError from wok.exception import MissingParameter, OperationFailed +from wok.xmlutils.utils import xpath_get_text from wok.plugins.kimchi import imageinfo from wok.plugins.kimchi import osinfo from wok.plugins.kimchi.isoinfo import IsoImage +from wok.plugins.kimchi.model.libvirtconnection import LibvirtConnection from wok.plugins.kimchi.utils import check_url_path, pool_name_from_uri +from wok.plugins.kimchi.utils import get_template_by_name from wok.plugins.kimchi.xmlutils.cpu import get_cpu_xml from wok.plugins.kimchi.xmlutils.disk import get_disk_xml from wok.plugins.kimchi.xmlutils.graphics import get_graphics_xml @@ -41,6 +45,9 @@ from wok.plugins.kimchi.xmlutils.interface import get_iface_xml from wok.plugins.kimchi.xmlutils.qemucmdline import get_qemucmdline_xml from wok.plugins.kimchi.xmlutils.serial import get_serial_xml +DISK_TYPE = {"QEMU QCOW Image": "qcow2", + "data": "raw"} +ISO_TYPE = "ISO 9660 CD-ROM" # In PowerPC, memories must be aligned to 256 MiB PPC_MEM_ALIGN = 256 @@ -61,6 +68,36 @@ class VMTemplate(object): self.info = {} self.fc_host_support = args.get('fc_host_support') + # search for template with the same name + template = get_template_by_name(args.get("name")) + + # no record found: create template + if len(template) == 0: + self._create_template(args, scan) + + # template already exists: load it + else: + self.info.update(args) + + # Merge graphics settings + graph_args = args.get('graphics') + if graph_args: + graphics = dict(self.info['graphics']) + graphics.update(graph_args) + args['graphics'] = graphics + + # validate disks + default_disk = self.info['disks'][0] + self.info.update(args) + self._validate_disks(default_disk) + + def _create_template(self, args, scan=False): + """ + Creates a new template + """ + # identify installation media + self._identify_installation_media(args) + # Fetch defaults based on the os distro and version try: distro, version = self._get_os_info(args, scan) @@ -69,23 +106,16 @@ class VMTemplate(object): os_distro = args.get('os_distro', distro) os_version = args.get('os_version', version) entry = osinfo.lookup(os_distro, os_version) + + # update info self.info.update(entry) # Auto-generate a template name if no one is passed if 'name' not in args or args['name'] == '': args['name'] = self._gen_name(distro, version) - self.name = args['name'] - - # Merge graphics settings - graph_args = args.get('graphics') - if graph_args: - graphics = dict(self.info['graphics']) - graphics.update(graph_args) - args['graphics'] = graphics + self.info["name"] = args['name'] - default_disk = self.info['disks'][0] - # Override template values according to 'args' - self.info.update(args) + def _validate_disks(self, default_disk): disks = self.info.get('disks') basic_disk = ['index', 'format', 'pool', 'size'] @@ -94,7 +124,6 @@ class VMTemplate(object): for index, disk in enumerate(disks): disk_info = dict(default_disk) - pool_type = self._get_storage_type(disk['pool']['name']) if pool_type in ['iscsi', 'scsi']: disk_info = {'index': 0, 'format': 'raw', 'volume': None} @@ -117,6 +146,74 @@ class VMTemplate(object): disk_info['index'] = disk_info.get('index', index) self.info['disks'][index] = disk_info + def _identify_installation_media(self, args): + path = args.get("installation_media") + + # user did not passed installation media: return + if path is None: + return + + # create magic object to discover file type + file_type = magic.open(magic.MAGIC_NONE) + file_type.load() + + # file is local and exists: check if it's disk or cdrom + if path.startswith("/") and os.path.exists(path): + ftype = file_type.file(path) + + # cdrom + if ISO_TYPE in ftype: + args["cdrom"] = path + return + + # disk + for types in DISK_TYPE.keys(): + if types in ftype: + + # no disk list: create it + if args.get("disks") is None: + args["disks"] = [] + + # append disk to the list + args["disks"].insert(0, {"base": path, + "format": DISK_TYPE[types], + "index": 0}) + # get storage pool + pool = self._get_storagepool_from_storagevolume(path) + args["disks"][0]['pool'] = pool + return + + # not local image: set as remote ISO + elif path.startswith("http") or path.startswith("ftp"): + args["cdrom"] = path + return + + def _get_storagepool_from_storagevolume(self, storagevolume_path): + conn = LibvirtConnection('qemu:///system').get() + storagepools = conn.listStoragePools() + storagepools += conn.listDefinedStoragePools() + + # discover storagepool + for storagepool in storagepools: + pool = conn.storagePoolLookupByName(storagepool) + spath = xpath_get_text(pool.XMLDesc(), "/pool/target/path")[0] + pool_type = xpath_get_text(pool.XMLDesc(), "/pool/@type")[0] + storagevolumes = sorted(map(lambda x: x.decode('utf-8'), + pool.listVolumes())) + for storagevolume in storagevolumes: + + # path does not exists: continue to the next + if not os.path.exists("%s/%s" % (spath, storagevolume)): + continue + + # storagepool found: return + volume = "%s/%s" % (spath, storagevolume) + if os.path.samefile(storagevolume_path, volume): + pool_uri = "/plugins/kimchi/storagepools/%s" % storagepool + return {"name": pool_uri, + "type": pool_type} + return {} + def _get_os_info(self, args, scan): distro = version = 'unknown' -- 2.1.0
participants (1)
-
Ramon Medeiros