Search in sources :

Example 1 with ExcludeList

use of com.cloud.deploy.DeploymentPlanner.ExcludeList in project cloudstack by apache.

the class UserVmManagerImpl method startVirtualMachine.

@Override
public Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> startVirtualMachine(long vmId, Long hostId, Map<VirtualMachineProfile.Param, Object> additionalParams, String deploymentPlannerToUse) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    // Input validation
    Account callerAccount = CallContext.current().getCallingAccount();
    UserVO callerUser = _userDao.findById(CallContext.current().getCallingUserId());
    // if account is removed, return error
    if (callerAccount != null && callerAccount.getRemoved() != null) {
        throw new InvalidParameterValueException("The account " + callerAccount.getId() + " is removed");
    }
    UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
    }
    _accountMgr.checkAccess(callerAccount, null, true, vm);
    Account owner = _accountDao.findById(vm.getAccountId());
    if (owner == null) {
        throw new InvalidParameterValueException("The owner of " + vm + " does not exist: " + vm.getAccountId());
    }
    if (owner.getState() == Account.State.disabled) {
        throw new PermissionDeniedException("The owner of " + vm + " is disabled: " + vm.getAccountId());
    }
    Host destinationHost = null;
    if (hostId != null) {
        Account account = CallContext.current().getCallingAccount();
        if (!_accountService.isRootAdmin(account.getId())) {
            throw new PermissionDeniedException("Parameter hostid can only be specified by a Root Admin, permission denied");
        }
        destinationHost = _hostDao.findById(hostId);
        if (destinationHost == null) {
            throw new InvalidParameterValueException("Unable to find the host to deploy the VM, host id=" + hostId);
        }
    }
    // check if vm is security group enabled
    if (_securityGroupMgr.isVmSecurityGroupEnabled(vmId) && _securityGroupMgr.getSecurityGroupsForVm(vmId).isEmpty() && !_securityGroupMgr.isVmMappedToDefaultSecurityGroup(vmId) && _networkModel.canAddDefaultSecurityGroup()) {
        // if vm is not mapped to security group, create a mapping
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Vm " + vm + " is security group enabled, but not mapped to default security group; creating the mapping automatically");
        }
        SecurityGroup defaultSecurityGroup = _securityGroupMgr.getDefaultSecurityGroup(vm.getAccountId());
        if (defaultSecurityGroup != null) {
            List<Long> groupList = new ArrayList<Long>();
            groupList.add(defaultSecurityGroup.getId());
            _securityGroupMgr.addInstanceToGroups(vmId, groupList);
        }
    }
    DataCenterDeployment plan = null;
    if (destinationHost != null) {
        s_logger.debug("Destination Host to deploy the VM is specified, specifying a deployment plan to deploy the VM");
        plan = new DataCenterDeployment(vm.getDataCenterId(), destinationHost.getPodId(), destinationHost.getClusterId(), destinationHost.getId(), null, null);
    }
    // Set parameters
    Map<VirtualMachineProfile.Param, Object> params = null;
    VMTemplateVO template = null;
    if (vm.isUpdateParameters()) {
        _vmDao.loadDetails(vm);
        // Check that the password was passed in and is valid
        template = _templateDao.findByIdIncludingRemoved(vm.getTemplateId());
        String password = "saved_password";
        if (template.getEnablePassword()) {
            if (vm.getDetail("password") != null) {
                password = DBEncryptionUtil.decrypt(vm.getDetail("password"));
            } else {
                password = _mgr.generateRandomPassword();
            }
        }
        if (!validPassword(password)) {
            throw new InvalidParameterValueException("A valid password for this virtual machine was not provided.");
        }
        // Check if an SSH key pair was selected for the instance and if so
        // use it to encrypt & save the vm password
        encryptAndStorePassword(vm, password);
        params = new HashMap<VirtualMachineProfile.Param, Object>();
        if (additionalParams != null) {
            params.putAll(additionalParams);
        }
        params.put(VirtualMachineProfile.Param.VmPassword, password);
    }
    VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
    DeploymentPlanner planner = null;
    if (deploymentPlannerToUse != null) {
        // if set to null, the deployment planner would be later figured out either from global config var, or from
        // the service offering
        planner = _planningMgr.getDeploymentPlannerByName(deploymentPlannerToUse);
        if (planner == null) {
            throw new InvalidParameterValueException("Can't find a planner by name " + deploymentPlannerToUse);
        }
    }
    String reservationId = vmEntity.reserve(planner, plan, new ExcludeList(), Long.toString(callerUser.getId()));
    vmEntity.deploy(reservationId, Long.toString(callerUser.getId()), params);
    Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> vmParamPair = new Pair(vm, params);
    if (vm != null && vm.isUpdateParameters()) {
        // display purposes
        if (template.getEnablePassword()) {
            vm.setPassword((String) vmParamPair.second().get(VirtualMachineProfile.Param.VmPassword));
            vm.setUpdateParameters(false);
            if (vm.getDetail("password") != null) {
                _vmDetailsDao.remove(_vmDetailsDao.findDetail(vm.getId(), "password").getId());
            }
            _vmDao.update(vm.getId(), vm);
        }
    }
    return vmParamPair;
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) Account(com.cloud.user.Account) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) VirtualMachineEntity(org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntity) ArrayList(java.util.ArrayList) VMTemplateVO(com.cloud.storage.VMTemplateVO) Host(com.cloud.host.Host) SecurityGroup(com.cloud.network.security.SecurityGroup) UserVO(com.cloud.user.UserVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DeploymentPlanner(com.cloud.deploy.DeploymentPlanner) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair)

