@@ -763,14 +763,10 @@ private UserVm rebootVirtualMachine(long userId, long vmId)
763763 /*
764764 * TODO: cleanup eventually - Refactored API call
765765 */
766+ // This method will be deprecated as we use ScaleVMCmd for both stopped VMs and running VMs
766767 public UserVm upgradeVirtualMachine (UpgradeVMCmd cmd ) throws ResourceAllocationException {
767768 Long vmId = cmd .getId ();
768769 Long svcOffId = cmd .getServiceOfferingId ();
769- return upgradeStoppedVirtualMachine (vmId , svcOffId );
770- }
771-
772-
773- private UserVm upgradeStoppedVirtualMachine (Long vmId , Long svcOffId ) throws ResourceAllocationException {
774770 Account caller = UserContext .current ().getCaller ();
775771
776772 // Verify input parameters
@@ -836,6 +832,70 @@ private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId) throws Res
836832 vmInstance .getServiceOfferingId (), vmInstance .getTemplateId (), vmInstance .getHypervisorType ().toString (), VirtualMachine .class .getName (), vmInstance .getUuid ());
837833
838834 return _vmDao .findById (vmInstance .getId ());
835+ }
836+
837+ private UserVm upgradeStoppedVirtualMachine (Long vmId , Long svcOffId ) throws ResourceAllocationException {
838+ Account caller = UserContext .current ().getCaller ();
839+
840+ // Verify input parameters
841+ //UserVmVO vmInstance = _vmDao.findById(vmId);
842+ VMInstanceVO vmInstance = _vmInstanceDao .findById (vmId );
843+ if (vmInstance == null ) {
844+ throw new InvalidParameterValueException (
845+ "unable to find a virtual machine with id " + vmId );
846+ }
847+
848+ _accountMgr .checkAccess (caller , null , true , vmInstance );
849+
850+ // Check resource limits for CPU and Memory.
851+ ServiceOfferingVO newServiceOffering = _offeringDao .findById (svcOffId );
852+ ServiceOfferingVO currentServiceOffering = _offeringDao .findByIdIncludingRemoved (vmInstance .getServiceOfferingId ());
853+
854+ int newCpu = newServiceOffering .getCpu ();
855+ int newMemory = newServiceOffering .getRamSize ();
856+ int currentCpu = currentServiceOffering .getCpu ();
857+ int currentMemory = currentServiceOffering .getRamSize ();
858+
859+ if (newCpu > currentCpu ) {
860+ _resourceLimitMgr .checkResourceLimit (caller , ResourceType .cpu ,
861+ newCpu - currentCpu );
862+ }
863+ if (newMemory > currentMemory ) {
864+ _resourceLimitMgr .checkResourceLimit (caller , ResourceType .memory ,
865+ newMemory - currentMemory );
866+ }
867+
868+ // Check that the specified service offering ID is valid
869+ _itMgr .checkIfCanUpgrade (vmInstance , svcOffId );
870+
871+ // remove diskAndMemory VM snapshots
872+ List <VMSnapshotVO > vmSnapshots = _vmSnapshotDao .findByVm (vmId );
873+ for (VMSnapshotVO vmSnapshotVO : vmSnapshots ) {
874+ if (vmSnapshotVO .getType () == VMSnapshot .Type .DiskAndMemory ){
875+ if (!_vmSnapshotMgr .deleteAllVMSnapshots (vmId , VMSnapshot .Type .DiskAndMemory )){
876+ String errMsg = "Failed to remove VM snapshot during upgrading, snapshot id " + vmSnapshotVO .getId ();
877+ s_logger .debug (errMsg );
878+ throw new CloudRuntimeException (errMsg );
879+ }
880+
881+ }
882+ }
883+
884+ _itMgr .upgradeVmDb (vmId , svcOffId );
885+
886+ // Increment or decrement CPU and Memory count accordingly.
887+ if (newCpu > currentCpu ) {
888+ _resourceLimitMgr .incrementResourceCount (caller .getAccountId (), ResourceType .cpu , new Long (newCpu - currentCpu ));
889+ } else if (currentCpu > newCpu ) {
890+ _resourceLimitMgr .decrementResourceCount (caller .getAccountId (), ResourceType .cpu , new Long (currentCpu - newCpu ));
891+ }
892+ if (newMemory > currentMemory ) {
893+ _resourceLimitMgr .incrementResourceCount (caller .getAccountId (), ResourceType .memory , new Long (newMemory - currentMemory ));
894+ } else if (currentMemory > newMemory ) {
895+ _resourceLimitMgr .decrementResourceCount (caller .getAccountId (), ResourceType .memory , new Long (currentMemory - newMemory ));
896+ }
897+
898+ return _vmDao .findById (vmInstance .getId ());
839899
840900 }
841901
@@ -1088,17 +1148,30 @@ public UserVm updateDefaultNicForVirtualMachine(UpdateDefaultNicForVMCmd cmd) th
10881148 }
10891149
10901150 @ Override
1091- @ ActionEvent (eventType = EventTypes .EVENT_VM_SCALE , eventDescription = "scaling Vm" )
1151+ @ ActionEvent (eventType = EventTypes .EVENT_VM_UPGRADE , eventDescription = "Upgrading VM" , async = true )
10921152 public UserVm
10931153 upgradeVirtualMachine (ScaleVMCmd cmd ) throws ResourceUnavailableException , ConcurrentOperationException , ManagementServerException , VirtualMachineMigrationException {
10941154
10951155 Long vmId = cmd .getId ();
10961156 Long newServiceOfferingId = cmd .getServiceOfferingId ();
1157+ UserContext .current ().setEventDetails ("Vm Id: " + vmId );
1158+
10971159 boolean result = upgradeVirtualMachine (vmId , newServiceOfferingId );
10981160 if (result ){
1099- return _vmDao .findById (vmId );
1100- }else {
1101- return null ;
1161+ UserVmVO vmInstance = _vmDao .findById (vmId );
1162+ if (vmInstance .getState ().equals (State .Stopped )){
1163+ // Generate usage event for VM upgrade
1164+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_UPGRADE , vmInstance .getAccountId (), vmInstance .getDataCenterId (), vmInstance .getId (), vmInstance .getHostName (),
1165+ vmInstance .getServiceOfferingId (), vmInstance .getTemplateId (), vmInstance .getHypervisorType ().toString (), VirtualMachine .class .getName (), vmInstance .getUuid ());
1166+ }
1167+ if (vmInstance .getState ().equals (State .Running )){
1168+ // Generate usage event for Dynamic scaling of VM
1169+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_DYNAMIC_SCALE , vmInstance .getAccountId (), vmInstance .getDataCenterId (), vmInstance .getId (), vmInstance .getHostName (),
1170+ vmInstance .getServiceOfferingId (), vmInstance .getTemplateId (), vmInstance .getHypervisorType ().toString (), VirtualMachine .class .getName (), vmInstance .getUuid ());
1171+ }
1172+ return vmInstance ;
1173+ } else {
1174+ throw new CloudRuntimeException ("Failed to scale the VM" );
11021175 }
11031176
11041177 }
@@ -1140,7 +1213,6 @@ public HashMap<Long, List<VmDiskStatsEntry>> getVmDiskStatistics(long hostId, St
11401213
11411214 @ Override
11421215 public boolean upgradeVirtualMachine (Long vmId , Long newServiceOfferingId ) throws ResourceUnavailableException , ConcurrentOperationException , ManagementServerException , VirtualMachineMigrationException {
1143- Account caller = UserContext .current ().getCaller ();
11441216
11451217 // Verify input parameters
11461218 VMInstanceVO vmInstance = _vmInstanceDao .findById (vmId );
@@ -1149,7 +1221,16 @@ public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId) throw
11491221 upgradeStoppedVirtualMachine (vmId , newServiceOfferingId );
11501222 return true ;
11511223 }
1224+ if (vmInstance .getState ().equals (State .Running )){
1225+ return upgradeRunningVirtualMachine (vmId , newServiceOfferingId );
1226+ }
1227+ return false ;
1228+ }
11521229
1230+ private boolean upgradeRunningVirtualMachine (Long vmId , Long newServiceOfferingId ) throws ResourceUnavailableException , ConcurrentOperationException , ManagementServerException , VirtualMachineMigrationException {
1231+
1232+ Account caller = UserContext .current ().getCaller ();
1233+ VMInstanceVO vmInstance = _vmInstanceDao .findById (vmId );
11531234 if (vmInstance .getHypervisorType () != HypervisorType .XenServer && vmInstance .getHypervisorType () != HypervisorType .VMware ){
11541235 throw new InvalidParameterValueException ("This operation not permitted for this hypervisor of the vm" );
11551236 }
@@ -1247,13 +1328,11 @@ public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId) throw
12471328 // Decrement CPU and Memory count accordingly.
12481329 if (newCpu > currentCpu ) {
12491330 _resourceLimitMgr .decrementResourceCount (caller .getAccountId (), ResourceType .cpu , new Long (newCpu - currentCpu ));
1250- }
1331+ }
12511332 if (newMemory > currentMemory ) {
12521333 _resourceLimitMgr .decrementResourceCount (caller .getAccountId (), ResourceType .memory , new Long (newMemory - currentMemory ));
12531334 }
12541335 }
1255-
1256-
12571336 }
12581337 }
12591338 }
0 commit comments