Search in sources :

Example 6 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UserConcentratedAllocator method calcHostAllocatedCpuMemoryCapacity.

/**
     *
     * @param hostId
     *            Host id to calculate against
     * @param capacityType
     *            CapacityVO.CAPACITY_TYPE_MEMORY or
     *            CapacityVO.CAPACITY_TYPE_CPU
     * @return
     */
private long calcHostAllocatedCpuMemoryCapacity(long hostId, short capacityType) {
    assert (capacityType == Capacity.CAPACITY_TYPE_MEMORY || capacityType == Capacity.CAPACITY_TYPE_CPU) : "Invalid capacity type passed in calcHostAllocatedCpuCapacity()";
    // List<VMInstanceVO> vms = _vmInstanceDao.listByLastHostId(hostId);
    List<VMInstanceVO> vms = null;
    long usedCapacity = 0;
    if (vms != null) {
        for (VMInstanceVO vm : vms) {
            if (skipCalculation(vm)) {
                continue;
            }
            ServiceOffering so = null;
            if (vm.getType() == VirtualMachine.Type.User) {
                UserVmVO userVm = _vmDao.findById(vm.getId());
                if (userVm == null) {
                    continue;
                }
            }
            so = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
            if (capacityType == Capacity.CAPACITY_TYPE_MEMORY) {
                usedCapacity += so.getRamSize() * 1024L * 1024L;
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Counting memory capacity used by vm: " + vm.getId() + ", size: " + so.getRamSize() + "MB, host: " + hostId + ", currently counted: " + usedCapacity + " Bytes");
                }
            } else if (capacityType == Capacity.CAPACITY_TYPE_CPU) {
                usedCapacity += so.getCpu() * so.getSpeed();
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Counting cpu capacity used by vm: " + vm.getId() + ", cpu: " + so.getCpu() + ", speed: " + so.getSpeed() + ", currently counted: " + usedCapacity + " Bytes");
                }
            }
        }
    }
    return usedCapacity;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ServiceOffering(com.cloud.offering.ServiceOffering) VMInstanceVO(com.cloud.vm.VMInstanceVO)

