Пример #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;
  }
  protected void configureFolders(String name, Map<String, Object> params)
      throws ConfigurationException {
    parentDir = (String) params.get("template.parent");
    if (parentDir == null) {
      throw new ConfigurationException("Unable to find the parent root for the templates");
    }

    String value = (String) params.get("public.templates.root.dir");
    if (value == null) {
      value = TemplateConstants.DEFAULT_TMPLT_ROOT_DIR;
    }

    if (value.startsWith(File.separator)) {
      publicTemplateRepo = value;
    } else {
      publicTemplateRepo = parentDir + File.separator + value;
    }

    if (!publicTemplateRepo.endsWith(File.separator)) {
      publicTemplateRepo += File.separator;
    }

    publicTemplateRepo += TemplateConstants.DEFAULT_TMPLT_FIRST_LEVEL_DIR;

    if (!_storage.mkdirs(publicTemplateRepo)) {
      throw new ConfigurationException("Unable to create public templates directory");
    }
  }
Пример #3
0
  private void startupCleanup(String parent) {
    s_logger.info("Cleanup mounted NFS mount points used in previous session");

    long mshostId = ManagementServerNode.getManagementServerId();

    // cleanup left-over NFS mounts from previous session
    String[] mounts = _storage.listFiles(parent + File.separator + String.valueOf(mshostId) + ".*");
    if (mounts != null && mounts.length > 0) {
      for (String mountPoint : mounts) {
        s_logger.info("umount NFS mount from previous session: " + mountPoint);

        String result = null;
        Script command = new Script(true, "umount", _timeout, s_logger);
        command.add(mountPoint);
        result = command.execute();
        if (result != null) {
          s_logger.warn("Unable to umount " + mountPoint + " due to " + result);
        }
        File file = new File(mountPoint);
        if (file.exists()) {
          file.delete();
        }
      }
    }
  }
  @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;
  }
Пример #5
0
 private String getOVFFilePath(String srcOVAFileName) {
   File file = new File(srcOVAFileName);
   assert (_storage != null);
   String[] files = _storage.listFiles(file.getParent());
   if (files != null) {
     for (String fileName : files) {
       if (fileName.toLowerCase().endsWith(".ovf")) {
         File ovfFile = new File(fileName);
         return file.getParent() + File.separator + ovfFile.getName();
       }
     }
   }
   return null;
 }
Пример #6
0
  private String setupMountPoint(String parent) {
    String mountPoint = null;
    long mshostId = ManagementServerNode.getManagementServerId();
    for (int i = 0; i < 10; i++) {
      String mntPt =
          parent
              + File.separator
              + String.valueOf(mshostId)
              + "."
              + Integer.toHexString(_rand.nextInt(Integer.MAX_VALUE));
      File file = new File(mntPt);
      if (!file.exists()) {
        if (_storage.mkdir(mntPt)) {
          mountPoint = mntPt;
          break;
        }
      }
      s_logger.error("Unable to create mount: " + mntPt);
    }

    return mountPoint;
  }
  public HttpTemplateDownloader(
      StorageLayer storageLayer,
      String downloadUrl,
      String toDir,
      DownloadCompleteCallback callback,
      long maxTemplateSizeInBytes,
      String user,
      String password,
      Proxy proxy,
      ResourceType resourceType) {
    this._storage = storageLayer;
    this.downloadUrl = downloadUrl;
    this.setToDir(toDir);
    this.status = TemplateDownloader.Status.NOT_STARTED;
    this.resourceType = resourceType;
    this.MAX_TEMPLATE_SIZE_IN_BYTES = maxTemplateSizeInBytes;

    this.totalBytes = 0;
    this.client = new HttpClient();

    myretryhandler =
        new HttpMethodRetryHandler() {
          public boolean retryMethod(
              final HttpMethod method, final IOException exception, int executionCount) {
            if (executionCount >= 2) {
              // Do not retry if over max retry count
              return false;
            }
            if (exception instanceof NoHttpResponseException) {
              // Retry if the server dropped connection on us
              return true;
            }
            if (!method.isRequestSent()) {
              // Retry if the request has not been sent fully or
              // if it's OK to retry methods that have been sent
              return true;
            }
            // otherwise do not retry
            return false;
          }
        };

    try {
      this.request = new GetMethod(downloadUrl);
      this.request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
      this.completionCallback = callback;
      // this.request.setFollowRedirects(false);

      File f = File.createTempFile("dnld", "tmp_", new File(toDir));

      if (_storage != null) {
        _storage.setWorldReadableAndWriteable(f);
      }

      toFile = f.getAbsolutePath();
      Pair<String, Integer> hostAndPort = validateUrl(downloadUrl);

      if (proxy != null) {
        client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort());
        if (proxy.getUserName() != null) {
          Credentials proxyCreds =
              new UsernamePasswordCredentials(proxy.getUserName(), proxy.getPassword());
          client.getState().setProxyCredentials(AuthScope.ANY, proxyCreds);
        }
      }
      if ((user != null) && (password != null)) {
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
        client
            .getState()
            .setCredentials(
                new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM),
                defaultcreds);
        s_logger.info(
            "Added username="******", password="******"for host "
                + hostAndPort.first()
                + ":"
                + hostAndPort.second());
      } else {
        s_logger.info(
            "No credentials configured for host="
                + hostAndPort.first()
                + ":"
                + hostAndPort.second());
      }
    } catch (IllegalArgumentException iae) {
      errorString = iae.getMessage();
      status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
      inited = false;
    } catch (Exception ex) {
      errorString = "Unable to start download -- check url? ";
      status = TemplateDownloader.Status.UNRECOVERABLE_ERROR;
      s_logger.warn("Exception in constructor -- " + ex.toString());
    } catch (Throwable th) {
      s_logger.warn("throwable caught ", th);
    }
  }
