Search in sources :

Example 1 with VirtualMachineMigrationException

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

the class MigrateSystemVMCmd method execute.

@Override
public void execute() {
    final Host destinationHost = _resourceService.getHost(getHostId());
    if (destinationHost == null) {
        throw new InvalidParameterValueException("Unable to find the host to migrate the VM, host id=" + getHostId());
    }
    try {
        CallContext.current().setEventDetails("VM Id: " + getVirtualMachineId() + " to host Id: " + getHostId());
        // FIXME : Should not be calling UserVmService to migrate all types of VMs - need a generic VM layer
        final VirtualMachine migratedVm = _userVmService.migrateVirtualMachine(getVirtualMachineId(), destinationHost);
        if (migratedVm != null) {
            // return the generic system VM instance response
            final SystemVmResponse response = _responseGenerator.createSystemVmResponse(migratedVm);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate the system vm");
        }
    } catch (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    } catch (final ManagementServerException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    } catch (final VirtualMachineMigrationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    }
}
Also used : SystemVmResponse(com.cloud.api.response.SystemVmResponse) ServerApiException(com.cloud.api.ServerApiException) ManagementServerException(com.cloud.legacymodel.exceptions.ManagementServerException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) Host(com.cloud.legacymodel.dc.Host) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VirtualMachine(com.cloud.legacymodel.vm.VirtualMachine)

Example 2 with VirtualMachineMigrationException

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

the class ScaleSystemVMCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("SystemVm Id: " + getId());
    final 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 (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (final ManagementServerException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (final VirtualMachineMigrationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    }
    if (result != null) {
        final 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(com.cloud.api.response.SystemVmResponse) ServerApiException(com.cloud.api.ServerApiException) ManagementServerException(com.cloud.legacymodel.exceptions.ManagementServerException) ServiceOffering(com.cloud.offering.ServiceOffering) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VirtualMachine(com.cloud.legacymodel.vm.VirtualMachine)

Example 3 with VirtualMachineMigrationException

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

the class MigrateVMCmd method execute.

@Override
public void execute() {
    if (getHostId() == null && getStoragePoolId() == null) {
        throw new InvalidParameterValueException("Either hostId or storageId must be specified");
    }
    if (getHostId() != null && getStoragePoolId() != null) {
        throw new InvalidParameterValueException("Only one of hostId and storageId can be specified");
    }
    final UserVm userVm = _userVmService.getUserVm(getVirtualMachineId());
    if (userVm == null) {
        throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId());
    }
    Host destinationHost = null;
    if (getHostId() != null) {
        destinationHost = _resourceService.getHost(getHostId());
        if (destinationHost == null) {
            throw new InvalidParameterValueException("Unable to find the host to migrate the VM, host id=" + getHostId());
        }
        if (destinationHost.getType() != HostType.Routing) {
            throw new InvalidParameterValueException("The specified host(" + destinationHost.getName() + ") is not suitable to migrate the VM, please specify another one");
        }
        CallContext.current().setEventDetails("VM Id: " + getVirtualMachineId() + " to host Id: " + getHostId());
    }
    StoragePool destStoragePool = null;
    if (getStoragePoolId() != null) {
        destStoragePool = _storageService.getStoragePool(getStoragePoolId());
        if (destStoragePool == null) {
            throw new InvalidParameterValueException("Unable to find the storage pool to migrate the VM");
        }
        CallContext.current().setEventDetails("VM Id: " + getVirtualMachineId() + " to storage pool Id: " + getStoragePoolId());
    }
    try {
        VirtualMachine migratedVm = null;
        if (getHostId() != null) {
            migratedVm = _userVmService.migrateVirtualMachine(getVirtualMachineId(), destinationHost);
        } else if (getStoragePoolId() != null) {
            migratedVm = _userVmService.vmStorageMigration(getVirtualMachineId(), destStoragePool);
        }
        if (migratedVm != null) {
            final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm) migratedVm).get(0);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate vm");
        }
    } catch (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    } catch (final ManagementServerException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    } catch (final VirtualMachineMigrationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) StoragePool(com.cloud.legacymodel.storage.StoragePool) ServerApiException(com.cloud.api.ServerApiException) ManagementServerException(com.cloud.legacymodel.exceptions.ManagementServerException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) Host(com.cloud.legacymodel.dc.Host) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) UserVmResponse(com.cloud.api.response.UserVmResponse) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VirtualMachine(com.cloud.legacymodel.vm.VirtualMachine)

