From 39e3ca91a99f978d27800bb7b4c59ec9b2669d7b Mon Sep 17 00:00:00 2001 From: Jeremy Date: Fri, 23 Jan 2015 13:01:23 -0700 Subject: [PATCH] Fixes module unload & adds host, port and allow-remote-console command line args. --- gns3server/config.py | 2 +- gns3server/main.py | 34 ++++++++++++++++++------------ gns3server/modules/base_manager.py | 1 + gns3server/modules/port_manager.py | 14 ++++++++---- gns3server/server.py | 6 +++--- tests/conftest.py | 2 +- 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/gns3server/config.py b/gns3server/config.py index 4dfefbcf..56e26f4b 100644 --- a/gns3server/config.py +++ b/gns3server/config.py @@ -128,7 +128,7 @@ class Config(object): dumped on the disk. :param section: Section name - :param content: A dictonary with section content + :param content: A dictionary with section content """ self._config[section] = content diff --git a/gns3server/main.py b/gns3server/main.py index ee3c3c4a..91d0d2dd 100644 --- a/gns3server/main.py +++ b/gns3server/main.py @@ -72,11 +72,12 @@ def locale_check(): def parse_arguments(): - parser = argparse.ArgumentParser(description='GNS 3 server') - parser.add_argument('--local', - action="store_true", - dest='local', - help='Local mode (allow some insecure operations)') + parser = argparse.ArgumentParser(description="GNS3 server version {}".format(__version__)) + parser.add_argument("-l", "--host", help="run on the given host/IP address", default="127.0.0.1", nargs="?") + parser.add_argument("-p", "--port", type=int, help="run on the given port", default=8000, nargs="?") + parser.add_argument("-v", "--version", help="show the version", action="version", version=__version__) + parser.add_argument("-L", "--local", action="store_true", help="Local mode (allow some insecure operations)") + parser.add_argument("-A", "--allow-remote-console", dest="allow", action="store_true", help="Allow remote connections to console ports") args = parser.parse_args() config = Config.instance() @@ -84,7 +85,16 @@ def parse_arguments(): if args.local: server_config["local"] = "true" + else: + server_config["local"] = "false" + if args.allow: + server_config["allow_remote_console"] = "true" + else: + server_config["allow_remote_console"] = "false" + + server_config["host"] = args.host + server_config["port"] = str(args.port) config.set_section_config("Server", server_config) @@ -96,7 +106,6 @@ def main(): # TODO: migrate command line options to argparse (don't forget the quiet mode). current_year = datetime.date.today().year - # TODO: Renable the test when we will have command line # user_log = logging.getLogger('user_facing') # if not options.quiet: @@ -107,7 +116,6 @@ def main(): # user_log.propagate = False # END OLD LOG CODE user_log = init_logger(logging.DEBUG, quiet=False) - # FIXME END Temporary parse_arguments() @@ -115,10 +123,8 @@ def main(): user_log.info("Copyright (c) 2007-{} GNS3 Technologies Inc.".format(current_year)) server_config = Config.instance().get_section_config("Server") - if server_config["local"]: - log.warning("Local mode is enabled. Beware it's allow a full control on your filesystem") - - # TODO: end todo + if server_config.getboolean("local"): + log.warning("Local mode is enabled. Beware, clients will have full control on your filesystem") # we only support Python 3 version >= 3.3 if sys.version_info < (3, 3): @@ -137,9 +143,9 @@ def main(): log.critical("The current working directory doesn't exist") return - # TODO: Renable console_bind_to_any when we will have command line parsing - # server = Server(options.host, options.port, options.console_bind_to_any) - server = Server("127.0.0.1", 8000, False) + host = server_config["host"] + port = int(server_config["port"]) + server = Server(host, port) server.run() if __name__ == '__main__': diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index 8913b21f..2c25c9bf 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -106,6 +106,7 @@ class BaseManager: if hasattr(BaseManager, "_instance"): BaseManager._instance = None + log.debug("Module {} unloaded".format(self.module_name)) def get_vm(self, uuid): """ diff --git a/gns3server/modules/port_manager.py b/gns3server/modules/port_manager.py index ed4404dc..b468dfce 100644 --- a/gns3server/modules/port_manager.py +++ b/gns3server/modules/port_manager.py @@ -17,17 +17,19 @@ import socket import ipaddress -import asyncio from aiohttp.web import HTTPConflict +from gns3server.config import Config + +import logging +log = logging.getLogger(__name__) class PortManager: - """ :param host: IP address to bind for console connections """ - def __init__(self, host="127.0.0.1", console_bind_to_any=False): + def __init__(self, host="127.0.0.1"): self._console_host = host self._udp_host = host @@ -37,7 +39,11 @@ class PortManager: self._used_tcp_ports = set() self._used_udp_ports = set() - if console_bind_to_any: + server_config = Config.instance().get_section_config("Server") + remote_console_connections = server_config.getboolean("allow_remote_console") + + if remote_console_connections: + log.warning("Remote console connections are allowed") if ipaddress.ip_address(host).version == 6: self._console_host = "::" else: diff --git a/gns3server/server.py b/gns3server/server.py index f0c885a9..f1dd6685 100644 --- a/gns3server/server.py +++ b/gns3server/server.py @@ -42,13 +42,13 @@ log = logging.getLogger(__name__) class Server: - def __init__(self, host, port, console_bind_to_any): + def __init__(self, host, port): self._host = host self._port = port self._loop = None self._start_time = time.time() - self._port_manager = PortManager(host, console_bind_to_any) + self._port_manager = PortManager(host) # TODO: server config file support, to be reviewed # # get the projects and temp directories from the configuration file (passed to the modules) @@ -80,7 +80,7 @@ class Server: for module in MODULES: log.debug("Unloading module {}".format(module.__name__)) m = module.instance() - self._loop.run_until_complete(m.unload()) + asyncio.async(m.unload()) self._loop.stop() def _signal_handling(self): diff --git a/tests/conftest.py b/tests/conftest.py index 41682bb3..630322ca 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -57,7 +57,7 @@ def _get_unused_port(): @pytest.fixture(scope="session") def server(request, loop, port_manager): - """A GNS 3 server""" + """A GNS3 server""" port = _get_unused_port() host = "localhost"