Example 2 with ExcludeList

use of com.cloud.deploy.DeploymentPlanner.ExcludeList in project cloudstack by apache.

the class VirtualMachineManagerImpl method getPoolListForVolumesForMigration.

private Map<Volume, StoragePool> getPoolListForVolumesForMigration(final VirtualMachineProfile profile, final Host host, final Map<Long, Long> volumeToPool) {
    final List<VolumeVO> allVolumes = _volsDao.findUsableVolumesForInstance(profile.getId());
    final Map<Volume, StoragePool> volumeToPoolObjectMap = new HashMap<>();
    for (final VolumeVO volume : allVolumes) {
        final Long poolId = volumeToPool.get(Long.valueOf(volume.getId()));
        final StoragePoolVO destPool = _storagePoolDao.findById(poolId);
        final StoragePoolVO currentPool = _storagePoolDao.findById(volume.getPoolId());
        final DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
        if (destPool != null) {
            // created is compliant with the pool type.
            if (_poolHostDao.findByPoolHost(destPool.getId(), host.getId()) == null || destPool.isLocal() != diskOffering.getUseLocalStorage()) {
                // Cannot find a pool for the volume. Throw an exception.
                throw new CloudRuntimeException("Cannot migrate volume " + volume + " to storage pool " + destPool + " while migrating vm to host " + host + ". Either the pool is not accessible from the host or because of the offering with which the volume is created it cannot be placed on " + "the given pool.");
            } else if (destPool.getId() == currentPool.getId()) {
            // If the pool to migrate to is the same as current pool, the volume doesn't need to be migrated.
            } else {
                volumeToPoolObjectMap.put(volume, destPool);
            }
        } else {
            if (currentPool.isManaged()) {
                volumeToPoolObjectMap.put(volume, currentPool);
            } else {
                // Find a suitable pool for the volume. Call the storage pool allocator to find the list of pools.
                final DiskProfile diskProfile = new DiskProfile(volume, diskOffering, profile.getHypervisorType());
                final DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), null, null);
                final List<StoragePool> poolList = new ArrayList<>();
                final ExcludeList avoid = new ExcludeList();
                for (final StoragePoolAllocator allocator : _storagePoolAllocators) {
                    final List<StoragePool> poolListFromAllocator = allocator.allocateToPool(diskProfile, profile, plan, avoid, StoragePoolAllocator.RETURN_UPTO_ALL);
                    if (poolListFromAllocator != null && !poolListFromAllocator.isEmpty()) {
                        poolList.addAll(poolListFromAllocator);
                    }
                }
                boolean currentPoolAvailable = false;
                if (poolList != null && !poolList.isEmpty()) {
                    // Volume needs to be migrated. Pick the first pool from the list. Add a mapping to migrate the
                    // volume to a pool only if it is required; that is the current pool on which the volume resides
                    // is not available on the destination host.
                    final Iterator<StoragePool> iter = poolList.iterator();
                    while (iter.hasNext()) {
                        if (currentPool.getId() == iter.next().getId()) {
                            currentPoolAvailable = true;
                            break;
                        }
                    }
                    if (!currentPoolAvailable) {
                        volumeToPoolObjectMap.put(volume, _storagePoolDao.findByUuid(poolList.get(0).getUuid()));
                    }
                }
                if (!currentPoolAvailable && !volumeToPoolObjectMap.containsKey(volume)) {
                    // Cannot find a pool for the volume. Throw an exception.
                    throw new CloudRuntimeException("Cannot find a storage pool which is available for volume " + volume + " while migrating virtual machine " + profile.getVirtualMachine() + " to host " + host);
                }
            }
        }
    }
    return volumeToPoolObjectMap;
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) StoragePool(com.cloud.storage.StoragePool) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VolumeVO(com.cloud.storage.VolumeVO) Volume(com.cloud.storage.Volume) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) StoragePoolAllocator(org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator)

