From 84eb8356e80bf5821fca5fed3d6d14f7fff79371 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 2 Mar 2016 09:49:52 +0100 Subject: [PATCH] Create a container class and a flag for enable it from command line Ref #417 --- gns3server/controller/__init__.py | 41 +++++++++++++++++++++++++++++ gns3server/run.py | 6 +++++ tests/conftest.py | 1 + tests/controller/test_controller.py | 36 +++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 gns3server/controller/__init__.py create mode 100644 tests/controller/test_controller.py diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py new file mode 100644 index 00000000..2c81fd95 --- /dev/null +++ b/gns3server/controller/__init__.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# +# Copyright (C) 2016 GNS3 Technologies Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +from ..config import Config + + +class Controller: + """The controller manage multiple gns3 servers""" + + def isEnabled(self): + """ + :returns: True if current instance is the controller + of our GNS3 infrastructure. + """ + return Config.instance().get_section_config("Server").getboolean("controller") + + @staticmethod + def instance(): + """ + Singleton to return only on instance of Controller. + :returns: instance of Controller + """ + + if not hasattr(Controller, '_instance') or Controller._instance is None: + Controller._instance = Controller() + return Controller._instance diff --git a/gns3server/run.py b/gns3server/run.py index 9604d467..4e86e356 100644 --- a/gns3server/run.py +++ b/gns3server/run.py @@ -90,6 +90,7 @@ def parse_arguments(argv): parser.add_argument("--host", help="run on the given host/IP address") parser.add_argument("--port", help="run on the given port", type=int) parser.add_argument("--ssl", action="store_true", help="run in SSL mode") + parser.add_argument("--controller", action="store_true", help="start as a GNS3 controller") parser.add_argument("--config", help="Configuration file") parser.add_argument("--certfile", help="SSL cert file") parser.add_argument("--certkey", help="SSL key file") @@ -117,6 +118,7 @@ def parse_arguments(argv): "certkey": config.get("certkey", ""), "record": config.get("record", ""), "local": config.getboolean("local", False), + "controller": config.getboolean("controller", False), "allow": config.getboolean("allow_remote_console", False), "quiet": config.getboolean("quiet", False), "debug": config.getboolean("debug", False), @@ -133,6 +135,7 @@ def set_config(args): config = Config.instance() server_config = config.get_section_config("Server") server_config["local"] = str(args.local) + server_config["controller"] = str(args.controller) server_config["allow_remote_console"] = str(args.allow) server_config["host"] = args.host server_config["port"] = str(args.port) @@ -201,6 +204,9 @@ def run(): set_config(args) server_config = Config.instance().get_section_config("Server") + if server_config.getboolean("controller"): + log.info("Controller mode is enabled.") + if server_config.getboolean("local"): log.warning("Local mode is enabled. Beware, clients will have full control on your filesystem") diff --git a/tests/conftest.py b/tests/conftest.py index 59eca77b..c8bf4c04 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -151,6 +151,7 @@ def run_around_tests(monkeypatch, port_manager): config.set("Server", "project_directory", os.path.join(tmppath, 'projects')) config.set("Server", "images_path", os.path.join(tmppath, 'images')) config.set("Server", "auth", False) + config.set("Server", "controller", False) # Prevent executions of the VM if we forgot to mock something config.set("VirtualBox", "vboxmanage_path", tmppath) diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py new file mode 100644 index 00000000..5ef9e375 --- /dev/null +++ b/tests/controller/test_controller.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# +# Copyright (C) 2016 GNS3 Technologies Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import pytest + +from gns3server.controller import Controller +from gns3server.config import Config + + +@pytest.fixture +def controller(): + Controller._instance = None + return Controller.instance() + + + +def test_isEnabled(controller): + Config.instance().set("Server", "controller", False) + assert not controller.isEnabled() + Config.instance().set("Server", "controller", True) + assert controller.isEnabled() +