Search in sources :

Example 46 with HostPodVO

use of com.cloud.dc.HostPodVO in project cosmic by MissionCriticalCloud.

the class RecreateHostAllocator method allocateTo.

@Override
public List<Host> allocateTo(final VirtualMachineProfile vm, final DeploymentPlan plan, final Type type, final ExcludeList avoid, final int returnUpTo) {
    List<Host> hosts = super.allocateTo(vm, plan, type, avoid, returnUpTo);
    if (hosts != null && !hosts.isEmpty()) {
        return hosts;
    }
    s_logger.debug("First fit was unable to find a host");
    final VirtualMachine.Type vmType = vm.getType();
    if (vmType == VirtualMachine.Type.User) {
        s_logger.debug("vm is not a system vm so let's just return empty list");
        return new ArrayList<>();
    }
    final DataCenter dc = _dcDao.findById(plan.getDataCenterId());
    final List<PodCluster> pcs = _resourceMgr.listByDataCenter(dc.getId());
    // basic network type for zone maps to direct untagged case
    if (dc.getNetworkType().equals(NetworkType.Basic)) {
        s_logger.debug("Direct Networking mode so we can only allow the host to be allocated in the same pod due to public ip address cannot change");
        final List<VolumeVO> vols = _volsDao.findByInstance(vm.getId());
        final VolumeVO vol = vols.get(0);
        final long podId = vol.getPodId();
        s_logger.debug("Pod id determined from volume " + vol.getId() + " is " + podId);
        final Iterator<PodCluster> it = pcs.iterator();
        while (it.hasNext()) {
            final PodCluster pc = it.next();
            if (pc.getPod().getId() != podId) {
                it.remove();
            }
        }
    }
    final Set<Pair<Long, Long>> avoidPcs = new HashSet<>();
    final Set<Long> hostIdsToAvoid = avoid.getHostsToAvoid();
    if (hostIdsToAvoid != null) {
        for (final Long hostId : hostIdsToAvoid) {
            final Host h = _hostDao.findById(hostId);
            if (h != null) {
                avoidPcs.add(new Pair<>(h.getPodId(), h.getClusterId()));
            }
        }
    }
    for (final Pair<Long, Long> pcId : avoidPcs) {
        s_logger.debug("Removing " + pcId + " from the list of available pods");
        pcs.remove(new PodCluster(new HostPodVO(pcId.first()), pcId.second() != null ? new ClusterVO(pcId.second()) : null));
    }
    for (final PodCluster p : pcs) {
        if (p.getPod().getAllocationState() != AllocationState.Enabled) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Pod name: " + p.getPod().getName() + ", podId: " + p.getPod().getId() + " is in " + p.getPod().getAllocationState().name() + " state, skipping this and trying other pods");
            }
            continue;
        }
        final Long clusterId = p.getCluster() == null ? null : p.getCluster().getId();
        if (p.getCluster() != null && p.getCluster().getAllocationState() != AllocationState.Enabled) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Cluster name: " + p.getCluster().getName() + ", clusterId: " + clusterId + " is in " + p.getCluster().getAllocationState().name() + " state, skipping this and trying other pod-clusters");
            }
            continue;
        }
        final DataCenterDeployment newPlan = new DataCenterDeployment(plan.getDataCenterId(), p.getPod().getId(), clusterId, null, null, null);
        hosts = super.allocateTo(vm, newPlan, type, avoid, returnUpTo);
        if (hosts != null && !hosts.isEmpty()) {
            return hosts;
        }
    }
    s_logger.debug("Unable to find any available pods at all!");
    return new ArrayList<>();
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) ArrayList(java.util.ArrayList) Host(com.cloud.host.Host) HostPodVO(com.cloud.dc.HostPodVO) DataCenter(com.cloud.dc.DataCenter) VolumeVO(com.cloud.storage.VolumeVO) PodCluster(com.cloud.dc.PodCluster) VirtualMachine(com.cloud.vm.VirtualMachine) Pair(com.cloud.utils.Pair) HashSet(java.util.HashSet)

Example 47 with HostPodVO

use of com.cloud.dc.HostPodVO in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method editPod.

