1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2025-07-31 12:41:08 +00:00

reorg imports and black format

This commit is contained in:
catborise 2022-11-02 16:05:41 +03:00
parent fcd4b79431
commit a1eab70e2d
27 changed files with 319 additions and 305 deletions

View file

@ -1,5 +1,5 @@
import string
import contextlib
import string
from vrtManager import util
from vrtManager.connection import wvmConnect

View file

@ -1,27 +1,26 @@
import contextlib
import json
import os.path
import time
try:
from libvirt import (
libvirtError,
VIR_DOMAIN_XML_SECURE,
VIR_DOMAIN_RUNNING,
VIR_DOMAIN_AFFECT_LIVE,
VIR_DOMAIN_AFFECT_CONFIG,
)
from libvirt import (
VIR_MIGRATE_LIVE,
VIR_MIGRATE_UNSAFE,
VIR_MIGRATE_PERSIST_DEST,
VIR_MIGRATE_UNDEFINE_SOURCE,
VIR_MIGRATE_OFFLINE,
VIR_MIGRATE_COMPRESSED,
VIR_DOMAIN_AFFECT_LIVE,
VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT,
VIR_DOMAIN_RUNNING,
VIR_DOMAIN_XML_SECURE,
VIR_MIGRATE_AUTO_CONVERGE,
VIR_MIGRATE_COMPRESSED,
VIR_MIGRATE_LIVE,
VIR_MIGRATE_OFFLINE,
VIR_MIGRATE_PERSIST_DEST,
VIR_MIGRATE_POSTCOPY,
VIR_MIGRATE_UNDEFINE_SOURCE,
VIR_MIGRATE_UNSAFE,
libvirtError,
)
from libvirt import VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT
from libvirt_qemu import qemuAgentCommand, VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT
from libvirt_qemu import VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT, qemuAgentCommand
except:
from libvirt import libvirtError, VIR_DOMAIN_XML_SECURE, VIR_MIGRATE_LIVE
@ -496,26 +495,21 @@ class wvmInstance(wvmConnect):
s_name = disk.xpath("source/@pool")[0]
s = self.wvm.storagePoolLookupByName(s_name)
src_file = s.storageVolLookupByName(v).path()
try:
with contextlib.suppress(Exception):
disk_format = disk.xpath("driver/@type")[0]
except:
pass
try:
with contextlib.suppress(Exception):
disk_cache = disk.xpath("driver/@cache")[0]
except:
pass
try:
with contextlib.suppress(Exception):
disk_io = disk.xpath("driver/@io")[0]
except:
pass
try:
with contextlib.suppress(Exception):
disk_discard = disk.xpath("driver/@discard")[0]
except:
pass
try:
with contextlib.suppress(Exception):
disk_zeroes = disk.xpath("driver/@detect_zeroes")[0]
except:
pass
readonly = True if disk.xpath("readonly") else False
shareable = True if disk.xpath("shareable") else False
@ -572,15 +566,14 @@ class wvmInstance(wvmConnect):
try:
dev = media.xpath("target/@dev")[0]
bus = media.xpath("target/@bus")[0]
try:
src_file = None
volume = src_file
with contextlib.suppress(Exception):
src_file = media.xpath("source/@file")[0]
vol = self.get_volume_by_path(src_file)
volume = vol.name()
stg = vol.storagePoolLookupByVolume()
storage = stg.name()
except:
src_file = None
volume = src_file
except:
pass
finally:
@ -1083,11 +1076,10 @@ class wvmInstance(wvmConnect):
graphic.set("listen", listener_addr)
listen.set("address", listener_addr)
else:
try:
with contextlib.suppress(Exception):
graphic.attrib.pop("listen")
listen.attrib.pop("address")
except:
pass
newxml = ElementTree.tostring(root).decode()
return self._defineXML(newxml)
@ -1158,10 +1150,9 @@ class wvmInstance(wvmConnect):
if passwd:
graphic.set("passwd", passwd)
else:
try:
with contextlib.suppress(Exception):
graphic.attrib.pop("passwd")
except:
pass
newxml = ElementTree.tostring(root).decode()
return self._defineXML(newxml)
@ -1177,10 +1168,9 @@ class wvmInstance(wvmConnect):
if keymap != "auto":
graphic.set("keymap", keymap)
else:
try:
with contextlib.suppress(Exception):
graphic.attrib.pop("keymap")
except:
pass
newxml = ElementTree.tostring(root).decode()
self._defineXML(newxml)
@ -1279,10 +1269,9 @@ class wvmInstance(wvmConnect):
for storage in storages:
stg = self.get_storage(storage)
if stg.info()[0] != 0:
try:
with contextlib.suppress(Exception):
stg.refresh(0)
except:
pass
for img in stg.listVolumes():
if img.lower().endswith(".iso"):
iso.append(img)
@ -1424,10 +1413,10 @@ class wvmInstance(wvmConnect):
device_name = elm.get("dev")
if device_name:
target_file = clone_data["disk-" + device_name]
try:
meta_prealloc = False
with contextlib.suppress(Exception):
meta_prealloc = clone_data["meta-" + device_name]
except:
meta_prealloc = False
elm.set("dev", device_name)
elm = disk.find("source")
@ -1522,12 +1511,7 @@ class wvmInstance(wvmConnect):
return bridge_name
def add_network(
self,
mac_address,
source,
source_type="net",
model="virtio",
nwfilter=None
self, mac_address, source, source_type="net", model="virtio", nwfilter=None
):
if source_type == "net":
@ -1588,11 +1572,7 @@ class wvmInstance(wvmConnect):
status = self.delete_network(net_mac)
try:
self.add_network(
net_mac,
net_source,
net_source_type,
net_model,
net_filter
net_mac, net_source, net_source_type, net_model, net_filter
)
except libvirtError:
if status is not None:

