private void createOrupdateConfigObject(
     Date date, String componentName, ConfigKey<?> key, String value) {
   ConfigurationVO vo = _configDao.findById(key.key());
   if (vo == null) {
     vo = new ConfigurationVO(componentName, key);
     vo.setUpdated(date);
     if (value != null) {
       vo.setValue(value);
     }
     _configDao.persist(vo);
   } else {
     if (vo.isDynamic() != key.isDynamic()
         || !ObjectUtils.equals(vo.getDescription(), key.description())
         || !ObjectUtils.equals(vo.getDefaultValue(), key.defaultValue())
         || !ObjectUtils.equals(vo.getScope(), key.scope().toString())
         || !ObjectUtils.equals(vo.getComponent(), componentName)) {
       vo.setDynamic(key.isDynamic());
       vo.setDescription(key.description());
       vo.setDefaultValue(key.defaultValue());
       vo.setScope(key.scope().toString());
       vo.setComponent(componentName);
       vo.setUpdated(date);
       _configDao.persist(vo);
     }
   }
 }
 private Long getMaxTemplateSizeInBytes() {
   try {
     return Long.parseLong(_configDao.getValue("max.template.iso.size")) * 1024L * 1024L * 1024L;
   } catch (NumberFormatException e) {
     return null;
   }
 }
 @Override
 public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
   _name = name;
   String elbEnabledString = _configDao.getValue(Config.ElasticLoadBalancerEnabled.key());
   _elbEnabled = Boolean.parseBoolean(elbEnabledString);
   s_logger.info("Firewall provider list is " + _firewallElements.iterator().next());
   return true;
 }
  private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDeployment plan) {
    DataCenterVO mockDc = mock(DataCenterVO.class);
    VMInstanceVO vm = mock(VMInstanceVO.class);
    UserVmVO userVm = mock(UserVmVO.class);
    ServiceOfferingVO offering = mock(ServiceOfferingVO.class);

    AccountVO account = mock(AccountVO.class);
    when(account.getId()).thenReturn(accountId);
    when(account.getAccountId()).thenReturn(accountId);
    when(vmProfile.getOwner()).thenReturn(account);
    when(vmProfile.getVirtualMachine()).thenReturn(vm);
    when(vmProfile.getId()).thenReturn(12L);
    when(vmDao.findById(12L)).thenReturn(userVm);
    when(userVm.getAccountId()).thenReturn(accountId);

    when(vm.getDataCenterId()).thenReturn(dataCenterId);
    when(dcDao.findById(1L)).thenReturn(mockDc);
    when(plan.getDataCenterId()).thenReturn(dataCenterId);
    when(plan.getClusterId()).thenReturn(null);
    when(plan.getPodId()).thenReturn(null);
    when(configDao.getValue(anyString())).thenReturn("false").thenReturn("CPU");

    // Mock offering details.
    when(vmProfile.getServiceOffering()).thenReturn(offering);
    when(offering.getId()).thenReturn(offeringId);
    when(vmProfile.getServiceOfferingId()).thenReturn(offeringId);
    when(offering.getCpu()).thenReturn(noOfCpusInOffering);
    when(offering.getSpeed()).thenReturn(cpuSpeedInOffering);
    when(offering.getRamSize()).thenReturn(ramInOffering);

    List<Long> clustersWithEnoughCapacity = new ArrayList<Long>();
    clustersWithEnoughCapacity.add(1L);
    clustersWithEnoughCapacity.add(2L);
    clustersWithEnoughCapacity.add(3L);
    when(capacityDao.listClustersInZoneOrPodByHostCapacities(
            dataCenterId,
            noOfCpusInOffering * cpuSpeedInOffering,
            ramInOffering * 1024L * 1024L,
            CapacityVO.CAPACITY_TYPE_CPU,
            true))
        .thenReturn(clustersWithEnoughCapacity);

    Map<Long, Double> clusterCapacityMap = new HashMap<Long, Double>();
    clusterCapacityMap.put(1L, 2048D);
    clusterCapacityMap.put(2L, 2048D);
    clusterCapacityMap.put(3L, 2048D);
    Pair<List<Long>, Map<Long, Double>> clustersOrderedByCapacity =
        new Pair<List<Long>, Map<Long, Double>>(clustersWithEnoughCapacity, clusterCapacityMap);
    when(capacityDao.orderClustersByAggregateCapacity(
            dataCenterId, CapacityVO.CAPACITY_TYPE_CPU, true))
        .thenReturn(clustersOrderedByCapacity);

    List<Long> disabledClusters = new ArrayList<Long>();
    List<Long> clustersWithDisabledPods = new ArrayList<Long>();
    when(clusterDao.listDisabledClusters(dataCenterId, null)).thenReturn(disabledClusters);
    when(clusterDao.listClustersWithDisabledPods(dataCenterId))
        .thenReturn(clustersWithDisabledPods);
  }
  @Override
  public boolean configure(final String name, final Map<String, Object> xmlParams)
      throws ConfigurationException {
    _serverId = _msServer.getId();

    Map<String, String> params = new HashMap<String, String>();
    params = _configDao.getConfiguration(Long.toHexString(_serverId), xmlParams);

    String value = params.get(Config.HAWorkers.key());
    final int count = NumbersUtil.parseInt(value, 1);
    _workers = new WorkerThread[count];
    for (int i = 0; i < _workers.length; i++) {
      _workers[i] = new WorkerThread("HA-Worker-" + i);
    }

    value = params.get("force.ha");
    _forceHA = Boolean.parseBoolean(value);

    value = params.get("time.to.sleep");
    _timeToSleep = NumbersUtil.parseInt(value, 60) * 1000;

    value = params.get("max.retries");
    _maxRetries = NumbersUtil.parseInt(value, 5);

    value = params.get("time.between.failures");
    _timeBetweenFailures = NumbersUtil.parseLong(value, 3600) * 1000;

    value = params.get("time.between.cleanup");
    _timeBetweenCleanups = NumbersUtil.parseLong(value, 3600 * 24);

    value = params.get("stop.retry.interval");
    _stopRetryInterval = NumbersUtil.parseInt(value, 10 * 60);

    value = params.get("restart.retry.interval");
    _restartRetryInterval = NumbersUtil.parseInt(value, 10 * 60);

    value = params.get("investigate.retry.interval");
    _investigateRetryInterval = NumbersUtil.parseInt(value, 1 * 60);

    value = params.get("migrate.retry.interval");
    _migrateRetryInterval = NumbersUtil.parseInt(value, 2 * 60);

    _instance = params.get("instance");
    if (_instance == null) {
      _instance = "VMOPS";
    }

    _haTag = params.get("ha.tag");

    _haDao.releaseWorkItems(_serverId);

    _stopped = true;

    _executor = Executors.newScheduledThreadPool(count, new NamedThreadFactory("HA"));

    return true;
  }
 private Long getMaxVolumeSizeInBytes() {
   try {
     return Long.parseLong(_configDao.getValue("storage.max.volume.upload.size"))
         * 1024L
         * 1024L
         * 1024L;
   } catch (NumberFormatException e) {
     return null;
   }
 }
  @Override
  public List<StorageTagVO> searchByIds(Long... stIds) {
    String batchCfg = _configDao.getValue("detail.batch.query.size");

    final int detailsBatchSize = batchCfg != null ? Integer.parseInt(batchCfg) : 2000;

    // query details by batches
    List<StorageTagVO> uvList = new ArrayList<StorageTagVO>();
    int curr_index = 0;

    if (stIds.length > detailsBatchSize) {
      while ((curr_index + detailsBatchSize) <= stIds.length) {
        Long[] ids = new Long[detailsBatchSize];

        for (int k = 0, j = curr_index; j < curr_index + detailsBatchSize; j++, k++) {
          ids[k] = stIds[j];
        }

        SearchCriteria<StorageTagVO> sc = stSearch.create();

        sc.setParameters("idIN", (Object[]) ids);

        List<StorageTagVO> vms = searchIncludingRemoved(sc, null, null, false);

        if (vms != null) {
          uvList.addAll(vms);
        }

        curr_index += detailsBatchSize;
      }
    }

    if (curr_index < stIds.length) {
      int batch_size = (stIds.length - curr_index);
      // set the ids value
      Long[] ids = new Long[batch_size];

      for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
        ids[k] = stIds[j];
      }

      SearchCriteria<StorageTagVO> sc = stSearch.create();

      sc.setParameters("idIN", (Object[]) ids);

      List<StorageTagVO> vms = searchIncludingRemoved(sc, null, null, false);

      if (vms != null) {
        uvList.addAll(vms);
      }
    }

    return uvList;
  }
  private List<SspClient> fetchSspClients(
      Long physicalNetworkId, Long dataCenterId, boolean enabled_only) {
    ArrayList<SspClient> clients = new ArrayList<SspClient>();

    boolean provider_found = false;
    PhysicalNetworkServiceProviderVO provider =
        _physicalNetworkServiceProviderDao.findByServiceProvider(physicalNetworkId, s_SSP_NAME);
    if (enabled_only) {
      if (provider != null && provider.getState() == State.Enabled) {
        provider_found = true;
      }
    } else {
      provider_found = true;
    }

    if (physicalNetworkId != null && provider_found) {
      SspCredentialVO credential = _sspCredentialDao.findByZone(dataCenterId);
      List<HostVO> hosts =
          _resourceMgr.listAllHostsInOneZoneByType(Host.Type.L2Networking, dataCenterId);
      for (HostVO host : hosts) {
        assert (credential != null);
        _hostDao.loadDetails(host);
        if ("v1Api".equals(host.getDetail("sspHost"))) {
          clients.add(
              new SspClient(
                  host.getDetail("url"), credential.getUsername(), credential.getPassword()));
        }
      }
    }
    if (clients.size() == 0) {
      String global_apiUrl = _configDao.getValueAndInitIfNotExist("ssp.url", "Network", null);
      String global_username =
          _configDao.getValueAndInitIfNotExist("ssp.username", "Network", null);
      String global_password =
          _configDao.getValueAndInitIfNotExist("ssp.password", "Network", null);
      if (global_apiUrl != null && global_username != null && global_password != null) {
        clients.add(new SspClient(global_apiUrl, global_username, global_password));
      }
    }
    return clients;
  }
 private String generateCopyUrl(String ipAddress, String dir, String path) {
   String hostname = ipAddress;
   String scheme = "http";
   boolean _sslCopy = false;
   String sslCfg = _configDao.getValue(Config.SecStorageEncryptCopy.toString());
   if (sslCfg != null) {
     _sslCopy = Boolean.parseBoolean(sslCfg);
   }
   if (_sslCopy) {
     hostname = ipAddress.replace(".", "-");
     hostname = hostname + ".realhostip.com";
     scheme = "https";
   }
   return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path;
 }
 private String generateCopyUrl(String ipAddress, String dir, String path) {
   String hostname = ipAddress;
   String scheme = "http";
   boolean _sslCopy = false;
   String sslCfg = _configDao.getValue(Config.SecStorageEncryptCopy.toString());
   String _ssvmUrlDomain = _configDao.getValue("secstorage.ssl.cert.domain");
   if (sslCfg != null) {
     _sslCopy = Boolean.parseBoolean(sslCfg);
   }
   if (_sslCopy && (_ssvmUrlDomain == null || _ssvmUrlDomain.isEmpty())) {
     s_logger.warn("Empty secondary storage url domain, ignoring SSL");
     _sslCopy = false;
   }
   if (_sslCopy) {
     if (_ssvmUrlDomain.startsWith("*")) {
       hostname = ipAddress.replace(".", "-");
       hostname = hostname + _ssvmUrlDomain.substring(1);
     } else {
       hostname = _ssvmUrlDomain;
     }
     scheme = "https";
   }
   return scheme + "://" + hostname + "/copy/SecStorage/" + dir + "/" + path;
 }
 @Override
 public List<ResourceTagJoinVO> searchByIds(Long... tagIds) {
   // set detail batch query size
   int DETAILS_BATCH_SIZE = 2000;
   String batchCfg = _configDao.getValue("detail.batch.query.size");
   if (batchCfg != null) {
     DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
   }
   // query details by batches
   List<ResourceTagJoinVO> uvList = new ArrayList<ResourceTagJoinVO>();
   // query details by batches
   int curr_index = 0;
   if (tagIds.length > DETAILS_BATCH_SIZE) {
     while ((curr_index + DETAILS_BATCH_SIZE) <= tagIds.length) {
       Long[] ids = new Long[DETAILS_BATCH_SIZE];
       for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
         ids[k] = tagIds[j];
       }
       SearchCriteria<ResourceTagJoinVO> sc = tagSearch.create();
       sc.setParameters("idIN", ids);
       List<ResourceTagJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
       if (vms != null) {
         uvList.addAll(vms);
       }
       curr_index += DETAILS_BATCH_SIZE;
     }
   }
   if (curr_index < tagIds.length) {
     int batch_size = (tagIds.length - curr_index);
     // set the ids value
     Long[] ids = new Long[batch_size];
     for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
       ids[k] = tagIds[j];
     }
     SearchCriteria<ResourceTagJoinVO> sc = tagSearch.create();
     sc.setParameters("idIN", ids);
     List<ResourceTagJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
     if (vms != null) {
       uvList.addAll(vms);
     }
   }
   return uvList;
 }
  @Override
  public boolean configure(String name, Map<String, Object> params) {
    final Map<String, String> configs = _configDao.getConfiguration("management-server", params);
    _proxy = configs.get(Config.SecStorageProxy.key());

    String cert = configs.get("secstorage.ssl.cert.domain");
    if (!"realhostip.com".equalsIgnoreCase(cert)) {
      s_logger.warn(
          "Only realhostip.com ssl cert is supported, ignoring self-signed and other certs");
    }

    _copyAuthPasswd = configs.get("secstorage.copy.password");

    DownloadListener dl = new DownloadListener(this);
    ComponentContext.inject(dl);
    _agentMgr.registerForHostEvents(dl, true, false, false);

    return true;
  }
  public boolean createNetwork(
      Network network,
      NetworkOffering offering,
      DeployDestination dest,
      ReservationContext context) {
    if (_sspUuidDao.findUuidByNetwork(network) != null) {
      s_logger.info("Network already has ssp TenantNetwork uuid :" + network.toString());
      return true;
    }
    if (!canHandle(network)) {
      return false;
    }

    String tenantUuid = _sspTenantDao.findUuidByZone(network.getDataCenterId());
    if (tenantUuid == null) {
      tenantUuid = _configDao.getValueAndInitIfNotExist("ssp.tenant", "Network", null);
    }

    boolean processed = false;
    for (SspClient client :
        fetchSspClients(network.getPhysicalNetworkId(), network.getDataCenterId(), true)) {
      SspClient.TenantNetwork sspNet = client.createTenantNetwork(tenantUuid, network.getName());
      if (sspNet != null) {
        SspUuidVO uuid = new SspUuidVO();
        uuid.setUuid(sspNet.uuid);
        uuid.setObjClass(SspUuidVO.objClassNetwork);
        uuid.setObjId(network.getId());
        _sspUuidDao.persist(uuid);
        return true;
      }
      processed = true;
    }
    if (processed) {
      s_logger.error("Could not allocate an uuid for network " + network.toString());
      return false;
    } else {
      s_logger.error("Skipping #createNetwork() for " + network.toString());
      return true;
    }
  }
  @Override
  public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    Map<String, String> configs = _configDao.getConfiguration(params);

    _userLimit = NumbersUtil.parseInt(configs.get(Config.RemoteAccessVpnUserLimit.key()), 8);

    _pskLength = NumbersUtil.parseInt(configs.get(Config.RemoteAccessVpnPskLength.key()), 24);

    validateRemoteAccessVpnConfiguration();

    VpnSearch = _remoteAccessVpnDao.createSearchBuilder();
    VpnSearch.and("accountId", VpnSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
    SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
    domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
    VpnSearch.join(
        "domainSearch",
        domainSearch,
        VpnSearch.entity().getDomainId(),
        domainSearch.entity().getId(),
        JoinBuilder.JoinType.INNER);
    VpnSearch.done();

    return true;
  }
