use of com.cloud.exception.ResourceUnavailableException in project cloudstack by apache.
the class ListNicsCmd method execute.
@Override
public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
try {
if (this.getKeyword() != null && !this.getKeyword().isEmpty() && this.getNicId() != null) {
List<? extends NicSecondaryIp> results = _networkService.listVmNicSecondaryIps(this);
ListResponse<NicSecondaryIpResponse> response = new ListResponse<NicSecondaryIpResponse>();
List<NicSecondaryIpResponse> resList = new ArrayList<NicSecondaryIpResponse>();
NicSecondaryIpResponse res = new NicSecondaryIpResponse();
List<NicSecondaryIpResponse> res_List = new ArrayList<NicSecondaryIpResponse>();
if (results != null) {
for (NicSecondaryIp r : results) {
NicSecondaryIpResponse ipRes = _responseGenerator.createSecondaryIPToNicResponse(r);
resList.add(ipRes);
res.setSecondaryIpsList(resList);
res.setObjectName("nic");
}
res_List.add(res);
response.setResponses(res_List);
}
response.setResponses(res_List);
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
List<? extends Nic> results = _networkService.listNics(this);
ListResponse<NicResponse> response = new ListResponse<NicResponse>();
List<NicResponse> resList = null;
if (results != null) {
resList = new ArrayList<NicResponse>(results.size());
for (Nic r : results) {
NicResponse resp = _responseGenerator.createNicResponse(r);
resp.setObjectName("nic");
resList.add(resp);
}
response.setResponses(resList);
}
response.setResponses(resList);
response.setResponseName(getCommandName());
this.setResponseObject(response);
}
} catch (Exception e) {
s_logger.warn("Failed to list secondary ip address per nic ");
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
}
}
use of com.cloud.exception.ResourceUnavailableException in project cloudstack by apache.
the class StartVMCmd method execute.
@Override
public void execute() {
try {
CallContext.current().setEventDetails("Vm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId()));
UserVm result;
result = _userVmService.startVirtualMachine(this);
if (result != null) {
UserVmResponse response = _responseGenerator.createUserVmResponse(getResponseView(), "virtualmachine", result).get(0);
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to start a vm");
}
} catch (ConcurrentOperationException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
} catch (StorageUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
} catch (ExecutionException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
} catch (ResourceUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
} catch (ResourceAllocationException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_ALLOCATION_ERROR, ex.getMessage());
} catch (InsufficientCapacityException ex) {
StringBuilder message = new StringBuilder(ex.getMessage());
if (ex instanceof InsufficientServerCapacityException) {
if (((InsufficientServerCapacityException) ex).isAffinityApplied()) {
message.append(", Please check the affinity groups provided, there may not be sufficient capacity to follow them");
}
}
s_logger.info(ex);
s_logger.info(message.toString(), ex);
throw new ServerApiException(ApiErrorCode.INSUFFICIENT_CAPACITY_ERROR, message.toString());
}
}
use of com.cloud.exception.ResourceUnavailableException in project cloudstack by apache.
the class VirtualMachineManagerImpl method orchestrateMigrateAway.
private void orchestrateMigrateAway(final String vmUuid, final long srcHostId, final DeploymentPlanner planner) throws InsufficientServerCapacityException {
final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
if (vm == null) {
String message = String.format("Unable to find VM with uuid [%s].", vmUuid);
s_logger.warn(message);
throw new CloudRuntimeException(message);
}
ServiceOfferingVO offeringVO = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm, null, offeringVO, null, null);
final Long hostId = vm.getHostId();
if (hostId == null) {
String message = String.format("Unable to migrate %s due to it does not have a host id.", vm.toString());
s_logger.warn(message);
throw new CloudRuntimeException(message);
}
final Host host = _hostDao.findById(hostId);
Long poolId = null;
final List<VolumeVO> vols = _volsDao.findReadyRootVolumesByInstance(vm.getId());
for (final VolumeVO rootVolumeOfVm : vols) {
final StoragePoolVO rootDiskPool = _storagePoolDao.findById(rootVolumeOfVm.getPoolId());
if (rootDiskPool != null) {
poolId = rootDiskPool.getId();
}
}
final ExcludeList excludes = new ExcludeList();
excludes.addHost(hostId);
DataCenterDeployment plan = getMigrationDeployment(vm, host, poolId, excludes);
DeployDestination dest = null;
while (true) {
try {
plan.setMigrationPlan(true);
dest = _dpMgr.planDeployment(profile, plan, excludes, planner);
} catch (final AffinityConflictException e2) {
String message = String.format("Unable to create deployment, affinity rules associted to the %s conflict.", vm.toString());
s_logger.warn(message, e2);
throw new CloudRuntimeException(message, e2);
}
if (dest == null) {
s_logger.warn("Unable to find destination for migrating the vm " + profile);
throw new InsufficientServerCapacityException("Unable to find a server to migrate to.", DataCenter.class, host.getDataCenterId());
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found destination " + dest + " for migrating to.");
}
excludes.addHost(dest.getHost().getId());
try {
migrate(vm, srcHostId, dest);
return;
} catch (ResourceUnavailableException | ConcurrentOperationException e) {
s_logger.warn(String.format("Unable to migrate %s to %s due to [%s]", vm.toString(), dest.getHost().toString(), e.getMessage()), e);
}
try {
advanceStop(vmUuid, true);
throw new CloudRuntimeException("Unable to migrate " + vm);
} catch (final ResourceUnavailableException | ConcurrentOperationException | OperationTimedoutException e) {
s_logger.error(String.format("Unable to stop %s due to [%s].", vm.toString(), e.getMessage()), e);
throw new CloudRuntimeException("Unable to migrate " + vm);
}
}
}
use of com.cloud.exception.ResourceUnavailableException in project cloudstack by apache.
the class VirtualMachineManagerImpl method advanceExpunge.
protected void advanceExpunge(VMInstanceVO vm) throws ResourceUnavailableException, OperationTimedoutException, ConcurrentOperationException {
if (vm == null || vm.getRemoved() != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to find vm or vm is destroyed: " + vm);
}
return;
}
advanceStop(vm.getUuid(), VmDestroyForcestop.value());
vm = _vmDao.findByUuid(vm.getUuid());
try {
if (!stateTransitTo(vm, VirtualMachine.Event.ExpungeOperation, vm.getHostId())) {
s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm);
throw new CloudRuntimeException("Unable to destroy " + vm);
}
} catch (final NoTransitionException e) {
s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm);
throw new CloudRuntimeException("Unable to destroy " + vm, e);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Destroying vm " + vm);
}
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
List<NicProfile> vmNics = profile.getNics();
s_logger.debug(String.format("Cleaning up NICS [%s] of %s.", vmNics.stream().map(nic -> nic.toString()).collect(Collectors.joining(", ")), vm.toString()));
final List<Command> nicExpungeCommands = hvGuru.finalizeExpungeNics(vm, profile.getNics());
_networkMgr.cleanupNics(profile);
s_logger.debug(String.format("Cleaning up hypervisor data structures (ex. SRs in XenServer) for managed storage. Data from %s.", vm.toString()));
final List<Command> volumeExpungeCommands = hvGuru.finalizeExpungeVolumes(vm);
final Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
List<Map<String, String>> targets = getTargets(hostId, vm.getId());
if (CollectionUtils.isNotEmpty(volumeExpungeCommands) && hostId != null) {
final Commands cmds = new Commands(Command.OnError.Stop);
for (final Command volumeExpungeCommand : volumeExpungeCommands) {
volumeExpungeCommand.setBypassHostMaintenance(expungeCommandCanBypassHostMaintenance(vm));
cmds.addCommand(volumeExpungeCommand);
}
_agentMgr.send(hostId, cmds);
handleUnsuccessfulCommands(cmds, vm);
}
if (hostId != null) {
volumeMgr.revokeAccess(vm.getId(), hostId);
}
volumeMgr.cleanupVolumes(vm.getId());
if (hostId != null && CollectionUtils.isNotEmpty(targets)) {
removeDynamicTargets(hostId, targets);
}
final VirtualMachineGuru guru = getVmGuru(vm);
guru.finalizeExpunge(vm);
userVmDetailsDao.removeDetails(vm.getId());
userVmDeployAsIsDetailsDao.removeDetails(vm.getId());
// Remove comments (if any)
annotationDao.removeByEntityType(AnnotationService.EntityType.VM.name(), vm.getUuid());
// send hypervisor-dependent commands before removing
final List<Command> finalizeExpungeCommands = hvGuru.finalizeExpunge(vm);
if (CollectionUtils.isNotEmpty(finalizeExpungeCommands) || CollectionUtils.isNotEmpty(nicExpungeCommands)) {
if (hostId != null) {
final Commands cmds = new Commands(Command.OnError.Stop);
addAllExpungeCommandsFromList(finalizeExpungeCommands, cmds, vm);
addAllExpungeCommandsFromList(nicExpungeCommands, cmds, vm);
_agentMgr.send(hostId, cmds);
if (!cmds.isSuccessful()) {
for (final Answer answer : cmds.getAnswers()) {
if (!answer.getResult()) {
s_logger.warn("Failed to expunge vm due to: " + answer.getDetails());
throw new CloudRuntimeException("Unable to expunge " + vm + " due to " + answer.getDetails());
}
}
}
}
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Expunged " + vm);
}
}
use of com.cloud.exception.ResourceUnavailableException in project cloudstack by apache.
the class VirtualMachineManagerImpl method findHostAndMigrate.
@Override
public void findHostAndMigrate(final String vmUuid, final Long newSvcOfferingId, final Map<String, String> customParameters, final ExcludeList excludes) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
if (vm == null) {
throw new CloudRuntimeException("Unable to find " + vmUuid);
}
ServiceOfferingVO newServiceOffering = _offeringDao.findById(newSvcOfferingId);
if (newServiceOffering.isDynamic()) {
newServiceOffering.setDynamicFlag(true);
newServiceOffering = _offeringDao.getComputeOffering(newServiceOffering, customParameters);
}
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm, null, newServiceOffering, null, null);
final Long srcHostId = vm.getHostId();
final Long oldSvcOfferingId = vm.getServiceOfferingId();
if (srcHostId == null) {
throw new CloudRuntimeException("Unable to scale the vm because it doesn't have a host id");
}
final Host host = _hostDao.findById(srcHostId);
final DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), null, null, null);
excludes.addHost(vm.getHostId());
vm.setServiceOfferingId(newSvcOfferingId);
DeployDestination dest = null;
try {
dest = _dpMgr.planDeployment(profile, plan, excludes, null);
} catch (final AffinityConflictException e2) {
String message = String.format("Unable to create deployment, affinity rules associted to the %s conflict.", vm.toString());
s_logger.warn(message, e2);
throw new CloudRuntimeException(message);
}
if (dest != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(" Found " + dest + " for scaling the vm to.");
}
}
if (dest == null) {
throw new InsufficientServerCapacityException("Unable to find a server to scale the vm to.", host.getClusterId());
}
excludes.addHost(dest.getHost().getId());
try {
migrateForScale(vm.getUuid(), srcHostId, dest, oldSvcOfferingId);
} catch (ResourceUnavailableException | ConcurrentOperationException e) {
s_logger.warn(String.format("Unable to migrate %s to %s due to [%s]", vm.toString(), dest.getHost().toString(), e.getMessage()), e);
throw e;
}
}
Aggregations