Revision 5ae4945a lib/ovf.py

b/lib/ovf.py
187 187
    except OSError, err:
188 188
      if err.errno == errno.EEXIST:
189 189
        new_path = utils.PathJoin(directory,
190
          "%s_%s%s" % (prefix, counter, suffix))
190
                                  "%s_%s%s" % (prefix, counter, suffix))
191 191
        counter += 1
192 192
      else:
193 193
        raise errors.OpPrereqError("Error moving the file %s to %s location:"
......
357 357
          sha1_sum = match.group(2)
358 358
          manifest_files[file_name] = sha1_sum
359 359
      files_with_paths = [utils.PathJoin(self.input_dir, file_name)
360
        for file_name in self.files_list]
360
                          for file_name in self.files_list]
361 361
      sha1_sums = utils.FingerprintFiles(files_with_paths)
362 362
      for file_name, value in manifest_files.iteritems():
363 363
        if sha1_sums.get(utils.PathJoin(self.input_dir, file_name)) != value:
......
401 401
      return {"hypervisor_name": constants.VALUE_AUTO}
402 402
    results = {
403 403
      "hypervisor_name": hypervisor_data.findtext("{%s}Name" % GANETI_SCHEMA,
404
                           default=constants.VALUE_AUTO),
404
                                                  default=constants.VALUE_AUTO),
405 405
    }
406 406
    parameters = hypervisor_data.find("{%s}Parameters" % GANETI_SCHEMA)
407 407
    results.update(self._GetDictParameters(parameters, GANETI_SCHEMA))
......
440 440
    vcpus = self._GetElementMatchingText(find_vcpus, match_vcpus)
441 441
    if vcpus:
442 442
      vcpus_count = vcpus.findtext("{%s}VirtualQuantity" % RASD_SCHEMA,
443
        default=constants.VALUE_AUTO)
443
                                   default=constants.VALUE_AUTO)
444 444
    else:
445 445
      vcpus_count = constants.VALUE_AUTO
446 446
    results["vcpus"] = str(vcpus_count)
......
451 451
    memory_raw = None
452 452
    if memory:
453 453
      alloc_units = memory.findtext("{%s}AllocationUnits" % RASD_SCHEMA)
454
      matching_units = [units for units, variants in
455
        ALLOCATION_UNITS.iteritems() if alloc_units.lower() in variants]
454
      matching_units = [units for units, variants in ALLOCATION_UNITS.items()
455
                        if alloc_units.lower() in variants]
456 456
      if matching_units == []:
457 457
        raise errors.OpPrereqError("Unit %s for RAM memory unknown" %
458 458
                                   alloc_units, errors.ECODE_INVAL)
459 459
      units = matching_units[0]
460 460
      memory_raw = int(memory.findtext("{%s}VirtualQuantity" % RASD_SCHEMA,
461
            default=constants.VALUE_AUTO))
461
                                       default=constants.VALUE_AUTO))
462 462
      memory_count = CONVERT_UNITS_TO_MB[units](memory_raw)
463 463
    else:
464 464
      memory_count = constants.VALUE_AUTO
465 465
    results["memory"] = str(memory_count)
466 466

  
467 467
    find_balance = ("{%s}GanetiSection/{%s}AutoBalance" %
468
                   (GANETI_SCHEMA, GANETI_SCHEMA))
468
                    (GANETI_SCHEMA, GANETI_SCHEMA))
469 469
    balance = self.tree.findtext(find_balance, default=constants.VALUE_AUTO)
470 470
    results["auto_balance"] = balance
471 471

  
......
513 513
    networks_search = ("{%s}NetworkSection/{%s}Network" %
514 514
                       (OVF_SCHEMA, OVF_SCHEMA))
515 515
    network_names = self._GetAttributes(networks_search,
516
      "{%s}name" % OVF_SCHEMA)
516
                                        "{%s}name" % OVF_SCHEMA)
517 517
    required = ["ip", "mac", "link", "mode"]
518 518
    for (counter, network_name) in enumerate(network_names):
519 519
      network_search = ("{%s}VirtualSystem/{%s}VirtualHardwareSection/{%s}Item"
......
524 524
      ganeti_match = ("{%s}name" % OVF_SCHEMA, network_name)
525 525
      network_data = self._GetElementMatchingText(network_search, network_match)
526 526
      network_ganeti_data = self._GetElementMatchingAttr(ganeti_search,
527
        ganeti_match)
527
                                                         ganeti_match)