Пример #8
0
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    s_logger.info("Configure VmwareManagerImpl, manager name: " + name);

    if (!_configDao.isPremium()) {
      s_logger.error("Vmware component can only run under premium distribution");
      throw new ConfigurationException("Vmware component can only run under premium distribution");
    }

    _instance = _configDao.getValue(Config.InstanceName.key());
    if (_instance == null) {
      _instance = "DEFAULT";
    }
    s_logger.info("VmwareManagerImpl config - instance.name: " + _instance);

    _mountParent = _configDao.getValue(Config.MountParent.key());
    if (_mountParent == null) {
      _mountParent = File.separator + "mnt";
    }

    if (_instance != null) {
      _mountParent = _mountParent + File.separator + _instance;
    }
    s_logger.info("VmwareManagerImpl config - _mountParent: " + _mountParent);

    String value = (String) params.get("scripts.timeout");
    _timeout = NumbersUtil.parseInt(value, 1440) * 1000;

    _storage = (StorageLayer) params.get(StorageLayer.InstanceConfigKey);
    if (_storage == null) {
      _storage = new JavaStorageLayer();
      _storage.configure("StorageLayer", params);
    }

    value = _configDao.getValue(Config.VmwareCreateFullClone.key());
    if (value == null) {
      _fullCloneFlag = false;
    } else {
      _fullCloneFlag = Boolean.parseBoolean(value);
    }
    _portsPerDvPortGroup =
        NumbersUtil.parseInt(
            _configDao.getValue(Config.VmwarePortsPerDVPortGroup.key()), _portsPerDvPortGroup);

    _serviceConsoleName = _configDao.getValue(Config.VmwareServiceConsole.key());
    if (_serviceConsoleName == null) {
      _serviceConsoleName = "Service Console";
    }

    _managemetPortGroupName = _configDao.getValue(Config.VmwareManagementPortGroup.key());
    if (_managemetPortGroupName == null) {
      _managemetPortGroupName = "Management Network";
    }

    _defaultSystemVmNicAdapterType = _configDao.getValue(Config.VmwareSystemVmNicDeviceType.key());
    if (_defaultSystemVmNicAdapterType == null) {
      _defaultSystemVmNicAdapterType = VirtualEthernetCardType.E1000.toString();
    }

    _additionalPortRangeStart =
        NumbersUtil.parseInt(
            _configDao.getValue(Config.VmwareAdditionalVncPortRangeStart.key()), 59000);
    if (_additionalPortRangeStart > 65535) {
      s_logger.warn(
          "Invalid port range start port ("
              + _additionalPortRangeStart
              + ") for additional VNC port allocation, reset it to default start port 59000");
      _additionalPortRangeStart = 59000;
    }

    _additionalPortRangeSize =
        NumbersUtil.parseInt(
            _configDao.getValue(Config.VmwareAdditionalVncPortRangeSize.key()), 1000);
    if (_additionalPortRangeSize < 0
        || _additionalPortRangeStart + _additionalPortRangeSize > 65535) {
      s_logger.warn(
          "Invalid port range size ("
              + _additionalPortRangeSize
              + " for range starts at "
              + _additionalPortRangeStart);
      _additionalPortRangeSize = Math.min(1000, 65535 - _additionalPortRangeStart);
    }

    _routerExtraPublicNics =
        NumbersUtil.parseInt(_configDao.getValue(Config.RouterExtraPublicNics.key()), 2);

    _reserveCpu = _configDao.getValue(Config.VmwareReserveCpu.key());
    if (_reserveCpu == null || _reserveCpu.isEmpty()) {
      _reserveCpu = "false";
    }
    _reserveMem = _configDao.getValue(Config.VmwareReserveMem.key());
    if (_reserveMem == null || _reserveMem.isEmpty()) {
      _reserveMem = "false";
    }

    _recycleHungWorker = _configDao.getValue(Config.VmwareRecycleHungWorker.key());
    if (_recycleHungWorker == null || _recycleHungWorker.isEmpty()) {
      _recycleHungWorker = "false";
    }

    _rootDiskController = _configDao.getValue(Config.VmwareRootDiskControllerType.key());
    if (_rootDiskController == null || _rootDiskController.isEmpty()) {
      _rootDiskController = DiskControllerType.ide.toString();
    }

    s_logger.info(
        "Additional VNC port allocation range is settled at "
            + _additionalPortRangeStart
            + " to "
            + (_additionalPortRangeStart + _additionalPortRangeSize));

    value = _configDao.getValue("vmware.host.scan.interval");
    _hostScanInterval = NumbersUtil.parseLong(value, DEFAULT_HOST_SCAN_INTERVAL);
    s_logger.info("VmwareManagerImpl config - vmware.host.scan.interval: " + _hostScanInterval);

    ((VmwareStorageManagerImpl) _storageMgr).configure(params);

    _agentMgr.registerForHostEvents(this, true, true, true);

    s_logger.info("VmwareManagerImpl has been successfully configured");
    return true;
  }
