From b42d751e89f0b47323913a9170c87341f711467f Mon Sep 17 00:00:00 2001 From: grossmj Date: Tue, 20 May 2014 17:21:45 -0600 Subject: [PATCH] Fix console port restoration for IOU and VPCS (when loading a project). --- gns3server/main.py | 4 ++-- gns3server/modules/iou/__init__.py | 7 ++++--- gns3server/modules/iou/iou_device.py | 26 ++++++++++++++++---------- gns3server/modules/iou/ioucon.py | 14 +++++--------- gns3server/modules/iou/schemas.py | 8 +++++++- gns3server/modules/vpcs/__init__.py | 9 ++++++--- gns3server/modules/vpcs/schemas.py | 6 ++++++ gns3server/modules/vpcs/vpcs_device.py | 25 +++++++++++++++---------- 8 files changed, 61 insertions(+), 38 deletions(-) diff --git a/gns3server/main.py b/gns3server/main.py index 71514fce..f51730ff 100644 --- a/gns3server/main.py +++ b/gns3server/main.py @@ -48,8 +48,8 @@ def locale_check(): or there: http://robjwells.com/post/61198832297/get-your-us-ascii-out-of-my-face """ - # no need to check on Windows - if sys.platform.startswith("win"): + # no need to check on Windows or when frozen + if sys.platform.startswith("win") or hasattr(sys, "frozen"): return language = encoding = None diff --git a/gns3server/modules/iou/__init__.py b/gns3server/modules/iou/__init__.py index e7d2c0c6..9b71e2bb 100644 --- a/gns3server/modules/iou/__init__.py +++ b/gns3server/modules/iou/__init__.py @@ -301,6 +301,7 @@ class IOU(IModule): Optional request parameters: - name (IOU name) + - console (IOU console port) Response parameters: - id (IOU instance identifier) @@ -314,9 +315,8 @@ class IOU(IModule): if not self.validate_request(request, IOU_CREATE_SCHEMA): return - name = None - if "name" in request: - name = request["name"] + name = request.get("name") + console = request.get("console") iou_path = request["path"] try: @@ -331,6 +331,7 @@ class IOU(IModule): self._working_dir, self._host, name, + console, self._console_start_port_range, self._console_end_port_range) diff --git a/gns3server/modules/iou/iou_device.py b/gns3server/modules/iou/iou_device.py index 527e7d14..3598e4ad 100644 --- a/gns3server/modules/iou/iou_device.py +++ b/gns3server/modules/iou/iou_device.py @@ -50,6 +50,7 @@ class IOUDevice(object): :param working_dir: path to a working directory :param host: host/address to bind for console and UDP connections :param name: name of this IOU device + :param console: TCP console port :param console_start_port_range: TCP console port range start :param console_end_port_range: TCP console port range end """ @@ -61,6 +62,7 @@ class IOUDevice(object): working_dir, host="127.0.0.1", name=None, + console=None, console_start_port_range=4001, console_end_port_range=4512): @@ -82,7 +84,7 @@ class IOUDevice(object): self._path = path self._iourc = "" self._iouyap = "" - self._console = None + self._console = console self._working_dir = None self._command = [] self._process = None @@ -109,16 +111,20 @@ class IOUDevice(object): # update the working directory self.working_dir = working_dir - # allocate a console port - try: - self._console = find_unused_port(self._console_start_port_range, - self._console_end_port_range, - self._host, - ignore_ports=self._allocated_console_ports) - except Exception as e: - raise IOUError(e) + if not self._console: + # allocate a console port + try: + self._console = find_unused_port(self._console_start_port_range, + self._console_end_port_range, + self._host, + ignore_ports=self._allocated_console_ports) + except Exception as e: + raise IOUError(e) + if self._console in self._allocated_console_ports: + raise IOUError("Console port {} is already in used another IOU device".format(console)) self._allocated_console_ports.append(self._console) + log.info("IOU device {name} [id={id}] has been created".format(name=self._name, id=self._id)) @@ -317,7 +323,7 @@ class IOUDevice(object): """ if console in self._allocated_console_ports: - raise IOUError("Console port {} is already in used by another IOU device".format(console)) + raise IOUError("Console port {} is already used by another IOU device".format(console)) self._allocated_console_ports.remove(self._console) self._console = console diff --git a/gns3server/modules/iou/ioucon.py b/gns3server/modules/iou/ioucon.py index a30c66e9..7578c0a3 100644 --- a/gns3server/modules/iou/ioucon.py +++ b/gns3server/modules/iou/ioucon.py @@ -393,14 +393,12 @@ class IOU(Router): except FileNotFoundError: pass except Exception as e: - raise NetioError("Couldn't unlink socket {}: {}" - .format(self.ttyC, e)) + raise NetioError("Couldn't unlink socket {}: {}".format(self.ttyC, e)) try: self.fd.bind(self.ttyC) except Exception as e: - raise NetioError("Couldn't create socket {}: {}" - .format(self.ttyC, e)) + raise NetioError("Couldn't create socket {}: {}".format(self.ttyC, e)) def _connect(self): # Keep trying until we connect or die trying @@ -411,8 +409,7 @@ class IOU(Router): log.debug("Waiting to connect to {}".format(self.ttyS)) time.sleep(RETRY_DELAY) except Exception as e: - raise NetioError("Couldn't connect to socket {}: {}" - .format(self.ttyS, e)) + raise NetioError("Couldn't connect to socket {}: {}".format(self.ttyS, e)) else: break @@ -465,8 +462,7 @@ def mkdir_netio(netio_dir): except FileExistsError: pass except Exception as e: - raise NetioError("Couldn't create directory {}: {}" - .format(netio_dir, e)) + raise NetioError("Couldn't create directory {}: {}".format(netio_dir, e)) def send_recv_loop(console, router, esc_char, stop_event): @@ -631,7 +627,7 @@ def start_ioucon(cmdline_args, stop_event): if args.debug: traceback.print_exc(file=sys.stderr) else: - print(e, file=sys.stderr) + log.error("ioucon: {}".format(e)) sys.exit(EXIT_FAILURE) log.info("exiting...") diff --git a/gns3server/modules/iou/schemas.py b/gns3server/modules/iou/schemas.py index ad232a4c..657590aa 100644 --- a/gns3server/modules/iou/schemas.py +++ b/gns3server/modules/iou/schemas.py @@ -26,13 +26,19 @@ IOU_CREATE_SCHEMA = { "type": "string", "minLength": 1, }, + "console": { + "description": "console TCP port", + "minimum": 1, + "maximum": 65535, + "type": "integer" + }, "path": { "description": "path to the IOU executable", "type": "string", "minLength": 1, } }, - "required": ["path"] + "required": ["path"], } IOU_DELETE_SCHEMA = { diff --git a/gns3server/modules/vpcs/__init__.py b/gns3server/modules/vpcs/__init__.py index 5c31310a..ea270e0c 100644 --- a/gns3server/modules/vpcs/__init__.py +++ b/gns3server/modules/vpcs/__init__.py @@ -213,6 +213,7 @@ class VPCS(IModule): Optional request parameters: - name (VPCS name) + - console (VPCS console port) Response parameters: - id (VPCS instance identifier) @@ -226,9 +227,10 @@ class VPCS(IModule): if request and not self.validate_request(request, VPCS_CREATE_SCHEMA): return - name = None - if request and "name" in request: - name = request["name"] + name = console = None + if request: + name = request.get("name") + console = request.get("console") try: try: @@ -245,6 +247,7 @@ class VPCS(IModule): self._working_dir, self._host, name, + console, self._console_start_port_range, self._console_end_port_range) diff --git a/gns3server/modules/vpcs/schemas.py b/gns3server/modules/vpcs/schemas.py index 229e2acc..681b48f6 100644 --- a/gns3server/modules/vpcs/schemas.py +++ b/gns3server/modules/vpcs/schemas.py @@ -26,6 +26,12 @@ VPCS_CREATE_SCHEMA = { "type": "string", "minLength": 1, }, + "console": { + "description": "console TCP port", + "minimum": 1, + "maximum": 65535, + "type": "integer" + }, }, } diff --git a/gns3server/modules/vpcs/vpcs_device.py b/gns3server/modules/vpcs/vpcs_device.py index 23bd6970..d9bdb33e 100644 --- a/gns3server/modules/vpcs/vpcs_device.py +++ b/gns3server/modules/vpcs/vpcs_device.py @@ -43,6 +43,7 @@ class VPCSDevice(object): :param working_dir: path to a working directory :param host: host/address to bind for console and UDP connections :param name: name of this VPCS device + :param console: TCP console port :param console_start_port_range: TCP console port range start :param console_end_port_range: TCP console port range end """ @@ -55,6 +56,7 @@ class VPCSDevice(object): working_dir, host="127.0.0.1", name=None, + console=None, console_start_port_range=4512, console_end_port_range=5000): @@ -77,7 +79,7 @@ class VPCSDevice(object): self._name = "VPCS{}".format(self._id) self._path = path - self._console = None + self._console = console self._working_dir = None self._command = [] self._process = None @@ -94,15 +96,18 @@ class VPCSDevice(object): # update the working directory self.working_dir = working_dir - # allocate a console port - try: - self._console = find_unused_port(self._console_start_port_range, - self._console_end_port_range, - self._host, - ignore_ports=self._allocated_console_ports) - except Exception as e: - raise VPCSError(e) + if not self._console: + # allocate a console port + try: + self._console = find_unused_port(self._console_start_port_range, + self._console_end_port_range, + self._host, + ignore_ports=self._allocated_console_ports) + except Exception as e: + raise VPCSError(e) + if self._console in self._allocated_console_ports: + raise VPCSError("Console port {} is already used by another VPCS device".format(console)) self._allocated_console_ports.append(self._console) log.info("VPCS device {name} [id={id}] has been created".format(name=self._name, @@ -250,7 +255,7 @@ class VPCSDevice(object): """ if console in self._allocated_console_ports: - raise VPCSError("Console port {} is already in used by another VPCS device".format(console)) + raise VPCSError("Console port {} is already used by another VPCS device".format(console)) self._allocated_console_ports.remove(self._console) self._console = console