@Override
@DB
public Pod editPod(final long id, String name, String startIp, String endIp, String gateway, String netmask, String allocationStateStr) {
    // verify parameters
    final HostPodVO pod = _podDao.findById(id);
    if (pod == null) {
        throw new InvalidParameterValueException("Unable to find pod by id " + id);
    }
    final String[] existingPodIpRange = pod.getDescription().split("-");
    String[] leftRangeToAdd = null;
    String[] rightRangeToAdd = null;
    boolean allowToDownsize = false;
    // pod has allocated private IP addresses
    if (podHasAllocatedPrivateIPs(id)) {
        if (netmask != null) {
            final long newCidr = NetUtils.getCidrSize(netmask);
            final long oldCidr = pod.getCidrSize();
            if (newCidr > oldCidr) {
                throw new CloudRuntimeException("The specified pod has allocated private IP addresses, so its IP address range can be extended only");
            }
        }
        if (startIp != null && !startIp.equals(existingPodIpRange[0])) {
            if (NetUtils.ipRangesOverlap(startIp, null, existingPodIpRange[0], existingPodIpRange[1])) {
                throw new CloudRuntimeException("The specified pod has allocated private IP addresses, so its IP address range can be extended only");
            } else {
                leftRangeToAdd = new String[2];
                final long endIpForUpdate = NetUtils.ip2Long(existingPodIpRange[0]) - 1;
                leftRangeToAdd[0] = startIp;
                leftRangeToAdd[1] = NetUtils.long2Ip(endIpForUpdate);
            }
        }
        if (endIp != null && !endIp.equals(existingPodIpRange[1])) {
            if (NetUtils.ipRangesOverlap(endIp, endIp, existingPodIpRange[0], existingPodIpRange[1])) {
                throw new CloudRuntimeException("The specified pod has allocated private IP addresses, so its IP address range can be extended only");
            } else {
                rightRangeToAdd = new String[2];
                final long startIpForUpdate = NetUtils.ip2Long(existingPodIpRange[1]) + 1;
                rightRangeToAdd[0] = NetUtils.long2Ip(startIpForUpdate);
                rightRangeToAdd[1] = endIp;
            }
        }
    } else {
        allowToDownsize = true;
    }
    if (gateway == null) {
        gateway = pod.getGateway();
    }
    if (netmask == null) {
        netmask = NetUtils.getCidrNetmask(pod.getCidrSize());
    }
    final String oldPodName = pod.getName();
    if (name == null) {
        name = oldPodName;
    }
    if (gateway == null) {
        gateway = pod.getGateway();
    }
    if (startIp == null) {
        startIp = existingPodIpRange[0];
    }
    if (endIp == null) {
        endIp = existingPodIpRange[1];
    }
    if (allocationStateStr == null) {
        allocationStateStr = pod.getAllocationState().toString();
    }
    // Verify pod's attributes
    final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask);
    final boolean checkForDuplicates = !oldPodName.equals(name);
    checkPodAttributes(id, name, pod.getDataCenterId(), gateway, cidr, startIp, endIp, allocationStateStr, checkForDuplicates, false);
    try {
        final String[] existingPodIpRangeFinal = existingPodIpRange;
        final String[] leftRangeToAddFinal = leftRangeToAdd;
        final String[] rightRangeToAddFinal = rightRangeToAdd;
        final boolean allowToDownsizeFinal = allowToDownsize;
        final String allocationStateStrFinal = allocationStateStr;
        final String startIpFinal = startIp;
        final String endIpFinal = endIp;
        final String nameFinal = name;
        final String gatewayFinal = gateway;
        Transaction.execute(new TransactionCallbackNoReturn() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                final long zoneId = pod.getDataCenterId();
                String startIp = startIpFinal;
                String endIp = endIpFinal;
                if (!allowToDownsizeFinal) {
                    if (leftRangeToAddFinal != null) {
                        _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), leftRangeToAddFinal[0], leftRangeToAddFinal[1]);
                    }
                    if (rightRangeToAddFinal != null) {
                        _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), rightRangeToAddFinal[0], rightRangeToAddFinal[1]);
                    }
                } else {
                    // delete the old range
                    _zoneDao.deletePrivateIpAddressByPod(pod.getId());
                    // add the new one
                    if (startIp == null) {
                        startIp = existingPodIpRangeFinal[0];
                    }
                    if (endIp == null) {
                        endIp = existingPodIpRangeFinal[1];
                    }
                    _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIp);
                }
                pod.setName(nameFinal);
                pod.setDataCenterId(zoneId);
                pod.setGateway(gatewayFinal);
                pod.setCidrAddress(getCidrAddress(cidr));
                pod.setCidrSize(getCidrSize(cidr));
                final String ipRange = startIp + "-" + endIp;
                pod.setDescription(ipRange);
                final AllocationState allocationState;
                if (allocationStateStrFinal != null && !allocationStateStrFinal.isEmpty()) {
                    allocationState = AllocationState.valueOf(allocationStateStrFinal);
                    pod.setAllocationState(allocationState);
                }
                _podDao.update(id, pod);
            }
        });
    } catch (final Exception e) {
        s_logger.error("Unable to edit pod due to " + e.getMessage(), e);
        throw new CloudRuntimeException("Failed to edit pod. Please contact Cloud Support.");
    }
    return pod;
}
Also used : InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AllocationState(com.cloud.model.enumeration.AllocationState) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) HostPodVO(com.cloud.dc.HostPodVO) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SQLException(java.sql.SQLException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) DB(com.cloud.utils.db.DB)

Example 48 with HostPodVO

use of com.cloud.dc.HostPodVO in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method checkIfPodIsDeletable.

