@@ -758,14 +758,10 @@ private UserVm rebootVirtualMachine(long userId, long vmId)
758758 /*
759759 * TODO: cleanup eventually - Refactored API call
760760 */
761+ // This method will be deprecated as we use ScaleVMCmd for both stopped VMs and running VMs
761762 public UserVm upgradeVirtualMachine (UpgradeVMCmd cmd ) throws ResourceAllocationException {
762763 Long vmId = cmd .getId ();
763764 Long svcOffId = cmd .getServiceOfferingId ();
764- return upgradeStoppedVirtualMachine (vmId , svcOffId );
765- }
766-
767-
768- private UserVm upgradeStoppedVirtualMachine (Long vmId , Long svcOffId ) throws ResourceAllocationException {
769765 Account caller = CallContext .current ().getCallingAccount ();
770766
771767 // Verify input parameters
@@ -831,6 +827,70 @@ private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId) throws Res
831827 vmInstance .getServiceOfferingId (), vmInstance .getTemplateId (), vmInstance .getHypervisorType ().toString (), VirtualMachine .class .getName (), vmInstance .getUuid ());
832828
833829 return _vmDao .findById (vmInstance .getId ());
830+ }
831+
832+ private UserVm upgradeStoppedVirtualMachine (Long vmId , Long svcOffId ) throws ResourceAllocationException {
833+ Account caller = CallContext .current ().getCallingAccount ();
834+
835+ // Verify input parameters
836+ //UserVmVO vmInstance = _vmDao.findById(vmId);
837+ VMInstanceVO vmInstance = _vmInstanceDao .findById (vmId );
838+ if (vmInstance == null ) {
839+ throw new InvalidParameterValueException (
840+ "unable to find a virtual machine with id " + vmId );
841+ }
842+
843+ _accountMgr .checkAccess (caller , null , true , vmInstance );
844+
845+ // Check resource limits for CPU and Memory.
846+ ServiceOfferingVO newServiceOffering = _offeringDao .findById (svcOffId );
847+ ServiceOfferingVO currentServiceOffering = _offeringDao .findByIdIncludingRemoved (vmInstance .getServiceOfferingId ());
848+
849+ int newCpu = newServiceOffering .getCpu ();
850+ int newMemory = newServiceOffering .getRamSize ();
851+ int currentCpu = currentServiceOffering .getCpu ();
852+ int currentMemory = currentServiceOffering .getRamSize ();
853+
854+ if (newCpu > currentCpu ) {
855+ _resourceLimitMgr .checkResourceLimit (caller , ResourceType .cpu ,
856+ newCpu - currentCpu );
857+ }
858+ if (newMemory > currentMemory ) {
859+ _resourceLimitMgr .checkResourceLimit (caller , ResourceType .memory ,
860+ newMemory - currentMemory );
861+ }
862+
863+ // Check that the specified service offering ID is valid
864+ _itMgr .checkIfCanUpgrade (vmInstance , svcOffId );
865+
866+ // remove diskAndMemory VM snapshots
867+ List <VMSnapshotVO > vmSnapshots = _vmSnapshotDao .findByVm (vmId );
868+ for (VMSnapshotVO vmSnapshotVO : vmSnapshots ) {
869+ if (vmSnapshotVO .getType () == VMSnapshot .Type .DiskAndMemory ){
870+ if (!_vmSnapshotMgr .deleteAllVMSnapshots (vmId , VMSnapshot .Type .DiskAndMemory )){
871+ String errMsg = "Failed to remove VM snapshot during upgrading, snapshot id " + vmSnapshotVO .getId ();
872+ s_logger .debug (errMsg );
873+ throw new CloudRuntimeException (errMsg );
874+ }
875+
876+ }
877+ }
878+
879+ _itMgr .upgradeVmDb (vmId , svcOffId );
880+
881+ // Increment or decrement CPU and Memory count accordingly.
882+ if (newCpu > currentCpu ) {
883+ _resourceLimitMgr .incrementResourceCount (caller .getAccountId (), ResourceType .cpu , new Long (newCpu - currentCpu ));
884+ } else if (currentCpu > newCpu ) {
885+ _resourceLimitMgr .decrementResourceCount (caller .getAccountId (), ResourceType .cpu , new Long (currentCpu - newCpu ));
886+ }
887+ if (newMemory > currentMemory ) {
888+ _resourceLimitMgr .incrementResourceCount (caller .getAccountId (), ResourceType .memory , new Long (newMemory - currentMemory ));
889+ } else if (currentMemory > newMemory ) {
890+ _resourceLimitMgr .decrementResourceCount (caller .getAccountId (), ResourceType .memory , new Long (currentMemory - newMemory ));
891+ }
892+
893+ return _vmDao .findById (vmInstance .getId ());
834894
835895 }
836896
@@ -1083,17 +1143,30 @@ public UserVm updateDefaultNicForVirtualMachine(UpdateDefaultNicForVMCmd cmd) th
10831143 }
10841144
10851145 @ Override
1086- @ ActionEvent (eventType = EventTypes .EVENT_VM_SCALE , eventDescription = "scaling Vm" )
1146+ @ ActionEvent (eventType = EventTypes .EVENT_VM_UPGRADE , eventDescription = "Upgrading VM" , async = true )
10871147 public UserVm
10881148 upgradeVirtualMachine (ScaleVMCmd cmd ) throws ResourceUnavailableException , ConcurrentOperationException , ManagementServerException , VirtualMachineMigrationException {
10891149
10901150 Long vmId = cmd .getId ();
10911151 Long newServiceOfferingId = cmd .getServiceOfferingId ();
1152+ CallContext .current ().setEventDetails ("Vm Id: " + vmId );
1153+
10921154 boolean result = upgradeVirtualMachine (vmId , newServiceOfferingId );
10931155 if (result ){
1094- return _vmDao .findById (vmId );
1095- }else {
1096- return null ;
1156+ UserVmVO vmInstance = _vmDao .findById (vmId );
1157+ if (vmInstance .getState ().equals (State .Stopped )){
1158+ // Generate usage event for VM upgrade
1159+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_UPGRADE , vmInstance .getAccountId (), vmInstance .getDataCenterId (), vmInstance .getId (), vmInstance .getHostName (),
1160+ vmInstance .getServiceOfferingId (), vmInstance .getTemplateId (), vmInstance .getHypervisorType ().toString (), VirtualMachine .class .getName (), vmInstance .getUuid ());
1161+ }
1162+ if (vmInstance .getState ().equals (State .Running )){
1163+ // Generate usage event for Dynamic scaling of VM
1164+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_DYNAMIC_SCALE , vmInstance .getAccountId (), vmInstance .getDataCenterId (), vmInstance .getId (), vmInstance .getHostName (),
1165+ vmInstance .getServiceOfferingId (), vmInstance .getTemplateId (), vmInstance .getHypervisorType ().toString (), VirtualMachine .class .getName (), vmInstance .getUuid ());
1166+ }
1167+ return vmInstance ;
1168+ } else {
1169+ throw new CloudRuntimeException ("Failed to scale the VM" );
10971170 }
10981171
10991172 }
@@ -1135,7 +1208,6 @@ public HashMap<Long, List<VmDiskStatsEntry>> getVmDiskStatistics(long hostId, St
11351208
11361209 @ Override
11371210 public boolean upgradeVirtualMachine (Long vmId , Long newServiceOfferingId ) throws ResourceUnavailableException , ConcurrentOperationException , ManagementServerException , VirtualMachineMigrationException {
1138- Account caller = CallContext .current ().getCallingAccount ();
11391211
11401212 // Verify input parameters
11411213 VMInstanceVO vmInstance = _vmInstanceDao .findById (vmId );
@@ -1144,7 +1216,16 @@ public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId) throw
11441216 upgradeStoppedVirtualMachine (vmId , newServiceOfferingId );
11451217 return true ;
11461218 }
1219+ if (vmInstance .getState ().equals (State .Running )){
1220+ return upgradeRunningVirtualMachine (vmId , newServiceOfferingId );
1221+ }
1222+ return false ;
1223+ }
11471224
1225+ private boolean upgradeRunningVirtualMachine (Long vmId , Long newServiceOfferingId ) throws ResourceUnavailableException , ConcurrentOperationException , ManagementServerException , VirtualMachineMigrationException {
1226+
1227+ Account caller = CallContext .current ().getCallingAccount ();
1228+ VMInstanceVO vmInstance = _vmInstanceDao .findById (vmId );
11481229 if (vmInstance .getHypervisorType () != HypervisorType .XenServer && vmInstance .getHypervisorType () != HypervisorType .VMware ){
11491230 throw new InvalidParameterValueException ("This operation not permitted for this hypervisor of the vm" );
11501231 }
@@ -1237,13 +1318,11 @@ public boolean upgradeVirtualMachine(Long vmId, Long newServiceOfferingId) throw
12371318 // Decrement CPU and Memory count accordingly.
12381319 if (newCpu > currentCpu ) {
12391320 _resourceLimitMgr .decrementResourceCount (caller .getAccountId (), ResourceType .cpu , new Long (newCpu - currentCpu ));
1240- }
1321+ }
12411322 if (newMemory > currentMemory ) {
12421323 _resourceLimitMgr .decrementResourceCount (caller .getAccountId (), ResourceType .memory , new Long (newMemory - currentMemory ));
12431324 }
12441325 }
1245-
1246-
12471326 }
12481327 }
12491328 }
0 commit comments