From e47f97facf81b7bbae4314a0fe8da9d2fe079a47 Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 27 Aug 2019 17:18:33 +0300 Subject: [PATCH] instance: seperate resize operations as cpu, mem and disk --- instances/templates/instance.html | 67 +++++++++++++++++++++++++------ instances/views.py | 63 +++++++++++++++++++++++++++++ vrtManager/instance.py | 53 +++++++++++++++++++++++- 3 files changed, 169 insertions(+), 14 deletions(-) diff --git a/instances/templates/instance.html b/instances/templates/instance.html index 3391569..ece29d4 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -341,17 +341,27 @@
- - - + + +
-
+
{% if request.user.is_superuser or request.user.is_staff or userinstance.is_change %}
{% csrf_token %}

{% trans "Logical host CPUs" %} : {{ vcpu_host }}

@@ -379,6 +389,22 @@
+ + {% ifequal status 5 %} + + {% else %} + + {% endifequal %} + + {% else %} + {% trans "You don't have permission for resizing instance" %} + + {% endif %} +
+
+
+ {% if request.user.is_superuser or request.user.is_staff or userinstance.is_change %} +
{% csrf_token %}

{% trans "Total host memory:" %} {{ memory_host|filesizeformat }}

@@ -406,6 +432,21 @@ {% trans "Custom value" %}
+ {% ifequal status 5 %} + + {% else %} + + {% endifequal %} + + {% else %} + {% trans "You don't have permission for resizing instance" %} + + {% endif %} +
+
+
+ {% if request.user.is_superuser or request.user.is_staff or userinstance.is_change %} +
{% csrf_token %}

{% trans "Disk allocation (B):" %}

{% for disk in disks %}
@@ -416,9 +457,9 @@
{% endfor %} {% ifequal status 5 %} - + {% else %} - + {% endifequal %}
{% else %} @@ -1867,7 +1908,7 @@ } }); } - if (~$.inArray(hash, ['#resize'])) { + if (~$.inArray(hash, ['#resize', "resizevm_cpu", "resizevm_mem", "resizevm_disk"])) { var btnsect = $('#navbtn>li>a'); $(btnsect).each(function () { if ($(this).attr('href') === '#resize') { diff --git a/instances/views.py b/instances/views.py index b0ecaba..5f8c55a 100644 --- a/instances/views.py +++ b/instances/views.py @@ -460,6 +460,69 @@ def instance(request, compute_id, vname): addlogmsg(request.user.username, instance.name, msg) return HttpResponseRedirect(request.get_full_path() + '#resize') + if 'resizevm_cpu' in request.POST and ( + request.user.is_superuser or request.user.is_staff or userinstance.is_change): + new_vcpu = request.POST.get('vcpu', '') + new_cur_vcpu = request.POST.get('cur_vcpu', '') + + quota_msg = check_user_quota(0, int(new_vcpu) - vcpu, 0, 0) + if not request.user.is_superuser and quota_msg: + msg = _("User %s quota reached, cannot resize CPU of '%s'!" % (quota_msg, instance.name)) + error_messages.append(msg) + else: + cur_vcpu = new_cur_vcpu + vcpu = new_vcpu + conn.resize_cpu(cur_vcpu, vcpu) + msg = _("Resize CPU") + addlogmsg(request.user.username, instance.name, msg) + return HttpResponseRedirect(request.get_full_path() + '#resize') + + if 'resizevm_mem' in request.POST and ( + request.user.is_superuser or request.user.is_staff or userinstance.is_change): + new_memory = request.POST.get('memory', '') + new_memory_custom = request.POST.get('memory_custom', '') + if new_memory_custom: + new_memory = new_memory_custom + new_cur_memory = request.POST.get('cur_memory', '') + new_cur_memory_custom = request.POST.get('cur_memory_custom', '') + if new_cur_memory_custom: + new_cur_memory = new_cur_memory_custom + quota_msg = check_user_quota(0, 0, int(new_memory) - memory, 0) + if not request.user.is_superuser and quota_msg: + msg = _("User %s quota reached, cannot resize memory of '%s'!" % (quota_msg, instance.name)) + error_messages.append(msg) + else: + cur_memory = new_cur_memory + memory = new_memory + conn.resize_mem(cur_memory, memory) + msg = _("Resize Memory") + addlogmsg(request.user.username, instance.name, msg) + return HttpResponseRedirect(request.get_full_path() + '#resize') + + if 'resizevm_disk' in request.POST and ( + request.user.is_superuser or request.user.is_staff or userinstance.is_change): + disks_new = [] + for disk in disks: + input_disk_size = filesizefstr(request.POST.get('disk_size_' + disk['dev'], '')) + if input_disk_size > disk['size'] + (64 << 20): + disk['size_new'] = input_disk_size + disks_new.append(disk) + disk_sum = sum([disk['size'] >> 30 for disk in disks_new]) + disk_new_sum = sum([disk['size_new'] >> 30 for disk in disks_new]) + quota_msg = check_user_quota(0, 0, 0, disk_new_sum - disk_sum) + if not request.user.is_superuser and quota_msg: + msg = _("User %s quota reached, cannot resize disks of '%s'!" % (quota_msg, instance.name)) + error_messages.append(msg) + else: + cur_memory = new_cur_memory + memory = new_memory + cur_vcpu = new_cur_vcpu + vcpu = new_vcpu + 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 'add_new_vol' in request.POST and allow_admin_or_not_template: connCreate = wvmCreate(compute.hostname, compute.login, diff --git a/vrtManager/instance.py b/vrtManager/instance.py index b8f29f8..3286f9d 100644 --- a/vrtManager/instance.py +++ b/vrtManager/instance.py @@ -741,7 +741,6 @@ class wvmInstance(wvmConnect): """ Function change ram and cpu on vds. """ - memory = int(memory) * 1024 cur_memory = int(cur_memory) * 1024 # if dom is running change only ram @@ -769,6 +768,58 @@ class wvmInstance(wvmConnect): new_xml = ElementTree.tostring(tree) self._defineXML(new_xml) + def resize_cpu(self, cur_vcpu, vcpu): + """ + Function change ram and cpu on vds. + """ + xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) + tree = ElementTree.fromstring(xml) + + set_vcpu = tree.find('vcpu') + set_vcpu.text = vcpu + set_vcpu.set('current', cur_vcpu) + + new_xml = ElementTree.tostring(tree) + self._defineXML(new_xml) + + def resize_mem(self, cur_memory, memory): + """ + Function change ram and cpu on vds. + """ + memory = int(memory) * 1024 + cur_memory = int(cur_memory) * 1024 + # if dom is running change only ram + if self.get_status() == VIR_DOMAIN_RUNNING: + self.set_memory(cur_memory, VIR_DOMAIN_AFFECT_LIVE) + self.set_memory(cur_memory, VIR_DOMAIN_AFFECT_CONFIG) + return + + xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) + tree = ElementTree.fromstring(xml) + + set_mem = tree.find('memory') + set_mem.text = str(memory) + set_cur_mem = tree.find('currentMemory') + set_cur_mem.text = str(cur_memory) + + new_xml = ElementTree.tostring(tree) + self._defineXML(new_xml) + + def resize_disk(self, disks=[]): + """ + Function change disks on vds. + """ + xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE) + tree = ElementTree.fromstring(xml) + + for disk in disks: + source_dev = disk['path'] + vol = self.get_volume_by_path(source_dev) + vol.resize(disk['size_new']) + + new_xml = ElementTree.tostring(tree) + self._defineXML(new_xml) + def get_iso_media(self): iso = [] storages = self.get_storages(only_actives=True)