Пример #9
0
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    s_logger.info("Configure VmwareManagerImpl, manager name: " + name);

    _name = name;

    ComponentLocator locator = ComponentLocator.getCurrentLocator();
    ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
    if (configDao == null) {
      throw new ConfigurationException("Unable to get the configuration dao.");
    }

    if (!configDao.isPremium()) {
      s_logger.error("Vmware component can only run under premium distribution");
      throw new ConfigurationException("Vmware component can only run under premium distribution");
    }

    _instance = configDao.getValue(Config.InstanceName.key());
    if (_instance == null) {
      _instance = "DEFAULT";
    }
    s_logger.info("VmwareManagerImpl config - instance.name: " + _instance);

    _mountParent = configDao.getValue(Config.MountParent.key());
    if (_mountParent == null) {
      _mountParent = File.separator + "mnt";
    }

    if (_instance != null) {
      _mountParent = _mountParent + File.separator + _instance;
    }
    s_logger.info("VmwareManagerImpl config - _mountParent: " + _mountParent);

    String value = (String) params.get("scripts.timeout");
    _timeout = NumbersUtil.parseInt(value, 1440) * 1000;

    _storage = (StorageLayer) params.get(StorageLayer.InstanceConfigKey);
    if (_storage == null) {
      value = (String) params.get(StorageLayer.ClassConfigKey);
      if (value == null) {
        value = "com.cloud.storage.JavaStorageLayer";
      }

      try {
        Class<?> clazz = Class.forName(value);
        _storage = (StorageLayer) ComponentLocator.inject(clazz);
        _storage.configure("StorageLayer", params);
      } catch (ClassNotFoundException e) {
        throw new ConfigurationException("Unable to find class " + value);
      }
    }

    _privateNetworkVSwitchName = configDao.getValue(Config.VmwarePrivateNetworkVSwitch.key());
    if (_privateNetworkVSwitchName == null) {
      _privateNetworkVSwitchName = "vSwitch0";
    }

    _publicNetworkVSwitchName = configDao.getValue(Config.VmwarePublicNetworkVSwitch.key());
    if (_publicNetworkVSwitchName == null) {
      _publicNetworkVSwitchName = "vSwitch0";
    }

    _guestNetworkVSwitchName = configDao.getValue(Config.VmwareGuestNetworkVSwitch.key());
    if (_guestNetworkVSwitchName == null) {
      _guestNetworkVSwitchName = "vSwitch0";
    }

    _serviceConsoleName = configDao.getValue(Config.VmwareServiceConsole.key());
    if (_serviceConsoleName == null) {
      _serviceConsoleName = "Service Console";
    }

    _managemetPortGroupName = configDao.getValue(Config.VmwareManagementPortGroup.key());
    if (_managemetPortGroupName == null) {
      _managemetPortGroupName = "Management Network";
    }

    configDao.getValue(Config.VmwareServiceConsole.key());
    _additionalPortRangeStart =
        NumbersUtil.parseInt(
            configDao.getValue(Config.VmwareAdditionalVncPortRangeStart.key()), 59000);
    if (_additionalPortRangeStart > 65535) {
      s_logger.warn(
          "Invalid port range start port ("
              + _additionalPortRangeStart
              + ") for additional VNC port allocation, reset it to default start port 59000");
      _additionalPortRangeStart = 59000;
    }

    _additionalPortRangeSize =
        NumbersUtil.parseInt(
            configDao.getValue(Config.VmwareAdditionalVncPortRangeSize.key()), 1000);
    if (_additionalPortRangeSize < 0
        || _additionalPortRangeStart + _additionalPortRangeSize > 65535) {
      s_logger.warn(
          "Invalid port range size ("
              + _additionalPortRangeSize
              + " for range starts at "
              + _additionalPortRangeStart);
      _additionalPortRangeSize = Math.min(1000, 65535 - _additionalPortRangeStart);
    }

    _maxHostsPerCluster =
        NumbersUtil.parseInt(
            configDao.getValue(Config.VmwarePerClusterHostMax.key()),
            VmwareManager.MAX_HOSTS_PER_CLUSTER);
    _cpuOverprovisioningFactor = configDao.getValue(Config.CPUOverprovisioningFactor.key());
    if (_cpuOverprovisioningFactor == null || _cpuOverprovisioningFactor.isEmpty())
      _cpuOverprovisioningFactor = "1";

    _memOverprovisioningFactor = configDao.getValue(Config.MemOverprovisioningFactor.key());
    if (_memOverprovisioningFactor == null || _memOverprovisioningFactor.isEmpty())
      _memOverprovisioningFactor = "1";

    _reserveCpu = configDao.getValue(Config.VmwareReserveCpu.key());
    if (_reserveCpu == null || _reserveCpu.isEmpty()) _reserveCpu = "false";
    _reserveMem = configDao.getValue(Config.VmwareReserveMem.key());
    if (_reserveMem == null || _reserveMem.isEmpty()) _reserveMem = "false";

    s_logger.info(
        "Additional VNC port allocation range is settled at "
            + _additionalPortRangeStart
            + " to "
            + (_additionalPortRangeStart + _additionalPortRangeSize));

    value = configDao.getValue(Config.VmwareGuestNicDeviceType.key());
    if (value == null || "E1000".equalsIgnoreCase(value))
      this._guestNicDeviceType = VirtualEthernetCardType.E1000;
    else if ("PCNet32".equalsIgnoreCase(value))
      this._guestNicDeviceType = VirtualEthernetCardType.PCNet32;
    else if ("Vmxnet2".equalsIgnoreCase(value))
      this._guestNicDeviceType = VirtualEthernetCardType.Vmxnet2;
    else if ("Vmxnet3".equalsIgnoreCase(value))
      this._guestNicDeviceType = VirtualEthernetCardType.Vmxnet3;
    else this._guestNicDeviceType = VirtualEthernetCardType.E1000;

    value = configDao.getValue("vmware.host.scan.interval");
    _hostScanInterval = NumbersUtil.parseLong(value, DEFAULT_HOST_SCAN_INTERVAL);
    s_logger.info("VmwareManagerImpl config - vmware.host.scan.interval: " + _hostScanInterval);

    ((VmwareStorageManagerImpl) _storageMgr).configure(params);

    _agentMgr.registerForHostEvents(this, true, true, true);

    s_logger.info("VmwareManagerImpl has been successfully configured");
    return true;
  }