Skip to content

Commit e434494

Browse files
committed
Add suppor edit VM (RAM and VCPU)
1 parent 6d5fa98 commit e434494

File tree

6 files changed

+120
-69
lines changed

6 files changed

+120
-69
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# WebVirtMgr panel - v2.3
1+
# WebVirtMgr panel - v2.3.2
22

33
* Minor improvements (Change codebase)
44
* Add support set VNC password (auto or manual) when VM created
5+
* Add support edit VM (only RAM and VCPU)
56

67
## 1. Introduction
78

newvm/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def newvm(request, host_id):
4242
all_networks = conn.networks_get_node()
4343
all_storages = conn.storages_get_node()
4444
all_img = conn.images_get_storages(all_storages)
45+
vcpu_range = [x for x in range(1, 9)]
46+
hdd_digits_size = [a for a in range(1, 601)]
47+
memory_range = ['128', '256', '512', '768', '1024', '2048', '4096', '8192', '16384']
4548

4649
if not all_networks:
4750
msg = _("You haven't defined any virtual networks")
@@ -50,7 +53,7 @@ def newvm(request, host_id):
5053
msg = _("You haven't defined have any storage pools")
5154
errors.append(msg)
5255

53-
hdd_digits_size = [a for a in range(1, 601)]
56+
5457

5558
if request.method == 'POST':
5659
if 'add_flavor' in request.POST:

templates/newvm.html

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,30 +133,19 @@ <h3 id="poolModalLabel">{% trans "Add New Virtual Machine" %}</h3>
133133
<label class="control-label">{% trans "VCPU" %}</label>
134134
<div class="controls">
135135
<select name="vcpu">
136-
<option value="1">1</option>
137-
<option value="2">2</option>
138-
<option value="3">3</option>
139-
<option value="4">4</option>
140-
<option value="5">5</option>
141-
<option value="6">6</option>
142-
<option value="7">7</option>
143-
<option value="8">8</option>
136+
{% for cpu in vcpu_range %}
137+
<option value="{{ cpu }}">{{ cpu }}</option>
138+
{% endfor %}
144139
</select>
145140
</div>
146141
</div>
147142
<div class="control-group">
148143
<label class="control-label">{% trans "RAM" %}</label>
149144
<div class="controls">
150145
<select name="ram">
151-
<option value="128">128 {% trans "MB" %}</option>
152-
<option value="256">256 {% trans "MB" %}</option>
153-
<option value="512">512 {% trans "MB" %}</option>
154-
<option value="768">768 {% trans "MB" %}</option>
155-
<option value="1024">1024 {% trans "MB" %}</option>
156-
<option value="2048">2048 {% trans "MB" %}</option>
157-
<option value="4096">4096 {% trans "MB" %}</option>
158-
<option value="8192">8192 {% trans "MB" %}</option>
159-
<option value="16384">16384 {% trans "MB" %}</option>
146+
{% for mem in memory_range %}
147+
<option value="{{ mem }}">{{ mem }} {% trans "MB" %}</option>
148+
{% endfor %}
160149
</select>
161150
</div>
162151
</div>
@@ -221,30 +210,19 @@ <h3 id="poolModalLabel">{% trans "Add New Flavor" %}</h3>
221210
<label class="control-label">{% trans "VCPU" %}</label>
222211
<div class="controls">
223212
<select name="vcpu">
224-
<option value="1">1</option>
225-
<option value="2">2</option>
226-
<option value="3">3</option>
227-
<option value="4">4</option>
228-
<option value="5">5</option>
229-
<option value="6">6</option>
230-
<option value="7">7</option>
231-
<option value="8">8</option>
213+
{% for cpu in vcpu_range %}
214+
<option value="{{ cpu }}">{{ cpu }}</option>
215+
{% endfor %}
232216
</select>
233217
</div>
234218
</div>
235219
<div class="control-group">
236220
<label class="control-label">{% trans "RAM" %}</label>
237221
<div class="controls">
238222
<select name="ram">
239-
<option value="128">128 {% trans "MB" %}</option>
240-
<option value="256">256 {% trans "MB" %}</option>
241-
<option value="512">512 {% trans "MB" %}</option>
242-
<option value="768">768 {% trans "MB" %}</option>
243-
<option value="1024">1024 {% trans "GB" %}</option>
244-
<option value="2048">2048 {% trans "GB" %}</option>
245-
<option value="4096">4096 {% trans "GB" %}</option>
246-
<option value="8192">8192 {% trans "GB" %}</option>
247-
<option value="16384">16384 {% trans "GB" %}</option>
223+
{% for mem in memory_range %}
224+
<option value="{{ mem }}">{{ mem }} {% trans "MB" %}</option>
225+
{% endfor %}
248226
</select>
249227
</div>
250228
</div>

