예제 #1
0
  @Override
  public FormatInfo process(String templatePath, ImageFormat format, String templateName)
      throws InternalErrorException {
    if (format != null) {
      if (s_logger.isInfoEnabled()) {
        s_logger.info("We currently don't handle conversion from " + format + " to OVA.");
      }
      return null;
    }

    s_logger.info(
        "Template processing. templatePath: " + templatePath + ", templateName: " + templateName);
    String templateFilePath =
        templatePath + File.separator + templateName + "." + ImageFormat.OVA.getFileExtension();
    if (!_storage.exists(templateFilePath)) {
      if (s_logger.isInfoEnabled()) {
        s_logger.info("Unable to find the vmware template file: " + templateFilePath);
      }
      return null;
    }

    s_logger.info(
        "Template processing - untar OVA package. templatePath: "
            + templatePath
            + ", templateName: "
            + templateName);
    String templateFileFullPath =
        templatePath + File.separator + templateName + "." + ImageFormat.OVA.getFileExtension();
    File templateFile = new File(templateFileFullPath);

    Script command = new Script("tar", 0, s_logger);
    command.add("--no-same-owner");
    command.add("-xf", templateFileFullPath);
    command.setWorkDir(templateFile.getParent());
    String result = command.execute();
    if (result != null) {
      s_logger.info(
          "failed to untar OVA package due to "
              + result
              + ". templatePath: "
              + templatePath
              + ", templateName: "
              + templateName);
      throw new InternalErrorException("failed to untar OVA package");
    }

    FormatInfo info = new FormatInfo();
    info.format = ImageFormat.OVA;
    info.filename = templateName + "." + ImageFormat.OVA.getFileExtension();
    info.size = _storage.getSize(templateFilePath);
    info.virtualSize = getTemplateVirtualSize(templatePath, info.filename);

    // delete original OVA file
    // templateFile.delete();
    return info;
  }
  @Override
  public FormatInfo process(String templatePath, ImageFormat format, String templateName) {
    if (format != null) {
      s_logger.debug("We currently don't handle conversion from " + format + " to QCOW2.");
      return null;
    }

    String qcow2Path =
        templatePath + File.separator + templateName + "." + ImageFormat.QCOW2.getFileExtension();

    if (!_storage.exists(qcow2Path)) {
      s_logger.debug("Unable to find the qcow2 file: " + qcow2Path);
      return null;
    }

    FormatInfo info = new FormatInfo();
    info.format = ImageFormat.QCOW2;
    info.filename = templateName + "." + ImageFormat.QCOW2.getFileExtension();

    File qcow2File = _storage.getFile(qcow2Path);

    info.size = _storage.getSize(qcow2Path);
    FileInputStream strm = null;
    byte[] b = new byte[8];
    try {
      strm = new FileInputStream(qcow2File);
      strm.skip(24);
      strm.read(b);
    } catch (Exception e) {
      s_logger.warn("Unable to read qcow2 file " + qcow2Path, e);
      return null;
    } finally {
      if (strm != null) {
        try {
          strm.close();
        } catch (IOException e) {
        }
      }
    }

    long templateSize = NumbersUtil.bytesToLong(b);
    info.virtualSize = templateSize;

    return info;
  }