@Override
  public boolean deleteCiscoVnmcResource(DeleteCiscoVnmcResourceCmd cmd) {
    Long vnmcResourceId = cmd.getCiscoVnmcResourceId();
    CiscoVnmcControllerVO vnmcResource = _ciscoVnmcDao.findById(vnmcResourceId);
    if (vnmcResource == null) {
      throw new InvalidParameterValueException(
          "Could not find a Cisco VNMC appliance with id " + vnmcResourceId);
    }

    // Check if there any ASA 1000v appliances
    Long physicalNetworkId = vnmcResource.getPhysicalNetworkId();
    PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (physicalNetwork != null) {
      List<CiscoAsa1000vDeviceVO> responseList =
          _ciscoAsa1000vDao.listByPhysicalNetwork(physicalNetworkId);
      if (responseList.size() > 0) {
        throw new CloudRuntimeException(
            "Cisco VNMC appliance with id "
                + vnmcResourceId
                + " cannot be deleted as there Cisco ASA 1000v appliances using it");
      }
    }

    HostVO vnmcHost = _hostDao.findById(vnmcResource.getHostId());
    Long hostId = vnmcHost.getId();
    vnmcHost.setResourceState(ResourceState.Maintenance);
    _hostDao.update(hostId, vnmcHost);
    _resourceMgr.deleteHost(hostId, false, false);
    _ciscoVnmcDao.remove(vnmcResourceId);

    return true;
  }
 @Override
 public boolean processDisconnect(long agentId, Status state) {
   UserContext context = UserContext.current();
   context.setAccountId(1);
   /* Stopped VMware Host's virtual routers */
   HostVO host = _hostDao.findById(agentId);
   if (host.getHypervisorType() != HypervisorType.VMware) {
     return true;
   }
   List<DomainRouterVO> routers = _routerDao.listByHostId(agentId);
   for (DomainRouterVO router : routers) {
     try {
       State oldState = router.getState();
       _routerMgr.stopRouter(router.getId(), true);
       // In case only vCenter is disconnected, we want to shut down router directly
       if (oldState == State.Running) {
         shutdownRouterVM(router);
       }
     } catch (ResourceUnavailableException e) {
       s_logger.warn(
           "Fail to stop router " + router.getInstanceName() + " when host disconnected!", e);
     } catch (ConcurrentOperationException e) {
       s_logger.warn(
           "Fail to stop router " + router.getInstanceName() + " when host disconnected!", e);
     }
   }
   return true;
 }
  @Override
  public Pair<AfterScanAction, Object> scanPool(Long pool) {
    long dataCenterId = pool.longValue();

    List<SecondaryStorageVmVO> ssVms =
        _secStorageVmDao.getSecStorageVmListInStates(
            SecondaryStorageVm.Role.templateProcessor,
            dataCenterId,
            State.Running,
            State.Migrating,
            State.Starting,
            State.Stopped,
            State.Stopping);
    int vmSize = (ssVms == null) ? 0 : ssVms.size();
    List<HostVO> ssHosts = _hostDao.listSecondaryStorageHosts(dataCenterId);
    int hostSize = (ssHosts == null) ? 0 : ssHosts.size();
    if (hostSize > vmSize) {
      s_logger.info(
          "No secondary storage vms found in datacenter id="
              + dataCenterId
              + ", starting a new one");
      return new Pair<AfterScanAction, Object>(
          AfterScanAction.expand, SecondaryStorageVm.Role.templateProcessor);
    }

    return new Pair<AfterScanAction, Object>(
        AfterScanAction.nop, SecondaryStorageVm.Role.templateProcessor);
  }
  @Override
  public String getSecondaryStorageStoreUrl(long dcId) {
    List<HostVO> secStorageHosts = _hostDao.listSecondaryStorageHosts(dcId);
    if (secStorageHosts.size() > 0) return secStorageHosts.get(0).getStorageUrl();

    return null;
  }
Esempio n. 5
0
  @Override
  public List<DomainRouterVO> startRouters(
      final RouterDeploymentDefinition routerDeploymentDefinition)
      throws StorageUnavailableException, InsufficientCapacityException,
          ConcurrentOperationException, ResourceUnavailableException {

    final List<DomainRouterVO> runningRouters = new ArrayList<DomainRouterVO>();

    for (DomainRouterVO router : routerDeploymentDefinition.getRouters()) {
      boolean skip = false;
      final State state = router.getState();
      if (router.getHostId() != null && state != State.Running) {
        final HostVO host = _hostDao.findById(router.getHostId());
        if (host == null || host.getState() != Status.Up) {
          skip = true;
        }
      }
      if (!skip) {
        if (state != State.Running) {
          router =
              startVirtualRouter(
                  router,
                  _accountMgr.getSystemUser(),
                  _accountMgr.getSystemAccount(),
                  routerDeploymentDefinition.getParams());
        }
        if (router != null) {
          runningRouters.add(router);
        }
      }
    }
    return runningRouters;
  }
  @Override
  public Status investigate(final long hostId) {
    final HostVO host = _hostDao.findById(hostId);
    if (host == null) {
      return null;
    }

    final Enumeration<Investigator> en = _investigators.enumeration();
    Status hostState = null;
    Investigator investigator = null;
    while (en.hasMoreElements()) {
      investigator = en.nextElement();
      hostState = investigator.isAgentAlive(host);
      if (hostState != null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              investigator.getName()
                  + " was able to determine host "
                  + hostId
                  + " is in "
                  + hostState.toString());
        }
        return hostState;
      }
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(
            investigator.getName() + " unable to determine the state of the host.  Moving on.");
      }
    }

    return null;
  }
