Search in sources :

Example 6 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project CloudStack-archive by CloudStack-extras.

the class DestroySystemVmCmd method execute.

@Override
public void execute() {
    UserContext.current().setEventDetails("Vm Id: " + getId());
    VirtualMachine instance = _mgr.destroySystemVM(this);
    if (instance != null) {
        SystemVmResponse response = _responseGenerator.createSystemVmResponse(instance);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Fail to destroy system vm");
    }
}
Also used : SystemVmResponse(com.cloud.api.response.SystemVmResponse) ServerApiException(com.cloud.api.ServerApiException) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 7 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cloudstack by apache.

the class ExplicitDedicationProcessor method process.

/**
     * This method will process the affinity group of type 'Explicit Dedication' for a deployment of a VM that demands dedicated resources.
     * For ExplicitDedicationProcessor we need to add dedicated resources into the IncludeList based on the level we have dedicated resources available.
     * For eg. if admin dedicates a pod to a domain, then all the user in that domain can use the resources of that pod.
     * We need to take care of the situation when dedicated resources further have resources dedicated to sub-domain/account.
     * This IncludeList is then used to update the avoid list for a given data center.
     */
@Override
public void process(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid) throws AffinityConflictException {
    VirtualMachine vm = vmProfile.getVirtualMachine();
    List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType());
    DataCenter dc = _dcDao.findById(vm.getDataCenterId());
    List<DedicatedResourceVO> resourceList = new ArrayList<DedicatedResourceVO>();
    if (vmGroupMappings != null && !vmGroupMappings.isEmpty()) {
        for (AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) {
            if (vmGroupMapping != null) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Processing affinity group " + vmGroupMapping.getAffinityGroupId() + "of type 'ExplicitDedication' for VM Id: " + vm.getId());
                }
                long affinityGroupId = vmGroupMapping.getAffinityGroupId();
                List<DedicatedResourceVO> dr = _dedicatedDao.listByAffinityGroupId(affinityGroupId);
                resourceList.addAll(dr);
            }
        }
        boolean canUse = false;
        if (plan.getHostId() != null) {
            HostVO host = _hostDao.findById(plan.getHostId());
            ClusterVO clusterofHost = _clusterDao.findById(host.getClusterId());
            HostPodVO podOfHost = _podDao.findById(host.getPodId());
            DataCenterVO zoneOfHost = _dcDao.findById(host.getDataCenterId());
            if (resourceList != null && resourceList.size() != 0) {
                for (DedicatedResourceVO resource : resourceList) {
                    if ((resource.getHostId() != null && resource.getHostId().longValue() == plan.getHostId().longValue()) || (resource.getClusterId() != null && resource.getClusterId().longValue() == clusterofHost.getId()) || (resource.getPodId() != null && resource.getPodId().longValue() == podOfHost.getId()) || (resource.getDataCenterId() != null && resource.getDataCenterId().longValue() == zoneOfHost.getId())) {
                        canUse = true;
                    }
                }
            }
            if (!canUse) {
                throw new CloudRuntimeException("Cannot use this host " + host.getName() + " for explicit dedication");
            }
        } else if (plan.getClusterId() != null) {
            ClusterVO cluster = _clusterDao.findById(plan.getClusterId());
            HostPodVO podOfCluster = _podDao.findById(cluster.getPodId());
            DataCenterVO zoneOfCluster = _dcDao.findById(cluster.getDataCenterId());
            List<HostVO> hostToUse = new ArrayList<HostVO>();
            // check whether this cluster or its pod is dedicated
            if (resourceList != null && resourceList.size() != 0) {
                for (DedicatedResourceVO resource : resourceList) {
                    if ((resource.getClusterId() != null && resource.getClusterId() == cluster.getId()) || (resource.getPodId() != null && resource.getPodId() == podOfCluster.getId()) || (resource.getDataCenterId() != null && resource.getDataCenterId() == zoneOfCluster.getId())) {
                        canUse = true;
                    }
                    // cluster
                    if (!canUse) {
                        if (resource.getHostId() != null) {
                            HostVO dHost = _hostDao.findById(resource.getHostId());
                            if (dHost.getClusterId() == cluster.getId()) {
                                hostToUse.add(dHost);
                            }
                        }
                    }
                }
            }
            if (hostToUse.isEmpty() && !canUse) {
                throw new CloudRuntimeException("Cannot use this cluster " + cluster.getName() + " for explicit dedication");
            }
            if (hostToUse != null && hostToUse.size() != 0) {
                // add other non-dedicated hosts to avoid list
                List<HostVO> hostList = _hostDao.findByClusterId(cluster.getId());
                for (HostVO host : hostList) {
                    if (!hostToUse.contains(host)) {
                        avoid.addHost(host.getId());
                    }
                }
            }
        } else if (plan.getPodId() != null) {
            HostPodVO pod = _podDao.findById(plan.getPodId());
            DataCenterVO zoneOfPod = _dcDao.findById(pod.getDataCenterId());
            List<ClusterVO> clustersToUse = new ArrayList<ClusterVO>();
            List<HostVO> hostsToUse = new ArrayList<HostVO>();
            // check whether this cluster or its pod is dedicated
            if (resourceList != null && resourceList.size() != 0) {
                for (DedicatedResourceVO resource : resourceList) {
                    if ((resource.getPodId() != null && resource.getPodId() == pod.getId()) || (resource.getDataCenterId() != null && resource.getDataCenterId() == zoneOfPod.getId())) {
                        canUse = true;
                    }
                    // this pod
                    if (!canUse) {
                        if (resource.getClusterId() != null) {
                            ClusterVO dCluster = _clusterDao.findById(resource.getClusterId());
                            if (dCluster.getPodId() == pod.getId()) {
                                clustersToUse.add(dCluster);
                            }
                        }
                        if (resource.getHostId() != null) {
                            HostVO dHost = _hostDao.findById(resource.getHostId());
                            if (dHost.getPodId() == pod.getId()) {
                                hostsToUse.add(dHost);
                            }
                        }
                    }
                }
            }
            if (hostsToUse.isEmpty() && clustersToUse.isEmpty() && !canUse) {
                throw new CloudRuntimeException("Cannot use this pod " + pod.getName() + " for explicit dedication");
            }
            if (clustersToUse != null && clustersToUse.size() != 0) {
                // add other non-dedicated clusters to avoid list
                List<ClusterVO> clusterList = _clusterDao.listByPodId(pod.getId());
                for (ClusterVO cluster : clusterList) {
                    if (!clustersToUse.contains(cluster)) {
                        avoid.addCluster(cluster.getId());
                    }
                }
            }
            if (hostsToUse != null && hostsToUse.size() != 0) {
                // add other non-dedicated hosts to avoid list
                List<HostVO> hostList = _hostDao.findByPodId(pod.getId());
                for (HostVO host : hostList) {
                    if (!hostsToUse.contains(host)) {
                        avoid.addHost(host.getId());
                    }
                }
            }
        } else {
            // check all resources under this zone
            if (resourceList != null && resourceList.size() != 0) {
                avoid = updateAvoidList(resourceList, avoid, dc);
            } else {
                avoid.addDataCenter(dc.getId());
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("No dedicated resources available for this domain or account under this group");
                }
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("ExplicitDedicationProcessor returns Avoid List as: Deploy avoids pods: " + avoid.getPodsToAvoid() + ", clusters: " + avoid.getClustersToAvoid() + ", hosts: " + avoid.getHostsToAvoid());
            }
        }
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) ClusterVO(com.cloud.dc.ClusterVO) ArrayList(java.util.ArrayList) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) DataCenter(com.cloud.dc.DataCenter) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 8 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cloudstack by apache.

