Adding edit and screenshot

main
root 2024-03-13 12:27:08 +01:00
parent e6d087f148
commit b6ece56080
4 changed files with 97 additions and 7 deletions

44
app.py
View File

@ -16,6 +16,8 @@ from functions.fvm import *
from functions.fvnc import *
from functions.fbackup import *
from functions.fpool import *
from functions.fedit import *
from functions.fscreen import *
app = Flask(__name__,static_url_path='',static_folder='static',template_folder='templates')
@ -281,6 +283,39 @@ def deluser():
return redirect('param')
############################
## EDIT ##
############################
@app.route('/editvm',methods=['POST'])
@login_required
def edit():
vm_name = request.form['edit']
state, maxmem, mem, cpus, cput = get_info_vm(vm_name)
#Memory in MB
return render_template('edit.html', vm_name=vm_name, max_Mermory=conn.getInfo()[1],max_vCPU= conn.getMaxVcpus(None),actual_vCPU=cpus, actual_ram=int(mem/1024),screen_64=get_screenshot(vm_name).decode('ASCII'))
@app.route('/editressources',methods=['POST'])
@login_required
def editressources():
vm_name = request.form['vm_name']
new_ram = int(request.form['new_mem'])
new_cpu = int(request.form['new_cpu'])
print("vm:"+vm_name)
print("ram:"+str(new_ram))
print("vcpu:"+str(new_cpu))
try:
set_memory(vm_name,int(new_ram*1024))
except Exception as e:
flash('Error editing '+vm_name+': '+str(e), category='danger')
try:
set_vcpu(vm_name,new_cpu)
except Exception as e:
flash('Error editing '+vm_name+': '+str(e), category='danger')
return redirect(url_for('state'))
############################
## ISO ##
############################
@ -397,7 +432,6 @@ def restsnap_lxc():
flash('Error restoring '+item+':'+str(e), category='danger')
return redirect(url_for('backup'))
###LAPIN
@app.route('/snapvm', methods = ['POST'])
@login_required
def snapvm():
@ -420,20 +454,18 @@ def delsnapvm():
except Exception as e:
flash('Error on deleting '+item+':'+str(e), category='danger')
return redirect(url_for('backup'))
"""
@app.route('/rest_snap_vm', methods = ['POST'])
@login_required
def restsnap_lxc():
def restsnap_vm():
vm_name=request.form['lxc_name']
item=request.form['item']
try:
rest_snap_lxc(lxc_name,item)
rest_snap_vm(vm_name,item)
flash('Restore '+item+' done', category='success')
except Exception as e:
flash('Error restoring '+item+':'+str(e), category='danger')
return redirect(url_for('backup'))
"""
##PINE LA
############################
## BUILD ##

View File

@ -0,0 +1,20 @@
from config import *
import base64
def get_screenshot(vm_name):
dom = conn.lookupByName(vm_name)
stream = conn.newStream()
imageType = dom.screenshot(stream,0)
file = "tmp_screen_" + dom.name()
fileHandler = open(file, 'wb')
streamBytes = stream.recv(262120)
while streamBytes != b'':
fileHandler.write(streamBytes)
streamBytes = stream.recv(262120)
fileHandler.close()
print('Screenshot saved as type: ' + imageType)
stream.finish()
with open(file, "rb") as f:
data = base64.b64encode(f.read())
os.remove(file)
return data

View File

@ -0,0 +1,33 @@
{% extends 'layout.html' %}
{% block content %}
{% include 'menu.html' %}
<div class="container">
<div class="card">
<div class="card-header">
Ressources of {{ vm_name }}
</div>
<div class="card-body">
<div class="row">
<div class="col-sm">
<img src="data:image/png;base64,{{ screen_64 }}" class="img-thumbnail">
</div>
<div class="col-sm">
Vcpu : {{ actual_vCPU }}</br>
Memory: {{ actual_ram }} MB</br>
</div>
<div class="col-sm">
<form action="/editressources" method="post">
<input type="hidden" name="vm_name" value="{{ vm_name }}"></input>
<input type="text" name="new_cpu" placeholder="{{ actual_vCPU }}" value="{{ actual_vCPU }}"></input> (Max : {{ max_vCPU }})<br>
<input type="text" name="new_mem" placeholder="{{ actual_ram }}" value="{{ actual_ram }}" ></input> MB (Max : {{ max_Mermory }} MB)<br><br>
<button type="submit" class="btn btn-outline-warning btn-hype">Set New Values</button>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -62,6 +62,10 @@
</td><td>
<form action="/snapvm" method="post"><button type="submit" class="btn btn-outline-secondary btn-hype disabled" value="{{ act_vm[0] }}" name="snap" onclick="loading();"><i class="fa-solid fa-box-archive"></i></button></form>
</td><td>
<form action="/editvm" method="post"><button type="submit" class="btn btn-outline-warning btn-hype" value="{{ act_vm[0] }}" name="edit" onclick="loading();"><i class="fa-solid fa-wrench"></i></button></form>
</td><td>
{% if act_vm[1] == 1 %}
<table class="table table-borderless"><tr><th>
<i class="fa-solid fa-compact-disc"></i>
@ -93,7 +97,8 @@
<form action="/destroy_vm" method="post"><button type="submit" class="btn btn-outline-danger btn-hype" value="{{ inact_vm[0] }}" name="destroy" onclick="loading();"><i class="fa fa-trash" aria-hidden="true"></i></button></form>
</td><td>
<form action="/snapvm" method="post"><button type="submit" class="btn btn-outline-secondary btn-hype" value="{{ inact_vm[0] }}" name="snap" onclick="loading();"><i class="fa-solid fa-box-archive"></i></button></form>
</td><td>
<form action="/editvm" method="post"><button type="submit" class="btn btn-outline-warning btn-hype disabled" value="{{ inact_vm[0] }}" name="edit" onclick="loading();"><i class="fa-solid fa-wrench"></i></button></form>
</td><td>
{% if inact_vm[1] == 1 %}
<table class="table table-borderless"><tr><th>