Example 7 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class ScaleSystemVMCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("SystemVm Id: " + getId());
    ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
    if (serviceOffering == null) {
        throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId);
    }
    VirtualMachine result = null;
    try {
        result = _mgr.upgradeSystemVM(this);
    } catch (ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (ManagementServerException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (VirtualMachineMigrationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
    if (result != null) {
        SystemVmResponse response = _responseGenerator.createSystemVmResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to upgrade system vm");
    }
}
Also used : SystemVmResponse(org.apache.cloudstack.api.response.SystemVmResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ManagementServerException(com.cloud.exception.ManagementServerException) ServiceOffering(com.cloud.offering.ServiceOffering) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 8 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UpgradeSystemVMCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Vm Id: " + getId());
    ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
    if (serviceOffering == null) {
        throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId);
    }
    VirtualMachine result = _mgr.upgradeSystemVM(this);
    if (result != null) {
        SystemVmResponse response = _responseGenerator.createSystemVmResponse(result);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Fail to reboot system vm");
    }
}
Also used : SystemVmResponse(org.apache.cloudstack.api.response.SystemVmResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ServiceOffering(com.cloud.offering.ServiceOffering) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 9 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UserVmManagerImpl method updateVirtualMachine.

@Override
public UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData, Boolean isDynamicallyScalable, HTTPMethod httpMethod, String customId, String hostName, String instanceName, List<Long> securityGroupIdList) throws ResourceUnavailableException, InsufficientCapacityException {
    UserVmVO vm = _vmDao.findById(id);
    if (vm == null) {
        throw new CloudRuntimeException("Unable to find virtual machine with id " + id);
    }
    if (instanceName != null) {
        VMInstanceVO vmInstance = _vmInstanceDao.findVMByInstanceName(instanceName);
        if (vmInstance != null && vmInstance.getId() != id) {
            throw new CloudRuntimeException("Instance name : " + instanceName + " is not unique");
        }
    }
    if (vm.getState() == State.Error || vm.getState() == State.Expunging) {
        s_logger.error("vm is not in the right state: " + id);
        throw new InvalidParameterValueException("Vm with id " + id + " is not in the right state");
    }
    if (displayName == null) {
        displayName = vm.getDisplayName();
    }
    if (ha == null) {
        ha = vm.isHaEnabled();
    }
    ServiceOffering offering = _serviceOfferingDao.findById(vm.getId(), vm.getServiceOfferingId());
    if (!offering.getOfferHA() && ha) {
        throw new InvalidParameterValueException("Can't enable ha for the vm as it's created from the Service offering having HA disabled");
    }
    if (isDisplayVmEnabled == null) {
        isDisplayVmEnabled = vm.isDisplayVm();
    }
    boolean updateUserdata = false;
    if (userData != null) {
        // check and replace newlines
        userData = userData.replace("\\n", "");
        validateUserData(userData, httpMethod);
        // update userData on domain router.
        updateUserdata = true;
    } else {
        userData = vm.getUserData();
    }
    if (isDynamicallyScalable == null) {
        isDynamicallyScalable = vm.isDynamicallyScalable();
    }
    if (osTypeId == null) {
        osTypeId = vm.getGuestOSId();
    }
    if (group != null) {
        addInstanceToGroup(id, group);
    }
    if (isDynamicallyScalable == null) {
        isDynamicallyScalable = vm.isDynamicallyScalable();
    }
    boolean isVMware = (vm.getHypervisorType() == HypervisorType.VMware);
    if (securityGroupIdList != null && isVMware) {
        throw new InvalidParameterValueException("Security group feature is not supported for vmWare hypervisor");
    } else {
        // Get default guest network in Basic zone
        Network defaultNetwork = null;
        try {
            DataCenterVO zone = _dcDao.findById(vm.getDataCenterId());
            if (zone.getNetworkType() == NetworkType.Basic) {
                // Get default guest network in Basic zone
                defaultNetwork = _networkModel.getExclusiveGuestNetwork(zone.getId());
            } else if (zone.isSecurityGroupEnabled()) {
                NicVO defaultNic = _nicDao.findDefaultNicForVM(vm.getId());
                if (defaultNic != null) {
                    defaultNetwork = _networkDao.findById(defaultNic.getNetworkId());
                }
            }
        } catch (InvalidParameterValueException e) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(e.getMessage(), e);
            }
            defaultNetwork = _networkModel.getDefaultNetworkForVm(id);
        }
        if (securityGroupIdList != null && _networkModel.isSecurityGroupSupportedInNetwork(defaultNetwork) && _networkModel.canAddDefaultSecurityGroup()) {
            if (vm.getState() == State.Stopped) {
                // Remove instance from security groups
                _securityGroupMgr.removeInstanceFromGroups(id);
                // Add instance in provided groups
                _securityGroupMgr.addInstanceToGroups(id, securityGroupIdList);
            } else {
                throw new InvalidParameterValueException("Virtual machine must be stopped prior to update security groups ");
            }
        }
    }
    if (hostName != null) {
        // Check is hostName is RFC compliant
        checkNameForRFCCompliance(hostName);
        if (vm.getHostName().equalsIgnoreCase(hostName)) {
            s_logger.debug("Vm " + vm + " is already set with the hostName specified: " + hostName);
            hostName = null;
        }
        // Verify that vm's hostName is unique
        List<NetworkVO> vmNtwks = new ArrayList<NetworkVO>();
        List<? extends Nic> nics = _nicDao.listByVmId(vm.getId());
        for (Nic nic : nics) {
            vmNtwks.add(_networkDao.findById(nic.getNetworkId()));
        }
        checkIfHostNameUniqueInNtwkDomain(hostName, vmNtwks);
    }
    _vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable, customId, hostName, instanceName);
    if (updateUserdata) {
        boolean result = updateUserDataInternal(_vmDao.findById(id));
        if (result) {
            s_logger.debug("User data successfully updated for vm id=" + id);
        } else {
            throw new CloudRuntimeException("Failed to reset userdata for the virtual machine ");
        }
    }
    return _vmDao.findById(id);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) NetworkVO(com.cloud.network.dao.NetworkVO) ServiceOffering(com.cloud.offering.ServiceOffering) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork)

Example 10 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UpdateServiceOfferingCmd method execute.

@Override
public void execute() {
    //Note
    //Once an offering is created, we cannot update the domainId field (keeping consistent with zones logic)
    ServiceOffering result = _configService.updateServiceOffering(this);
    if (result != null) {
        ServiceOfferingResponse response = _responseGenerator.createServiceOfferingResponse(result);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update service offering");
    }
}
Also used : ServiceOfferingResponse(org.apache.cloudstack.api.response.ServiceOfferingResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ServiceOffering(com.cloud.offering.ServiceOffering)

Aggregations

ServiceOffering (com.cloud.offering.ServiceOffering)46 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)18 Account (com.cloud.user.Account)14 ArrayList (java.util.ArrayList)13 DataCenter (com.cloud.dc.DataCenter)11 VirtualMachine (com.cloud.vm.VirtualMachine)9 ServerApiException (org.apache.cloudstack.api.ServerApiException)8 ServerApiException (com.cloud.api.ServerApiException)5 VirtualMachineTemplate (com.cloud.template.VirtualMachineTemplate)5 UserVm (com.cloud.uservm.UserVm)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 HashMap (java.util.HashMap)5 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)4 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 Host (com.cloud.host.Host)4 HostVO (com.cloud.host.HostVO)4 DiskOffering (com.cloud.offering.DiskOffering)4 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)4 VMTemplateVO (com.cloud.storage.VMTemplateVO)4 ServiceOfferingResponse (com.cloud.api.response.ServiceOfferingResponse)3