1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-12 19:38:57 +00:00

Create a container class and a flag for enable it from command line

Ref #417
This commit is contained in:
Julien Duponchelle 2016-03-02 09:49:52 +01:00
parent c833a20a8c
commit 84eb8356e8
No known key found for this signature in database
GPG Key ID: F1E2485547D4595D
4 changed files with 84 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
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

View File

@ -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")

View File

@ -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)

View File

@ -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 <http://www.gnu.org/licenses/>.
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()