templates/vds.html

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h2>{% trans "Virtual Machine" %}</h2>
2828
</div>
2929
{% endfor %}
3030
{% endif %}
31-
<div class="span9">
31+
<div class="span11">
3232
<ul>
3333
<li>{% trans "Action For This Server" %}</li><br>
3434
<form action="" method="post">{% csrf_token %}
@@ -59,6 +59,11 @@ <h2>{% trans "Virtual Machine" %}</h2>
5959
<a role="button" class="btn disabled" style="height:40px;width:60px;"><i class="icon-download-alt"></i><br>{% trans "Suspend" %}</a>
6060
<button type="submit" name="snapshot" role="button" class="btn" style="height:50px;width:80px;"><i class="icon-download-alt"></i><br>{% trans "Snapshot" %}</button>
6161
{% endifequal %}
62+
{% ifequal dom.info.0 5 %}
63+
<a href="#editModal" role="button" class="btn" data-toggle="modal" style="height:40px;width:60px;"><i class="icon-pencil"></i><br>{% trans "Edit" %}</a>
64+
{% else %}
65+
<a href="#" role="button" class="btn disabled" style="height:40px;width:60px;"><i class="icon-pencil"></i><br>{% trans "Edit" %}</a>
66+
{% endifequal %}
6267
</form>
6368
{% if not vm.vnc_passwd and dom.info.0 == 5 %}
6469
<a href="#noVNCModal" role="button" class="btn" data-toggle="modal">{% trans "Enable noVNC" %}</a>
@@ -70,7 +75,7 @@ <h2>{% trans "Virtual Machine" %}</h2>
7075
<p><b>{% trans "Name" %}:</b></p>
7176
<p><b>{% trans "Status" %}:</b></p>
7277
</div>
73-
<div class="span6">
78+
<div class="span7">
7479
<p>{{ vname }}</p>
7580
<p>{% if dom.info %}
7681
<font color={% ifequal dom.info.0 5 %}"red">{% trans "Shuttoff" %}{% endifequal %}{% ifequal dom.info.0 1 %}"green">{% trans "Running" %}{% endifequal %}{% ifequal dom.info.0 3 %}"orange">{% trans "Suspend" %}{% endifequal %}</font>
@@ -88,10 +93,10 @@ <h2>{% trans "Virtual Machine" %}</h2>
8893
<p><b>{% trans "Memory" %}:</b></p>
8994
<p><b>{% trans "Network" %}:</b></p>
9095
</div>
91-
<div class="span6">
92-
<p>VCPU {{ dom_info.0 }} ({% trans "Usage" %}: {{ cpu_usage }}%)</p>
93-
<p>{{ dom_info.1 }} {% trans "MB" %} ({% trans "Usage" %}: {{ mem_usage.1 }}%)</p>
94-
<p>{{ dom_info.2 }} ({{ dom_info.3 }})</p>
96+
<div class="span7">
97+
<p>VCPU {{ vcpu }} ({% trans "Usage" %}: {{ cpu_usage }}%)</p>
98+
<p>{{ memory }} {% trans "MB" %} ({% trans "Usage" %}: {{ memory_usage }}%)</p>
99+
<p>{{ mac }} ({{ network }})</p>
95100
</div>
96101
</div>
97102
<hr>
@@ -102,7 +107,7 @@ <h2>{% trans "Virtual Machine" %}</h2>
102107
<p><b>{% trans "Disk" %} {{ dev_bus }}:</b></p>
103108
{% endfor %}
104109
</div>
105-
<div class="span6">
110+
<div class="span7">
106111
{% for dev_bus, hdd_dev in hdd_image.items reversed %}
107112
<p> {{ hdd_dev.0 }} ({{ hdd_dev.1 }})</p>
108113
{% endfor %}
@@ -148,6 +153,7 @@ <h2>{% trans "Virtual Machine" %}</h2>
148153
</ul>
149154
</div> <!-- /span7 -->
150155

