mirror of
				https://github.com/retspen/webvirtcloud
				synced 2025-07-31 12:41:08 +00:00 
			
		
		
		
	instances.views.check_user_quota correct cpu/memory sum
instance resize respects quota
This commit is contained in:
		
							parent
							
								
									27030113fb
								
							
						
					
					
						commit
						8148620025
					
				
					 1 changed files with 27 additions and 14 deletions
				
			
		|  | @ -217,18 +217,26 @@ def instance(request, compute_id, vname): | |||
|             index += 1 | ||||
|         return free_names | ||||
| 
 | ||||
|     def check_user_quota(): | ||||
|         # TODO: count cpus, memory correctly | ||||
|         userinstances = UserInstance.objects.filter(user__id=request.user.id) | ||||
|         instances_count = len(userinstances) | ||||
|         cpus_count = instances_count | ||||
|         memory_count = instances_count * 2048 | ||||
|     def check_user_quota(instance, cpu, memory): | ||||
|         user_instances = UserInstance.objects.filter(user_id=request.user.id) | ||||
|         instance += len(user_instances) | ||||
|         for usr_inst in user_instances: | ||||
|             if connection_manager.host_is_up(usr_inst.instance.compute.type, | ||||
|                                              usr_inst.instance.compute.hostname): | ||||
|                 conn = wvmHostDetails(usr_inst.instance.compute, | ||||
|                                       usr_inst.instance.compute.login, | ||||
|                                       usr_inst.instance.compute.password, | ||||
|                                       usr_inst.instance.compute.type) | ||||
|                 wvm_instance = conn.get_user_instances(usr_inst.instance.name) | ||||
|                 cpu += int(wvm_instance['vcpu']) | ||||
|                 memory += int(wvm_instance['memory'])/1024 | ||||
|          | ||||
|         ua = request.user.userattributes | ||||
|         if ua.max_instances > 0 and instances_count > ua.max_instances: | ||||
|         if ua.max_instances > 0 and instance > ua.max_instances: | ||||
|             return "instance" | ||||
|         if ua.max_cpus > 0 and cpus_count > ua.max_cpus: | ||||
|         if ua.max_cpus > 0 and cpu > ua.max_cpus: | ||||
|             return "cpu" | ||||
|         if ua.max_memory > 0 and memory_count > ua.max_memory: | ||||
|         if ua.max_memory > 0 and memory > ua.max_memory: | ||||
|             return "memory" | ||||
|         return "" | ||||
| 
 | ||||
|  | @ -393,10 +401,15 @@ def instance(request, compute_id, vname): | |||
|                     if input_disk_size > disk['size']+(64<<20): | ||||
|                         disk['size_new'] = input_disk_size | ||||
|                         disks_new.append(disk)  | ||||
|                 conn.resize(cur_memory, memory, cur_vcpu, vcpu, disks_new) | ||||
|                 msg = _("Resize") | ||||
|                 addlogmsg(request.user.username, instance.name, msg) | ||||
|                 return HttpResponseRedirect(request.get_full_path() + '#resize') | ||||
|                 quota_msg = check_user_quota(0, vcpu, memory) | ||||
|                 if not request.user.is_superuser and quota_msg:     | ||||
|                     msg = _("User %s quota reached, cannot resize '%s'!" % (quota_msg, instance.name)) | ||||
|                     error_messages.append(msg) | ||||
|                 else: | ||||
|                     conn.resize(cur_memory, memory, cur_vcpu, vcpu, disks_new) | ||||
|                     msg = _("Resize") | ||||
|                     addlogmsg(request.user.username, instance.name, msg) | ||||
|                     return HttpResponseRedirect(request.get_full_path() + '#resize') | ||||
| 
 | ||||
|             if 'umount_iso' in request.POST: | ||||
|                 image = request.POST.get('path', '') | ||||
|  | @ -556,7 +569,7 @@ def instance(request, compute_id, vname): | |||
|                     clone_data = {} | ||||
|                     clone_data['name'] = request.POST.get('name', '') | ||||
| 
 | ||||
|                     quota_msg = check_user_quota() | ||||
|                     quota_msg = check_user_quota(1, conn.get_vcpu(), conn.get_memory()) | ||||
|                     check_instance = Instance.objects.filter(name=clone_data['name']) | ||||
|                      | ||||
|                     if not request.user.is_superuser and quota_msg:     | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue