mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
Use black with -l 120 param.
This commit is contained in:
1 parent
f928738bd5
commit
c021e21309
194 files changed
+6034
-4564
No files matched your search
@@ -27,16 +27,12 @@ from gns3server.utils import parse_version
|
||||
from gns3server.utils.http_client import HTTPClient
|
||||
from gns3server.utils.asyncio import wait_run_in_executor
|
||||
|
||||
from ...compute.virtualbox import (
|
||||
VirtualBox,
|
||||
VirtualBoxError
|
||||
)
|
||||
from ...compute.virtualbox import VirtualBox, VirtualBoxError
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
|
||||
def __init__(self, controller):
|
||||
|
||||
self._engine = "virtualbox"
|
||||
@@ -48,7 +44,7 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
|
||||
try:
|
||||
result = await self._virtualbox_manager.execute(subcommand, args, timeout)
|
||||
return ("\n".join(result))
|
||||
return "\n".join(result)
|
||||
except VirtualBoxError as e:
|
||||
raise GNS3VMError(f"Error while executing VBoxManage command: {e}")
|
||||
|
||||
@@ -61,8 +57,8 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
|
||||
result = await self._execute("showvminfo", [self._vmname, "--machinereadable"])
|
||||
for info in result.splitlines():
|
||||
if '=' in info:
|
||||
name, value = info.split('=', 1)
|
||||
if "=" in info:
|
||||
name, value = info.split("=", 1)
|
||||
if name == "VMState":
|
||||
return value.strip('"')
|
||||
return "unknown"
|
||||
@@ -77,7 +73,7 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
properties = await self._execute("list", ["systemproperties"])
|
||||
for prop in properties.splitlines():
|
||||
try:
|
||||
name, value = prop.split(':', 1)
|
||||
name, value = prop.split(":", 1)
|
||||
except ValueError:
|
||||
continue
|
||||
self._system_properties[name.strip()] = value.strip()
|
||||
@@ -92,14 +88,19 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
if "API version" not in self._system_properties:
|
||||
raise VirtualBoxError(f"Can't access to VirtualBox API version:\n{self._system_properties}")
|
||||
from cpuinfo import get_cpu_info
|
||||
|
||||
cpu_info = await wait_run_in_executor(get_cpu_info)
|
||||
vendor_id = cpu_info.get('vendor_id_raw')
|
||||
vendor_id = cpu_info.get("vendor_id_raw")
|
||||
if vendor_id == "GenuineIntel":
|
||||
if parse_version(self._system_properties["API version"]) < parse_version("6_1"):
|
||||
raise VirtualBoxError("VirtualBox version 6.1 or above is required to run the GNS3 VM with nested virtualization enabled on Intel processors")
|
||||
raise VirtualBoxError(
|
||||
"VirtualBox version 6.1 or above is required to run the GNS3 VM with nested virtualization enabled on Intel processors"
|
||||
)
|
||||
elif vendor_id == "AuthenticAMD":
|
||||
if parse_version(self._system_properties["API version"]) < parse_version("6_0"):
|
||||
raise VirtualBoxError("VirtualBox version 6.0 or above is required to run the GNS3 VM with nested virtualization enabled on AMD processors")
|
||||
raise VirtualBoxError(
|
||||
"VirtualBox version 6.0 or above is required to run the GNS3 VM with nested virtualization enabled on AMD processors"
|
||||
)
|
||||
else:
|
||||
log.warning(f"Could not determine CPU vendor: {vendor_id}")
|
||||
|
||||
@@ -113,8 +114,8 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
result = await self._execute("showvminfo", [self._vmname, "--machinereadable"])
|
||||
interface = -1
|
||||
for info in result.splitlines():
|
||||
if '=' in info:
|
||||
name, value = info.split('=', 1)
|
||||
if "=" in info:
|
||||
name, value = info.split("=", 1)
|
||||
if name.startswith("nic") and value.strip('"') == network_backend:
|
||||
try:
|
||||
interface = int(name[3:])
|
||||
@@ -132,8 +133,8 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
|
||||
result = await self._execute("showvminfo", [self._vmname, "--machinereadable"])
|
||||
for info in result.splitlines():
|
||||
if '=' in info:
|
||||
name, value = info.split('=', 1)
|
||||
if "=" in info:
|
||||
name, value = info.split("=", 1)
|
||||
if name == f"hostonlyadapter{interface_number}":
|
||||
return value.strip('"')
|
||||
return None
|
||||
@@ -150,7 +151,7 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
flag_dhcp_server_found = False
|
||||
for prop in properties.splitlines():
|
||||
try:
|
||||
name, value = prop.split(':', 1)
|
||||
name, value = prop.split(":", 1)
|
||||
except ValueError:
|
||||
continue
|
||||
if name.strip() == "NetworkName" and value.strip().endswith(vboxnet):
|
||||
@@ -171,7 +172,7 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
properties = await self._execute("list", ["hostonlyifs"])
|
||||
for prop in properties.splitlines():
|
||||
try:
|
||||
name, value = prop.split(':', 1)
|
||||
name, value = prop.split(":", 1)
|
||||
except ValueError:
|
||||
continue
|
||||
if name.strip() == "Name" and value.strip() == vboxnet:
|
||||
@@ -186,7 +187,7 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
properties = await self._execute("list", ["hostonlyifs"])
|
||||
for prop in properties.splitlines():
|
||||
try:
|
||||
name, value = prop.split(':', 1)
|
||||
name, value = prop.split(":", 1)
|
||||
except ValueError:
|
||||
continue
|
||||
if name.strip() == "Name":
|
||||
@@ -202,8 +203,8 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
|
||||
result = await self._execute("showvminfo", [self._vmname, "--machinereadable"])
|
||||
for info in result.splitlines():
|
||||
if '=' in info:
|
||||
name, value = info.split('=', 1)
|
||||
if "=" in info:
|
||||
name, value = info.split("=", 1)
|
||||
if name.startswith("Forwarding") and value.strip('"').startswith("GNS3VM"):
|
||||
return True
|
||||
return False
|
||||
@@ -237,7 +238,9 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
|
||||
vboxnet = await self._look_for_vboxnet(hostonly_interface_number)
|
||||
if vboxnet is None:
|
||||
raise GNS3VMError(f'A VirtualBox host-only network could not be found on network adapter {hostonly_interface_number} for "{self._vmname}"')
|
||||
raise GNS3VMError(
|
||||
f'A VirtualBox host-only network could not be found on network adapter {hostonly_interface_number} for "{self._vmname}"'
|
||||
)
|
||||
|
||||
if not (await self._check_vboxnet_exists(vboxnet)):
|
||||
if sys.platform.startswith("win") and vboxnet == "vboxnet0":
|
||||
@@ -245,13 +248,17 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
# on Windows. Try to patch this with the first available vboxnet we find.
|
||||
first_available_vboxnet = await self._find_first_available_vboxnet()
|
||||
if first_available_vboxnet is None:
|
||||
raise GNS3VMError(f'Please add a VirtualBox host-only network with DHCP enabled and attached it to network adapter {hostonly_interface_number} for "{self._vmname}"')
|
||||
raise GNS3VMError(
|
||||
f'Please add a VirtualBox host-only network with DHCP enabled and attached it to network adapter {hostonly_interface_number} for "{self._vmname}"'
|
||||
)
|
||||
await self.set_hostonly_network(hostonly_interface_number, first_available_vboxnet)
|
||||
vboxnet = first_available_vboxnet
|
||||
else:
|
||||
raise GNS3VMError('VirtualBox host-only network "{}" does not exist, please make the sure the network adapter {} configuration is valid for "{}"'.format(vboxnet,
|
||||
hostonly_interface_number,
|
||||
self._vmname))
|
||||
raise GNS3VMError(
|
||||
'VirtualBox host-only network "{}" does not exist, please make the sure the network adapter {} configuration is valid for "{}"'.format(
|
||||
vboxnet, hostonly_interface_number, self._vmname
|
||||
)
|
||||
)
|
||||
|
||||
if not (await self._check_dhcp_server(vboxnet)):
|
||||
raise GNS3VMError(f'DHCP must be enabled on VirtualBox host-only network "{vboxnet}"')
|
||||
@@ -287,15 +294,17 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
except OSError as e:
|
||||
raise GNS3VMError(f"Error while getting random port: {e}")
|
||||
|
||||
if (await self._check_vbox_port_forwarding()):
|
||||
if await self._check_vbox_port_forwarding():
|
||||
# delete the GNS3VM NAT port forwarding rule if it exists
|
||||
log.info(f"Removing GNS3VM NAT port forwarding rule from interface {nat_interface_number}")
|
||||
await self._execute("controlvm", [self._vmname, f"natpf{nat_interface_number}", "delete", "GNS3VM"])
|
||||
|
||||
# add a GNS3VM NAT port forwarding rule to redirect 127.0.0.1 with random port to the port in the VM
|
||||
log.info(f"Adding GNS3VM NAT port forwarding rule with port {api_port} to interface {nat_interface_number}")
|
||||
await self._execute("controlvm", [self._vmname, f"natpf{nat_interface_number}",
|
||||
f"GNS3VM,tcp,{ip_address},{api_port},,{self.port}"])
|
||||
await self._execute(
|
||||
"controlvm",
|
||||
[self._vmname, f"natpf{nat_interface_number}", f"GNS3VM,tcp,{ip_address},{api_port},,{self.port}"],
|
||||
)
|
||||
|
||||
self.ip_address = await self._get_ip(hostonly_interface_number, api_port)
|
||||
log.info(f"GNS3 VM has been started with IP {self.ip_address}")
|
||||
@@ -320,7 +329,8 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
if json_data:
|
||||
for interface in json_data:
|
||||
if "name" in interface and interface["name"] == "eth{}".format(
|
||||
hostonly_interface_number - 1):
|
||||
hostonly_interface_number - 1
|
||||
):
|
||||
if "ip_address" in interface and len(interface["ip_address"]) > 0:
|
||||
return interface["ip_address"]
|
||||
except ValueError:
|
||||
@@ -405,7 +415,11 @@ class VirtualBoxGNS3VM(BaseGNS3VM):
|
||||
:param hostonly_network_name: name of the VirtualBox host-only network
|
||||
"""
|
||||
|
||||
await self._execute("modifyvm", [self._vmname, f"--hostonlyadapter{adapter_number}", hostonly_network_name], timeout=3)
|
||||
log.info('VirtualBox host-only network "{}" set on network adapter {} for "{}"'.format(hostonly_network_name,
|
||||
adapter_number,
|
||||
self._vmname))
|
||||
await self._execute(
|
||||
"modifyvm", [self._vmname, f"--hostonlyadapter{adapter_number}", hostonly_network_name], timeout=3
|
||||
)
|
||||
log.info(
|
||||
'VirtualBox host-only network "{}" set on network adapter {} for "{}"'.format(
|
||||
hostonly_network_name, adapter_number, self._vmname
|
||||
)
|
||||
)
|
||||
Reference in new issue
Block a user