mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Refactor port manager
This commit is contained in:
1 parent
ae8e2f4199
commit
73a481e510
8 files changed
+80
-41
No files matched your search
@@ -43,6 +43,21 @@ class BaseManager:
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
|
||||
@property
|
||||
def port_manager(self):
|
||||
"""
|
||||
Returns the port_manager for this VMs
|
||||
|
||||
:returns: Port manager
|
||||
"""
|
||||
|
||||
return self._port_manager
|
||||
|
||||
@port_manager.setter
|
||||
def port_manager(self, new_port_manager):
|
||||
self._port_manager = new_port_manager
|
||||
|
||||
|
||||
@classmethod
|
||||
@asyncio.coroutine
|
||||
def destroy(cls):
|
||||
@@ -73,7 +88,7 @@ class BaseManager:
|
||||
else:
|
||||
if identifier in self._vms:
|
||||
raise VMError("VM identifier {} is already used by another VM instance".format(identifier))
|
||||
vm = self._VM_CLASS(vmname, identifier, self.port_manager)
|
||||
vm = self._VM_CLASS(vmname, identifier, self)
|
||||
yield from vm.wait_for_creation()
|
||||
self._vms[vm.id] = vm
|
||||
return vm
|
||||
|
||||
@@ -26,20 +26,23 @@ log = logging.getLogger(__name__)
|
||||
|
||||
class BaseVM:
|
||||
|
||||
def __init__(self, name, identifier, port_manager):
|
||||
def __init__(self, name, identifier, manager):
|
||||
|
||||
self._loop = asyncio.get_event_loop()
|
||||
self._queue = asyncio.Queue()
|
||||
self._name = name
|
||||
self._id = identifier
|
||||
self._created = asyncio.Future()
|
||||
self._port_manager = port_manager
|
||||
self._manager = manager
|
||||
self._config = Config.instance()
|
||||
self._worker = asyncio.async(self._run())
|
||||
log.info("{type} device {name} [id={id}] has been created".format(type=self.__class__.__name__,
|
||||
name=self._name,
|
||||
id=self._id))
|
||||
|
||||
#TODO: When delete release console ports
|
||||
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
"""
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
import socket
|
||||
import ipaddress
|
||||
|
||||
import asyncio
|
||||
|
||||
class PortManager:
|
||||
"""
|
||||
@@ -42,6 +42,23 @@ class PortManager:
|
||||
else:
|
||||
self._console_host = host
|
||||
|
||||
@classmethod
|
||||
def instance(cls):
|
||||
"""
|
||||
Singleton to return only one instance of BaseManager.
|
||||
|
||||
:returns: instance of Manager
|
||||
"""
|
||||
|
||||
if not hasattr(cls, "_instance") or cls._instance is None:
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
|
||||
@classmethod
|
||||
@asyncio.coroutine
|
||||
def destroy(cls):
|
||||
cls._instance = None
|
||||
|
||||
@property
|
||||
def console_host(self):
|
||||
|
||||
|
||||
@@ -48,20 +48,20 @@ class VPCSDevice(BaseVM):
|
||||
|
||||
:param name: name of this VPCS device
|
||||
:param vpcs_id: VPCS instance ID
|
||||
:param path: path to VPCS executable
|
||||
:param manager: parent VM Manager
|
||||
:param working_dir: path to a working directory
|
||||
:param console: TCP console port
|
||||
"""
|
||||
def __init__(self, name, vpcs_id, port_manager, working_dir=None, console=None):
|
||||
def __init__(self, name, vpcs_id, manager, working_dir=None, console=None):
|
||||
|
||||
super().__init__(name, vpcs_id, port_manager)
|
||||
super().__init__(name, vpcs_id, manager)
|
||||
|
||||
#self._path = path
|
||||
#self._working_dir = working_dir
|
||||
# TODO: Hardcodded for testing
|
||||
#self._working_dir = working_dir
|
||||
self._working_dir = "/tmp"
|
||||
|
||||
self._path = self._config.get_section_config("VPCS").get("path", "vpcs")
|
||||
|
||||
self._working_dir = "/tmp"
|
||||
self._console = console
|
||||
|
||||
self._command = []
|
||||
@@ -83,9 +83,9 @@ class VPCSDevice(BaseVM):
|
||||
#
|
||||
try:
|
||||
if not self._console:
|
||||
self._console = port_manager.get_free_port()
|
||||
self._console = self._manager.port_manager.get_free_console_port()
|
||||
else:
|
||||
self._console = port_manager.reserve_port(self._console)
|
||||
self._console = self._manager.port_manager.reserve_console_port(self._console)
|
||||
except Exception as e:
|
||||
raise VPCSError(e)
|
||||
|
||||
|
||||
Reference in new issue
Block a user