protected void checkIfPodIsDeletable(final long podId) {
    final HostPodVO pod = _podDao.findById(podId);
    final String errorMsg = "The pod cannot be deleted because ";
    // Check if there are allocated private IP addresses in the pod
    if (_privateIpAddressDao.countIPs(podId, pod.getDataCenterId(), true) != 0) {
        throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this pod.");
    }
    // Check if there are any non-removed volumes in the pod.
    if (!_volumeDao.findByPod(podId).isEmpty()) {
        throw new CloudRuntimeException(errorMsg + "there are storage volumes in this pod.");
    }
    // Check if there are any non-removed hosts in the pod.
    if (!_hostDao.findByPodId(podId).isEmpty()) {
        throw new CloudRuntimeException(errorMsg + "there are servers in this pod.");
    }
    // Check if there are any non-removed vms in the pod.
    if (!_vmInstanceDao.listByPodId(podId).isEmpty()) {
        throw new CloudRuntimeException(errorMsg + "there are virtual machines in this pod.");
    }
    // Check if there are any non-removed clusters in the pod.
    if (!_clusterDao.listByPodId(podId).isEmpty()) {
        throw new CloudRuntimeException(errorMsg + "there are clusters in this pod.");
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) HostPodVO(com.cloud.dc.HostPodVO)

Example 49 with HostPodVO

use of com.cloud.dc.HostPodVO in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method podHasAllocatedPrivateIPs.

private boolean podHasAllocatedPrivateIPs(final long podId) {
    final HostPodVO pod = _podDao.findById(podId);
    final int count = _privateIpAddressDao.countIPs(podId, pod.getDataCenterId(), true);
    return count > 0;
}
Also used : HostPodVO(com.cloud.dc.HostPodVO)

Example 50 with HostPodVO

use of com.cloud.dc.HostPodVO in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method createPod.

@Override
@DB
public HostPodVO createPod(final long userId, final String podName, final long zoneId, final String gateway, final String cidr, final String startIp, String endIp, final String allocationStateStr, final boolean skipGatewayOverlapCheck) {
    // Check if the zone is valid
    if (!validZone(zoneId)) {
        throw new InvalidParameterValueException("Please specify a valid zone.");
    }
    // Check if zone is disabled
    final DataCenterVO zone = _zoneDao.findById(zoneId);
    final Account account = CallContext.current().getCallingAccount();
    if (AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(account.getId())) {
        throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zoneId);
    }
    final String cidrAddress = getCidrAddress(cidr);
    final int cidrSize = getCidrSize(cidr);
    // end ip of the pod's cidr
    if (startIp != null) {
        if (endIp == null) {
            endIp = NetUtils.getIpRangeEndIpFromCidr(cidrAddress, cidrSize);
        }
    }
    // Validate new pod settings
    checkPodAttributes(-1, podName, zoneId, gateway, cidr, startIp, endIp, allocationStateStr, true, skipGatewayOverlapCheck);
    // Create the new pod in the database
    final String ipRange;
    if (startIp != null) {
        ipRange = startIp + "-" + endIp;
    } else {
        throw new InvalidParameterValueException("Start ip is required parameter");
    }
    final HostPodVO podFinal = new HostPodVO(podName, zoneId, gateway, cidrAddress, cidrSize, ipRange);
    final AllocationState allocationState;
    if (allocationStateStr != null && !allocationStateStr.isEmpty()) {
        allocationState = AllocationState.valueOf(allocationStateStr);
        podFinal.setAllocationState(allocationState);
    }
    final String endIpFinal = endIp;
    return Transaction.execute(new TransactionCallback<HostPodVO>() {

        @Override
        public HostPodVO doInTransaction(final TransactionStatus status) {
            final HostPodVO pod = _podDao.persist(podFinal);
            if (startIp != null) {
                _zoneDao.addPrivateIpAddress(zoneId, pod.getId(), startIp, endIpFinal);
            }
            final String[] linkLocalIpRanges = getLinkLocalIPRange();
            if (linkLocalIpRanges != null) {
                _zoneDao.addLinkLocalIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]);
            }
            return pod;
        }
    });
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) AllocationState(com.cloud.model.enumeration.AllocationState) TransactionStatus(com.cloud.utils.db.TransactionStatus) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) HostPodVO(com.cloud.dc.HostPodVO) DB(com.cloud.utils.db.DB)

Aggregations

HostPodVO (com.cloud.dc.HostPodVO)126 ArrayList (java.util.ArrayList)52 HostVO (com.cloud.host.HostVO)47 ClusterVO (com.cloud.dc.ClusterVO)46 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)46 DataCenterVO (com.cloud.dc.DataCenterVO)39 Account (com.cloud.user.Account)25 DB (com.cloud.utils.db.DB)25 Test (org.junit.Test)23 ConfigurationException (javax.naming.ConfigurationException)22 TransactionStatus (com.cloud.utils.db.TransactionStatus)21 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)20 VMInstanceVO (com.cloud.vm.VMInstanceVO)18 Random (java.util.Random)18 VolumeVO (com.cloud.storage.VolumeVO)17 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)16 HashMap (java.util.HashMap)16 List (java.util.List)16 Zone (com.cloud.db.model.Zone)15 DataCenter (com.cloud.dc.DataCenter)14