Example 4 with VirtualMachineMigrationException

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

the class MigrateVirtualMachineWithVolumeCmd method execute.

@Override
public void execute() {
    final UserVm userVm = _userVmService.getUserVm(getVirtualMachineId());
    if (userVm == null) {
        throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId());
    }
    final Host destinationHost = _resourceService.getHost(getHostId());
    if (destinationHost == null) {
        throw new InvalidParameterValueException("Unable to find the host to migrate the VM, host id =" + getHostId());
    }
    try {
        final VirtualMachine migratedVm = _userVmService.migrateVirtualMachineWithVolume(getVirtualMachineId(), destinationHost, getVolumeToPool());
        if (migratedVm != null) {
            final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm) migratedVm).get(0);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate vm");
        }
    } catch (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException | ManagementServerException | VirtualMachineMigrationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) ManagementServerException(com.cloud.legacymodel.exceptions.ManagementServerException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) Host(com.cloud.legacymodel.dc.Host) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) UserVmResponse(com.cloud.api.response.UserVmResponse) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) VirtualMachine(com.cloud.legacymodel.vm.VirtualMachine)

Example 5 with VirtualMachineMigrationException

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

the class UserVmManagerImpl method migrateVirtualMachine.

@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_MIGRATE, eventDescription = "migrating VM", async = true)
public VirtualMachine migrateVirtualMachine(final Long vmId, final Host destinationHost) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, VirtualMachineMigrationException {
    // access check - only root admin can migrate VM
    final Account caller = CallContext.current().getCallingAccount();
    if (!_accountMgr.isRootAdmin(caller.getId())) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Caller is not a root admin, permission denied to migrate the VM");
        }
        throw new PermissionDeniedException("No permission to migrate VM, Only Root Admin can migrate a VM!");
    }
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("Unable to find the VM by id=" + vmId);
    }
    // business logic
    if (vm.getState() != State.Running) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("VM is not Running, unable to migrate the vm " + vm);
        }
        final InvalidParameterValueException ex = new InvalidParameterValueException("VM is not Running, unable to migrate the vm with specified id");
        ex.addProxyObject(vm.getUuid(), "vmId");
        throw ex;
    }
    if (serviceOfferingDetailsDao.findDetail(vm.getServiceOfferingId(), GPU.Keys.pciDevice.toString()) != null) {
        throw new InvalidParameterValueException("Live Migration of GPU enabled VM is not supported");
    }
    if (!vm.getHypervisorType().equals(HypervisorType.XenServer) && !vm.getHypervisorType().equals(HypervisorType.KVM)) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(vm + " is not XenServer/KVM, cannot migrate this VM.");
        }
        throw new InvalidParameterValueException("Unsupported Hypervisor Type for VM migration, we support XenServer/KVM only");
    }
    if (isVMUsingLocalStorage(vm)) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug(vm + " is using Local Storage, cannot migrate this VM.");
        }
        throw new InvalidParameterValueException("Unsupported operation, VM uses Local storage, cannot migrate");
    }
    // check if migrating to same host
    final long srcHostId = vm.getHostId();
    if (destinationHost.getId() == srcHostId) {
        throw new InvalidParameterValueException("Cannot migrate VM, VM is already presnt on this host, please specify valid destination host to migrate the VM");
    }
    // check if host is UP
    if (destinationHost.getState() != HostStatus.Up || destinationHost.getResourceState() != ResourceState.Enabled) {
        throw new InvalidParameterValueException("Cannot migrate VM, destination host is not in correct state, has status: " + destinationHost.getState() + ", state: " + destinationHost.getResourceState());
    }
    if (vm.getType() != VirtualMachineType.User) {
        // for System VMs check that the destination host is within the same
        // cluster
        final HostVO srcHost = _hostDao.findById(srcHostId);
        if (srcHost != null && srcHost.getClusterId() != null && destinationHost.getClusterId() != null) {
            if (srcHost.getClusterId().longValue() != destinationHost.getClusterId().longValue()) {
                throw new InvalidParameterValueException("Cannot migrate the VM, destination host is not in the same cluster as current host of the VM");
            }
        }
    }
    checkHostsDedication(vm, srcHostId, destinationHost.getId());
    // call to core process
    final Zone zone = zoneRepository.findById(destinationHost.getDataCenterId()).orElse(null);
    final HostPodVO pod = _podDao.findById(destinationHost.getPodId());
    final Cluster cluster = _clusterDao.findById(destinationHost.getClusterId());
    final DeployDestination dest = new DeployDestination(zone, pod, cluster, destinationHost);
    // check max guest vm limit for the destinationHost
    final HostVO destinationHostVO = _hostDao.findById(destinationHost.getId());
    if (_capacityMgr.checkIfHostReachMaxGuestLimit(destinationHostVO)) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Host name: " + destinationHost.getName() + ": " + destinationHost.getName() + " already has max Running VMs(count includes system VMs), cannot migrate to this host");
        }
        throw new VirtualMachineMigrationException("Destination host: " + destinationHost.getName() + " already has max Running VMs(count includes system VMs), cannot migrate to this host");
    }
    // Check host tags
    final ServiceOffering serviceOffering = _serviceOfferingDao.findById(vm.getServiceOfferingId());
    if (serviceOffering != null) {
        final String requiredHostTag = serviceOffering.getHostTag();
        final List<String> hostTags = _hostTagsDao.gethostTags(destinationHost.getId());
        boolean foundRequiredHostTag = false;
        for (final String hostTag : hostTags) {
            if (hostTag.equals(requiredHostTag)) {
                foundRequiredHostTag = true;
                break;
            }
        }
        if (!foundRequiredHostTag && requiredHostTag != null) {
            throw new VirtualMachineMigrationException("Destination host: " + destinationHost.getName() + " does not have required host tag " + requiredHostTag);
        }
    }
    final UserVmVO uservm = _vmDao.findById(vmId);
    if (uservm != null) {
        collectVmDiskStatistics(uservm);
    }
    _itMgr.migrate(vm.getUuid(), srcHostId, dest);
    final VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
    if (vmInstance.getType().equals(VirtualMachineType.User)) {
        return _vmDao.findById(vmId);
    } else {
        return vmInstance;
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) ServiceOffering(com.cloud.offering.ServiceOffering) Zone(com.cloud.db.model.Zone) Cluster(com.cloud.legacymodel.dc.Cluster) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) DeployDestination(com.cloud.deploy.DeployDestination) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

VirtualMachineMigrationException (com.cloud.legacymodel.exceptions.VirtualMachineMigrationException)10 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)8 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)8 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)7 ManagementServerException (com.cloud.legacymodel.exceptions.ManagementServerException)7 ServerApiException (com.cloud.api.ServerApiException)6 VirtualMachine (com.cloud.legacymodel.vm.VirtualMachine)5 UserVmResponse (com.cloud.api.response.UserVmResponse)4 Host (com.cloud.legacymodel.dc.Host)4 UserVm (com.cloud.uservm.UserVm)4 HostVO (com.cloud.host.HostVO)3 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)3 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)3 Account (com.cloud.legacymodel.user.Account)3 ServiceOffering (com.cloud.offering.ServiceOffering)3 SystemVmResponse (com.cloud.api.response.SystemVmResponse)2 ActionEvent (com.cloud.event.ActionEvent)2 Zone (com.cloud.db.model.Zone)1 HostPodVO (com.cloud.dc.HostPodVO)1 DeployDestination (com.cloud.deploy.DeployDestination)1