Search in sources :

Example 96 with VMInstanceVO

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

the class ImplicitDedicationPlanner method getResourceUsage.

@Override
public PlannerResourceUsage getResourceUsage(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid) throws InsufficientServerCapacityException {
    // Check if strict or preferred mode should be used.
    boolean preferred = isServiceOfferingUsingPlannerInPreferredMode(vmProfile.getServiceOfferingId());
    // If service offering in strict mode return resource usage as Dedicated
    if (!preferred) {
        return PlannerResourceUsage.Dedicated;
    } else {
        // service offering is in implicit mode.
        // find is it possible to deploy in dedicated mode,
        // if its possible return dedicated else return shared.
        List<Long> clusterList = super.orderClusters(vmProfile, plan, avoid);
        Set<Long> hostsToAvoid = avoid.getHostsToAvoid();
        Account account = vmProfile.getOwner();
        // Get the list of all the hosts in the given clusters
        List<Long> allHosts = new ArrayList<Long>();
        for (Long cluster : clusterList) {
            List<HostVO> hostsInCluster = resourceMgr.listAllHostsInCluster(cluster);
            for (HostVO hostVO : hostsInCluster) {
                allHosts.add(hostVO.getId());
            }
        }
        // Go over all the hosts in the cluster and get a list of
        // 1. All empty hosts, not running any vms.
        // 2. Hosts running vms for this account and created by a service
        // offering which uses an
        // implicit dedication planner.
        // 3. Hosts running vms created by implicit planner and in strict
        // mode of other accounts.
        // 4. Hosts running vms from other account or from this account but
        // created by a service offering which uses
        // any planner besides implicit.
        Set<Long> emptyHosts = new HashSet<Long>();
        Set<Long> hostRunningVmsOfAccount = new HashSet<Long>();
        Set<Long> hostRunningStrictImplicitVmsOfOtherAccounts = new HashSet<Long>();
        Set<Long> allOtherHosts = new HashSet<Long>();
        for (Long host : allHosts) {
            List<VMInstanceVO> vms = getVmsOnHost(host);
            // emptyHost should contain only Hosts which are not having any VM's (user/system) on it.
            if (vms == null || vms.isEmpty()) {
                emptyHosts.add(host);
            } else if (checkHostSuitabilityForImplicitDedication(account.getAccountId(), vms)) {
                hostRunningVmsOfAccount.add(host);
            } else if (checkIfAllVmsCreatedInStrictMode(account.getAccountId(), vms)) {
                hostRunningStrictImplicitVmsOfOtherAccounts.add(host);
            } else {
                allOtherHosts.add(host);
            }
        }
        // Hosts running vms of other accounts created by ab implicit
        // planner in strict mode should always be avoided.
        avoid.addHostList(hostRunningStrictImplicitVmsOfOtherAccounts);
        if (!hostRunningVmsOfAccount.isEmpty() && (hostsToAvoid == null || !hostsToAvoid.containsAll(hostRunningVmsOfAccount))) {
            // If so, we'll try and use these hosts. We can deploy in Dedicated mode
            return PlannerResourceUsage.Dedicated;
        } else if (!emptyHosts.isEmpty() && (hostsToAvoid == null || !hostsToAvoid.containsAll(emptyHosts))) {
            // The scenario will fail where actual Empty hosts and uservms not running host.
            return PlannerResourceUsage.Dedicated;
        } else {
            if (!allOtherHosts.isEmpty() && (hostsToAvoid == null || !hostsToAvoid.containsAll(allOtherHosts))) {
                return PlannerResourceUsage.Shared;
            }
        }
        return PlannerResourceUsage.Shared;
    }
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostVO(com.cloud.host.HostVO) HashSet(java.util.HashSet)

Example 97 with VMInstanceVO

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

the class ImplicitPlannerTest method initializeForImplicitPlannerTest.

private void initializeForImplicitPlannerTest(boolean preferred) {
    String plannerMode = new String("Strict");
    if (preferred) {
        plannerMode = new String("Preferred");
    }
    Map<String, String> details = new HashMap<String, String>();
    details.put("ImplicitDedicationMode", plannerMode);
    when(serviceOfferingDetailsDao.listDetailsKeyPairs(offeringId)).thenReturn(details);
    // Initialize hosts in clusters
    HostVO host1 = mock(HostVO.class);
    when(host1.getId()).thenReturn(5L);
    HostVO host2 = mock(HostVO.class);
    when(host2.getId()).thenReturn(6L);
    HostVO host3 = mock(HostVO.class);
    when(host3.getId()).thenReturn(7L);
    List<HostVO> hostsInCluster1 = new ArrayList<HostVO>();
    List<HostVO> hostsInCluster2 = new ArrayList<HostVO>();
    List<HostVO> hostsInCluster3 = new ArrayList<HostVO>();
    hostsInCluster1.add(host1);
    hostsInCluster2.add(host2);
    hostsInCluster3.add(host3);
    when(resourceMgr.listAllHostsInCluster(1)).thenReturn(hostsInCluster1);
    when(resourceMgr.listAllHostsInCluster(2)).thenReturn(hostsInCluster2);
    when(resourceMgr.listAllHostsInCluster(3)).thenReturn(hostsInCluster3);
    // Mock vms on each host.
    long offeringIdForVmsOfThisAccount = 15L;
    long offeringIdForVmsOfOtherAccount = 16L;
    UserVmVO vm1 = mock(UserVmVO.class);
    when(vm1.getAccountId()).thenReturn(accountId);
    when(vm1.getServiceOfferingId()).thenReturn(offeringIdForVmsOfThisAccount);
    UserVmVO vm2 = mock(UserVmVO.class);
    when(vm2.getAccountId()).thenReturn(accountId);
    when(vm2.getServiceOfferingId()).thenReturn(offeringIdForVmsOfThisAccount);
    // Vm from different account
    UserVmVO vm3 = mock(UserVmVO.class);
    when(vm3.getAccountId()).thenReturn(201L);
    when(vm3.getServiceOfferingId()).thenReturn(offeringIdForVmsOfOtherAccount);
    List<VMInstanceVO> vmsForHost1 = new ArrayList<VMInstanceVO>();
    List<VMInstanceVO> vmsForHost2 = new ArrayList<VMInstanceVO>();
    List<VMInstanceVO> vmsForHost3 = new ArrayList<VMInstanceVO>();
    List<VMInstanceVO> stoppedVmsForHost = new ArrayList<VMInstanceVO>();
    // Host 2 is empty.
    vmsForHost1.add(vm1);
    vmsForHost1.add(vm2);
    vmsForHost3.add(vm3);
    when(vmInstanceDao.listUpByHostId(5L)).thenReturn(vmsForHost1);
    when(vmInstanceDao.listUpByHostId(6L)).thenReturn(vmsForHost2);
    when(vmInstanceDao.listUpByHostId(7L)).thenReturn(vmsForHost3);
    when(vmInstanceDao.listByLastHostId(5L)).thenReturn(stoppedVmsForHost);
    when(vmInstanceDao.listByLastHostId(6L)).thenReturn(stoppedVmsForHost);
    when(vmInstanceDao.listByLastHostId(7L)).thenReturn(stoppedVmsForHost);
    // Mock the offering with which the vm was created.
    ServiceOfferingVO offeringForVmOfThisAccount = mock(ServiceOfferingVO.class);
    when(serviceOfferingDao.findByIdIncludingRemoved(offeringIdForVmsOfThisAccount)).thenReturn(offeringForVmOfThisAccount);
    when(offeringForVmOfThisAccount.getDeploymentPlanner()).thenReturn(planner.getName());
    ServiceOfferingVO offeringForVMOfOtherAccount = mock(ServiceOfferingVO.class);
    when(serviceOfferingDao.findByIdIncludingRemoved(offeringIdForVmsOfOtherAccount)).thenReturn(offeringForVMOfOtherAccount);
    when(offeringForVMOfOtherAccount.getDeploymentPlanner()).thenReturn("FirstFitPlanner");
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) Matchers.anyString(org.mockito.Matchers.anyString) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) HostVO(com.cloud.host.HostVO)