156+
151157
<div id="powerModal" class="modal hide fade">
152158
<div class="modal-header">
153159
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
@@ -169,11 +175,11 @@ <h3 id="rebootModalLabel">{% trans "Power On Server" %}</h3>
169175
</fieldset>
170176
</div>
171177
<div class="modal-footer">
172-
<button class="btn" data-dismiss="modal">{% trans "Cancel" %}</button>
173-
<input class="btn btn-primary" type="submit" value="{% trans "Send" %}">
178+
<button class="btn" data-dismiss="modal">{% trans "Cancel" %}</button>
179+
<input class="btn btn-primary" type="submit" value="{% trans "Send" %}">
174180
</form>
175181
</div>
176-
</div> <!-- /powerModal -->
182+
</div> <!-- /powerModal -->
177183

178184
<div id="deleteModal" class="modal hide fade">
179185
<div class="modal-header">
@@ -189,13 +195,13 @@ <h3 id="deleteModalLabel">{% trans "Undefined Server" %}</h3>
189195
<input type="checkbox" name="image" value="true">{% trans "First device" %}</label>
190196
</div>
191197
</fieldset>
192-
</div>
193-
<div class="modal-footer">
198+
</div>
199+
<div class="modal-footer">
194200
<button class="btn" data-dismiss="modal">{% trans "Cancel" %}</button>
195201
<input class="btn btn-danger" type="submit" name="delete" value="{% trans "Delete" %}">
196-
</form>
197-
</div>
198-
</div> <!-- /deleteModal -->
202+
</form>
203+
</div>
204+
</div> <!-- /deleteModal -->
199205

200206
<div id="noVNCModal" class="modal hide fade">
201207
<div class="modal-header">
@@ -219,13 +225,51 @@ <h3 id="deleteModalLabel">{% trans "Set VNC Password" %}</h3>
219225
</div>
220226
</div>
221227
</fieldset>
222-
</div>
223-
<div class="modal-footer">
228+
</div>
229+
<div class="modal-footer">
224230
<button class="btn" data-dismiss="modal">{% trans "Cancel" %}</button>
225231
<input class="btn btn-success" type="submit" name="vnc_pass" value="{% trans "Create" %}">
226-
</form>
232+
</form>
233+
</div>
234+
</div> <!-- /noVNCModal -->
235+
236+
<div id="editModal" class="modal hide fade">
237+
<div class="modal-header">
238+
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
239+
<h3 id="deleteModalLabel">{% trans "Edit Server" %}</h3>
227240
</div>
228-
</div> <!-- /noVNCModal -->
241+
<div class="modal-body">
242+
<form class="form-horizontal" action="" method="post">{% csrf_token %}
243+
<fieldset>
244+
<p>{% trans "Change VCPU or RAM" %}</p><br />
245+
<div class="control-group">
246+
<label class="control-label">{% trans "VCPU" %}</label>
247+
<div class="controls">
248+
<select name="vcpu">
249+
{% for cpu in vcpu_range %}
250+
<option value="{{ cpu }}" {% if cpu == vcpu %}selected{% endif %}>{{ cpu }}</option>
251+
{% endfor %}
252+
</select>
253+
</div>
254+
</div>
255+
<div class="control-group">
256+
<label class="control-label">{% trans "RAM" %}</label>
257+
<div class="controls">
258+
<select name="ram">
259+
{% for mem in memory_range %}
260+
<option value="{{ mem }}" {% if mem == memory %}selected{% endif %}>{{ mem }} {% trans "MB" %}</option>
261+
{% endfor %}
262+
</select>
263+
</div>
264+
</div>
265+
</fieldset>
266+
</div>
267+
<div class="modal-footer">
268+
<button class="btn" data-dismiss="modal">{% trans "Cancel" %}</button>
269+
<input class="btn btn-success" type="submit" name="edit" value="{% trans "Change" %}">
270+
</form>
271+
</div>
272+
</div> <!-- /editModal -->
229273

230274
</div><!--/span-->
231275
</div><!--/row-fluid-->

