Adding Network info and edition

This commit is contained in:
root
2024-04-11 11:06:52 +02:00
parent 843555a9d5
commit e2fcc56ce0
18 changed files with 170 additions and 198 deletions
+30 -10
View File
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+22 -3
View File
@@ -1,4 +1,5 @@
from config import *
from functions.fnet import *
import base64
import time
import lxc
@@ -65,7 +66,18 @@ def get_info_vm(vm_name):
state, maxmem, mem, cpus, cput = dom.info()
return state, maxmem, mem, cpus, cput
#Screenshot
def attach_net(vm_name,net_name):
dom = conn.lookupByName(vm_name)
network_xml = f'<interface type="network"><source network="{net_name}"/><model type="virtio"/></interface>'
dom.attachDeviceFlags(network_xml, libvirt.VIR_DOMAIN_AFFECT_CONFIG | libvirt.VIR_DOMAIN_AFFECT_LIVE)
def detach_net(vm_name, net_name, net_mac,net_int):
dom = conn.lookupByName(vm_name)
# network_xml = f'<interface type="network"><mac address="{net_mac}"/><source network="{net_name}"/><model type="virtio"/></interface>'
network_xml = f'<interface type="bridge"><mac address="{net_mac}"/><source network="{net_name}" bridge="{net_int}"/></interface>'
print(network_xml)
dom.detachDeviceFlags(network_xml, libvirt.VIR_DOMAIN_AFFECT_CONFIG | libvirt.VIR_DOMAIN_AFFECT_LIVE)
def get_screenshot(vm_name):
dom = conn.lookupByName(vm_name)
stream = conn.newStream()
@@ -83,8 +95,6 @@ def get_screenshot(vm_name):
os.remove(file)
return data
#Autostart
def get_autostart(vm_name):
dom = conn.lookupByName(vm_name)
return dom.autostart()
@@ -97,3 +107,12 @@ def unset_autostart(vm_name):
dom = conn.lookupByName(vm_name)
dom.setAutostart(0)
"""
def get_net_vm(vm_name):
netws, nu2 = get_virt_int()
vm_net_list=[]
for net in netws:
if vm_name in net[1][0]:
vm_net_list.append(net[0][0][0])
return vm_net_list
"""
-99
View File
@@ -1,99 +0,0 @@
from config import *
import base64
import time
import lxc
def get_version():
vlxc = lxc.version
vlibvirt = str(libvirt.getVersion()/1000000) #1000000 * major + 1000 * minor + release
vhype = hype_version
return vlxc, vlibvirt, vhype
def set_memory(vm_name,memory_new):
dom = conn.lookupByName(vm_name)
dom.shutdown()
time.sleep(3)
alive=0
while alive < 5:
if dom.isActive():
time.sleep(3)
alive+=1
else:
alive=6
if dom.isActive():
dom.destroy()
dom.setMaxMemory(memory_new)
dom.setMemoryFlags(memory_new)
dom.create()
time.sleep(3)
alive=0
while alive < 3:
if dom.isActive():
alive=4
else:
time.sleep(3)
alive+=1
def set_vcpu(vm_name,vcpu_new):
dom = conn.lookupByName(vm_name)
dom.shutdown()
time.sleep(3)
alive=0
while alive < 5:
if dom.isActive():
time.sleep(3)
alive+=1
else:
alive=6
if dom.isActive():
dom.destroy()
dom.setVcpusFlags(vcpu_new,libvirt.VIR_DOMAIN_AFFECT_CONFIG | libvirt.VIR_DOMAIN_VCPU_MAXIMUM)
dom.setVcpusFlags(vcpu_new,libvirt.VIR_DOMAIN_AFFECT_CONFIG | libvirt.VIR_DOMAIN_VCPU_CURRENT)
dom.create()
time.sleep(3)
alive=0
while alive < 3:
if dom.isActive():
alive=4
else:
time.sleep(3)
alive+=1
def get_info_vm(vm_name):
dom = conn.lookupByName(vm_name)
state, maxmem, mem, cpus, cput = dom.info()
return state, maxmem, mem, cpus, cput
#Screenshot
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()
stream.finish()
with open(file, "rb") as f:
data = base64.b64encode(f.read())
os.remove(file)
return data
#Autostart
def get_autostart(vm_name):
dom = conn.lookupByName(vm_name)
return dom.autostart()
def set_autostart(vm_name):
dom = conn.lookupByName(vm_name)
dom.setAutostart(1)
def unset_autostart(vm_name):
dom = conn.lookupByName(vm_name)
dom.setAutostart(0)
+70 -25
View File
@@ -2,6 +2,35 @@ from app import *
import ipaddress
from xml.dom import minidom
def ip_fom_mac(vm_name,net_mac):
dom=conn.lookupByName(vm_name)
mac_ip=[]
try:
ifaces = dom.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT)
except:
ifaces = None
result="---.---.---.---"
if ifaces is None:
result="---.---.---.---"
else:
for (name, val) in ifaces.items():
if val['addrs']:
for addr in val['addrs']:
if addr['addr']:
mac_ip.append([val['hwaddr'],addr['addr']])
break
else:
result = "---.---.---.---"
for int in mac_ip:
if int[0] == net_mac:
result=int[1]
else:
result = "---.---.---.---"
return result
def diff_net(min_dhcp,max_dhcp):
ip1 = ipaddress.IPv4Address(min_dhcp)
ip2 = ipaddress.IPv4Address(max_dhcp)
@@ -43,28 +72,44 @@ def create_network(net_name,net_int):
net_use.create()
net_use.setAutostart(True)
def list_network_VM():
net_tree = []
net_na = []
#Host Interfaces
net_hard_int = conn.listInterfaces()
#Virtual Interfaces
net_list = conn.listAllNetworks()
for net_name in net_list:
if net_name.isActive():
net_sub_tree=[]
net_sub_tree.append(net_name.name())
net_use = conn.networkLookupByName(net_name.name())
ports = net_use.listAllPorts()
net_ports = []
for port in ports:
raw_xml = port.XMLDesc()
net_xml = minidom.parseString(raw_xml)
net_element = net_xml.documentElement
net_VM = net_element.getElementsByTagName("name")[0]
net_ports.append(net_VM.firstChild.data)
net_sub_tree.append(net_ports)
net_tree.append(net_sub_tree)
else:
net_na.append(net_name.name())
return net_hard_int,net_tree,net_na
def get_mac_net(vm_name,net_name):
dom = conn.lookupByName(vm_name)
raw_xml = dom.XMLDesc()
net_xml = minidom.parseString(raw_xml)
VM_mac = []
for device in net_xml.getElementsByTagName('devices'):
ifaces = device.getElementsByTagName('interface')
for iface in ifaces:
sources = iface.getElementsByTagName('source')
for source in sources:
if source.getAttribute('network') == net_name:
mac = iface.getElementsByTagName('mac')
target = iface.getElementsByTagName('target')
ip = ip_fom_mac(vm_name,str(mac[0].getAttribute('address')))
VM_mac.append([target[0].getAttribute('dev'),mac[0].getAttribute('address'),ip])
return VM_mac
def get_virt_int():
net_na = []
out=[]
for network in conn.listAllNetworks():
if network.isActive():
int_name=[]
int_mac = conn.interfaceLookupByName(network.bridgeName())
int_name.append([network.name(),network.bridgeName(),int_mac.MACString()])
net_detail = conn.networkLookupByName(network.name())
net_vms = net_detail.listAllPorts()
vm_name=[]
for vm in net_vms:
net_VM_info = []
raw_xml = vm.XMLDesc()
net_xml = minidom.parseString(raw_xml)
net_element = net_xml.documentElement
net_VM = net_element.getElementsByTagName("name")[0]
net_VM_info = [net_VM.firstChild.data,get_mac_net(net_VM.firstChild.data,network.name())]
if net_VM_info not in vm_name:
vm_name.append(net_VM_info)
out.append([int_name,vm_name])
else:
net_na.append(network.name())
return out, net_na
+25
View File
@@ -109,6 +109,31 @@
</div>
</div>
<br>
<div class="card">
<div class="card-header">
Networking for {{ vm_name }}
</div>
<div class="card-body">
Attach new network<hr>
<div class="row">
<div class="col-sm">
<form action="/addnetvm" method="post">
<input type="hidden" name="vm_name" value="{{ vm_name }}">
<label for="new_net_vm">Network</label>
<select id="new_net_vm" class="form-control" name="new_net_vm" placeholder="Profile" required>
{%for net in list_net %}
<option value={{ net }}>{{ net }}</option>
{%endfor%}
</select><br>
<button type="submit" class="btn btn-outline-info btn-hype"><i class="fa-solid fa-plus"></i> Add</button>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
+23 -5
View File
@@ -29,15 +29,33 @@ Virtual Interfaces:
{% endfor %}
{% for item in net_tree %}
<li class="list-group-item bg-info text-dark">
<strong><i class="fa-solid fa-ethernet"></i> {{ item[0] }}</strong>
<table class="table table-sm text-dark table-borderless"><tr><td>
<strong><i class="fa-solid fa-network-wired"></i> {{ item[0][0][0] }} - <i class="fa-solid fa-ethernet"></i> {{ item[0][0][1] }} - {{ item[0][0][2] }}</strong>
</td><td>
<form action="/delnet" method="post">
<input type="hidden" id="net_del" name="net_del" class="form-control" value="{{ item[0] }}">
<button name="delete" class="btn btn-outline-secondary" type="submit" onclick="loading();">Delete</button>
</form><br>
<input type="hidden" id="net_del" name="net_del" class="form-control" value="{{ item[0][0][0] }}">
<button name="delete" class="btn btn-outline-danger btn-sm" type="submit" onclick="loading();"><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>
</td></tr></table>
<ul class="list-group">
{% for sub_item in item[1] %}
<li class="list-group-item bg-warning text-dark">
<i class="fa-solid fa-desktop"></i> {{ sub_item }}
<p class="text-start"><i class="fa-solid fa-desktop"></i> {{ sub_item[0] }}<br></p>
{% for pitem in sub_item[1] %}
<table class="table table-sm text-dark table-responsive table-borderless"><tr><td>
<center><i class="fa-solid fa-ethernet"></i> {{ pitem[0] }} - <i class="fa-solid fa-at"></i> {{ pitem[1] }} - {{ pitem[2] }}<br></center>
</td><td>
</td><td>
<form action="/delnetvm" method="post">
<input type="hidden" id="vm_name" name="vm_name" class="form-control" value="{{ sub_item[0] }}">
<input type="hidden" id="del_net_vm" name="del_net_vm" class="form-control" value="{{ item[0][0][0] }}">
<input type="hidden" id="del_mac_vm" name="del_mac_vm" class="form-control" value="{{ pitem[1] }}">
<input type="hidden" id="del_net_int" name="del_net_int" class="form-control" value="{{ item[0][0][1] }}">
<button name="delete" class="btn btn-outline-danger btn-sm" type="submit" onclick="loading();"><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>
</td></tr></table>
{% endfor %}
</li>
{% endfor %}
</ul>
-56
View File
@@ -1,56 +0,0 @@
{% extends 'layout.html' %}
{% block content %}
{% include 'menu.html' %}
<!--
<div class="container">
<div class="row justify-content-md-center">
<div class="col">
<ul class="list-group">
<li class="list-group-item bg-primary text-dark">
Physical interface
<center>
{% for pint in net_hard_int %}
<strong><i class="fa-solid fa-ethernet"></i> {{ pint }}</strong><br>
{% endfor %}
</center></li>
{% for item in net_tree %}
<li class="list-group-item bg-info text-dark">
<center><strong><i class="fa-solid fa-ethernet"></i> {{ item[0] }}</strong></center><br>
<ul class="list-group">
{% for sub_item in item[1] %}
<li class="list-group-item bg-warning text-dark">
<center><i class="fa-solid fa-desktop"></i> {{ sub_item }}</center>
</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
-->
<div class="container">
<div class="row justify-content-md-center">
<div class="col">
{% for pint in net_hard_int %}
<strong><i class="fa-solid fa-ethernet"></i> {{ pint }}</strong><br>
{% endfor %}
</div>
</div>
<div class="row justify-content-md-center">
<div class="col">
<div class="mermaid">
graph TD
{% for item in net_tree %}
root[Root] --- {{ item[0] }}[{{ item[0] }}]
{% for sub_item in item[1] %}
{{ item[0] }}[{{ item[0] }}] --- {{ sub_item }}[{{ sub_item }}]
{% endfor %}
{% endfor %}
</div>
</div>
</div>
</div>
<script>mermaid.initialize({startOnLoad:true});</script>
{% endblock %}