Exemple #15
0
  @Override
  public boolean start() {
    Integer apiPort = null; // api port, null by default
    SearchCriteria<ConfigurationVO> sc = _configDao.createSearchCriteria();
    sc.addAnd("name", SearchCriteria.Op.EQ, Config.IntegrationAPIPort.key());
    List<ConfigurationVO> values = _configDao.search(sc, null);
    if ((values != null) && (values.size() > 0)) {
      ConfigurationVO apiPortConfig = values.get(0);
      if (apiPortConfig.getValue() != null) {
        apiPort = Integer.parseInt(apiPortConfig.getValue());
      }
    }

    Map<String, String> configs = _configDao.getConfiguration();
    String strSnapshotLimit = configs.get(Config.ConcurrentSnapshotsThresholdPerHost.key());
    if (strSnapshotLimit != null) {
      Long snapshotLimit = NumbersUtil.parseLong(strSnapshotLimit, 1L);
      if (snapshotLimit.longValue() <= 0) {
        s_logger.debug(
            "Global config parameter "
                + Config.ConcurrentSnapshotsThresholdPerHost.toString()
                + " is less or equal 0; defaulting to unlimited");
      } else {
        _dispatcher.setCreateSnapshotQueueSizeLimit(snapshotLimit);
      }
    }

    Set<Class<?>> cmdClasses = new HashSet<Class<?>>();
    for (PluggableService pluggableService : _pluggableServices) {
      cmdClasses.addAll(pluggableService.getCommands());
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("Discovered plugin " + pluggableService.getClass().getSimpleName());
      }
    }

    for (Class<?> cmdClass : cmdClasses) {
      APICommand at = cmdClass.getAnnotation(APICommand.class);
      if (at == null) {
        throw new CloudRuntimeException(
            String.format(
                "%s is claimed as a API command, but it doesn't have @APICommand annotation",
                cmdClass.getName()));
      }
      String apiName = at.name();
      if (_apiNameCmdClassMap.containsKey(apiName)) {
        s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
        continue;
      }
      _apiNameCmdClassMap.put(apiName, cmdClass);
    }

    encodeApiResponse = Boolean.valueOf(_configDao.getValue(Config.EncodeApiResponse.key()));
    String jsonType = _configDao.getValue(Config.JavaScriptDefaultContentType.key());
    if (jsonType != null) {
      jsonContentType = jsonType;
    }

    if (apiPort != null) {
      ListenerThread listenerThread = new ListenerThread(this, apiPort);
      listenerThread.start();
    }

    return true;
  }
 public String getFirstnameAttribute() {
   final String firstnameAttribute = _configDao.getValue("ldap.firstname.attribute");
   return firstnameAttribute == null ? "givenname" : firstnameAttribute;
 }
 public String getLastnameAttribute() {
   final String lastnameAttribute = _configDao.getValue("ldap.lastname.attribute");
   return lastnameAttribute == null ? "sn" : lastnameAttribute;
 }
 public String getBindPrincipal() {
   return _configDao.getValue("ldap.bind.principal");
 }
 public String getEmailAttribute() {
   final String emailAttribute = _configDao.getValue("ldap.email.attribute");
   return emailAttribute == null ? "mail" : emailAttribute;
 }
 public String getBaseDn() {
   return _configDao.getValue("ldap.basedn");
 }
 public String getBindPassword() {
   return _configDao.getValue("ldap.bind.password");
 }
 public String getGroupObject() {
   final String groupObject = _configDao.getValue("ldap.group.object");
   return groupObject == null ? "groupOfUniqueNames" : groupObject;
 }
 public String getGroupUniqueMemeberAttribute() {
   final String uniqueMemberAttribute = _configDao.getValue("ldap.group.user.uniquemember");
   return uniqueMemberAttribute == null ? "uniquemember" : uniqueMemberAttribute;
 }
 public String getUsernameAttribute() {
   final String usernameAttribute = _configDao.getValue("ldap.username.attribute");
   return usernameAttribute == null ? "uid" : usernameAttribute;
 }
 public String getUserObject() {
   final String userObject = _configDao.getValue("ldap.user.object");
   return userObject == null ? "inetOrgPerson" : userObject;
 }
 public String getTrustStorePassword() {
   return _configDao.getValue("ldap.truststore.password");
 }
 public String getTrustStore() {
   return _configDao.getValue("ldap.truststore");
 }
 public String getSearchGroupPrinciple() {
   return _configDao.getValue("ldap.search.group.principle");
 }
 @Override
 public <T> void set(ConfigKey<T> key, T value) {
   _configDao.update(key.key(), value.toString());
 }
  private boolean preparePxeInAdvancedZone(
      VirtualMachineProfile profile,
      NicProfile nic,
      Network network,
      DeployDestination dest,
      ReservationContext context)
      throws Exception {
    DomainRouterVO vr = getVirtualRouter(network);
    List<NicVO> nics = _nicDao.listByVmId(vr.getId());
    NicVO mgmtNic = null;
    for (NicVO nicvo : nics) {
      if (ControlNetworkGuru.class.getSimpleName().equals(nicvo.getReserver())) {
        mgmtNic = nicvo;
        break;
      }
    }

    if (mgmtNic == null) {
      throw new CloudRuntimeException(
          String.format("cannot find management nic on virtual router[id:%s]", vr.getId()));
    }

    String internalServerIp = _configDao.getValue(Config.BaremetalInternalStorageServer.key());
    if (internalServerIp == null) {
      throw new CloudRuntimeException(
          String.format(
              "please specify 'baremetal.internal.storage.server.ip', which is the http server/nfs server storing kickstart files and ISO files, in global setting"));
    }

    List<String> tuple = parseKickstartUrl(profile);
    String cmd =
        String.format(
            "/usr/bin/prepare_pxe.sh %s %s %s %s %s %s",
            tuple.get(1),
            tuple.get(2),
            profile.getTemplate().getUuid(),
            String.format("01-%s", nic.getMacAddress().replaceAll(":", "-")).toLowerCase(),
            tuple.get(0),
            nic.getMacAddress().toLowerCase());
    s_logger.debug(
        String.format(
            "prepare pxe on virtual router[ip:%s], cmd: %s", mgmtNic.getIp4Address(), cmd));
    Pair<Boolean, String> ret =
        SshHelper.sshExecute(
            mgmtNic.getIp4Address(), 3922, "root", getSystemVMKeyFile(), null, cmd);
    if (!ret.first()) {
      throw new CloudRuntimeException(
          String.format(
              "failed preparing PXE in virtual router[id:%s], because %s",
              vr.getId(), ret.second()));
    }

    // String internalServerIp = "10.223.110.231";
    cmd =
        String.format(
            "/usr/bin/baremetal_snat.sh %s %s %s",
            mgmtNic.getIp4Address(), internalServerIp, mgmtNic.getGateway());
    s_logger.debug(
        String.format(
            "prepare SNAT on virtual router[ip:%s], cmd: %s", mgmtNic.getIp4Address(), cmd));
    ret =
        SshHelper.sshExecute(
            mgmtNic.getIp4Address(), 3922, "root", getSystemVMKeyFile(), null, cmd);
    if (!ret.first()) {
      throw new CloudRuntimeException(
          String.format(
              "failed preparing PXE in virtual router[id:%s], because %s",
              vr.getId(), ret.second()));
    }

    return true;
  }