vds/views.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ def vds(request, host_id, vname):
3232
errors.append(e.message)
3333
else:
3434
all_vm = conn.vds_get_node()
35-
dom_info = conn.vds_get_info(vname)
35+
vcpu, memory, mac, network = conn.vds_get_info(vname)
3636
cpu_usage = conn.vds_cpu_usage(vname)
37-
mem_usage = conn.vds_memory_usage(vname)
37+
memory_usage = conn.vds_memory_usage(vname)[1]
3838
hdd_image = conn.vds_get_hdd(vname)
3939
iso_images = sorted(conn.get_all_media())
40-
media = conn.vds_get_media(vname)
40+
media = conn.vds_get_media(vname)
4141
dom = conn.lookupVM(vname)
42+
vcpu_range = [str(x) for x in range(1, 9)]
43+
memory_range = [128, 256, 512, 768, 1024, 2048, 4096, 8192, 16384]
4244

4345
try:
4446
vm = Vm.objects.get(vname=vname)
@@ -103,26 +105,30 @@ def vds(request, host_id, vname):
103105
image = request.POST.get('iso_img', '')
104106
try:
105107
conn.vds_umount_iso(vname, image)
106-
try:
107-
if vm.vnc_passwd:
108-
conn.vds_set_vnc_passwd(vname, vm.vnc_passwd)
109-
except:
110-
pass
108+
if vm:
109+
conn.vds_set_vnc_passwd(vname, vm.vnc_passwd)
111110
return HttpResponseRedirect(request.get_full_path())
112111
except libvirtError as msg_error:
113112
errors.append(msg_error.message)
114113
if 'add_iso' in request.POST:
115114
image = request.POST.get('iso_img', '')
116115
try:
117116
conn.vds_mount_iso(vname, image)
118-
try:
119-
if vm.vnc_passwd:
120-
conn.vds_set_vnc_passwd(vname, vm.vnc_passwd)
121-
except:
122-
pass
117+
if vm:
118+
conn.vds_set_vnc_passwd(vname, vm.vnc_passwd)
123119
return HttpResponseRedirect(request.get_full_path())
124120
except libvirtError as msg_error:
125121
errors.append(msg_error.message)
122+
if 'edit' in request.POST:
123+
vcpu = request.POST.get('vcpu', '')
124+
ram = request.POST.get('ram', '')
125+
try:
126+
conn.vds_edit(vname, ram, vcpu)
127+
if vm:
128+
conn.vds_set_vnc_passwd(vname, vm.vnc_passwd)
129+
return HttpResponseRedirect(request.get_full_path())
130+
except libvirtError as msg_error:
131+
errors.append(msg_error.message)
126132
if 'vnc_pass' in request.POST:
127133
if request.POST.get('auto_pass', ''):
128134
from string import letters, digits

webvirtmgr/server.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ def node_get_info(self):
292292
"/sysinfo/processor/entry[6]"))
293293
except:
294294
info.append('Unknown')
295-
info.append(self.conn.getLibVersion())
296295
info.append(self.conn.getURI())
296+
info.append(self.conn.getLibVersion())
297297
return info
298298

299299

@@ -856,6 +856,25 @@ def vds_set_vnc_passwd(self, vname, passwd):
856856
self.conn.defineXML(xmldom)
857857

858858

859+
def vds_edit(self, vname, ram, vcpu):
860+
"""
861+
862+
Function change ram and cpu on vds.
863+
864+
"""
865+
866+
dom = self.lookupVM(vname)
867+
xml = dom.XMLDesc(0)
868+
memory = int(ram) * 1024
869+
xml_memory = "<memory unit='KiB'>%s</memory>" % memory
870+
xml_memory_chage = re.sub('\<memory.*memory\>', xml_memory, xml)
871+
xml_curmemory = "<currentMemory unit='KiB'>%s</currentMemory>" % memory
872+
xml_curmemory_chage = re.sub('\<currentMemory.*currentMemory\>', xml_curmemory, xml_memory_chage)
873+
xml_vcpu = "<vcpu>%s</vcpu>" % vcpu
874+
xml_vcpu_change = re.sub('\<vcpu.*vcpu\>', xml_vcpu, xml_curmemory_chage)
875+
self.conn.defineXML(xml_vcpu_change)
876+
877+
859878
def get_all_media(self):
860879
"""
861880

0 commit comments

Comments
 (0)