Am 29. August 2011 18:21 schrieb Agata Murawska <[email protected]>:
> + def GetDisksNames(self):
> + results = []
> + disks_search = "{%s}DiskSection/{%s}Disk" % (OVF_SCHEMA, OVF_SCHEMA)
> + disk_ids = self._GetAttributes(disks_search, "{%s}fileRef" % OVF_SCHEMA)
> + for disk in disk_ids:
> + disk_search = "{%s}References/{%s}File" % (OVF_SCHEMA, OVF_SCHEMA)
> + disk_match = ("{%s}id" % OVF_SCHEMA, disk)
> + disk_elem = self._GetElementMatchingAttr(disk_search, disk_match)
> + if disk_elem == None:
Never compare directly to None, always use “is” operator.
> + def _ParseDiskOptions(self):
> + """Parses disk options given in a command line.
> +
> + @rtype: dict
> + @return: dictionary of disk-related options
> +
> + @raise errors.OpPrereqError: disk description does not contain size
> + information or size information is invalid
> +
> + """
> + assert self.options.disks
> + results = {}
> + for (disk_id, disk_desc) in self.options.disks:
> + results["disk%s_ivname" % disk_id] = "disk/%s" % disk_id
> + if disk_desc.get("size"):
> + try:
> + disk_size = utils.ParseUnit(disk_desc["size"])
> + except ValueError:
> + raise errors.OpPrereqError("Invalid disk size for disk %s: %s" %
> + (disk_id, disk_desc["size"]))
> + args = [
> + "qemu-img",
> + "create",
> + "-f",
> + "raw",
> + "%s/disk%s.raw" % (self.output_dir, disk_id),
utils.PathJoin
> + disk_size,
> + ]
> + utils.RunCmd(args)
You don't check whether the command succeeded. At least a warning
should be given.
> + 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")
> + results["disk_count"] = str(len(self.options.disks))
> + return results
> +
> + def _GetDiskFormat(self, disk_path):
> + """Figures out the format of the disk using qemu-img.
> +
> + @type disk_path: string
> + @param disk_path: path to the disk we want to know the format of
> + @rtype: string
> + @return: disk format
> +
> + """
> + args = ["qemu-img", "info", disk_path]
> + result = utils.RunCmd(args, cwd=os.getcwd())
> + if result.failed:
> + raise errors.OpPrereqError("Gathering info about the disk using
> qemu-img"
> + " failed, output was:\n\t%s" %
> result.stderr)
> + result = result.output
> + regexp = r"file format: (\S+)"
> + match = re.search(regexp, result)
> + if match:
> + disk_format = match.group(1)
> + else:
> + raise errors.OpPrereqError("No file format information found in:\n%s" %
> + result)
> + # TODO: checking if disk_format is supported by qemu-img?
> + return disk_format
> +
> + def _ConvertDiskToRaw(self, disk_path, new_disk_path):
> + """Performes conversion to raw format.
> +
> + @type disk_path: string
> + @param disk_path: path to the disk that should be converted
> + @type new_disk_path: string
> + @param new_disk_path: path to the output disk
> +
> + """
> + args = [
> + "qemu-img",
> + "convert",
> + "-O",
> + "raw",
> + disk_path,
> + new_disk_path,
> + ]
> + result = utils.RunCmd(args, cwd=os.getcwd())
> + if result.failed:
> + raise errors.OpPrereqError("Convertion to raw failed, qemu-img output
> was"
> + ":\n\t%s" % result.stderr)
> +
> + def _GetDiskInfo(self):
> + """Gathers information about disks used by instance, perfomes conversion.
> +
> + @rtype: dict
> + @return: dictionary of disk-related options
> +
> + """
> + results = {}
> + disks_list = self.ovf_reader.GetDisksNames()
> + 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")
> + if disk_compression:
> + raise NotImplementedError()
> + disk_path = utils.PathJoin(self.input_dir, disk_name)
> + (name, _) = os.path.splitext(disk_name)
> + new_disk_path = "%s/%s.raw" % (self.output_dir, name)
utils.PathJoin
> + if self._GetDiskFormat(disk_path) != "raw":
> + logging.info("Conversion to raw format is required")
> + logging.warning("Conversion of disk image to raw format, this may
> take"
> + " a while")
> + self._ConvertDiskToRaw(disk_path, new_disk_path)
> + disk_size = os.path.getsize(new_disk_path) / (1024 * 1024)
> + results["disk%s_dump" % str(counter)] = "%s.raw" % name
> + results["disk%s_size" % str(counter)] = str(disk_size)
> + results["disk%s_ivname" % str(counter)] = "disk/%s" % str(counter)
> + if disks_list:
> + results["disk_count"] = str(len(disks_list))
> + return results
>
> def Save(self):
> - pass
> + """Saves all the gathered information in a constant.EXPORT_CONF_FILE
> file.
> +
> + """
> + logging.info("Conversion was succesfull, saving %s in %s directory",
> + constants.EXPORT_CONF_FILE, self.output_dir)
> + results = {}
> + results[constants.INISECT_INS] = {}
Please initialize the dictionary right away, e.g.:
results = {
constants.INISECT_INS: {},
constants.INISECT_BEP: {},
…
}
> + results[constants.INISECT_BEP] = {}
> + results[constants.INISECT_EXP] = {}
> + results[constants.INISECT_OSP] = {}
> + results[constants.INISECT_HYP] = {}
> +
> + results[constants.INISECT_INS].update(self.results_disk)
> + results[constants.INISECT_INS]["name"] = self.results_name
> +
> + output_file_name = "%s/%s" % (self.output_dir,
> constants.EXPORT_CONF_FILE)
utils.PathJoin
> + output_file = open(output_file_name, "w")
utils.WriteFile?
> + for section, options in results.iteritems():
> + output_file.write("[%s]\n" % section)
> + for name, value in options.iteritems():
> + output_file.write("%s = %s\n" % (name, value))
> + output_file.write("\n")
> + output_file.close()
Michael