Esempio n. 7
0
  private void updateNetworkLabels(HostVO host) {
    // check if networkLabels need to be updated in details
    // we send only private and storage network label to the resource.
    String privateNetworkLabel =
        _networkMgr.getDefaultManagementTrafficLabel(
            host.getDataCenterId(), host.getHypervisorType());
    String storageNetworkLabel =
        _networkMgr.getDefaultStorageTrafficLabel(host.getDataCenterId(), host.getHypervisorType());

    String privateDevice = host.getDetail("private.network.device");
    String storageDevice = host.getDetail("storage.network.device1");

    boolean update = false;

    if (privateNetworkLabel != null && !privateNetworkLabel.equalsIgnoreCase(privateDevice)) {
      host.setDetail("private.network.device", privateNetworkLabel);
      update = true;
    }
    if (storageNetworkLabel != null && !storageNetworkLabel.equalsIgnoreCase(storageDevice)) {
      host.setDetail("storage.network.device1", storageNetworkLabel);
      update = true;
    }
    if (update) {
      _hostDao.saveDetails(host);
    }
  }
  @Override
  public Status investigate(final long hostId) {
    final HostVO host = _hostDao.findById(hostId);
    if (host == null) {
      return null;
    }

    Status hostState = null;
    for (Investigator investigator : investigators) {
      hostState = investigator.isAgentAlive(host);
      if (hostState != null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              investigator.getName()
                  + " was able to determine host "
                  + hostId
                  + " is in "
                  + hostState.toString());
        }
        return hostState;
      }
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(
            investigator.getName() + " unable to determine the state of the host.  Moving on.");
      }
    }

    return null;
  }
  @Override
  public void shutdown(final NetworkProfile profile, final NetworkOffering offering) {
    final NetworkVO networkObject = networkDao.findById(profile.getId());
    if (networkObject.getBroadcastDomainType() != BroadcastDomainType.Lswitch
        || networkObject.getBroadcastUri() == null) {
      s_logger.warn(
          "BroadcastUri is empty or incorrect for guestnetwork " + networkObject.getDisplayText());
      return;
    }

    final List<NiciraNvpDeviceVO> devices =
        niciraNvpDao.listByPhysicalNetwork(networkObject.getPhysicalNetworkId());
    if (devices.isEmpty()) {
      s_logger.error(
          "No NiciraNvp Controller on physical network " + networkObject.getPhysicalNetworkId());
      return;
    }
    final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
    final HostVO niciraNvpHost = hostDao.findById(niciraNvpDevice.getHostId());

    final DeleteLogicalSwitchCommand cmd =
        new DeleteLogicalSwitchCommand(
            BroadcastDomainType.getValue(networkObject.getBroadcastUri()));
    final DeleteLogicalSwitchAnswer answer =
        (DeleteLogicalSwitchAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);

    if (answer == null || !answer.getResult()) {
      s_logger.error("DeleteLogicalSwitchCommand failed");
    }

    super.shutdown(profile, offering);
  }
  // Host.status is up and Host.type is routing
  protected List<Long> findHostByPod(long podId, Long excludeHostId) {
    List<Long> hostIds = _hostDao.listBy(null, podId, null, Type.Routing, Status.Up);
    if (excludeHostId != null) {
      hostIds.remove(excludeHostId);
    }

    return hostIds;
  }
  @Override
  public boolean generateVMSetupCommand(Long ssAHostId) {
    HostVO ssAHost = _hostDao.findById(ssAHostId);
    if (ssAHost.getType() != Host.Type.SecondaryStorageVM) {
      return false;
    }
    SecondaryStorageVmVO secStorageVm = _secStorageVmDao.findByInstanceName(ssAHost.getName());
    if (secStorageVm == null) {
      s_logger.warn("secondary storage VM " + ssAHost.getName() + " doesn't exist");
      return false;
    }

    SecStorageVMSetupCommand setupCmd = new SecStorageVMSetupCommand();
    if (_allowedInternalSites != null) {
      List<String> allowedCidrs = new ArrayList<String>();
      String[] cidrs = _allowedInternalSites.split(",");
      for (String cidr : cidrs) {
        if (NetUtils.isValidCIDR(cidr) || NetUtils.isValidIp(cidr)) {
          allowedCidrs.add(cidr);
        }
      }
      List<? extends Nic> nics =
          _networkMgr.getNicsForTraffic(secStorageVm.getId(), TrafficType.Management);
      Nic privateNic = nics.get(0);
      String privateCidr =
          NetUtils.ipAndNetMaskToCidr(privateNic.getIp4Address(), privateNic.getNetmask());
      String publicCidr =
          NetUtils.ipAndNetMaskToCidr(
              secStorageVm.getPublicIpAddress(), secStorageVm.getPublicNetmask());
      if (NetUtils.isNetworkAWithinNetworkB(privateCidr, publicCidr)
          || NetUtils.isNetworkAWithinNetworkB(publicCidr, privateCidr)) {
        s_logger.info(
            "private and public interface overlaps, add a default route through private interface. privateCidr: "
                + privateCidr
                + ", publicCidr: "
                + publicCidr);
        allowedCidrs.add(NetUtils.ALL_CIDRS);
      }
      setupCmd.setAllowedInternalSites(allowedCidrs.toArray(new String[allowedCidrs.size()]));
    }
    String copyPasswd = _configDao.getValue("secstorage.copy.password");
    setupCmd.setCopyPassword(copyPasswd);
    setupCmd.setCopyUserName(TemplateConstants.DEFAULT_HTTP_AUTH_USER);
    Answer answer = _agentMgr.easySend(ssAHostId, setupCmd);
    if (answer != null && answer.getResult()) {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug("Successfully programmed http auth into " + secStorageVm.getHostName());
      }
      return true;
    } else {
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(
            "failed to program http auth into secondary storage vm : "
                + secStorageVm.getHostName());
      }
      return false;
    }
  }