the class HostAntiAffinityProcessor method check.

@Override
public boolean check(VirtualMachineProfile vmProfile, DeployDestination plannedDestination) throws AffinityConflictException {
    if (plannedDestination.getHost() == null) {
        return true;
    }
    long plannedHostId = plannedDestination.getHost().getId();
    VirtualMachine vm = vmProfile.getVirtualMachine();
    List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType());
    for (AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) {
        // if more than 1 VM's are present in the group then check for
        // conflict due to parallel deployment
        List<Long> groupVMIds = _affinityGroupVMMapDao.listVmIdsByAffinityGroup(vmGroupMapping.getAffinityGroupId());
        groupVMIds.remove(vm.getId());
        for (Long groupVMId : groupVMIds) {
            VMReservationVO vmReservation = _reservationDao.findByVmId(groupVMId);
            if (vmReservation != null && vmReservation.getHostId() != null && vmReservation.getHostId().equals(plannedHostId)) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Planned destination for VM " + vm.getId() + " conflicts with an existing VM " + vmReservation.getVmId() + " reserved on the same host " + plannedHostId);
                }
                return false;
            }
        }
    }
    return true;
}
Also used : VMReservationVO(org.apache.cloudstack.engine.cloud.entity.api.db.VMReservationVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 9 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cloudstack by apache.

the class LibvirtComputingResourceTest method testFenceCommand.

@Test
public void testFenceCommand() {
    final VirtualMachine vm = Mockito.mock(VirtualMachine.class);
    ;
    final com.cloud.host.Host host = Mockito.mock(com.cloud.host.Host.class);
    final FenceCommand command = new FenceCommand(vm, host);
    final KVMHAMonitor monitor = Mockito.mock(KVMHAMonitor.class);
    final NfsStoragePool storagePool = Mockito.mock(NfsStoragePool.class);
    final List<NfsStoragePool> pools = new ArrayList<NfsStoragePool>();
    pools.add(storagePool);
    when(libvirtComputingResource.getMonitor()).thenReturn(monitor);
    when(monitor.getStoragePools()).thenReturn(pools);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getMonitor();
    verify(monitor, times(1)).getStoragePools();
}
Also used : AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) FenceCommand(com.cloud.agent.api.FenceCommand) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) NfsStoragePool(com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool) ArrayList(java.util.ArrayList) VirtualMachine(com.cloud.vm.VirtualMachine) Test(org.junit.Test)

