X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/b179ce7256d4e6533aa9ef97985818f7052449f3..ee9516c87345c4f5edf5e9722bd4cabeaf704f98:/lib/ovf.py diff --git a/lib/ovf.py b/lib/ovf.py index 6b99782..dc75816 100644 --- a/lib/ovf.py +++ b/lib/ovf.py @@ -1,7 +1,7 @@ #!/usr/bin/python # -# Copyright (C) 2011 Google Inc. +# Copyright (C) 2011, 2012 Google Inc. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -45,9 +45,15 @@ try: except ImportError: import elementtree.ElementTree as ET +try: + ParseError = ET.ParseError # pylint: disable=E1103 +except AttributeError: + ParseError = None + from ganeti import constants from ganeti import errors from ganeti import utils +from ganeti import pathutils # Schemas used in OVF format @@ -72,28 +78,43 @@ FILE_EXTENSIONS = [ ] COMPRESSION_TYPE = "gzip" +NO_COMPRESSION = [None, "identity"] COMPRESS = "compression" DECOMPRESS = "decompression" ALLOWED_ACTIONS = [COMPRESS, DECOMPRESS] +VMDK = "vmdk" +RAW = "raw" +COW = "cow" +ALLOWED_FORMATS = [RAW, COW, VMDK] + # ResourceType values RASD_TYPE = { "vcpus": "3", "memory": "4", + "scsi-controller": "6", + "ethernet-adapter": "10", + "disk": "17", +} + +SCSI_SUBTYPE = "lsilogic" +VS_TYPE = { + "ganeti": "ganeti-ovf", + "external": "vmx-04", } # AllocationUnits values and conversion ALLOCATION_UNITS = { - 'b': ["bytes", "b"], - 'kb': ["kilobytes", "kb", "byte * 2^10", "kibibytes", "kib"], - 'mb': ["megabytes", "mb", "byte * 2^20", "mebibytes", "mib"], - 'gb': ["gigabytes", "gb", "byte * 2^30", "gibibytes", "gib"], + "b": ["bytes", "b"], + "kb": ["kilobytes", "kb", "byte * 2^10", "kibibytes", "kib"], + "mb": ["megabytes", "mb", "byte * 2^20", "mebibytes", "mib"], + "gb": ["gigabytes", "gb", "byte * 2^30", "gibibytes", "gib"], } CONVERT_UNITS_TO_MB = { - 'b': lambda x: x / (1024 * 1024), - 'kb': lambda x: x / 1024, - 'mb': lambda x: x, - 'gb': lambda x: x * 1024, + "b": lambda x: x / (1024 * 1024), + "kb": lambda x: x / 1024, + "mb": lambda x: x, + "gb": lambda x: x * 1024, } # Names of the config fields @@ -107,6 +128,33 @@ DISK_TEMPLATE = "disk_template" TAGS = "tags" VERSION = "version" +# Instance IDs of System and SCSI controller +INSTANCE_ID = { + "system": 0, + "vcpus": 1, + "memory": 2, + "scsi": 3, +} + +# Disk format descriptions +DISK_FORMAT = { + RAW: "http://en.wikipedia.org/wiki/Byte", + VMDK: "http://www.vmware.com/interfaces/specifications/vmdk.html" + "#monolithicSparse", + COW: "http://www.gnome.org/~markmc/qcow-image-format.html", +} + + +def CheckQemuImg(): + """ Make sure that qemu-img is present before performing operations. + + @raise errors.OpPrereqError: when qemu-img was not found in the system + + """ + if not constants.QEMUIMG_PATH: + raise errors.OpPrereqError("qemu-img not found at build time, unable" + " to continue", errors.ECODE_STATE) + def LinkFile(old_path, prefix=None, suffix=None, directory=None): """Create link with a given prefix and suffix. @@ -140,11 +188,12 @@ def LinkFile(old_path, prefix=None, suffix=None, directory=None): except OSError, err: if err.errno == errno.EEXIST: new_path = utils.PathJoin(directory, - "%s_%s%s" % (prefix, counter, suffix)) + "%s_%s%s" % (prefix, counter, suffix)) counter += 1 else: raise errors.OpPrereqError("Error moving the file %s to %s location:" - " %s" % (old_path, new_path, err)) + " %s" % (old_path, new_path, err), + errors.ECODE_ENVIRON) return new_path @@ -179,9 +228,9 @@ class OVFReader(object): self.tree = ET.ElementTree() try: self.tree.parse(input_path) - except xml.parsers.expat.ExpatError, err: + except (ParseError, xml.parsers.expat.ExpatError), err: raise errors.OpPrereqError("Error while reading %s file: %s" % - (OVF_EXT, err)) + (OVF_EXT, err), errors.ECODE_ENVIRON) # Create a list of all files in the OVF package (input_dir, input_file) = os.path.split(input_path) @@ -198,7 +247,8 @@ class OVFReader(object): for file_name in files_list: file_path = utils.PathJoin(input_dir, file_name) if not os.path.exists(file_path): - raise errors.OpPrereqError("File does not exist: %s" % file_path) + raise errors.OpPrereqError("File does not exist: %s" % file_path, + errors.ECODE_ENVIRON) logging.info("Files in the OVF package: %s", " ".join(files_list)) self.files_list = files_list self.input_dir = input_dir @@ -308,12 +358,13 @@ class OVFReader(object): sha1_sum = match.group(2) manifest_files[file_name] = sha1_sum files_with_paths = [utils.PathJoin(self.input_dir, file_name) - for file_name in self.files_list] + for file_name in self.files_list] sha1_sums = utils.FingerprintFiles(files_with_paths) for file_name, value in manifest_files.iteritems(): if sha1_sums.get(utils.PathJoin(self.input_dir, file_name)) != value: raise errors.OpPrereqError("SHA1 checksum of %s does not match the" - " value in manifest file" % file_name) + " value in manifest file" % file_name, + errors.ECODE_ENVIRON) logging.info("SHA1 checksums verified") def GetInstanceName(self): @@ -351,7 +402,7 @@ class OVFReader(object): return {"hypervisor_name": constants.VALUE_AUTO} results = { "hypervisor_name": hypervisor_data.findtext("{%s}Name" % GANETI_SCHEMA, - default=constants.VALUE_AUTO), + default=constants.VALUE_AUTO), } parameters = hypervisor_data.find("{%s}Parameters" % GANETI_SCHEMA) results.update(self._GetDictParameters(parameters, GANETI_SCHEMA)) @@ -390,7 +441,7 @@ class OVFReader(object): vcpus = self._GetElementMatchingText(find_vcpus, match_vcpus) if vcpus: vcpus_count = vcpus.findtext("{%s}VirtualQuantity" % RASD_SCHEMA, - default=constants.VALUE_AUTO) + default=constants.VALUE_AUTO) else: vcpus_count = constants.VALUE_AUTO results["vcpus"] = str(vcpus_count) @@ -401,21 +452,21 @@ class OVFReader(object): memory_raw = None if memory: alloc_units = memory.findtext("{%s}AllocationUnits" % RASD_SCHEMA) - matching_units = [units for units, variants in - ALLOCATION_UNITS.iteritems() if alloc_units.lower() in variants] + matching_units = [units for units, variants in ALLOCATION_UNITS.items() + if alloc_units.lower() in variants] if matching_units == []: - raise errors.OpPrereqError("Unit %s for RAM memory unknown", - alloc_units) + raise errors.OpPrereqError("Unit %s for RAM memory unknown" % + alloc_units, errors.ECODE_INVAL) units = matching_units[0] memory_raw = int(memory.findtext("{%s}VirtualQuantity" % RASD_SCHEMA, - default=constants.VALUE_AUTO)) + default=constants.VALUE_AUTO)) memory_count = CONVERT_UNITS_TO_MB[units](memory_raw) else: memory_count = constants.VALUE_AUTO results["memory"] = str(memory_count) find_balance = ("{%s}GanetiSection/{%s}AutoBalance" % - (GANETI_SCHEMA, GANETI_SCHEMA)) + (GANETI_SCHEMA, GANETI_SCHEMA)) balance = self.tree.findtext(find_balance, default=constants.VALUE_AUTO) results["auto_balance"] = balance @@ -463,8 +514,8 @@ class OVFReader(object): networks_search = ("{%s}NetworkSection/{%s}Network" % (OVF_SCHEMA, OVF_SCHEMA)) network_names = self._GetAttributes(networks_search, - "{%s}name" % OVF_SCHEMA) - required = ["ip", "mac", "link", "mode"] + "{%s}name" % OVF_SCHEMA) + required = ["ip", "mac", "link", "mode", "network"] for (counter, network_name) in enumerate(network_names): network_search = ("{%s}VirtualSystem/{%s}VirtualHardwareSection/{%s}Item" % (OVF_SCHEMA, OVF_SCHEMA, OVF_SCHEMA)) @@ -474,7 +525,7 @@ class OVFReader(object): ganeti_match = ("{%s}name" % OVF_SCHEMA, network_name) network_data = self._GetElementMatchingText(network_search, network_match) network_ganeti_data = self._GetElementMatchingAttr(ganeti_search, - ganeti_match) + ganeti_match) ganeti_data = {} if network_ganeti_data: @@ -486,6 +537,8 @@ class OVFReader(object): GANETI_SCHEMA) ganeti_data["link"] = network_ganeti_data.findtext("{%s}Link" % GANETI_SCHEMA) + ganeti_data["network"] = network_ganeti_data.findtext("{%s}Net" % + GANETI_SCHEMA) mac_data = None if network_data: mac_data = network_data.findtext("{%s}Address" % RASD_SCHEMA) @@ -532,18 +585,39 @@ class OVFReader(object): disk_elem = self._GetElementMatchingAttr(disk_search, disk_match) if disk_elem is None: raise errors.OpPrereqError("%s file corrupted - disk %s not found in" - " references" % (OVF_EXT, disk)) + " references" % (OVF_EXT, disk), + errors.ECODE_ENVIRON) disk_name = disk_elem.get("{%s}href" % OVF_SCHEMA) disk_compression = disk_elem.get("{%s}compression" % OVF_SCHEMA) results.append((disk_name, disk_compression)) return results +def SubElementText(parent, tag, text, attrib={}, **extra): +# pylint: disable=W0102 + """This is just a wrapper on ET.SubElement that always has text content. + + """ + if text is None: + return None + elem = ET.SubElement(parent, tag, attrib=attrib, **extra) + elem.text = str(text) + return elem + + class OVFWriter(object): """Writer class for OVF files. @type tree: ET.ElementTree @ivar tree: XML tree that we are constructing + @type virtual_system_type: string + @ivar virtual_system_type: value of vssd:VirtualSystemType, for external usage + in VMWare this requires to be vmx + @type hardware_list: list + @ivar hardware_list: list of items prepared for VirtualHardwareSection + @type next_instance_id: int + @ivar next_instance_id: next instance id to be used when creating elements on + hardware_list """ def __init__(self, has_gnt_section): @@ -564,7 +638,190 @@ class OVFWriter(object): } if has_gnt_section: env_attribs["xmlns:gnt"] = GANETI_SCHEMA + self.virtual_system_type = VS_TYPE["ganeti"] + else: + self.virtual_system_type = VS_TYPE["external"] self.tree = ET.Element("Envelope", attrib=env_attribs) + self.hardware_list = [] + # INSTANCE_ID contains statically assigned IDs, starting from 0 + self.next_instance_id = len(INSTANCE_ID) # FIXME: hackish + + def SaveDisksData(self, disks): + """Convert disk information to certain OVF sections. + + @type disks: list + @param disks: list of dictionaries of disk options from config.ini + + """ + references = ET.SubElement(self.tree, "References") + disk_section = ET.SubElement(self.tree, "DiskSection") + SubElementText(disk_section, "Info", "Virtual disk information") + for counter, disk in enumerate(disks): + file_id = "file%s" % counter + disk_id = "disk%s" % counter + file_attribs = { + "ovf:href": disk["path"], + "ovf:size": str(disk["real-size"]), + "ovf:id": file_id, + } + disk_attribs = { + "ovf:capacity": str(disk["virt-size"]), + "ovf:diskId": disk_id, + "ovf:fileRef": file_id, + "ovf:format": DISK_FORMAT.get(disk["format"], disk["format"]), + } + if "compression" in disk: + file_attribs["ovf:compression"] = disk["compression"] + ET.SubElement(references, "File", attrib=file_attribs) + ET.SubElement(disk_section, "Disk", attrib=disk_attribs) + + # Item in VirtualHardwareSection creation + disk_item = ET.Element("Item") + SubElementText(disk_item, "rasd:ElementName", disk_id) + SubElementText(disk_item, "rasd:HostResource", "ovf:/disk/%s" % disk_id) + SubElementText(disk_item, "rasd:InstanceID", self.next_instance_id) + SubElementText(disk_item, "rasd:Parent", INSTANCE_ID["scsi"]) + SubElementText(disk_item, "rasd:ResourceType", RASD_TYPE["disk"]) + self.hardware_list.append(disk_item) + self.next_instance_id += 1 + + def SaveNetworksData(self, networks): + """Convert network information to NetworkSection. + + @type networks: list + @param networks: list of dictionaries of network options form config.ini + + """ + network_section = ET.SubElement(self.tree, "NetworkSection") + SubElementText(network_section, "Info", "List of logical networks") + for counter, network in enumerate(networks): + network_name = "%s%s" % (network["mode"], counter) + network_attrib = {"ovf:name": network_name} + ET.SubElement(network_section, "Network", attrib=network_attrib) + + # Item in VirtualHardwareSection creation + network_item = ET.Element("Item") + SubElementText(network_item, "rasd:Address", network["mac"]) + SubElementText(network_item, "rasd:Connection", network_name) + SubElementText(network_item, "rasd:ElementName", network_name) + SubElementText(network_item, "rasd:InstanceID", self.next_instance_id) + SubElementText(network_item, "rasd:ResourceType", + RASD_TYPE["ethernet-adapter"]) + self.hardware_list.append(network_item) + self.next_instance_id += 1 + + @staticmethod + def _SaveNameAndParams(root, data): + """Save name and parameters information under root using data. + + @type root: ET.Element + @param root: root element for the Name and Parameters + @type data: dict + @param data: data from which we gather the values + + """ + assert(data.get("name")) + name = SubElementText(root, "gnt:Name", data["name"]) + params = ET.SubElement(root, "gnt:Parameters") + for name, value in data.iteritems(): + if name != "name": + SubElementText(params, "gnt:%s" % name, value) + + def SaveGanetiData(self, ganeti, networks): + """Convert Ganeti-specific information to GanetiSection. + + @type ganeti: dict + @param ganeti: dictionary of Ganeti-specific options from config.ini + @type networks: list + @param networks: list of dictionaries of network options form config.ini + + """ + ganeti_section = ET.SubElement(self.tree, "gnt:GanetiSection") + + SubElementText(ganeti_section, "gnt:Version", ganeti.get("version")) + SubElementText(ganeti_section, "gnt:DiskTemplate", + ganeti.get("disk_template")) + SubElementText(ganeti_section, "gnt:AutoBalance", + ganeti.get("auto_balance")) + SubElementText(ganeti_section, "gnt:Tags", ganeti.get("tags")) + + osys = ET.SubElement(ganeti_section, "gnt:OperatingSystem") + self._SaveNameAndParams(osys, ganeti["os"]) + + hypervisor = ET.SubElement(ganeti_section, "gnt:Hypervisor") + self._SaveNameAndParams(hypervisor, ganeti["hypervisor"]) + + network_section = ET.SubElement(ganeti_section, "gnt:Network") + for counter, network in enumerate(networks): + network_name = "%s%s" % (network["mode"], counter) + nic_attrib = {"ovf:name": network_name} + nic = ET.SubElement(network_section, "gnt:Nic", attrib=nic_attrib) + SubElementText(nic, "gnt:Mode", network["mode"]) + SubElementText(nic, "gnt:MACAddress", network["mac"]) + SubElementText(nic, "gnt:IPAddress", network["ip"]) + SubElementText(nic, "gnt:Link", network["link"]) + SubElementText(nic, "gnt:Net", network["network"]) + + def SaveVirtualSystemData(self, name, vcpus, memory): + """Convert virtual system information to OVF sections. + + @type name: string + @param name: name of the instance + @type vcpus: int + @param vcpus: number of VCPUs + @type memory: int + @param memory: RAM memory in MB + + """ + assert(vcpus > 0) + assert(memory > 0) + vs_attrib = {"ovf:id": name} + virtual_system = ET.SubElement(self.tree, "VirtualSystem", attrib=vs_attrib) + SubElementText(virtual_system, "Info", "A virtual machine") + + name_section = ET.SubElement(virtual_system, "Name") + name_section.text = name + os_attrib = {"ovf:id": "0"} + os_section = ET.SubElement(virtual_system, "OperatingSystemSection", + attrib=os_attrib) + SubElementText(os_section, "Info", "Installed guest operating system") + hardware_section = ET.SubElement(virtual_system, "VirtualHardwareSection") + SubElementText(hardware_section, "Info", "Virtual hardware requirements") + + # System description + system = ET.SubElement(hardware_section, "System") + SubElementText(system, "vssd:ElementName", "Virtual Hardware Family") + SubElementText(system, "vssd:InstanceID", INSTANCE_ID["system"]) + SubElementText(system, "vssd:VirtualSystemIdentifier", name) + SubElementText(system, "vssd:VirtualSystemType", self.virtual_system_type) + + # Item for vcpus + vcpus_item = ET.SubElement(hardware_section, "Item") + SubElementText(vcpus_item, "rasd:ElementName", + "%s virtual CPU(s)" % vcpus) + SubElementText(vcpus_item, "rasd:InstanceID", INSTANCE_ID["vcpus"]) + SubElementText(vcpus_item, "rasd:ResourceType", RASD_TYPE["vcpus"]) + SubElementText(vcpus_item, "rasd:VirtualQuantity", vcpus) + + # Item for memory + memory_item = ET.SubElement(hardware_section, "Item") + SubElementText(memory_item, "rasd:AllocationUnits", "byte * 2^20") + SubElementText(memory_item, "rasd:ElementName", "%sMB of memory" % memory) + SubElementText(memory_item, "rasd:InstanceID", INSTANCE_ID["memory"]) + SubElementText(memory_item, "rasd:ResourceType", RASD_TYPE["memory"]) + SubElementText(memory_item, "rasd:VirtualQuantity", memory) + + # Item for scsi controller + scsi_item = ET.SubElement(hardware_section, "Item") + SubElementText(scsi_item, "rasd:Address", INSTANCE_ID["system"]) + SubElementText(scsi_item, "rasd:ElementName", "scsi_controller0") + SubElementText(scsi_item, "rasd:InstanceID", INSTANCE_ID["scsi"]) + SubElementText(scsi_item, "rasd:ResourceSubType", SCSI_SUBTYPE) + SubElementText(scsi_item, "rasd:ResourceType", RASD_TYPE["scsi-controller"]) + + # Other items - from self.hardware_list + for item in self.hardware_list: + hardware_section.append(item) def PrettyXmlDump(self): """Formatter of the XML file. @@ -611,7 +868,8 @@ class Converter(object): """ input_path = os.path.abspath(input_path) if not os.path.isfile(input_path): - raise errors.OpPrereqError("File does not exist: %s" % input_path) + raise errors.OpPrereqError("File does not exist: %s" % input_path, + errors.ECODE_ENVIRON) self.options = options self.temp_file_manager = utils.TemporaryFileManager() self.temp_dir = None @@ -647,7 +905,7 @@ class Converter(object): # For now we only support gzip, as it is used in ovftool if compression != COMPRESSION_TYPE: raise errors.OpPrereqError("Unsupported compression type: %s" - % compression) + % compression, errors.ECODE_INVAL) disk_file = os.path.basename(disk_path) if action == DECOMPRESS: (disk_name, _) = os.path.splitext(disk_file) @@ -655,13 +913,14 @@ class Converter(object): elif action == COMPRESS: prefix = disk_file new_path = utils.GetClosedTempfile(suffix=COMPRESSION_EXT, prefix=prefix, - dir=self.output_dir) + dir=self.output_dir) self.temp_file_manager.Add(new_path) args = ["gzip", "-c", disk_path] run_result = utils.RunCmd(args, output=new_path) if run_result.failed: raise errors.OpPrereqError("Disk %s failed with output: %s" - % (action, run_result.stderr)) + % (action, run_result.stderr), + errors.ECODE_ENVIRON) logging.info("The %s of the disk is completed", action) return (COMPRESSION_EXT, new_path) @@ -678,17 +937,18 @@ class Converter(object): @raise errors.OpPrereqError: convertion of the disk failed """ + CheckQemuImg() disk_file = os.path.basename(disk_path) (disk_name, disk_extension) = os.path.splitext(disk_file) if disk_extension != disk_format: logging.warning("Conversion of disk image to %s format, this may take" " a while", disk_format) - new_disk_path = utils.GetClosedTempfile(suffix=".%s" % disk_format, - prefix=disk_name, dir=self.output_dir) + new_disk_path = utils.GetClosedTempfile( + suffix=".%s" % disk_format, prefix=disk_name, dir=self.output_dir) self.temp_file_manager.Add(new_disk_path) args = [ - "qemu-img", + constants.QEMUIMG_PATH, "convert", "-O", disk_format, @@ -698,7 +958,8 @@ class Converter(object): run_result = utils.RunCmd(args, cwd=os.getcwd()) if run_result.failed: raise errors.OpPrereqError("Convertion to %s failed, qemu-img output was" - ": %s" % (disk_format, run_result.stderr)) + ": %s" % (disk_format, run_result.stderr), + errors.ECODE_ENVIRON) return (".%s" % disk_format, new_disk_path) @staticmethod @@ -715,11 +976,13 @@ class Converter(object): @raise errors.OpPrereqError: format information cannot be retrieved """ - args = ["qemu-img", "info", disk_path] + CheckQemuImg() + args = [constants.QEMUIMG_PATH, "info", disk_path] run_result = utils.RunCmd(args, cwd=os.getcwd()) if run_result.failed: raise errors.OpPrereqError("Gathering info about the disk using qemu-img" - " failed, output was: %s" % run_result.stderr) + " failed, output was: %s" % run_result.stderr, + errors.ECODE_ENVIRON) result = run_result.output regexp = r"%s" % regexp match = re.search(regexp, result) @@ -727,7 +990,8 @@ class Converter(object): disk_format = match.group(1) else: raise errors.OpPrereqError("No file information matching %s found in:" - " %s" % (regexp, result)) + " %s" % (regexp, result), + errors.ECODE_ENVIRON) return disk_format def Parse(self): @@ -812,20 +1076,21 @@ class OVFImporter(Converter): self._UnpackOVA(input_path) else: raise errors.OpPrereqError("Unknown file extension; expected %s or %s" - " file" % (OVA_EXT, OVF_EXT)) + " file" % (OVA_EXT, OVF_EXT), + errors.ECODE_INVAL) assert ((input_extension == OVA_EXT and self.temp_dir) or (input_extension == OVF_EXT and not self.temp_dir)) assert self.input_dir in self.input_path if self.options.output_dir: self.output_dir = os.path.abspath(self.options.output_dir) - if (os.path.commonprefix([constants.EXPORT_DIR, self.output_dir]) != - constants.EXPORT_DIR): + if (os.path.commonprefix([pathutils.EXPORT_DIR, self.output_dir]) != + pathutils.EXPORT_DIR): logging.warning("Export path is not under %s directory, import to" " Ganeti using gnt-backup may fail", - constants.EXPORT_DIR) + pathutils.EXPORT_DIR) else: - self.output_dir = constants.EXPORT_DIR + self.output_dir = pathutils.EXPORT_DIR self.ovf_reader = OVFReader(self.input_path) self.ovf_reader.VerifyManifest() @@ -844,7 +1109,7 @@ class OVFImporter(Converter): input_name = None if not tarfile.is_tarfile(input_path): raise errors.OpPrereqError("The provided %s file is not a proper tar" - " archive", OVA_EXT) + " archive" % OVA_EXT, errors.ECODE_ENVIRON) ova_content = tarfile.open(input_path) temp_dir = tempfile.mkdtemp() self.temp_dir = temp_dir @@ -854,14 +1119,14 @@ class OVFImporter(Converter): utils.PathJoin(temp_dir, file_normname) except ValueError, err: raise errors.OpPrereqError("File %s inside %s package is not safe" % - (file_name, OVA_EXT)) + (file_name, OVA_EXT), errors.ECODE_ENVIRON) if file_name.endswith(OVF_EXT): input_name = file_name if not input_name: raise errors.OpPrereqError("No %s file in %s package found" % - (OVF_EXT, OVA_EXT)) + (OVF_EXT, OVA_EXT), errors.ECODE_ENVIRON) logging.warning("Unpacking the %s archive, this may take a while", - input_path) + input_path) self.input_dir = temp_dir self.input_path = utils.PathJoin(self.temp_dir, input_name) try: @@ -875,7 +1140,7 @@ class OVFImporter(Converter): extract(self.temp_dir) except tarfile.TarError, err: raise errors.OpPrereqError("Error while extracting %s archive: %s" % - (OVA_EXT, err)) + (OVA_EXT, err), errors.ECODE_ENVIRON) logging.info("OVA package extracted to %s directory", self.temp_dir) def Parse(self): @@ -889,43 +1154,47 @@ class OVFImporter(Converter): """ self.results_name = self._GetInfo("instance name", self.options.name, - self._ParseNameOptions, self.ovf_reader.GetInstanceName) + self._ParseNameOptions, + self.ovf_reader.GetInstanceName) if not self.results_name: - raise errors.OpPrereqError("Name of instance not provided") + raise errors.OpPrereqError("Name of instance not provided", + errors.ECODE_INVAL) self.output_dir = utils.PathJoin(self.output_dir, self.results_name) try: utils.Makedirs(self.output_dir) except OSError, err: raise errors.OpPrereqError("Failed to create directory %s: %s" % - (self.output_dir, err)) + (self.output_dir, err), errors.ECODE_ENVIRON) - self.results_template = self._GetInfo("disk template", - self.options.disk_template, self._ParseTemplateOptions, + self.results_template = self._GetInfo( + "disk template", self.options.disk_template, self._ParseTemplateOptions, self.ovf_reader.GetDiskTemplate) if not self.results_template: logging.info("Disk template not given") - self.results_hypervisor = self._GetInfo("hypervisor", - self.options.hypervisor, self._ParseHypervisorOptions, + self.results_hypervisor = self._GetInfo( + "hypervisor", self.options.hypervisor, self._ParseHypervisorOptions, self.ovf_reader.GetHypervisorData) assert self.results_hypervisor["hypervisor_name"] if self.results_hypervisor["hypervisor_name"] == constants.VALUE_AUTO: logging.debug("Default hypervisor settings from the cluster will be used") - self.results_os = self._GetInfo("OS", self.options.os, - self._ParseOSOptions, self.ovf_reader.GetOSData) + self.results_os = self._GetInfo( + "OS", self.options.os, self._ParseOSOptions, self.ovf_reader.GetOSData) if not self.results_os.get("os_name"): - raise errors.OpPrereqError("OS name must be provided") + raise errors.OpPrereqError("OS name must be provided", + errors.ECODE_INVAL) - self.results_backend = self._GetInfo("backend", self.options.beparams, + self.results_backend = self._GetInfo( + "backend", self.options.beparams, self._ParseBackendOptions, self.ovf_reader.GetBackendData) assert self.results_backend.get("vcpus") assert self.results_backend.get("memory") assert self.results_backend.get("auto_balance") is not None - self.results_tags = self._GetInfo("tags", self.options.tags, - self._ParseTags, self.ovf_reader.GetTagsData) + self.results_tags = self._GetInfo( + "tags", self.options.tags, self._ParseTags, self.ovf_reader.GetTagsData) ovf_version = self.ovf_reader.GetVersionData() if ovf_version: @@ -933,21 +1202,22 @@ class OVFImporter(Converter): else: self.results_version = constants.EXPORT_VERSION - self.results_network = self._GetInfo("network", self.options.nics, - self._ParseNicOptions, self.ovf_reader.GetNetworkData, - ignore_test=self.options.no_nics) + self.results_network = self._GetInfo( + "network", self.options.nics, self._ParseNicOptions, + self.ovf_reader.GetNetworkData, ignore_test=self.options.no_nics) - self.results_disk = self._GetInfo("disk", self.options.disks, - self._ParseDiskOptions, self._GetDiskInfo, + self.results_disk = self._GetInfo( + "disk", self.options.disks, self._ParseDiskOptions, self._GetDiskInfo, ignore_test=self.results_template == constants.DT_DISKLESS) if not self.results_disk and not self.results_network: raise errors.OpPrereqError("Either disk specification or network" - " description must be present") + " description must be present", + errors.ECODE_STATE) @staticmethod def _GetInfo(name, cmd_arg, cmd_function, nocmd_function, - ignore_test=False): + ignore_test=False): """Get information about some section - e.g. disk, network, hypervisor. @type name: string @@ -968,7 +1238,7 @@ class OVFImporter(Converter): results = cmd_function() else: logging.info("Information for %s will be parsed from %s file", - name, OVF_EXT) + name, OVF_EXT) results = nocmd_function() logging.info("Options for %s were succesfully read", name) return results @@ -1062,6 +1332,8 @@ class OVFImporter(Converter): results["nic%s_mac" % nic_id] = nic_desc.get("mac", constants.VALUE_AUTO) results["nic%s_link" % nic_id] = \ nic_desc.get("link", constants.VALUE_AUTO) + results["nic%s_network" % nic_id] = \ + nic_desc.get("network", constants.VALUE_AUTO) if nic_desc.get("mode") == "bridged": results["nic%s_ip" % nic_id] = constants.VALUE_NONE else: @@ -1079,6 +1351,7 @@ class OVFImporter(Converter): information or size information is invalid or creation failed """ + CheckQemuImg() assert self.options.disks results = {} for (disk_id, disk_desc) in self.options.disks: @@ -1088,10 +1361,11 @@ class OVFImporter(Converter): disk_size = utils.ParseUnit(disk_desc["size"]) except ValueError: raise errors.OpPrereqError("Invalid disk size for disk %s: %s" % - (disk_id, disk_desc["size"])) + (disk_id, disk_desc["size"]), + errors.ECODE_INVAL) new_path = utils.PathJoin(self.output_dir, str(disk_id)) args = [ - "qemu-img", + constants.QEMUIMG_PATH, "create", "-f", "raw", @@ -1101,12 +1375,14 @@ class OVFImporter(Converter): run_result = utils.RunCmd(args) if run_result.failed: raise errors.OpPrereqError("Creation of disk %s failed, output was:" - " %s" % (new_path, run_result.stderr)) + " %s" % (new_path, run_result.stderr), + errors.ECODE_ENVIRON) results["disk%s_size" % disk_id] = str(disk_size) results["disk%s_dump" % disk_id] = "disk%s.raw" % disk_id else: raise errors.OpPrereqError("Disks created for import must have their" - " size specified") + " size specified", + errors.ECODE_INVAL) results["disk_count"] = str(len(self.options.disks)) return results @@ -1124,19 +1400,20 @@ class OVFImporter(Converter): for (counter, (disk_name, disk_compression)) in enumerate(disks_list): if os.path.dirname(disk_name): raise errors.OpPrereqError("Disks are not allowed to have absolute" - " paths or paths outside main OVF directory") + " paths or paths outside main OVF" + " directory", errors.ECODE_ENVIRON) disk, _ = os.path.splitext(disk_name) disk_path = utils.PathJoin(self.input_dir, disk_name) - if disk_compression: + if disk_compression not in NO_COMPRESSION: _, disk_path = self._CompressDisk(disk_path, disk_compression, - DECOMPRESS) + DECOMPRESS) disk, _ = os.path.splitext(disk) if self._GetDiskQemuInfo(disk_path, "file format: (\S+)") != "raw": logging.info("Conversion to raw format is required") ext, new_disk_path = self._ConvertDisk("raw", disk_path) final_disk_path = LinkFile(new_disk_path, prefix=disk, suffix=ext, - directory=self.output_dir) + directory=self.output_dir) final_name = os.path.basename(final_disk_path) disk_size = os.path.getsize(final_disk_path) / (1024 * 1024) results["disk%s_dump" % counter] = final_name @@ -1184,7 +1461,7 @@ class OVFImporter(Converter): results[constants.INISECT_HYP].update(self.results_hypervisor) output_file_name = utils.PathJoin(self.output_dir, - constants.EXPORT_CONF_FILE) + constants.EXPORT_CONF_FILE) output = [] for section, options in results.iteritems(): @@ -1199,7 +1476,8 @@ class OVFImporter(Converter): try: utils.WriteFile(output_file_name, data=output_contents) except errors.ProgrammerError, err: - raise errors.OpPrereqError("Saving the config file failed: %s" % err) + raise errors.OpPrereqError("Saving the config file failed: %s" % err, + errors.ECODE_ENVIRON) self.Cleanup() @@ -1210,8 +1488,8 @@ class ConfigParserWithDefaults(ConfigParser.SafeConfigParser): """ def get(self, section, options, raw=None, vars=None): # pylint: disable=W0622 try: - result = ConfigParser.SafeConfigParser.get(self, section, options, \ - raw=raw, vars=vars) + result = ConfigParser.SafeConfigParser.get(self, section, options, + raw=raw, vars=vars) except ConfigParser.NoOptionError: result = None return result @@ -1241,6 +1519,8 @@ class OVFExporter(Converter): @ivar output_path: complete path to .ovf file @type config_parser: L{ConfigParserWithDefaults} @ivar config_parser: parser for the config.ini file + @type reference_files: list + @ivar reference_files: files referenced in the ovf file @type results_disk: list @ivar results_disk: list of dictionaries of disk options from config.ini @type results_network: list @@ -1277,7 +1557,7 @@ class OVFExporter(Converter): self.config_parser.read(input_path) except ConfigParser.MissingSectionHeaderError, err: raise errors.OpPrereqError("Error when trying to read %s: %s" % - (input_path, err)) + (input_path, err), errors.ECODE_ENVIRON) if self.options.ova_package: self.temp_dir = tempfile.mkdtemp() self.packed_dir = self.output_dir @@ -1299,7 +1579,8 @@ class OVFExporter(Converter): else: name = self.config_parser.get(constants.INISECT_INS, NAME) if name is None: - raise errors.OpPrereqError("No instance name found") + raise errors.OpPrereqError("No instance name found", + errors.ECODE_ENVIRON) return name def _ParseVCPUs(self): @@ -1313,7 +1594,8 @@ class OVFExporter(Converter): """ vcpus = self.config_parser.getint(constants.INISECT_BEP, VCPUS) if vcpus == 0: - raise errors.OpPrereqError("No CPU information found") + raise errors.OpPrereqError("No CPU information found", + errors.ECODE_ENVIRON) return vcpus def _ParseMemory(self): @@ -1327,7 +1609,8 @@ class OVFExporter(Converter): """ memory = self.config_parser.getint(constants.INISECT_BEP, MEMORY) if memory == 0: - raise errors.OpPrereqError("No memory information found") + raise errors.OpPrereqError("No memory information found", + errors.ECODE_ENVIRON) return memory def _ParseGaneti(self): @@ -1342,7 +1625,8 @@ class OVFExporter(Converter): results["hypervisor"] = {} hyp_name = self.config_parser.get(constants.INISECT_INS, HYPERV) if hyp_name is None: - raise errors.OpPrereqError("No hypervisor information found") + raise errors.OpPrereqError("No hypervisor information found", + errors.ECODE_ENVIRON) results["hypervisor"]["name"] = hyp_name pairs = self.config_parser.items(constants.INISECT_HYP) for (name, value) in pairs: @@ -1351,7 +1635,8 @@ class OVFExporter(Converter): results["os"] = {} os_name = self.config_parser.get(constants.INISECT_EXP, OS) if os_name is None: - raise errors.OpPrereqError("No operating system information found") + raise errors.OpPrereqError("No operating system information found", + errors.ECODE_ENVIRON) results["os"]["name"] = os_name pairs = self.config_parser.items(constants.INISECT_OSP) for (name, value) in pairs: @@ -1380,21 +1665,25 @@ class OVFExporter(Converter): counter = 0 while True: data_link = \ - self.config_parser.get(constants.INISECT_INS, "nic%s_link" % counter) + self.config_parser.get(constants.INISECT_INS, + "nic%s_link" % counter) if data_link is None: break results.append({ "mode": self.config_parser.get(constants.INISECT_INS, - "nic%s_mode" % counter), + "nic%s_mode" % counter), "mac": self.config_parser.get(constants.INISECT_INS, - "nic%s_mac" % counter), + "nic%s_mac" % counter), "ip": self.config_parser.get(constants.INISECT_INS, - "nic%s_ip" % counter), + "nic%s_ip" % counter), + "network": self.config_parser.get(constants.INISECT_INS, + "nic%s_network" % counter), "link": data_link, }) if results[counter]["mode"] not in constants.NIC_VALID_MODES: raise errors.OpPrereqError("Network mode %s not recognized" - % results[counter]["mode"]) + % results[counter]["mode"], + errors.ECODE_INVAL) counter += 1 return results @@ -1412,23 +1701,24 @@ class OVFExporter(Converter): disk_path = utils.PathJoin(self.input_dir, disk_file) results = {} if not os.path.isfile(disk_path): - raise errors.OpPrereqError("Disk image does not exist: %s" % disk_path) + raise errors.OpPrereqError("Disk image does not exist: %s" % disk_path, + errors.ECODE_ENVIRON) if os.path.dirname(disk_file): raise errors.OpPrereqError("Path for the disk: %s contains a directory" - " name" % disk_path) + " name" % disk_path, errors.ECODE_ENVIRON) disk_name, _ = os.path.splitext(disk_file) ext, new_disk_path = self._ConvertDisk(self.options.disk_format, disk_path) results["format"] = self.options.disk_format - results["virt-size"] = self._GetDiskQemuInfo(new_disk_path, - "virtual size: \S+ \((\d+) bytes\)") + results["virt-size"] = self._GetDiskQemuInfo( + new_disk_path, "virtual size: \S+ \((\d+) bytes\)") if compression: ext2, new_disk_path = self._CompressDisk(new_disk_path, "gzip", - COMPRESS) + COMPRESS) disk_name, _ = os.path.splitext(disk_name) results["compression"] = "gzip" ext += ext2 final_disk_path = LinkFile(new_disk_path, prefix=disk_name, suffix=ext, - directory=self.output_dir) + directory=self.output_dir) final_disk_name = os.path.basename(final_disk_path) results["real-size"] = os.path.getsize(final_disk_path) results["path"] = final_disk_name @@ -1461,7 +1751,7 @@ class OVFExporter(Converter): utils.Makedirs(self.output_dir) except OSError, err: raise errors.OpPrereqError("Failed to create directory %s: %s" % - (self.output_dir, err)) + (self.output_dir, err), errors.ECODE_ENVIRON) self.references_files = [] self.results_name = self._ParseName() @@ -1495,7 +1785,8 @@ class OVFExporter(Converter): try: utils.WriteFile(path, data=data) except errors.ProgrammerError, err: - raise errors.OpPrereqError("Saving the manifest file failed: %s" % err) + raise errors.OpPrereqError("Saving the manifest file failed: %s" % err, + errors.ECODE_ENVIRON) @staticmethod def _PrepareTarFile(tar_path, files_list): @@ -1510,8 +1801,9 @@ class OVFExporter(Converter): logging.info("Preparing tarball for the OVF package") open(tar_path, mode="w").close() ova_package = tarfile.open(name=tar_path, mode="w") - for file_name in files_list: - ova_package.add(file_name) + for file_path in files_list: + file_name = os.path.basename(file_path) + ova_package.add(file_path, arcname=file_name) ova_package.close() def Save(self): @@ -1528,6 +1820,14 @@ class OVFExporter(Converter): self.output_path = utils.PathJoin(self.output_dir, output_file) files_list = [self.output_path] + self.ovf_writer.SaveDisksData(self.results_disk) + self.ovf_writer.SaveNetworksData(self.results_network) + if not self.options.ext_usage: + self.ovf_writer.SaveGanetiData(self.results_ganeti, self.results_network) + + self.ovf_writer.SaveVirtualSystemData(self.results_name, self.results_vcpus, + self.results_memory) + data = self.ovf_writer.PrettyXmlDump() utils.WriteFile(self.output_path, data=data) @@ -1545,7 +1845,8 @@ class OVFExporter(Converter): utils.Makedirs(self.packed_dir) except OSError, err: raise errors.OpPrereqError("Failed to create directory %s: %s" % - (self.packed_dir, err)) + (self.packed_dir, err), + errors.ECODE_ENVIRON) self._PrepareTarFile(packed_path, files_list) logging.info("Creation of the OVF package was successfull") self.Cleanup()