Example 3 with ExcludeList

use of com.cloud.deploy.DeploymentPlanner.ExcludeList in project cloudstack by apache.

the class GarbageCollectingStoragePoolAllocator method select.

@Override
public List<StoragePool> select(DiskProfile dskCh, VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid, int returnUpTo) {
    s_logger.debug("GarbageCollectingStoragePoolAllocator looking for storage pool");
    if (!_storagePoolCleanupEnabled) {
        s_logger.debug("Storage pool cleanup is not enabled, so GarbageCollectingStoragePoolAllocator is being skipped.");
        return null;
    }
    // Clean up all storage pools
    storageMgr.cleanupStorage(false);
    // Determine what allocator to use
    StoragePoolAllocator allocator;
    if (dskCh.useLocalStorage()) {
        allocator = _localStoragePoolAllocator;
    } else {
        allocator = _firstFitStoragePoolAllocator;
    }
    // Try to find a storage pool after cleanup
    ExcludeList myAvoids = new ExcludeList(avoid.getDataCentersToAvoid(), avoid.getPodsToAvoid(), avoid.getClustersToAvoid(), avoid.getHostsToAvoid(), avoid.getPoolsToAvoid());
    return allocator.allocateToPool(dskCh, vmProfile, plan, myAvoids, returnUpTo);
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) StoragePoolAllocator(org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator)

Example 4 with ExcludeList

use of com.cloud.deploy.DeploymentPlanner.ExcludeList in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method startVirtualMachine.