Esempio n. 12
0
 @Override
 protected void injectMockito() {
   if (host == null) {
     return;
   }
   List<HostVO> results = new ArrayList<HostVO>();
   results.add(host);
   Mockito.when(hostDao.listAll()).thenReturn(results);
   Mockito.when(hostDao.findById(Matchers.anyLong())).thenReturn(host);
   Mockito.when(hostDao.findHypervisorHostInCluster(Matchers.anyLong())).thenReturn(results);
   List<EndPoint> eps = new ArrayList<EndPoint>();
   eps.add(
       RemoteHostEndPoint.getHypervisorHostEndPoint(
           host.getId(), host.getPrivateIpAddress(), host.getPublicIpAddress()));
   Mockito.when(selector.selectAll(Matchers.any(DataStore.class))).thenReturn(eps);
   Mockito.when(selector.select(Matchers.any(DataObject.class))).thenReturn(eps.get(0));
   Mockito.when(selector.select(Matchers.any(DataObject.class), Matchers.any(DataObject.class)))
       .thenReturn(eps.get(0));
 }
  @Override
  public boolean deleteHost(Long hostId) {
    List<SnapshotVO> snapshots = _snapshotDao.listByHostId(hostId);
    if (snapshots != null && !snapshots.isEmpty()) {
      throw new CloudRuntimeException(
          "Can not delete this secondary storage due to there are still snapshots on it ");
    }
    List<Long> list = _templateDao.listPrivateTemplatesByHost(hostId);
    if (list != null && !list.isEmpty()) {
      throw new CloudRuntimeException(
          "Can not delete this secondary storage due to there are still private template on it ");
    }
    _vmTemplateHostDao.deleteByHost(hostId);
    HostVO host = _hostDao.findById(hostId);
    host.setGuid(null);
    _hostDao.update(hostId, host);
    _hostDao.remove(hostId);

    return true;
  }
  @Test
  public void investigateHostStatusFailure() {
    Mockito.when(_hostDao.findById(Mockito.anyLong())).thenReturn(hostVO);
    // Set the list of investigators, CheckOnAgentInvestigator suffices for now
    // Also no need to mock isAgentAlive() as actual implementation returns null
    Investigator investigator = Mockito.mock(CheckOnAgentInvestigator.class);
    List<Investigator> investigators = new ArrayList<Investigator>();
    investigators.add(investigator);
    highAvailabilityManager.setInvestigators(investigators);

    assertNull(highAvailabilityManager.investigate(1l));
  }
  @Override
  public CiscoVnmcResourceResponse createCiscoVnmcResourceResponse(
      CiscoVnmcController ciscoVnmcResourceVO) {
    HostVO ciscoVnmcHost = _hostDao.findById(ciscoVnmcResourceVO.getHostId());

    CiscoVnmcResourceResponse response = new CiscoVnmcResourceResponse();
    response.setId(ciscoVnmcResourceVO.getUuid());
    response.setPhysicalNetworkId(ciscoVnmcResourceVO.getPhysicalNetworkId());
    response.setProviderName(ciscoVnmcResourceVO.getProviderName());
    response.setResourceName(ciscoVnmcHost.getName());

    return response;
  }
  @Test
  public void investigateHostStatusSuccess() {
    Mockito.when(_hostDao.findById(Mockito.anyLong())).thenReturn(hostVO);
    // Set the list of investigators, CheckOnAgentInvestigator suffices for now
    Investigator investigator = Mockito.mock(CheckOnAgentInvestigator.class);
    List<Investigator> investigators = new ArrayList<Investigator>();
    investigators.add(investigator);
    highAvailabilityManager.setInvestigators(investigators);
    // Mock isAgentAlive to return host status as Down
    Mockito.when(investigator.isAgentAlive(hostVO)).thenReturn(Status.Down);

    assertTrue(highAvailabilityManager.investigate(1l) == Status.Down);
  }