528 528

  
529 529
      ganeti_data = {}
530 530
      if network_ganeti_data:
......
703 703
      SubElementText(network_item, "rasd:ElementName", network_name)
704 704
      SubElementText(network_item, "rasd:InstanceID", self.next_instance_id)
705 705
      SubElementText(network_item, "rasd:ResourceType",
706
        RASD_TYPE["ethernet-adapter"])
706
                     RASD_TYPE["ethernet-adapter"])
707 707
      self.hardware_list.append(network_item)
708 708
      self.next_instance_id += 1
709 709

  
......
737 737

  
738 738
    SubElementText(ganeti_section, "gnt:Version", ganeti.get("version"))
739 739
    SubElementText(ganeti_section, "gnt:DiskTemplate",
740
      ganeti.get("disk_template"))
740
                   ganeti.get("disk_template"))
741 741
    SubElementText(ganeti_section, "gnt:AutoBalance",
742
      ganeti.get("auto_balance"))
742
                   ganeti.get("auto_balance"))
743 743
    SubElementText(ganeti_section, "gnt:Tags", ganeti.get("tags"))
744 744

  
745 745
    osys = ET.SubElement(ganeti_section, "gnt:OperatingSystem")
......
779 779
    name_section.text = name
780 780
    os_attrib = {"ovf:id": "0"}
781 781
    os_section = ET.SubElement(virtual_system, "OperatingSystemSection",
782
      attrib=os_attrib)
782
                               attrib=os_attrib)
783 783
    SubElementText(os_section, "Info", "Installed guest operating system")
784 784
    hardware_section = ET.SubElement(virtual_system, "VirtualHardwareSection")
785 785
    SubElementText(hardware_section, "Info", "Virtual hardware requirements")
......
794 794
    # Item for vcpus
795 795
    vcpus_item = ET.SubElement(hardware_section, "Item")
796 796
    SubElementText(vcpus_item, "rasd:ElementName",
797
      "%s virtual CPU(s)" % vcpus)
797
                   "%s virtual CPU(s)" % vcpus)
798 798
    SubElementText(vcpus_item, "rasd:InstanceID", INSTANCE_ID["vcpus"])
799 799
    SubElementText(vcpus_item, "rasd:ResourceType", RASD_TYPE["vcpus"])
800 800
    SubElementText(vcpus_item, "rasd:VirtualQuantity", vcpus)
......
909 909
    elif action == COMPRESS:
910 910
      prefix = disk_file
911 911
    new_path = utils.GetClosedTempfile(suffix=COMPRESSION_EXT, prefix=prefix,
912
      dir=self.output_dir)
912
                                       dir=self.output_dir)
913 913
    self.temp_file_manager.Add(new_path)
914 914
    args = ["gzip", "-c", disk_path]
915 915
    run_result = utils.RunCmd(args, output=new_path)
......
940 940
      logging.warning("Conversion of disk image to %s format, this may take"
941 941
                      " a while", disk_format)
942 942

  
943
    new_disk_path = utils.GetClosedTempfile(suffix=".%s" % disk_format,
944
      prefix=disk_name, dir=self.output_dir)
943
    new_disk_path = utils.GetClosedTempfile(
944
      suffix=".%s" % disk_format, prefix=disk_name, dir=self.output_dir)
945 945
    self.temp_file_manager.Add(new_disk_path)
946 946
    args = [
947 947
      constants.QEMUIMG_PATH,
......
1122 1122
      raise errors.OpPrereqError("No %s file in %s package found" %
1123 1123
                                 (OVF_EXT, OVA_EXT), errors.ECODE_ENVIRON)
1124 1124
    logging.warning("Unpacking the %s archive, this may take a while",
1125
      input_path)
1125
                    input_path)
1126 1126
    self.input_dir = temp_dir
1127 1127
    self.input_path = utils.PathJoin(self.temp_dir, input_name)
1128 1128
    try:
