Search in sources :

Example 6 with CloudException

use of com.cloud.legacymodel.exceptions.CloudException in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method stopVirtualMachine.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_STOP, eventDescription = "stopping Vm", async = true)
public UserVm stopVirtualMachine(final long vmId, final boolean forced) throws ConcurrentOperationException {
    // Input validation
    final Account caller = CallContext.current().getCallingAccount();
    final Long userId = CallContext.current().getCallingUserId();
    // if account is removed, return error
    if (caller != null && caller.getRemoved() != null) {
        throw new PermissionDeniedException("The account " + caller.getUuid() + " is removed");
    }
    final UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
    }
    _userDao.findById(userId);
    final boolean status;
    try {
        final VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
        if (forced || vm.getState() == State.Paused) {
            status = vmEntity.stopForced(Long.toString(userId));
        } else {
            status = vmEntity.stop(Long.toString(userId));
        }
        if (status) {
            return _vmDao.findById(vmId);
        } else {
            return null;
        }
    } catch (final ResourceUnavailableException e) {
        throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
    } catch (final CloudException e) {
        throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VirtualMachineEntity(com.cloud.engine.cloud.entity.api.VirtualMachineEntity) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) CloudException(com.cloud.legacymodel.exceptions.CloudException) ActionEvent(com.cloud.event.ActionEvent)

Example 7 with CloudException

use of com.cloud.legacymodel.exceptions.CloudException in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method destroyVm.

@Override
public UserVm destroyVm(final long vmId) throws ResourceUnavailableException, ConcurrentOperationException {
    // Account caller = CallContext.current().getCallingAccount();
    // Long userId = CallContext.current().getCallingUserId();
    final Long userId = 2L;
    // Verify input parameters
    final UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null || vm.getRemoved() != null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a virtual machine with specified vmId");
        throw ex;
    }
    if (vm.getState() == State.Destroyed || vm.getState() == State.Expunging) {
        s_logger.trace("Vm id=" + vmId + " is already destroyed");
        return vm;
    }
    final boolean status;
    final State vmState = vm.getState();
    try {
        final VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
        status = vmEntity.destroy(Long.toString(userId));
    } catch (final CloudException e) {
        final CloudRuntimeException ex = new CloudRuntimeException("Unable to destroy with specified vmId", e);
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    if (status) {
        // Mark the account's volumes as destroyed
        final List<VolumeVO> volumes = _volsDao.findByInstance(vmId);
        if (vmState != State.Error) {
            // Get serviceOffering for Virtual Machine
            final ServiceOfferingVO offering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId());
            // Update Resource Count for the given account
            resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
        }
        return _vmDao.findById(vmId);
    } else {
        final CloudRuntimeException ex = new CloudRuntimeException("Failed to destroy vm with specified vmId");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) AllocationState(com.cloud.model.enumeration.AllocationState) ResourceState(com.cloud.legacymodel.resource.ResourceState) State(com.cloud.legacymodel.vm.VirtualMachine.State) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VirtualMachineEntity(com.cloud.engine.cloud.entity.api.VirtualMachineEntity) CloudException(com.cloud.legacymodel.exceptions.CloudException) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO)

Example 8 with CloudException

use of com.cloud.legacymodel.exceptions.CloudException in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method stopVirtualMachine.

@Override
public boolean stopVirtualMachine(final long userId, final long vmId) {
    boolean status;
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Stopping vm=" + vmId);
    }
    final UserVmVO vm = _vmDao.findById(vmId);
    if (vm == null || vm.getRemoved() != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("VM is either removed or deleted.");
        }
        return true;
    }
    _userDao.findById(userId);
    try {
        final VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
        status = vmEntity.stop(Long.toString(userId));
    } catch (final ResourceUnavailableException e) {
        s_logger.debug("Unable to stop due to ", e);
        status = false;
    } catch (final CloudException e) {
        throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
    }
    return status;
}
Also used : CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VirtualMachineEntity(com.cloud.engine.cloud.entity.api.VirtualMachineEntity) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudException(com.cloud.legacymodel.exceptions.CloudException)

Aggregations

CloudException (com.cloud.legacymodel.exceptions.CloudException)8 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)4 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)4 VirtualMachineEntity (com.cloud.engine.cloud.entity.api.VirtualMachineEntity)3 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)3 CallContext (com.cloud.context.CallContext)2 Account (com.cloud.legacymodel.user.Account)2 AffinityGroup (com.cloud.affinity.AffinityGroup)1 DispatchTask (com.cloud.api.dispatch.DispatchTask)1 ExceptionResponse (com.cloud.api.response.ExceptionResponse)1 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)1 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)1 ActionEvent (com.cloud.event.ActionEvent)1 JobInfo (com.cloud.jobs.JobInfo)1 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)1 NetworkRuleConflictException (com.cloud.legacymodel.exceptions.NetworkRuleConflictException)1 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)1 ResourceAllocationException (com.cloud.legacymodel.exceptions.ResourceAllocationException)1 ResourceState (com.cloud.legacymodel.resource.ResourceState)1 User (com.cloud.legacymodel.user.User)1