Esempio n. 17
0
  @Override
  public HostVO createHostVOForConnectedAgent(HostVO host, StartupCommand[] cmd) {
    StartupCommand firstCmd = cmd[0];
    if (!(firstCmd instanceof StartupRoutingCommand)) {
      return null;
    }

    StartupRoutingCommand ssCmd = ((StartupRoutingCommand) firstCmd);
    if (ssCmd.getHypervisorType() != HypervisorType.KVM) {
      return null;
    }

    /* KVM requires host are the same in cluster */
    ClusterVO clusterVO = _clusterDao.findById(host.getClusterId());
    List<HostVO> hostsInCluster = _resourceMgr.listAllHostsInCluster(clusterVO.getId());
    if (!hostsInCluster.isEmpty()) {
      HostVO oneHost = hostsInCluster.get(0);
      _hostDao.loadDetails(oneHost);
      String hostOsInCluster = oneHost.getDetail("Host.OS");
      String hostOs = ssCmd.getHostDetails().get("Host.OS");
      if (!hostOsInCluster.equalsIgnoreCase(hostOs)) {
        throw new IllegalArgumentException(
            "Can't add host: "
                + firstCmd.getPrivateIpAddress()
                + " with hostOS: "
                + hostOs
                + " into a cluster,"
                + "in which there are "
                + hostOsInCluster
                + " hosts added");
      }
    }

    _hostDao.loadDetails(host);

    return _resourceMgr.fillRoutingHostVO(host, ssCmd, HypervisorType.KVM, host.getDetails(), null);
  }
  private synchronized Map<Long, ZoneHostInfo> getZoneHostInfo() {
    Date cutTime = DateUtil.currentGMTTime();
    List<RunningHostCountInfo> l =
        _hostDao.getRunningHostCounts(
            new Date(cutTime.getTime() - _clusterMgr.getHeartbeatThreshold()));

    RunningHostInfoAgregator aggregator = new RunningHostInfoAgregator();
    if (l.size() > 0) {
      for (RunningHostCountInfo countInfo : l) {
        aggregator.aggregate(countInfo);
      }
    }

    return aggregator.getZoneHostInfoMap();
  }
  @Override
  public AgentControlAnswer onConsoleAccessAuthentication(ConsoleAccessAuthenticationCommand cmd) {
    long vmId = 0;

    if (cmd.getVmId() != null && cmd.getVmId().isEmpty()) {
      if (s_logger.isTraceEnabled()) {
        s_logger.trace("Invalid vm id sent from proxy(happens when proxy session has terminated)");
      }
      return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }

    try {
      vmId = Long.parseLong(cmd.getVmId());
    } catch (NumberFormatException e) {
      s_logger.error(
          "Invalid vm id " + cmd.getVmId() + " sent from console access authentication", e);
      return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }

    // TODO authentication channel between console proxy VM and management
    // server needs to be secured,
    // the data is now being sent through private network, but this is
    // apparently not enough
    VMInstanceVO vm = _instanceDao.findById(vmId);
    if (vm == null) {
      return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }

    if (vm.getHostId() == null) {
      s_logger.warn("VM " + vmId + " lost host info, failed authentication request");
      return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }

    HostVO host = _hostDao.findById(vm.getHostId());
    if (host == null) {
      s_logger.warn("VM " + vmId + "'s host does not exist, fail authentication request");
      return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }

    String sid = cmd.getSid();
    if (sid == null || !sid.equals(vm.getVncPassword())) {
      s_logger.warn("sid " + sid + " in url does not match stored sid " + vm.getVncPassword());
      return new ConsoleAccessAuthenticationAnswer(cmd, false);
    }

    return new ConsoleAccessAuthenticationAnswer(cmd, true);
  }
  public boolean isZoneReady(Map<Long, ZoneHostInfo> zoneHostInfoMap, long dataCenterId) {
    ZoneHostInfo zoneHostInfo = zoneHostInfoMap.get(dataCenterId);
    if (zoneHostInfo != null
        && (zoneHostInfo.getFlags() & RunningHostInfoAgregator.ZoneHostInfo.ROUTING_HOST_MASK)
            != 0) {
      VMTemplateVO template = _templateDao.findSystemVMTemplate(dataCenterId);
      HostVO secHost = _hostDao.findSecondaryStorageHost(dataCenterId);
      if (secHost == null) {
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              "No secondary storage available in zone "
                  + dataCenterId
                  + ", wait until it is ready to launch secondary storage vm");
        }
        return false;
      }

      boolean templateReady = false;
      if (template != null) {
        VMTemplateHostVO templateHostRef =
            _vmTemplateHostDao.findByHostTemplate(secHost.getId(), template.getId());
        templateReady =
            (templateHostRef != null) && (templateHostRef.getDownloadState() == Status.DOWNLOADED);
      }

      if (templateReady) {

        List<Pair<Long, Integer>> l =
            _storagePoolHostDao.getDatacenterStoragePoolHostInfo(dataCenterId, !_useLocalStorage);
        if (l != null && l.size() > 0 && l.get(0).second().intValue() > 0) {

          return true;
        } else {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug(
                "Primary storage is not ready, wait until it is ready to launch secondary storage vm");
          }
        }
      } else {
        if (s_logger.isTraceEnabled()) {
          s_logger.trace("Zone host is ready, but secondary storage vm template is not ready");
        }
      }
    }
    return false;
  }
  @Override
  public boolean shutdown(Network network, ReservationContext context, boolean cleanup)
      throws ConcurrentOperationException, ResourceUnavailableException {

    unassignAsa1000vFromNetwork(network);

    String vlan = network.getBroadcastUri().getHost();
    long vlanId = Long.parseLong(vlan);
    List<CiscoVnmcControllerVO> devices =
        _ciscoVnmcDao.listByPhysicalNetwork(network.getPhysicalNetworkId());
    if (!devices.isEmpty()) {
      CiscoVnmcControllerVO ciscoVnmcDevice = devices.get(0);
      HostVO ciscoVnmcHost = _hostDao.findById(ciscoVnmcDevice.getHostId());
      cleanupLogicalEdgeFirewall(vlanId, ciscoVnmcHost.getId());
    }

    return true;
  }
Esempio n. 22
0
  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;
  }
  @Override
  public boolean deleteTrafficMonitor(DeleteTrafficMonitorCmd cmd) {
    long hostId = cmd.getId();
    User caller = _accountMgr.getActiveUser(UserContext.current().getCallerUserId());
    HostVO trafficMonitor = _hostDao.findById(hostId);
    if (trafficMonitor == null) {
      throw new InvalidParameterValueException(
          "Could not find an traffic monitor with ID: " + hostId);
    }

    try {
      if (_resourceMgr.maintain(hostId) && _resourceMgr.deleteHost(hostId, false, false)) {
        return true;
      } else {
        return false;
      }
    } catch (AgentUnavailableException e) {
      s_logger.debug(e);
      return false;
    }
  }