@Override
public Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> startVirtualMachine(final long vmId, final Long hostId, final Map<VirtualMachineProfile.Param, Object> additionalParams, final String deploymentPlannerToUse) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    // Input validation
    final Account callerAccount = CallContext.current().getCallingAccount();
    final UserVO callerUser = _userDao.findById(CallContext.current().getCallingUserId());
    // if account is removed, return error
    if (callerAccount != null && callerAccount.getRemoved() != null) {
        throw new InvalidParameterValueException("The account " + callerAccount.getId() + " is removed");
    }
    final UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
    }
    _accountMgr.checkAccess(callerAccount, null, true, vm);
    final Account owner = _accountDao.findById(vm.getAccountId());
    if (owner == null) {
        throw new InvalidParameterValueException("The owner of " + vm + " does not exist: " + vm.getAccountId());
    }
    if (owner.getState() == Account.State.disabled) {
        throw new PermissionDeniedException("The owner of " + vm + " is disabled: " + vm.getAccountId());
    }
    Host destinationHost = null;
    if (hostId != null) {
        final Account account = CallContext.current().getCallingAccount();
        if (!_accountService.isRootAdmin(account.getId())) {
            throw new PermissionDeniedException("Parameter hostid can only be specified by a Root Admin, permission denied");
        }
        destinationHost = _hostDao.findById(hostId);
        if (destinationHost == null) {
            throw new InvalidParameterValueException("Unable to find the host to deploy the VM, host id=" + hostId);
        }
    }
    DataCenterDeployment plan = null;
    boolean deployOnGivenHost = false;
    if (destinationHost != null) {
        s_logger.debug("Destination Host to deploy the VM is specified, specifying a deployment plan to deploy the VM");
        plan = new DataCenterDeployment(vm.getDataCenterId(), destinationHost.getPodId(), destinationHost.getClusterId(), destinationHost.getId(), null, null);
        deployOnGivenHost = true;
    }
    // Set parameters
    Map<VirtualMachineProfile.Param, Object> params = null;
    VMTemplateVO template = null;
    if (vm.isUpdateParameters()) {
        _vmDao.loadDetails(vm);
        // Check that the password was passed in and is valid
        template = _templateDao.findByIdIncludingRemoved(vm.getTemplateId());
        String password = "saved_password";
        if (template.getEnablePassword()) {
            if (vm.getDetail("password") != null) {
                password = DBEncryptionUtil.decrypt(vm.getDetail("password"));
            } else {
                password = _mgr.generateRandomPassword();
            }
        }
        if (!validPassword(password)) {
            throw new InvalidParameterValueException("A valid password for this virtual machine was not provided.");
        }
        // Check if an SSH key pair was selected for the instance and if so
        // use it to encrypt & save the vm password
        encryptAndStorePassword(vm, password);
        params = new HashMap<>();
        if (additionalParams != null) {
            params.putAll(additionalParams);
        }
        params.put(VirtualMachineProfile.Param.VmPassword, password);
    }
    final VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
    DeploymentPlanner planner = null;
    if (deploymentPlannerToUse != null) {
        // if set to null, the deployment planner would be later figured out either from global config var, or from
        // the service offering
        planner = _planningMgr.getDeploymentPlannerByName(deploymentPlannerToUse);
        if (planner == null) {
            throw new InvalidParameterValueException("Can't find a planner by name " + deploymentPlannerToUse);
        }
    }
    final String reservationId = vmEntity.reserve(planner, plan, new ExcludeList(), Long.toString(callerUser.getId()));
    vmEntity.deploy(reservationId, Long.toString(callerUser.getId()), params, deployOnGivenHost);
    final Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> vmParamPair = new Pair(vm, params);
    if (vm != null && vm.isUpdateParameters()) {
        // display purposes
        if (template.getEnablePassword()) {
            vm.setPassword((String) vmParamPair.second().get(VirtualMachineProfile.Param.VmPassword));
            vm.setUpdateParameters(false);
            if (vm.getDetail("password") != null) {
                _vmDetailsDao.remove(_vmDetailsDao.findDetail(vm.getId(), "password").getId());
            }
            _vmDao.update(vm.getId(), vm);
        }
    }
    return vmParamPair;
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) Account(com.cloud.user.Account) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) VirtualMachineEntity(com.cloud.engine.cloud.entity.api.VirtualMachineEntity) VMTemplateVO(com.cloud.storage.VMTemplateVO) Host(com.cloud.host.Host) UserVO(com.cloud.user.UserVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) DeploymentPlanner(com.cloud.deploy.DeploymentPlanner) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair)

Example 5 with ExcludeList

use of com.cloud.deploy.DeploymentPlanner.ExcludeList in project cosmic by MissionCriticalCloud.

the class FirstFitPlannerTest method checkClusterReorderingBasedOnImplicitHostTags.

@Test
public void checkClusterReorderingBasedOnImplicitHostTags() throws InsufficientServerCapacityException {
    final VirtualMachineProfileImpl vmProfile = mock(VirtualMachineProfileImpl.class);
    final DataCenterDeployment plan = mock(DataCenterDeployment.class);
    final ExcludeList avoids = mock(ExcludeList.class);
    initializeForTest(vmProfile, plan, avoids);
    final List<Long> clusterList = planner.orderClusters(vmProfile, plan, avoids);
    final List<Long> reorderedClusterList = new ArrayList<>();
    reorderedClusterList.add(4L);
    reorderedClusterList.add(3L);
    reorderedClusterList.add(1L);
    reorderedClusterList.add(5L);
    reorderedClusterList.add(6L);
    reorderedClusterList.add(2L);
    assertTrue("Reordered cluster list is not ownering the implict host tags", (clusterList.equals(reorderedClusterList)));
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)78 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)45 ArrayList (java.util.ArrayList)32 StoragePool (com.cloud.storage.StoragePool)30 Test (org.junit.Test)30 HashMap (java.util.HashMap)23 Volume (com.cloud.storage.Volume)21 VirtualMachineProfileImpl (com.cloud.vm.VirtualMachineProfileImpl)18 List (java.util.List)18 Host (com.cloud.host.Host)16 HostVO (com.cloud.host.HostVO)16 Pair (com.cloud.utils.Pair)16 StoragePoolAllocator (org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator)16 DiskProfile (com.cloud.vm.DiskProfile)15 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)15 DiskOfferingVO (com.cloud.storage.DiskOfferingVO)14 VolumeVO (com.cloud.storage.VolumeVO)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 Account (com.cloud.user.Account)12 Map (java.util.Map)11