......
1150 1150

  
1151 1151
    """
1152 1152
    self.results_name = self._GetInfo("instance name", self.options.name,
1153
      self._ParseNameOptions, self.ovf_reader.GetInstanceName)
1153
                                      self._ParseNameOptions,
1154
                                      self.ovf_reader.GetInstanceName)
1154 1155
    if not self.results_name:
1155 1156
      raise errors.OpPrereqError("Name of instance not provided",
1156 1157
                                 errors.ECODE_INVAL)
......
1162 1163
      raise errors.OpPrereqError("Failed to create directory %s: %s" %
1163 1164
                                 (self.output_dir, err), errors.ECODE_ENVIRON)
1164 1165

  
1165
    self.results_template = self._GetInfo("disk template",
1166
      self.options.disk_template, self._ParseTemplateOptions,
1166
    self.results_template = self._GetInfo(
1167
      "disk template", self.options.disk_template, self._ParseTemplateOptions,
1167 1168
      self.ovf_reader.GetDiskTemplate)
1168 1169
    if not self.results_template:
1169 1170
      logging.info("Disk template not given")
1170 1171

  
1171
    self.results_hypervisor = self._GetInfo("hypervisor",
1172
      self.options.hypervisor, self._ParseHypervisorOptions,
1172
    self.results_hypervisor = self._GetInfo(
1173
      "hypervisor", self.options.hypervisor, self._ParseHypervisorOptions,
1173 1174
      self.ovf_reader.GetHypervisorData)
1174 1175
    assert self.results_hypervisor["hypervisor_name"]
1175 1176
    if self.results_hypervisor["hypervisor_name"] == constants.VALUE_AUTO:
1176 1177
      logging.debug("Default hypervisor settings from the cluster will be used")
1177 1178

  
1178
    self.results_os = self._GetInfo("OS", self.options.os,
1179
      self._ParseOSOptions, self.ovf_reader.GetOSData)
1179
    self.results_os = self._GetInfo(
1180
      "OS", self.options.os, self._ParseOSOptions, self.ovf_reader.GetOSData)
1180 1181
    if not self.results_os.get("os_name"):
1181 1182
      raise errors.OpPrereqError("OS name must be provided",
1182 1183
                                 errors.ECODE_INVAL)
1183 1184

  
1184
    self.results_backend = self._GetInfo("backend", self.options.beparams,
1185
    self.results_backend = self._GetInfo(
1186
      "backend", self.options.beparams,
1185 1187
      self._ParseBackendOptions, self.ovf_reader.GetBackendData)
1186 1188
    assert self.results_backend.get("vcpus")
1187 1189
    assert self.results_backend.get("memory")
1188 1190
    assert self.results_backend.get("auto_balance") is not None
1189 1191

  
1190
    self.results_tags = self._GetInfo("tags", self.options.tags,
1191
      self._ParseTags, self.ovf_reader.GetTagsData)
1192
    self.results_tags = self._GetInfo(
1193
      "tags", self.options.tags, self._ParseTags, self.ovf_reader.GetTagsData)
1192 1194

  
1193 1195
    ovf_version = self.ovf_reader.GetVersionData()
1194 1196
    if ovf_version:
......
1196 1198
    else:
1197 1199
      self.results_version = constants.EXPORT_VERSION
1198 1200

  
1199
    self.results_network = self._GetInfo("network", self.options.nics,
1200
      self._ParseNicOptions, self.ovf_reader.GetNetworkData,
1201
      ignore_test=self.options.no_nics)
1201
    self.results_network = self._GetInfo(
1202
      "network", self.options.nics, self._ParseNicOptions,
1203
      self.ovf_reader.GetNetworkData, ignore_test=self.options.no_nics)
1202 1204

  
1203
    self.results_disk = self._GetInfo("disk", self.options.disks,
1204
      self._ParseDiskOptions, self._GetDiskInfo,
1205
    self.results_disk = self._GetInfo(
1206
      "disk", self.options.disks, self._ParseDiskOptions, self._GetDiskInfo,
1205 1207
      ignore_test=self.results_template == constants.DT_DISKLESS)
1206 1208

  
1207 1209
    if not self.results_disk and not self.results_network:
......
1211 1213

  
1212 1214
  @staticmethod
1213 1215
  def _GetInfo(name, cmd_arg, cmd_function, nocmd_function,
1214
    ignore_test=False):
1216
               ignore_test=False):
1215 1217
    """Get information about some section - e.g. disk, network, hypervisor.
1216 1218

  
1217 1219
    @type name: string
......
1232 1234
      results = cmd_function()
1233 1235
    else:
1234 1236
      logging.info("Information for %s will be parsed from %s file",
1235
        name, OVF_EXT)
1237
                   name, OVF_EXT)
1236 1238
      results = nocmd_function()
1237 1239
    logging.info("Options for %s were succesfully read", name)
1238 1240
    return results
......
1398 1400
      disk_path = utils.PathJoin(self.input_dir, disk_name)