Esempio n. 24
0
  @Override
  public boolean release(
      final Network network,
      final NicProfile nic,
      final VirtualMachineProfile vm,
      final ReservationContext context)
      throws ConcurrentOperationException, ResourceUnavailableException {
    if (!canHandle(network, Service.Connectivity)) {
      return false;
    }
    if (nic.getBroadcastType() != Networks.BroadcastDomainType.Vswitch) {
      return false;
    }

    if (nic.getTrafficType() != Networks.TrafficType.Guest) {
      return false;
    }

    final HostVO host = _hostDao.findById(vm.getVirtualMachine().getHostId());
    _ovsTunnelMgr.checkAndRemoveHostFromTunnelNetwork(network, host);
    return true;
  }
  @Override
  public ServerResource reloadResource(HostVO host) {
    String resourceName = host.getResource();
    ServerResource resource = getResource(resourceName);

    if (resource != null) {
      _hostDao.loadDetails(host);

      HashMap<String, Object> params = buildConfigParams(host);
      try {
        resource.configure(host.getName(), params);
      } catch (ConfigurationException e) {
        s_logger.warn("Unable to configure resource due to " + e.getMessage());
        return null;
      }
      if (!resource.start()) {
        s_logger.warn("Unable to start the resource");
        return null;
      }
    }
    return resource;
  }
Esempio n. 26
0
  @Override
  public Map<? extends ServerResource, Map<String, String>> find(
      long dcId,
      Long podId,
      Long clusterId,
      URI uri,
      String username,
      String password,
      List<String> hostTags)
      throws DiscoveryException {

    ClusterVO cluster = _clusterDao.findById(clusterId);
    if (cluster == null || cluster.getHypervisorType() != HypervisorType.KVM) {
      if (s_logger.isInfoEnabled())
        s_logger.info("invalid cluster id or cluster is not for KVM hypervisors");
      return null;
    }

    Map<KvmDummyResourceBase, Map<String, String>> resources =
        new HashMap<KvmDummyResourceBase, Map<String, String>>();
    Map<String, String> details = new HashMap<String, String>();
    if (!uri.getScheme().equals("http")) {
      String msg =
          "urlString is not http so we're not taking care of the discovery for this: " + uri;
      s_logger.debug(msg);
      return null;
    }
    com.trilead.ssh2.Connection sshConnection = null;
    String agentIp = null;
    try {

      String hostname = uri.getHost();
      InetAddress ia = InetAddress.getByName(hostname);
      agentIp = ia.getHostAddress();
      String guid = UUID.nameUUIDFromBytes(agentIp.getBytes()).toString();
      String guidWithTail = guid + "-LibvirtComputingResource"; /*tail added by agent.java*/
      if (_resourceMgr.findHostByGuid(guidWithTail) != null) {
        s_logger.debug(
            "Skipping " + agentIp + " because " + guidWithTail + " is already in the database.");
        return null;
      }

      sshConnection = new com.trilead.ssh2.Connection(agentIp, 22);

      sshConnection.connect(null, 60000, 60000);
      if (!sshConnection.authenticateWithPassword(username, password)) {
        s_logger.debug("Failed to authenticate");
        throw new DiscoveredWithErrorException("Authentication error");
      }

      if (!SSHCmdHelper.sshExecuteCmd(sshConnection, "lsmod|grep kvm", 3)) {
        s_logger.debug("It's not a KVM enabled machine");
        return null;
      }

      List<PhysicalNetworkSetupInfo> netInfos =
          _networkMgr.getPhysicalNetworkInfo(dcId, HypervisorType.KVM);
      String kvmPrivateNic = _kvmPrivateNic;
      String kvmPublicNic = _kvmPublicNic;
      String kvmGuestNic = _kvmGuestNic;

      for (PhysicalNetworkSetupInfo info : netInfos) {
        if (info.getPrivateNetworkName() != null) {
          kvmPrivateNic = info.getPrivateNetworkName();
        }
        if (info.getPublicNetworkName() != null) {
          kvmPublicNic = info.getPublicNetworkName();
        }
        if (info.getGuestNetworkName() != null) {
          kvmGuestNic = info.getGuestNetworkName();
        }
      }

      String parameters =
          " -m " + _hostIp + " -z " + dcId + " -p " + podId + " -c " + clusterId + " -g " + guid
              + " -a";

      if (kvmPublicNic != null) {
        parameters += " --pubNic=" + kvmPublicNic;
      }

      if (kvmPrivateNic != null) {
        parameters += " --prvNic=" + kvmPrivateNic;
      }

      if (kvmGuestNic != null) {
        parameters += " --guestNic=" + kvmGuestNic;
      }

      SSHCmdHelper.sshExecuteCmd(sshConnection, "cloud-setup-agent " + parameters, 3);

      KvmDummyResourceBase kvmResource = new KvmDummyResourceBase();
      Map<String, Object> params = new HashMap<String, Object>();

      params.put("zone", Long.toString(dcId));
      params.put("pod", Long.toString(podId));
      params.put("cluster", Long.toString(clusterId));
      params.put("guid", guid);
      params.put("agentIp", agentIp);
      kvmResource.configure("kvm agent", params);
      resources.put(kvmResource, details);

      HostVO connectedHost = waitForHostConnect(dcId, podId, clusterId, guidWithTail);
      if (connectedHost == null) return null;

      details.put("guid", guidWithTail);

      // place a place holder guid derived from cluster ID
      if (cluster.getGuid() == null) {
        cluster.setGuid(UUID.nameUUIDFromBytes(String.valueOf(clusterId).getBytes()).toString());
        _clusterDao.update(clusterId, cluster);
      }

      // save user name and password
      _hostDao.loadDetails(connectedHost);
      Map<String, String> hostDetails = connectedHost.getDetails();
      hostDetails.put("password", password);
      hostDetails.put("username", username);
      _hostDao.saveDetails(connectedHost);
      return resources;
    } catch (DiscoveredWithErrorException e) {
      throw e;
    } catch (Exception e) {
      String msg = " can't setup agent, due to " + e.toString() + " - " + e.getMessage();
      s_logger.warn(msg);
    } finally {
      if (sshConnection != null) sshConnection.close();
    }

    return null;
  }