Example 98 with VMInstanceVO

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

the class BareMetalResourceBase method execute.

protected StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vm = cmd.getVirtualMachine();
    OutputInterpreter.AllLinesParser interpreter = new OutputInterpreter.AllLinesParser();
    if (!doScript(_getStatusCommand, interpreter)) {
        return new StartAnswer(cmd, "Cannot get current power status of " + getName());
    }
    if (isPowerOn(interpreter.getLines())) {
        if (!doScript(_rebootCommand)) {
            return new StartAnswer(cmd, "IPMI reboot failed");
        }
    } else {
        if (!doScript(_powerOnCommand)) {
            return new StartAnswer(cmd, "IPMI power on failed");
        }
    }
    if (_isEchoScAgent) {
        SecurityGroupHttpClient hc = new SecurityGroupHttpClient();
        boolean echoRet = hc.echo(vm.getNics()[0].getIp(), TimeUnit.MINUTES.toMillis(30), TimeUnit.MINUTES.toMillis(1));
        if (!echoRet) {
            return new StartAnswer(cmd, String.format("Call security group agent on vm[%s] timeout", vm.getNics()[0].getIp()));
        }
    }
    if (provisionDoneNotificationOn) {
        QueryBuilder<VMInstanceVO> q = QueryBuilder.create(VMInstanceVO.class);
        q.and(q.entity().getInstanceName(), SearchCriteria.Op.EQ, vm.getName());
        VMInstanceVO vmvo = q.find();
        if (vmvo.getLastHostId() == null) {
            // this is new created vm
            long timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(isProvisionDoneNotificationTimeout);
            while (timeout > System.currentTimeMillis()) {
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    s_logger.warn(e.getMessage(), e);
                }
                q = QueryBuilder.create(VMInstanceVO.class);
                q.and(q.entity().getInstanceName(), SearchCriteria.Op.EQ, vm.getName());
                vmvo = q.find();
                if (vmvo == null) {
                    return new StartAnswer(cmd, String.format("cannot find vm[name:%s] while waiting for baremtal provision done notification", vm.getName()));
                }
                if (VirtualMachine.State.Running == vmvo.getState()) {
                    return new StartAnswer(cmd);
                }
                s_logger.debug(String.format("still wait for baremetal provision done notification for vm[name:%s], current vm state is %s", vmvo.getInstanceName(), vmvo.getState()));
            }
            return new StartAnswer(cmd, String.format("timeout after %s seconds, no baremetal provision done notification received. vm[name:%s] failed to start", isProvisionDoneNotificationTimeout, vm.getName()));
        }
    }
    s_logger.debug("Start bare metal vm " + vm.getName() + "successfully");
    _vmName = vm.getName();
    return new StartAnswer(cmd);
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) VMInstanceVO(com.cloud.vm.VMInstanceVO) OutputInterpreter(com.cloud.utils.script.OutputInterpreter) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO)