1399 1401
      if disk_compression not in NO_COMPRESSION:
1400 1402
        _, disk_path = self._CompressDisk(disk_path, disk_compression,
1401
          DECOMPRESS)
1403
                                          DECOMPRESS)
1402 1404
        disk, _ = os.path.splitext(disk)
1403 1405
      if self._GetDiskQemuInfo(disk_path, "file format: (\S+)") != "raw":
1404 1406
        logging.info("Conversion to raw format is required")
1405 1407
      ext, new_disk_path = self._ConvertDisk("raw", disk_path)
1406 1408

  
1407 1409
      final_disk_path = LinkFile(new_disk_path, prefix=disk, suffix=ext,
1408
        directory=self.output_dir)
1410
                                 directory=self.output_dir)
1409 1411
      final_name = os.path.basename(final_disk_path)
1410 1412
      disk_size = os.path.getsize(final_disk_path) / (1024 * 1024)
1411 1413
      results["disk%s_dump" % counter] = final_name
......
1453 1455
    results[constants.INISECT_HYP].update(self.results_hypervisor)
1454 1456

  
1455 1457
    output_file_name = utils.PathJoin(self.output_dir,
1456
      constants.EXPORT_CONF_FILE)
1458
                                      constants.EXPORT_CONF_FILE)
1457 1459

  
1458 1460
    output = []
1459 1461
    for section, options in results.iteritems():
......
1480 1482
  """
1481 1483
  def get(self, section, options, raw=None, vars=None): # pylint: disable=W0622
1482 1484
    try:
1483
      result = ConfigParser.SafeConfigParser.get(self, section, options, \
1484
        raw=raw, vars=vars)
1485
      result = ConfigParser.SafeConfigParser.get(self, section, options,
1486
                                                 raw=raw, vars=vars)
1485 1487
    except ConfigParser.NoOptionError:
1486 1488
      result = None
1487 1489
    return result
......
1662 1664
        break
1663 1665
      results.append({
1664 1666
        "mode": self.config_parser.get(constants.INISECT_INS,
1665
           "nic%s_mode" % counter),
1667
                                       "nic%s_mode" % counter),
1666 1668
        "mac": self.config_parser.get(constants.INISECT_INS,
1667
           "nic%s_mac" % counter),
1669
                                      "nic%s_mac" % counter),
1668 1670
        "ip": self.config_parser.get(constants.INISECT_INS,
1669
           "nic%s_ip" % counter),
1671
                                     "nic%s_ip" % counter),
1670 1672
        "link": data_link,
1671 1673
      })
1672 1674
      if results[counter]["mode"] not in constants.NIC_VALID_MODES:
......
1698 1700
    disk_name, _ = os.path.splitext(disk_file)
1699 1701
    ext, new_disk_path = self._ConvertDisk(self.options.disk_format, disk_path)
1700 1702
    results["format"] = self.options.disk_format
1701
    results["virt-size"] = self._GetDiskQemuInfo(new_disk_path,
1702
      "virtual size: \S+ \((\d+) bytes\)")
1703
    results["virt-size"] = self._GetDiskQemuInfo(
1704
      new_disk_path, "virtual size: \S+ \((\d+) bytes\)")
1703 1705
    if compression:
1704 1706
      ext2, new_disk_path = self._CompressDisk(new_disk_path, "gzip",
1705
        COMPRESS)
1707
                                               COMPRESS)
1706 1708
      disk_name, _ = os.path.splitext(disk_name)
1707 1709
      results["compression"] = "gzip"
1708 1710
      ext += ext2
1709 1711
    final_disk_path = LinkFile(new_disk_path, prefix=disk_name, suffix=ext,
1710
      directory=self.output_dir)
1712
                               directory=self.output_dir)
1711 1713
    final_disk_name = os.path.basename(final_disk_path)
1712 1714
    results["real-size"] = os.path.getsize(final_disk_path)
1713 1715
    results["path"] = final_disk_name
......
1815 1817
      self.ovf_writer.SaveGanetiData(self.results_ganeti, self.results_network)
1816 1818

  
1817 1819
    self.ovf_writer.SaveVirtualSystemData(self.results_name, self.results_vcpus,
1818
      self.results_memory)
1820
                                          self.results_memory)
1819 1821

  
1820 1822
    data = self.ovf_writer.PrettyXmlDump()
1821 1823
    utils.WriteFile(self.output_path, data=data)

Also available in: Unified diff