Esempio n. 27
0
  @DB
  @Override
  public void allocateVmCapacity(VirtualMachine vm, boolean fromLastHost) {

    long hostId = vm.getHostId();
    HostVO host = _hostDao.findById(hostId);
    long clusterId = host.getClusterId();
    float cpuOvercommitRatio =
        Float.parseFloat(_clusterDetailsDao.findDetail(clusterId, "cpuOvercommitRatio").getValue());
    float memoryOvercommitRatio =
        Float.parseFloat(
            _clusterDetailsDao.findDetail(clusterId, "memoryOvercommitRatio").getValue());

    ServiceOfferingVO svo = _offeringsDao.findById(vm.getServiceOfferingId());

    CapacityVO capacityCpu = _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_CPU);
    CapacityVO capacityMem = _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_MEMORY);

    if (capacityCpu == null || capacityMem == null || svo == null) {
      return;
    }

    int cpu = (int) (svo.getCpu() * svo.getSpeed());
    long ram = (long) (svo.getRamSize() * 1024L * 1024L);

    Transaction txn = Transaction.currentTxn();

    try {
      txn.start();
      capacityCpu = _capacityDao.lockRow(capacityCpu.getId(), true);
      capacityMem = _capacityDao.lockRow(capacityMem.getId(), true);

      long usedCpu = capacityCpu.getUsedCapacity();
      long usedMem = capacityMem.getUsedCapacity();
      long reservedCpu = capacityCpu.getReservedCapacity();
      long reservedMem = capacityMem.getReservedCapacity();
      long actualTotalCpu = capacityCpu.getTotalCapacity();
      long actualTotalMem = capacityMem.getTotalCapacity();
      long totalCpu = (long) (actualTotalCpu * cpuOvercommitRatio);
      long totalMem = (long) (actualTotalMem * memoryOvercommitRatio);
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(
            "Hosts's actual total CPU: "
                + actualTotalCpu
                + " and CPU after applying overprovisioning: "
                + totalCpu);
      }

      long freeCpu = totalCpu - (reservedCpu + usedCpu);
      long freeMem = totalMem - (reservedMem + usedMem);

      if (s_logger.isDebugEnabled()) {
        s_logger.debug("We are allocating VM, increasing the used capacity of this host:" + hostId);
        s_logger.debug(
            "Current Used CPU: " + usedCpu + " , Free CPU:" + freeCpu + " ,Requested CPU: " + cpu);
        s_logger.debug(
            "Current Used RAM: " + usedMem + " , Free RAM:" + freeMem + " ,Requested RAM: " + ram);
      }
      capacityCpu.setUsedCapacity(usedCpu + cpu);
      capacityMem.setUsedCapacity(usedMem + ram);

      if (fromLastHost) {
        /* alloc from reserved */
        if (s_logger.isDebugEnabled()) {
          s_logger.debug(
              "We are allocating VM to the last host again, so adjusting the reserved capacity if it is not less than required");
          s_logger.debug("Reserved CPU: " + reservedCpu + " , Requested CPU: " + cpu);
          s_logger.debug("Reserved RAM: " + reservedMem + " , Requested RAM: " + ram);
        }
        if (reservedCpu >= cpu && reservedMem >= ram) {
          capacityCpu.setReservedCapacity(reservedCpu - cpu);
          capacityMem.setReservedCapacity(reservedMem - ram);
        }
      } else {
        /* alloc from free resource */
        if (!((reservedCpu + usedCpu + cpu <= totalCpu)
            && (reservedMem + usedMem + ram <= totalMem))) {
          if (s_logger.isDebugEnabled()) {
            s_logger.debug(
                "Host doesnt seem to have enough free capacity, but increasing the used capacity anyways, since the VM is already starting on this host ");
          }
        }
      }

      s_logger.debug(
          "CPU STATS after allocation: for host: "
              + hostId
              + ", old used: "
              + usedCpu
              + ", old reserved: "
              + reservedCpu
              + ", actual total: "
              + actualTotalCpu
              + ", total with overprovisioning: "
              + totalCpu
              + "; new used:"
              + capacityCpu.getUsedCapacity()
              + ", reserved:"
              + capacityCpu.getReservedCapacity()
              + "; requested cpu:"
              + cpu
              + ",alloc_from_last:"
              + fromLastHost);

      s_logger.debug(
          "RAM STATS after allocation: for host: "
              + hostId
              + ", old used: "
              + usedMem
              + ", old reserved: "
              + reservedMem
              + ", total: "
              + totalMem
              + "; new used: "
              + capacityMem.getUsedCapacity()
              + ", reserved: "
              + capacityMem.getReservedCapacity()
              + "; requested mem: "
              + ram
              + ",alloc_from_last:"
              + fromLastHost);

      _capacityDao.update(capacityCpu.getId(), capacityCpu);
      _capacityDao.update(capacityMem.getId(), capacityMem);
      txn.commit();
    } catch (Exception e) {
      txn.rollback();
      return;
    }
  }
