|
86 | 86 | import com.cloud.exception.InsufficientCapacityException;
|
87 | 87 | import com.cloud.exception.InsufficientServerCapacityException;
|
88 | 88 | import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
|
| 89 | +import com.cloud.exception.InvalidParameterValueException; |
89 | 90 | import com.cloud.exception.ManagementServerException;
|
90 | 91 | import com.cloud.exception.OperationTimedoutException;
|
91 | 92 | import com.cloud.exception.ResourceUnavailableException;
|
|
126 | 127 | import com.cloud.user.User;
|
127 | 128 | import com.cloud.user.dao.AccountDao;
|
128 | 129 | import com.cloud.user.dao.UserDao;
|
129 |
| -import com.cloud.uservm.UserVm; |
130 | 130 | import com.cloud.utils.Journal;
|
131 | 131 | import com.cloud.utils.NumbersUtil;
|
132 | 132 | import com.cloud.utils.Pair;
|
@@ -1542,7 +1542,7 @@ public void run() {
|
1542 | 1542 | }
|
1543 | 1543 |
|
1544 | 1544 | @Override
|
1545 |
| - public boolean isVirtualMachineUpgradable(UserVm vm, ServiceOffering offering) { |
| 1545 | + public boolean isVirtualMachineUpgradable(VirtualMachine vm, ServiceOffering offering) { |
1546 | 1546 | Enumeration<HostAllocator> en = _hostAllocators.enumeration();
|
1547 | 1547 | boolean isMachineUpgradable = true;
|
1548 | 1548 | while (isMachineUpgradable && en.hasMoreElements()) {
|
@@ -2346,4 +2346,84 @@ public String getHostUuid() {
|
2346 | 2346 | public VMInstanceVO findById(long vmId) {
|
2347 | 2347 | return _vmDao.findById(vmId);
|
2348 | 2348 | }
|
| 2349 | + |
| 2350 | + @Override |
| 2351 | + public void checkIfCanUpgrade(VirtualMachine vmInstance, long newServiceOfferingId) { |
| 2352 | + ServiceOfferingVO newServiceOffering = _offeringDao.findById(newServiceOfferingId); |
| 2353 | + if (newServiceOffering == null) { |
| 2354 | + throw new InvalidParameterValueException("Unable to find a service offering with id " + newServiceOfferingId); |
| 2355 | + } |
| 2356 | + |
| 2357 | + // Check that the VM is stopped |
| 2358 | + if (!vmInstance.getState().equals(State.Stopped)) { |
| 2359 | + s_logger.warn("Unable to upgrade virtual machine " + vmInstance.toString() + " in state " + vmInstance.getState()); |
| 2360 | + throw new InvalidParameterValueException("Unable to upgrade virtual machine " + vmInstance.toString() + " " + |
| 2361 | + "in state " + vmInstance.getState() |
| 2362 | + + "; make sure the virtual machine is stopped and not in an error state before upgrading."); |
| 2363 | + } |
| 2364 | + |
| 2365 | + // Check if the service offering being upgraded to is what the VM is already running with |
| 2366 | + if (vmInstance.getServiceOfferingId() == newServiceOffering.getId()) { |
| 2367 | + if (s_logger.isInfoEnabled()) { |
| 2368 | + s_logger.info("Not upgrading vm " + vmInstance.toString() + " since it already has the requested " + |
| 2369 | + "service offering (" + newServiceOffering.getName() + ")"); |
| 2370 | + } |
| 2371 | + |
| 2372 | + throw new InvalidParameterValueException("Not upgrading vm " + vmInstance.toString() + " since it already " + |
| 2373 | + "has the requested service offering (" + newServiceOffering.getName() + ")"); |
| 2374 | + } |
| 2375 | + |
| 2376 | + ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId()); |
| 2377 | + |
| 2378 | + // Check that the service offering being upgraded to has the same Guest IP type as the VM's current service offering |
| 2379 | + // NOTE: With the new network refactoring in 2.2, we shouldn't need the check for same guest IP type anymore. |
| 2380 | + /* |
| 2381 | + * if (!currentServiceOffering.getGuestIpType().equals(newServiceOffering.getGuestIpType())) { String errorMsg = |
| 2382 | + * "The service offering being upgraded to has a guest IP type: " + newServiceOffering.getGuestIpType(); errorMsg += |
| 2383 | + * ". Please select a service offering with the same guest IP type as the VM's current service offering (" + |
| 2384 | + * currentServiceOffering.getGuestIpType() + ")."; throw new InvalidParameterValueException(errorMsg); } |
| 2385 | + */ |
| 2386 | + |
| 2387 | + // Check that the service offering being upgraded to has the same storage pool preference as the VM's current service |
| 2388 | + // offering |
| 2389 | + if (currentServiceOffering.getUseLocalStorage() != newServiceOffering.getUseLocalStorage()) { |
| 2390 | + throw new InvalidParameterValueException("Unable to upgrade virtual machine " + vmInstance.toString() |
| 2391 | + + ", cannot switch between local storage and shared storage service offerings. Current offering " + |
| 2392 | + "useLocalStorage=" + currentServiceOffering.getUseLocalStorage() |
| 2393 | + + ", target offering useLocalStorage=" + newServiceOffering.getUseLocalStorage()); |
| 2394 | + } |
| 2395 | + |
| 2396 | + // if vm is a system vm, check if it is a system service offering, if yes return with error as it cannot be used for user vms |
| 2397 | + if (currentServiceOffering.getSystemUse() != newServiceOffering.getSystemUse()) { |
| 2398 | + throw new InvalidParameterValueException("isSystem property is different for current service offering and new service offering"); |
| 2399 | + } |
| 2400 | + |
| 2401 | + // Check that there are enough resources to upgrade the service offering |
| 2402 | + if (!isVirtualMachineUpgradable(vmInstance, newServiceOffering)) { |
| 2403 | + throw new InvalidParameterValueException("Unable to upgrade virtual machine, not enough resources available " + |
| 2404 | + "for an offering of " + newServiceOffering.getCpu() + " cpu(s) at " |
| 2405 | + + newServiceOffering.getSpeed() + " Mhz, and " + newServiceOffering.getRamSize() + " MB of memory"); |
| 2406 | + } |
| 2407 | + |
| 2408 | + // Check that the service offering being upgraded to has all the tags of the current service offering |
| 2409 | + List<String> currentTags = _configMgr.csvTagsToList(currentServiceOffering.getTags()); |
| 2410 | + List<String> newTags = _configMgr.csvTagsToList(newServiceOffering.getTags()); |
| 2411 | + if (!newTags.containsAll(currentTags)) { |
| 2412 | + throw new InvalidParameterValueException("Unable to upgrade virtual machine; the new service offering " + |
| 2413 | + "does not have all the tags of the " |
| 2414 | + + "current service offering. Current service offering tags: " + currentTags + "; " + "new service " + |
| 2415 | + "offering tags: " + newTags); |
| 2416 | + } |
| 2417 | + } |
| 2418 | + |
| 2419 | + @Override |
| 2420 | + public boolean upgradeVmDb(long vmId, long serviceOfferingId) { |
| 2421 | + VMInstanceVO vmForUpdate = _vmDao.createForUpdate(); |
| 2422 | + vmForUpdate.setServiceOfferingId(serviceOfferingId); |
| 2423 | + ServiceOffering newSvcOff = _configMgr.getServiceOffering(serviceOfferingId); |
| 2424 | + vmForUpdate.setHaEnabled(newSvcOff.getOfferHA()); |
| 2425 | + vmForUpdate.setLimitCpuUse(newSvcOff.getLimitCpuUse()); |
| 2426 | + vmForUpdate.setServiceOfferingId(newSvcOff.getId()); |
| 2427 | + return _vmDao.update(vmId, vmForUpdate); |
| 2428 | + } |
2349 | 2429 | }
|
0 commit comments