Search in sources :

Example 1 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project CloudStack-archive by CloudStack-extras.

the class UpgradeSystemVMCmd method execute.

@Override
public void execute() {
    UserContext.current().setEventDetails("Vm Id: " + getId());
    ServiceOffering serviceOffering = _configService.getServiceOffering(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());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Fail to reboot system vm");
    }
}
Also used : SystemVmResponse(com.cloud.api.response.SystemVmResponse) ServerApiException(com.cloud.api.ServerApiException) ServiceOffering(com.cloud.offering.ServiceOffering) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 2 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project CloudStack-archive by CloudStack-extras.

the class UpgradeVMCmd method execute.

@Override
public void execute() {
    UserContext.current().setEventDetails("Vm Id: " + getId());
    ServiceOffering serviceOffering = _configService.getServiceOffering(serviceOfferingId);
    if (serviceOffering == null) {
        throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId);
    }
    UserVm result = _userVmService.upgradeVirtualMachine(this);
    if (result != null) {
        UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to upgrade vm");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) ServiceOffering(com.cloud.offering.ServiceOffering) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UserVmResponse(com.cloud.api.response.UserVmResponse)

Example 3 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project CloudStack-archive by CloudStack-extras.

the class DeployVMCmd method create.

@Override
public void create() throws ResourceAllocationException {
    try {
        //Verify that all objects exist before passing them to the service
        Account owner = _accountService.getActiveAccountById(getEntityOwnerId());
        DataCenter zone = _configService.getZone(zoneId);
        if (zone == null) {
            throw new InvalidParameterValueException("Unable to find zone by id=" + zoneId);
        }
        ServiceOffering serviceOffering = _configService.getServiceOffering(serviceOfferingId);
        if (serviceOffering == null) {
            throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId);
        }
        VirtualMachineTemplate template = _templateService.getTemplate(templateId);
        // Make sure a valid template ID was specified
        if (template == null) {
            throw new InvalidParameterValueException("Unable to use template " + templateId);
        }
        if (diskOfferingId != null) {
            DiskOffering diskOffering = _configService.getDiskOffering(diskOfferingId);
            if (diskOffering == null) {
                throw new InvalidParameterValueException("Unable to find disk offering " + diskOfferingId);
            }
        }
        UserVm vm = null;
        if (getHypervisor() == HypervisorType.BareMetal) {
            vm = _bareMetalVmService.createVirtualMachine(this);
        } else {
            if (zone.getNetworkType() == NetworkType.Basic) {
                if (getNetworkIds() != null) {
                    throw new InvalidParameterValueException("Can't specify network Ids in Basic zone");
                } else {
                    vm = _userVmService.createBasicSecurityGroupVirtualMachine(zone, serviceOffering, template, getSecurityGroupIdList(), owner, name, displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap(), ipAddress, keyboard);
                }
            } else {
                if (zone.isSecurityGroupEnabled()) {
                    vm = _userVmService.createAdvancedSecurityGroupVirtualMachine(zone, serviceOffering, template, getNetworkIds(), getSecurityGroupIdList(), owner, name, displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap(), ipAddress, keyboard);
                } else {
                    if (getSecurityGroupIdList() != null && !getSecurityGroupIdList().isEmpty()) {
                        throw new InvalidParameterValueException("Can't create vm with security groups; security group feature is not enabled per zone");
                    }
                    vm = _userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, getNetworkIds(), owner, name, displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap(), ipAddress, keyboard);
                }
            }
        }
        if (vm != null) {
            setEntityId(vm.getId());
        } else {
            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to deploy vm");
        }
    } catch (InsufficientCapacityException ex) {
        s_logger.info(ex);
        s_logger.trace(ex);
        throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, ex.getMessage());
    } catch (ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage());
    }
}
Also used : Account(com.cloud.user.Account) UserVm(com.cloud.uservm.UserVm) DataCenter(com.cloud.dc.DataCenter) DiskOffering(com.cloud.offering.DiskOffering) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ServerApiException(com.cloud.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ServiceOffering(com.cloud.offering.ServiceOffering) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException)

Example 4 with ServiceOffering

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

the class RandomAllocator method allocateTo.

@Override
public List<Host> allocateTo(VirtualMachineProfile vmProfile, DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo, boolean considerReservedCapacity) {
    long dcId = plan.getDataCenterId();
    Long podId = plan.getPodId();
    Long clusterId = plan.getClusterId();
    ServiceOffering offering = vmProfile.getServiceOffering();
    List<Host> suitableHosts = new ArrayList<Host>();
    if (type == Host.Type.Storage) {
        return suitableHosts;
    }
    String hostTag = offering.getHostTag();
    if (hostTag != null) {
        s_logger.debug("Looking for hosts in dc: " + dcId + "  pod:" + podId + "  cluster:" + clusterId + " having host tag:" + hostTag);
    } else {
        s_logger.debug("Looking for hosts in dc: " + dcId + "  pod:" + podId + "  cluster:" + clusterId);
    }
    // list all computing hosts, regardless of whether they support routing...it's random after all
    List<? extends Host> hosts = new ArrayList<HostVO>();
    if (hostTag != null) {
        hosts = _hostDao.listByHostTag(type, clusterId, podId, dcId, hostTag);
    } else {
        hosts = _resourceMgr.listAllUpAndEnabledHosts(type, clusterId, podId, dcId);
    }
    s_logger.debug("Random Allocator found " + hosts.size() + "  hosts");
    if (hosts.size() == 0) {
        return suitableHosts;
    }
    Collections.shuffle(hosts);
    for (Host host : hosts) {
        if (suitableHosts.size() == returnUpTo) {
            break;
        }
        if (!avoid.shouldAvoid(host)) {
            suitableHosts.add(host);
        } else {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Host name: " + host.getName() + ", hostId: " + host.getId() + " is in avoid set, skipping this and trying other available hosts");
            }
        }
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Random Host Allocator returning " + suitableHosts.size() + " suitable hosts");
    }
    return suitableHosts;
}
Also used : ServiceOffering(com.cloud.offering.ServiceOffering) ArrayList(java.util.ArrayList) Host(com.cloud.host.Host)

Example 5 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)

Aggregations

ServiceOffering (com.cloud.offering.ServiceOffering)103 ArrayList (java.util.ArrayList)34 Account (com.cloud.user.Account)30 DataCenter (com.cloud.dc.DataCenter)23 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)20 HashMap (java.util.HashMap)18 VirtualMachine (com.cloud.vm.VirtualMachine)17 VMTemplateVO (com.cloud.storage.VMTemplateVO)14 UserVm (com.cloud.uservm.UserVm)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 Map (java.util.Map)14 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)13 ServerApiException (com.cloud.api.ServerApiException)12 HostVO (com.cloud.host.HostVO)12 DiskOffering (com.cloud.offering.DiskOffering)11 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)11 Host (com.cloud.host.Host)10 Network (com.cloud.network.Network)10 List (java.util.List)10 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)9