Esempio n. 28
0
  @DB
  @Override
  public boolean releaseVmCapacity(
      VirtualMachine vm, boolean moveFromReserved, boolean moveToReservered, Long hostId) {
    ServiceOfferingVO svo = _offeringsDao.findById(vm.getServiceOfferingId());
    CapacityVO capacityCpu = _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_CPU);
    CapacityVO capacityMemory =
        _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_MEMORY);
    Long clusterId = null;
    if (hostId != null) {
      HostVO host = _hostDao.findById(hostId);
      clusterId = host.getClusterId();
    }
    if (capacityCpu == null || capacityMemory == null || svo == null) {
      return false;
    }

    Transaction txn = Transaction.currentTxn();
    try {
      txn.start();

      capacityCpu = _capacityDao.lockRow(capacityCpu.getId(), true);
      capacityMemory = _capacityDao.lockRow(capacityMemory.getId(), true);

      long usedCpu = capacityCpu.getUsedCapacity();
      long usedMem = capacityMemory.getUsedCapacity();
      long reservedCpu = capacityCpu.getReservedCapacity();
      long reservedMem = capacityMemory.getReservedCapacity();
      long actualTotalCpu = capacityCpu.getTotalCapacity();
      float cpuOvercommitRatio =
          Float.parseFloat(
              _clusterDetailsDao.findDetail(clusterId, "cpuOvercommitRatio").getValue());
      float memoryOvercommitRatio =
          Float.parseFloat(
              _clusterDetailsDao.findDetail(clusterId, "memoryOvercommitRatio").getValue());
      int vmCPU = (int) (svo.getCpu() * svo.getSpeed());
      long vmMem = (long) (svo.getRamSize() * 1024L * 1024L);
      long actualTotalMem = capacityMemory.getTotalCapacity();
      long totalMem = (long) (actualTotalMem * memoryOvercommitRatio);
      long totalCpu = (long) (actualTotalCpu * cpuOvercommitRatio);
      if (s_logger.isDebugEnabled()) {
        s_logger.debug(
            "Hosts's actual total CPU: "
                + actualTotalCpu
                + " and CPU after applying overprovisioning: "
                + totalCpu);
        s_logger.debug(
            "Hosts's actual total RAM: "
                + actualTotalMem
                + " and RAM after applying overprovisioning: "
                + totalMem);
      }

      if (!moveFromReserved) {
        /* move resource from used */
        if (usedCpu >= vmCPU) {
          capacityCpu.setUsedCapacity(usedCpu - vmCPU);
        }
        if (usedMem >= vmMem) {
          capacityMemory.setUsedCapacity(usedMem - vmMem);
        }

        if (moveToReservered) {
          if (reservedCpu + vmCPU <= totalCpu) {
            capacityCpu.setReservedCapacity(reservedCpu + vmCPU);
          }
          if (reservedMem + vmMem <= totalMem) {
            capacityMemory.setReservedCapacity(reservedMem + vmMem);
          }
        }
      } else {
        if (reservedCpu >= vmCPU) {
          capacityCpu.setReservedCapacity(reservedCpu - vmCPU);
        }
        if (reservedMem >= vmMem) {
          capacityMemory.setReservedCapacity(reservedMem - vmMem);
        }
      }

      s_logger.debug(
          "release cpu from host: "
              + hostId
              + ", old used: "
              + usedCpu
              + ",reserved: "
              + reservedCpu
              + ", actual total: "
              + actualTotalCpu
              + ", total with overprovisioning: "
              + totalCpu
              + "; new used: "
              + capacityCpu.getUsedCapacity()
              + ",reserved:"
              + capacityCpu.getReservedCapacity()
              + "; movedfromreserved: "
              + moveFromReserved
              + ",moveToReservered"
              + moveToReservered);

      s_logger.debug(
          "release mem from host: "
              + hostId
              + ", old used: "
              + usedMem
              + ",reserved: "
              + reservedMem
              + ", total: "
              + totalMem
              + "; new used: "
              + capacityMemory.getUsedCapacity()
              + ",reserved:"
              + capacityMemory.getReservedCapacity()
              + "; movedfromreserved: "
              + moveFromReserved
              + ",moveToReservered"
              + moveToReservered);

      _capacityDao.update(capacityCpu.getId(), capacityCpu);
      _capacityDao.update(capacityMemory.getId(), capacityMemory);
      txn.commit();
      return true;
    } catch (Exception e) {
      s_logger.debug("Failed to transit vm's state, due to " + e.getMessage());
      txn.rollback();
      return false;
    }
  }
Esempio n. 29
0
 @Override
 public void processCancelMaintenaceEventAfter(Long hostId) {
   updateCapacityForHost(_hostDao.findById(hostId));
 }