View file

@ -1,17 +1,29 @@
from xml.etree import ElementTree
from libvirt import VIR_INTERFACE_XML_INACTIVE
from vrtManager import util
from vrtManager.connection import wvmConnect
class wvmInterfaces(wvmConnect):
def define_iface(self, xml, flag=0):
self.wvm.interfaceDefineXML(xml, flag)
def create_iface(
self, name, itype, mode, netdev, ipv4_type, ipv4_addr, ipv4_gw, ipv6_type, ipv6_addr, ipv6_gw, stp, delay
self,
name,
itype,
mode,
netdev,
ipv4_type,
ipv4_addr,
ipv4_gw,
ipv6_type,
ipv6_addr,
ipv6_gw,
stp,
delay,
):
xml = f"""<interface type='{itype}' name='{name}'>
<start mode='{mode}'/>"""
@ -74,15 +86,21 @@ class wvmInterface(wvmConnect):
def get_ipv4_type(self):
try:
xml = self._XMLDesc(VIR_INTERFACE_XML_INACTIVE)
ipaddr = util.get_xml_path(xml, "/interface/protocol[@family='ipv4']/ip/@address")
ipaddr = util.get_xml_path(
xml, "/interface/protocol[@family='ipv4']/ip/@address"
)
return "static" if ipaddr else "dhcp"
except Exception:
return None
def get_ipv4(self):
xml = self._XMLDesc()
int_ipv4_ip = util.get_xml_path(xml, "/interface/protocol[@family='ipv4']/ip/@address")
int_ipv4_mask = util.get_xml_path(xml, "/interface/protocol[@family='ipv4']/ip/@prefix")
int_ipv4_ip = util.get_xml_path(
xml, "/interface/protocol[@family='ipv4']/ip/@address"
)
int_ipv4_mask = util.get_xml_path(
xml, "/interface/protocol[@family='ipv4']/ip/@prefix"
)
if not int_ipv4_ip or not int_ipv4_mask:
return None
else:
@ -91,15 +109,21 @@ class wvmInterface(wvmConnect):
def get_ipv6_type(self):
try:
xml = self._XMLDesc(VIR_INTERFACE_XML_INACTIVE)
ipaddr = util.get_xml_path(xml, "/interface/protocol[@family='ipv6']/ip/@address")
ipaddr = util.get_xml_path(
xml, "/interface/protocol[@family='ipv6']/ip/@address"
)
return "static" if ipaddr else "dhcp"
except Exception:
return None
def get_ipv6(self):
xml = self._XMLDesc()
int_ipv6_ip = util.get_xml_path(xml, "/interface/protocol[@family='ipv6']/ip/@address")
int_ipv6_mask = util.get_xml_path(xml, "/interface/protocol[@family='ipv6']/ip/@prefix")
int_ipv6_ip = util.get_xml_path(
xml, "/interface/protocol[@family='ipv6']/ip/@address"
)
int_ipv6_mask = util.get_xml_path(
xml, "/interface/protocol[@family='ipv6']/ip/@prefix"
)
if not int_ipv6_ip or not int_ipv6_mask:
return None
else:
@ -133,7 +157,15 @@ class wvmInterface(wvmConnect):
mac = iface.find("mac")
if mac is not None:
address = mac.get("address")
ifaces.append({"name": name, "type": if_type, "state": state, "speed": speed, "mac": address})
ifaces.append(
{
"name": name,
"type": if_type,
"state": state,
"speed": speed,
"mac": address,
}
)
return ifaces
def get_details(self):

View file

@ -242,7 +242,7 @@ class wvmStorage(wvmConnect):
def refresh(self):
return self.pool.refresh(0)
def get_volume_details(self, volname):
def get_volumes_details(self):
with contextlib.suppress(Exception):
self.refresh()