Example 99 with VMInstanceVO

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

the class ApiResponseHelper method createNicResponse.

@Override
public NicResponse createNicResponse(Nic result) {
    NicResponse response = new NicResponse();
    NetworkVO network = _entityMgr.findById(NetworkVO.class, result.getNetworkId());
    VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, result.getInstanceId());
    UserVmJoinVO userVm = _entityMgr.findById(UserVmJoinVO.class, result.getInstanceId());
    response.setId(result.getUuid());
    response.setNetworkid(network.getUuid());
    if (vm != null) {
        response.setVmId(vm.getUuid());
    }
    if (userVm != null) {
        if (userVm.getTrafficType() != null) {
            response.setTrafficType(userVm.getTrafficType().toString());
        }
        if (userVm.getGuestType() != null) {
            response.setType(userVm.getGuestType().toString());
        }
    }
    response.setIpaddress(result.getIPv4Address());
    if (result.getSecondaryIp()) {
        List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(result.getId());
        if (secondaryIps != null) {
            List<NicSecondaryIpResponse> ipList = new ArrayList<NicSecondaryIpResponse>();
            for (NicSecondaryIpVO ip : secondaryIps) {
                NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
                ipRes.setId(ip.getUuid());
                ipRes.setIpAddr(ip.getIp4Address());
                ipList.add(ipRes);
            }
            response.setSecondaryIps(ipList);
        }
    }
    response.setGateway(result.getIPv4Gateway());
    response.setNetmask(result.getIPv4Netmask());
    response.setMacAddress(result.getMacAddress());
    if (result.getIPv6Address() != null) {
        response.setIp6Address(result.getIPv6Address());
    }
    if (result.getIPv6Cidr() != null) {
        response.setIp6Cidr(result.getIPv6Cidr());
    }
    response.setDeviceId(String.valueOf(result.getDeviceId()));
    response.setIsDefault(result.isDefaultNic());
    if (result instanceof NicVO) {
        if (((NicVO) result).getNsxLogicalSwitchUuid() != null) {
            response.setNsxLogicalSwitch(((NicVO) result).getNsxLogicalSwitchUuid());
        }
        if (((NicVO) result).getNsxLogicalSwitchPortUuid() != null) {
            response.setNsxLogicalSwitchPort(((NicVO) result).getNsxLogicalSwitchPortUuid());
        }
    }
    return response;
}
Also used : NicSecondaryIpResponse(org.apache.cloudstack.api.response.NicSecondaryIpResponse) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) NicVO(com.cloud.vm.NicVO) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO) NicResponse(org.apache.cloudstack.api.response.NicResponse)