Esempio n. 30
0
  @PostConstruct
  protected void init() {
    AllFieldsSearch = createSearchBuilder();
    AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
    AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
    AllFieldsSearch.and("role", AllFieldsSearch.entity().getRole(), Op.EQ);
    AllFieldsSearch.and("domainId", AllFieldsSearch.entity().getDomainId(), Op.EQ);
    AllFieldsSearch.and("host", AllFieldsSearch.entity().getHostId(), Op.EQ);
    AllFieldsSearch.and("lastHost", AllFieldsSearch.entity().getLastHostId(), Op.EQ);
    AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ);
    AllFieldsSearch.and("states", AllFieldsSearch.entity().getState(), Op.IN);
    SearchBuilder<RouterNetworkVO> joinRouterNetwork = _routerNetworkDao.createSearchBuilder();
    joinRouterNetwork.and("networkId", joinRouterNetwork.entity().getNetworkId(), Op.EQ);
    AllFieldsSearch.join(
        "networkRouter",
        joinRouterNetwork,
        joinRouterNetwork.entity().getRouterId(),
        AllFieldsSearch.entity().getId(),
        JoinType.INNER);
    AllFieldsSearch.and("podId", AllFieldsSearch.entity().getPodIdToDeployIn(), Op.EQ);
    AllFieldsSearch.and("elementId", AllFieldsSearch.entity().getElementId(), Op.EQ);
    AllFieldsSearch.and("vpcId", AllFieldsSearch.entity().getVpcId(), Op.EQ);
    AllFieldsSearch.done();

    VpcSearch = createSearchBuilder();
    VpcSearch.and("role", VpcSearch.entity().getRole(), Op.EQ);
    VpcSearch.and("vpcId", VpcSearch.entity().getVpcId(), Op.EQ);
    VpcSearch.done();

    IdNetworkIdStatesSearch = createSearchBuilder();
    IdNetworkIdStatesSearch.and("id", IdNetworkIdStatesSearch.entity().getId(), Op.EQ);
    SearchBuilder<RouterNetworkVO> joinRouterNetwork1 = _routerNetworkDao.createSearchBuilder();
    joinRouterNetwork1.and("networkId", joinRouterNetwork1.entity().getNetworkId(), Op.EQ);
    IdNetworkIdStatesSearch.join(
        "networkRouter",
        joinRouterNetwork1,
        joinRouterNetwork1.entity().getRouterId(),
        IdNetworkIdStatesSearch.entity().getId(),
        JoinType.INNER);
    IdNetworkIdStatesSearch.and("states", IdNetworkIdStatesSearch.entity().getState(), Op.IN);
    IdNetworkIdStatesSearch.done();

    HostUpSearch = createSearchBuilder();
    HostUpSearch.select(null, Func.DISTINCT, HostUpSearch.entity().getId());
    HostUpSearch.and("host", HostUpSearch.entity().getHostId(), Op.EQ);
    HostUpSearch.and("states", HostUpSearch.entity().getState(), Op.NIN);
    SearchBuilder<RouterNetworkVO> joinRouterNetwork3 = _routerNetworkDao.createSearchBuilder();
    joinRouterNetwork3.and("networkId", joinRouterNetwork3.entity().getNetworkId(), Op.EQ);
    joinRouterNetwork3.and("type", joinRouterNetwork3.entity().getGuestType(), Op.EQ);
    HostUpSearch.join(
        "networkRouter",
        joinRouterNetwork3,
        joinRouterNetwork3.entity().getRouterId(),
        HostUpSearch.entity().getId(),
        JoinType.INNER);
    HostUpSearch.done();

    StateNetworkTypeSearch = createSearchBuilder();
    StateNetworkTypeSearch.select(null, Func.DISTINCT, StateNetworkTypeSearch.entity().getId());
    StateNetworkTypeSearch.and("state", StateNetworkTypeSearch.entity().getState(), Op.EQ);
    SearchBuilder<RouterNetworkVO> joinRouterNetwork4 = _routerNetworkDao.createSearchBuilder();
    joinRouterNetwork4.and("networkId", joinRouterNetwork4.entity().getNetworkId(), Op.EQ);
    joinRouterNetwork4.and("type", joinRouterNetwork4.entity().getGuestType(), Op.EQ);
    StateNetworkTypeSearch.join(
        "networkRouter",
        joinRouterNetwork4,
        joinRouterNetwork4.entity().getRouterId(),
        StateNetworkTypeSearch.entity().getId(),
        JoinType.INNER);

    SearchBuilder<HostVO> joinHost = _hostsDao.createSearchBuilder();
    joinHost.and("mgmtServerId", joinHost.entity().getManagementServerId(), Op.EQ);
    StateNetworkTypeSearch.join(
        "host",
        joinHost,
        joinHost.entity().getId(),
        StateNetworkTypeSearch.entity().getHostId(),
        JoinType.INNER);
    StateNetworkTypeSearch.done();

    SearchByStateAndManagementServerId = createSearchBuilder();
    SearchByStateAndManagementServerId.and(
        "state", SearchByStateAndManagementServerId.entity().getState(), Op.EQ);

    SearchBuilder<HostVO> joinHost2 = _hostsDao.createSearchBuilder();
    joinHost2.and("mgmtServerId", joinHost2.entity().getManagementServerId(), Op.EQ);
    SearchByStateAndManagementServerId.join(
        "host",
        joinHost2,
        joinHost2.entity().getId(),
        SearchByStateAndManagementServerId.entity().getHostId(),
        JoinType.INNER);
    SearchByStateAndManagementServerId.done();

    OutsidePodSearch = createSearchBuilder();
    SearchBuilder<RouterNetworkVO> joinRouterNetwork2 = _routerNetworkDao.createSearchBuilder();
    joinRouterNetwork2.and("networkId", joinRouterNetwork2.entity().getNetworkId(), Op.EQ);
    OutsidePodSearch.join(
        "networkRouter",
        joinRouterNetwork2,
        joinRouterNetwork2.entity().getRouterId(),
        OutsidePodSearch.entity().getId(),
        JoinType.INNER);
    OutsidePodSearch.and("podId", OutsidePodSearch.entity().getPodIdToDeployIn(), Op.NEQ);
    OutsidePodSearch.and("state", OutsidePodSearch.entity().getState(), Op.EQ);
    OutsidePodSearch.and("role", OutsidePodSearch.entity().getRole(), Op.EQ);
    OutsidePodSearch.done();

    clusterSearch = createSearchBuilder();
    clusterSearch.and("state", clusterSearch.entity().getState(), Op.EQ);
    SearchBuilder<HostVO> clusterHost = _hostsDao.createSearchBuilder();
    clusterHost.and("clusterId", clusterHost.entity().getClusterId(), Op.EQ);
    clusterSearch.join(
        "host",
        clusterHost,
        clusterSearch.entity().getHostId(),
        clusterHost.entity().getId(),
        JoinType.INNER);
    clusterSearch.done();

    RunningSearch = createSearchBuilder();
    RunningSearch.and("dc", RunningSearch.entity().getDataCenterId(), Op.EQ);
    RunningSearch.and("account", RunningSearch.entity().getAccountId(), Op.EQ);
    RunningSearch.and("domainId", RunningSearch.entity().getDomainId(), Op.EQ);
    RunningSearch.and("state", RunningSearch.entity().getState(), Op.EQ);
    RunningSearch.and("podId", RunningSearch.entity().getPodIdToDeployIn(), Op.EQ);
    RunningSearch.done();
  }