Example 10 with VirtualMachine

use of com.cloud.vm.VirtualMachine in project cloudstack by apache.

the class NuageVspGuestNetworkGuruTest method testReserve.

@Test
public void testReserve() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, URISyntaxException {
    final NetworkVO network = mock(NetworkVO.class);
    when(network.getId()).thenReturn(NETWORK_ID);
    when(network.getUuid()).thenReturn("aaaaaa");
    when(network.getDataCenterId()).thenReturn(NETWORK_ID);
    when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
    when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
    when(network.getDomainId()).thenReturn(NETWORK_ID);
    when(network.getAccountId()).thenReturn(NETWORK_ID);
    when(network.getVpcId()).thenReturn(null);
    when(network.getBroadcastUri()).thenReturn(new URI("vsp://aaaaaa-aavvv/10.1.1.1"));
    final DataCenterVO dataCenter = mock(DataCenterVO.class);
    when(_dataCenterDao.findById(NETWORK_ID)).thenReturn(dataCenter);
    final AccountVO networksAccount = mock(AccountVO.class);
    when(networksAccount.getId()).thenReturn(NETWORK_ID);
    when(networksAccount.getUuid()).thenReturn("aaaa-abbbb");
    when(networksAccount.getType()).thenReturn(Account.ACCOUNT_TYPE_NORMAL);
    when(_accountDao.findById(NETWORK_ID)).thenReturn(networksAccount);
    final DomainVO networksDomain = mock(DomainVO.class);
    when(networksDomain.getId()).thenReturn(NETWORK_ID);
    when(networksDomain.getUuid()).thenReturn("aaaaa-bbbbb");
    when(_domainDao.findById(NETWORK_ID)).thenReturn(networksDomain);
    final NicVO nicvo = mock(NicVO.class);
    when(nicvo.getId()).thenReturn(NETWORK_ID);
    when(nicvo.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
    when(nicvo.getUuid()).thenReturn("aaaa-fffff");
    when(nicvo.getNetworkId()).thenReturn(NETWORK_ID);
    when(nicvo.getInstanceId()).thenReturn(NETWORK_ID);
    when(_nicDao.findById(NETWORK_ID)).thenReturn(nicvo);
    when(_nicDao.findDefaultNicForVM(NETWORK_ID)).thenReturn(nicvo);
    final VirtualMachine vm = mock(VirtualMachine.class);
    when(vm.getId()).thenReturn(NETWORK_ID);
    when(vm.getType()).thenReturn(VirtualMachine.Type.User);
    final VirtualMachineProfile vmProfile = mock(VirtualMachineProfile.class);
    when(vmProfile.getType()).thenReturn(VirtualMachine.Type.User);
    when(vmProfile.getInstanceName()).thenReturn("");
    when(vmProfile.getUuid()).thenReturn("aaaa-bbbbb");
    when(vmProfile.getVirtualMachine()).thenReturn(vm);
    NicProfile nicProfile = mock(NicProfile.class);
    when(nicProfile.getUuid()).thenReturn("aaa-bbbb");
    when(nicProfile.getId()).thenReturn(NETWORK_ID);
    when(nicProfile.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
    final NetworkOfferingVO ntwkoffering = mock(NetworkOfferingVO.class);
    when(ntwkoffering.getId()).thenReturn(NETWORK_ID);
    when(_networkOfferingDao.findById(NETWORK_ID)).thenReturn(ntwkoffering);
    when(_networkDao.acquireInLockTable(NETWORK_ID, 1200)).thenReturn(network);
    when(_ipAddressDao.findByVmIdAndNetworkId(NETWORK_ID, NETWORK_ID)).thenReturn(null);
    when(_domainDao.findById(NETWORK_ID)).thenReturn(mock(DomainVO.class));
    final Answer answer = mock(Answer.class);
    when(answer.getResult()).thenReturn(true);
    when(_agentManager.easySend(eq(NETWORK_ID), (Command) any())).thenReturn(answer);
    final ReservationContext reservationContext = mock(ReservationContext.class);
    when(reservationContext.getAccount()).thenReturn(networksAccount);
    when(reservationContext.getDomain()).thenReturn(networksDomain);
    _nuageVspGuestNetworkGuru.reserve(nicProfile, network, vmProfile, mock(DeployDestination.class), reservationContext);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NicProfile(com.cloud.vm.NicProfile) URI(java.net.URI) AccountVO(com.cloud.user.AccountVO) ReservationContext(com.cloud.vm.ReservationContext) DomainVO(com.cloud.domain.DomainVO) Answer(com.cloud.agent.api.Answer) DeployDestination(com.cloud.deploy.DeployDestination) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) NicVO(com.cloud.vm.NicVO) VirtualMachine(com.cloud.vm.VirtualMachine) NuageTest(com.cloud.NuageTest) Test(org.junit.Test)

Aggregations

VirtualMachine (com.cloud.vm.VirtualMachine)65 ArrayList (java.util.ArrayList)17 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)16 HostVO (com.cloud.host.HostVO)15 DataCenter (com.cloud.dc.DataCenter)10 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)10 ServiceOffering (com.cloud.offering.ServiceOffering)9 HashMap (java.util.HashMap)9 List (java.util.List)9 ServerApiException (org.apache.cloudstack.api.ServerApiException)9 Test (org.junit.Test)9 Host (com.cloud.host.Host)8 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 ServerApiException (com.cloud.api.ServerApiException)7 NicProfile (com.cloud.vm.NicProfile)7 VMInstanceVO (com.cloud.vm.VMInstanceVO)7 SystemVmResponse (org.apache.cloudstack.api.response.SystemVmResponse)7 SystemVmResponse (com.cloud.api.response.SystemVmResponse)6 ManagementServerException (com.cloud.exception.ManagementServerException)6