mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Use Pydantic to validate the server config file.
This commit is contained in:
1 parent
478119b40d
commit
30ebae207f
61 files changed
+871
-695
No files matched your search
@@ -418,7 +418,6 @@ class BaseManager:
|
||||
return ""
|
||||
orig_path = path
|
||||
|
||||
server_config = self.config.get_section_config("Server")
|
||||
img_directory = self.get_images_directory()
|
||||
valid_directory_prefices = images_directories(self._NODE_TYPE)
|
||||
if extra_dir:
|
||||
@@ -445,7 +444,7 @@ class BaseManager:
|
||||
raise ImageMissingError(orig_path)
|
||||
|
||||
# For local server we allow using absolute path outside image directory
|
||||
if server_config.getboolean("local", False) is True:
|
||||
if Config.instance().settings.Server.local is True:
|
||||
log.debug("Searching for '{}'".format(orig_path))
|
||||
path = force_unix_path(path)
|
||||
if os.path.exists(path):
|
||||
|
||||
@@ -373,9 +373,8 @@ class BaseNode:
|
||||
Returns the VNC console port range.
|
||||
"""
|
||||
|
||||
server_config = self._manager.config.get_section_config("Server")
|
||||
vnc_console_start_port_range = server_config.getint("vnc_console_start_port_range", 5900)
|
||||
vnc_console_end_port_range = server_config.getint("vnc_console_end_port_range", 10000)
|
||||
vnc_console_start_port_range = self._manager.config.settings.Server.vnc_console_start_port_range
|
||||
vnc_console_end_port_range = self._manager.config.settings.Server.vnc_console_end_port_range
|
||||
|
||||
if not 5900 <= vnc_console_start_port_range <= 65535:
|
||||
raise NodeError("The VNC console start port range must be between 5900 and 65535")
|
||||
@@ -685,8 +684,7 @@ class BaseNode:
|
||||
:returns: path to uBridge
|
||||
"""
|
||||
|
||||
path = self._manager.config.get_section_config("Server").get("ubridge_path", "ubridge")
|
||||
path = shutil.which(path)
|
||||
path = shutil.which(self._manager.config.settings.Server.ubridge_path)
|
||||
return path
|
||||
|
||||
async def _ubridge_send(self, command):
|
||||
@@ -721,8 +719,7 @@ class BaseNode:
|
||||
if require_privileged_access and not self._manager.has_privileged_access(self.ubridge_path):
|
||||
raise NodeError("uBridge requires root access or the capability to interact with network adapters")
|
||||
|
||||
server_config = self._manager.config.get_section_config("Server")
|
||||
server_host = server_config.get("host")
|
||||
server_host = self._manager.config.settings.Server.host
|
||||
if not self.ubridge:
|
||||
self._ubridge_hypervisor = Hypervisor(self._project, self.ubridge_path, self.working_dir, server_host)
|
||||
log.info("Starting new uBridge hypervisor {}:{}".format(self._ubridge_hypervisor.host, self._ubridge_hypervisor.port))
|
||||
|
||||
@@ -36,12 +36,16 @@ class Nat(Cloud):
|
||||
def __init__(self, name, node_id, project, manager, ports=None):
|
||||
|
||||
if sys.platform.startswith("linux"):
|
||||
nat_interface = Config.instance().get_section_config("Server").get("default_nat_interface", "virbr0")
|
||||
nat_interface = Config.instance().settings.Server.default_nat_interface
|
||||
if not nat_interface:
|
||||
nat_interface = "virbr0"
|
||||
if nat_interface not in [interface["name"] for interface in gns3server.utils.interfaces.interfaces()]:
|
||||
raise NodeError("NAT interface {} is missing, please install libvirt".format(nat_interface))
|
||||
interface = nat_interface
|
||||
else:
|
||||
nat_interface = Config.instance().get_section_config("Server").get("default_nat_interface", "vmnet8")
|
||||
nat_interface = Config.instance().settings.Server.default_nat_interface
|
||||
if not nat_interface:
|
||||
nat_interface = "vmnet8"
|
||||
interfaces = list(filter(lambda x: nat_interface in x.lower(),
|
||||
[interface["name"] for interface in gns3server.utils.interfaces.interfaces()]))
|
||||
if not len(interfaces):
|
||||
|
||||
@@ -248,7 +248,7 @@ class Dynamips(BaseManager):
|
||||
def find_dynamips(self):
|
||||
|
||||
# look for Dynamips
|
||||
dynamips_path = self.config.get_section_config("Dynamips").get("dynamips_path", "dynamips")
|
||||
dynamips_path = self.config.settings.Dynamips.dynamips_path
|
||||
if not os.path.isabs(dynamips_path):
|
||||
dynamips_path = shutil.which(dynamips_path)
|
||||
|
||||
@@ -279,8 +279,7 @@ class Dynamips(BaseManager):
|
||||
|
||||
# FIXME: hypervisor should always listen to 127.0.0.1
|
||||
# See https://github.com/GNS3/dynamips/issues/62
|
||||
server_config = self.config.get_section_config("Server")
|
||||
server_host = server_config.get("host")
|
||||
server_host = self.config.settings.Server.host
|
||||
|
||||
try:
|
||||
info = socket.getaddrinfo(server_host, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
|
||||
@@ -310,7 +309,7 @@ class Dynamips(BaseManager):
|
||||
|
||||
async def ghost_ios_support(self, vm):
|
||||
|
||||
ghost_ios_support = self.config.get_section_config("Dynamips").getboolean("ghost_ios_support", True)
|
||||
ghost_ios_support = self.config.settings.Dynamips.ghost_ios_support
|
||||
if ghost_ios_support:
|
||||
async with Dynamips._ghost_ios_lock:
|
||||
try:
|
||||
@@ -483,11 +482,11 @@ class Dynamips(BaseManager):
|
||||
except IndexError:
|
||||
raise DynamipsError("WIC slot {} doesn't exist on this router".format(wic_slot_id))
|
||||
|
||||
mmap_support = self.config.get_section_config("Dynamips").getboolean("mmap_support", True)
|
||||
mmap_support = self.config.settings.Dynamips.mmap_support
|
||||
if mmap_support is False:
|
||||
await vm.set_mmap(False)
|
||||
|
||||
sparse_memory_support = self.config.get_section_config("Dynamips").getboolean("sparse_memory_support", True)
|
||||
sparse_memory_support = self.config.settings.Dynamips.sparse_memory_support
|
||||
if sparse_memory_support is False:
|
||||
await vm.set_sparsemem(False)
|
||||
|
||||
|
||||
@@ -95,9 +95,6 @@ class IOUVM(BaseNode):
|
||||
self._application_id = application_id
|
||||
self._l1_keepalives = False # used to overcome the always-up Ethernet interfaces (not supported by all IOSes).
|
||||
|
||||
def _config(self):
|
||||
return self._manager.config.get_section_config("IOU")
|
||||
|
||||
def _nvram_changed(self, path):
|
||||
"""
|
||||
Called when the NVRAM file has changed
|
||||
@@ -248,7 +245,7 @@ class IOUVM(BaseNode):
|
||||
:returns: path to IOURC
|
||||
"""
|
||||
|
||||
iourc_path = self._config().get("iourc_path")
|
||||
iourc_path = self._manager.config.settings.IOU.iourc_path
|
||||
if not iourc_path:
|
||||
# look for the iourc file in the temporary dir.
|
||||
path = os.path.join(self.temporary_directory, "iourc")
|
||||
@@ -401,7 +398,7 @@ class IOUVM(BaseNode):
|
||||
|
||||
try:
|
||||
# we allow license check to be disabled server wide
|
||||
server_wide_license_check = self._config().getboolean("license_check", True)
|
||||
server_wide_license_check = self._manager.config.settings.IOU.license_check
|
||||
except ValueError:
|
||||
raise IOUError("Invalid licence check setting")
|
||||
|
||||
|
||||
@@ -43,15 +43,13 @@ class PortManager:
|
||||
self._used_tcp_ports = set()
|
||||
self._used_udp_ports = set()
|
||||
|
||||
server_config = Config.instance().get_section_config("Server")
|
||||
|
||||
console_start_port_range = server_config.getint("console_start_port_range", 5000)
|
||||
console_end_port_range = server_config.getint("console_end_port_range", 10000)
|
||||
console_start_port_range = Config.instance().settings.Server.console_start_port_range
|
||||
console_end_port_range = Config.instance().settings.Server.console_end_port_range
|
||||
self._console_port_range = (console_start_port_range, console_end_port_range)
|
||||
log.debug(f"Console port range is {console_start_port_range}-{console_end_port_range}")
|
||||
|
||||
udp_start_port_range = server_config.getint("udp_start_port_range", 20000)
|
||||
udp_end_port_range = server_config.getint("udp_end_port_range", 30000)
|
||||
udp_start_port_range = Config.instance().settings.Server.udp_start_port_range
|
||||
udp_end_port_range = Config.instance().settings.Server.udp_end_port_range
|
||||
self._udp_port_range = (udp_start_port_range, udp_end_port_range)
|
||||
log.debug(f"UDP port range is {udp_start_port_range}-{udp_end_port_range}")
|
||||
|
||||
@@ -86,8 +84,7 @@ class PortManager:
|
||||
Bind console host to 0.0.0.0 if remote connections are allowed.
|
||||
"""
|
||||
|
||||
server_config = Config.instance().get_section_config("Server")
|
||||
remote_console_connections = server_config.getboolean("allow_remote_console")
|
||||
remote_console_connections = Config.instance().settings.Server.allow_remote_console
|
||||
if remote_console_connections:
|
||||
log.warning("Remote console connections are allowed")
|
||||
self._console_host = "0.0.0.0"
|
||||
|
||||
@@ -85,13 +85,9 @@ class Project:
|
||||
"variables": self._variables
|
||||
}
|
||||
|
||||
def _config(self):
|
||||
|
||||
return Config.instance().get_section_config("Server")
|
||||
|
||||
def is_local(self):
|
||||
|
||||
return self._config().getboolean("local", False)
|
||||
return Config.instance().settings.Server.local
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
|
||||
@@ -73,9 +73,9 @@ class QemuVM(BaseNode):
|
||||
def __init__(self, name, node_id, project, manager, linked_clone=True, qemu_path=None, console=None, console_type="telnet", aux=None, aux_type="none", platform=None):
|
||||
|
||||
super().__init__(name, node_id, project, manager, console=console, console_type=console_type, linked_clone=linked_clone, aux=aux, aux_type=aux_type, wrap_console=True, wrap_aux=True)
|
||||
server_config = manager.config.get_section_config("Server")
|
||||
self._host = server_config.get("host", "127.0.0.1")
|
||||
self._monitor_host = server_config.get("monitor_host", "127.0.0.1")
|
||||
|
||||
self._host = manager.config.settings.Server.host
|
||||
self._monitor_host = manager.config.settings.Qemu.monitor_host
|
||||
self._process = None
|
||||
self._cpulimit_process = None
|
||||
self._monitor = None
|
||||
@@ -1055,7 +1055,7 @@ class QemuVM(BaseNode):
|
||||
await self.resume()
|
||||
return
|
||||
|
||||
if self._manager.config.get_section_config("Qemu").getboolean("monitor", True):
|
||||
if self._manager.config.settings.Qemu.enable_monitor:
|
||||
try:
|
||||
info = socket.getaddrinfo(self._monitor_host, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
|
||||
if not info:
|
||||
@@ -2112,17 +2112,8 @@ class QemuVM(BaseNode):
|
||||
:returns: Boolean True if we need to enable hardware acceleration
|
||||
"""
|
||||
|
||||
enable_hardware_accel = self.manager.config.get_section_config("Qemu").getboolean("enable_hardware_acceleration", True)
|
||||
require_hardware_accel = self.manager.config.get_section_config("Qemu").getboolean("require_hardware_acceleration", True)
|
||||
if sys.platform.startswith("linux"):
|
||||
# compatibility: these options were used before version 2.0 and have priority
|
||||
enable_kvm = self.manager.config.get_section_config("Qemu").getboolean("enable_kvm")
|
||||
if enable_kvm is not None:
|
||||
enable_hardware_accel = enable_kvm
|
||||
require_kvm = self.manager.config.get_section_config("Qemu").getboolean("require_kvm")
|
||||
if require_kvm is not None:
|
||||
require_hardware_accel = require_kvm
|
||||
|
||||
enable_hardware_accel = self.manager.config.settings.Qemu.enable_hardware_acceleration
|
||||
require_hardware_accel = self.manager.config.settings.Qemu.require_hardware_acceleration
|
||||
if enable_hardware_accel and "-no-kvm" not in options and "-no-hax" not in options:
|
||||
# Turn OFF hardware acceleration for non x86 architectures
|
||||
if sys.platform.startswith("win"):
|
||||
@@ -2137,7 +2128,7 @@ class QemuVM(BaseNode):
|
||||
|
||||
if sys.platform.startswith("linux") and not os.path.exists("/dev/kvm"):
|
||||
if require_hardware_accel:
|
||||
raise QemuError("KVM acceleration cannot be used (/dev/kvm doesn't exist). It is possible to turn off KVM support in the gns3_server.conf by adding enable_kvm = false to the [Qemu] section.")
|
||||
raise QemuError("KVM acceleration cannot be used (/dev/kvm doesn't exist). It is possible to turn off KVM support in the gns3_server.conf by adding enable_hardware_acceleration = false to the [Qemu] section.")
|
||||
else:
|
||||
return False
|
||||
elif sys.platform.startswith("win"):
|
||||
|
||||
@@ -57,7 +57,7 @@ class VirtualBox(BaseManager):
|
||||
def find_vboxmanage(self):
|
||||
|
||||
# look for VBoxManage
|
||||
vboxmanage_path = self.config.get_section_config("VirtualBox").get("vboxmanage_path")
|
||||
vboxmanage_path = self.config.settings.VirtualBox.vboxmanage_path
|
||||
if vboxmanage_path:
|
||||
if not os.path.isabs(vboxmanage_path):
|
||||
vboxmanage_path = shutil.which(vboxmanage_path)
|
||||
|
||||
@@ -91,7 +91,7 @@ class VMware(BaseManager):
|
||||
"""
|
||||
|
||||
# look for vmrun
|
||||
vmrun_path = self.config.get_section_config("VMware").get("vmrun_path")
|
||||
vmrun_path = self.config.settings.VMware.vmrun_path
|
||||
if not vmrun_path:
|
||||
if sys.platform.startswith("win"):
|
||||
vmrun_path = shutil.which("vmrun")
|
||||
@@ -309,8 +309,8 @@ class VMware(BaseManager):
|
||||
|
||||
def is_managed_vmnet(self, vmnet):
|
||||
|
||||
self._vmnet_start_range = self.config.get_section_config("VMware").getint("vmnet_start_range", self._vmnet_start_range)
|
||||
self._vmnet_end_range = self.config.get_section_config("VMware").getint("vmnet_end_range", self._vmnet_end_range)
|
||||
self._vmnet_start_range = self.config.settings.VMware.vmnet_start_range
|
||||
self._vmnet_end_range = self.config.settings.VMware.vmnet_end_range
|
||||
match = re.search(r"vmnet([0-9]+)$", vmnet, re.IGNORECASE)
|
||||
if match:
|
||||
vmnet_number = match.group(1)
|
||||
|
||||
@@ -336,7 +336,7 @@ class VMwareVM(BaseNode):
|
||||
# special case on OSX, we cannot bind VMnet interfaces using the libpcap
|
||||
await self._ubridge_send('bridge add_nio_fusion_vmnet {name} "{interface}"'.format(name=vnet, interface=vmnet_interface))
|
||||
else:
|
||||
block_host_traffic = self.manager.config.get_section_config("VMware").getboolean("block_host_traffic", False)
|
||||
block_host_traffic = self.manager.config.VMware.block_host_traffic
|
||||
await self._add_ubridge_ethernet_connection(vnet, vmnet_interface, block_host_traffic)
|
||||
|
||||
if isinstance(nio, NIOUDP):
|
||||
|
||||
@@ -138,7 +138,7 @@ class VPCSVM(BaseNode):
|
||||
:returns: path to VPCS
|
||||
"""
|
||||
|
||||
vpcs_path = self._manager.config.get_section_config("VPCS").get("vpcs_path", "vpcs")
|
||||
vpcs_path = self._manager.config.settings.VPCS.vpcs_path
|
||||
if not os.path.isabs(vpcs_path):
|
||||
vpcs_path = shutil.which(vpcs_path)
|
||||
return vpcs_path
|
||||
|
||||
Reference in new issue
Block a user