Example 100 with VMInstanceVO

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

the class TemplateManagerImpl method templateIsDeleteable.

@Override
public boolean templateIsDeleteable(VMTemplateHostVO templateHostRef) {
    VMTemplateVO template = _tmpltDao.findByIdIncludingRemoved(templateHostRef.getTemplateId());
    long templateId = template.getId();
    HostVO secondaryStorageHost = _hostDao.findById(templateHostRef.getHostId());
    long zoneId = secondaryStorageHost.getDataCenterId();
    DataCenterVO zone = _dcDao.findById(zoneId);
    // Check if there are VMs running in the template host ref's zone that
    // use the template
    List<VMInstanceVO> nonExpungedVms = _vmInstanceDao.listNonExpungedByZoneAndTemplate(zoneId, templateId);
    if (!nonExpungedVms.isEmpty()) {
        s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are non-expunged VMs deployed from this template.");
        return false;
    }
    List<UserVmVO> userVmUsingIso = _userVmDao.listByIsoId(templateId);
    // check if there is any VM using this ISO.
    if (!userVmUsingIso.isEmpty()) {
        s_logger.debug("ISO " + template.getName() + " in zone " + zone.getName() + " is not deleteable because it is attached to " + userVmUsingIso.size() + " VMs");
        return false;
    }
    // Check if there are any snapshots for the template in the template
    // host ref's zone
    List<VolumeVO> volumes = _volumeDao.findByTemplateAndZone(templateId, zoneId);
    for (VolumeVO volume : volumes) {
        List<SnapshotVO> snapshots = _snapshotDao.listByVolumeIdVersion(volume.getId(), "2.1");
        if (!snapshots.isEmpty()) {
            s_logger.debug("Template " + template.getName() + " in zone " + zone.getName() + " is not deleteable because there are 2.1 snapshots using this template.");
            return false;
        }
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) UserVmVO(com.cloud.vm.UserVmVO) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) VMTemplateHostVO(com.cloud.storage.VMTemplateHostVO) HostVO(com.cloud.host.HostVO)

Aggregations

VMInstanceVO (com.cloud.vm.VMInstanceVO)131 ArrayList (java.util.ArrayList)40 HostVO (com.cloud.host.HostVO)34 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)28 Account (com.cloud.user.Account)28 VolumeVO (com.cloud.storage.VolumeVO)24 Test (org.junit.Test)24 HostPodVO (com.cloud.dc.HostPodVO)15 HashMap (java.util.HashMap)14 NetworkVO (com.cloud.network.dao.NetworkVO)13 User (com.cloud.user.User)13 NicVO (com.cloud.vm.NicVO)13 VmWorkJobVO (org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)13 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)12 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)12 ActionEvent (com.cloud.event.ActionEvent)11 Random (java.util.Random)11 Answer (com.cloud.agent.api.Answer)10 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10