From 7b642552dde0fb41ad93eab72eca1e6ca0ad2ecc Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 7 Mar 2017 15:36:35 +0100 Subject: [PATCH] Fix GNS3VM settings are lost at startup Fix #926 --- gns3server/controller/__init__.py | 27 ++++++++++++++++++--------- gns3server/web/web_server.py | 10 +--------- tests/conftest.py | 1 + tests/controller/test_controller.py | 17 +---------------- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 4b40761e..da0f6f9c 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -45,7 +45,7 @@ class Controller: self.gns3vm = GNS3VM(self) self.symbols = Symbols() # Store settings shared by the different GUI will be replace by dedicated API later - self._settings = {} + self._settings = None self._config_file = os.path.join(Config.instance().config_dir, "gns3_controller.conf") log.info("Load controller configuration file {}".format(self._config_file)) @@ -67,6 +67,7 @@ class Controller: if name == "gns3vm": name = "Main server" + computes = yield from self._load_controller_settings() yield from self.add_compute(compute_id="local", name=name, protocol=server_config.get("protocol", "http"), @@ -76,7 +77,11 @@ class Controller: user=server_config.get("user", ""), password=server_config.get("password", ""), force=True) - yield from self._load_controller_settings() + for c in computes: + try: + yield from self.add_compute(**c) + except aiohttp.web_exceptions.HTTPConflict: + pass # Skip not available servers at loading yield from self.load_projects() yield from self.gns3vm.auto_start_vm() yield from self._project_auto_open() @@ -100,6 +105,9 @@ class Controller: """ Save the controller configuration on disk """ + # We don't save during the loading otherwise we could lost stuff + if self._settings is None: + return data = { "computes": [], "settings": self._settings, @@ -135,18 +143,17 @@ class Controller: data = json.load(f) except (OSError, ValueError) as e: log.critical("Cannot load %s: %s", self._config_file, str(e)) - return + self._settings = {} + return [] - if "settings" in data: + if "settings" in data and data["settings"] is not None: self._settings = data["settings"] + else: + self._settings = {} if "gns3vm" in data: self.gns3vm.settings = data["gns3vm"] - for c in data["computes"]: - try: - yield from self.add_compute(**c) - except aiohttp.web_exceptions.HTTPConflict: - pass # Skip not available servers at loading + return data["computes"] @asyncio.coroutine def load_projects(self): @@ -225,12 +232,14 @@ class Controller: "headless": vm_settings.get("headless", False), "vmname": vmname } + self._settings = {} @property def settings(self): """ Store settings shared by the different GUI will be replace by dedicated API later. Dictionnary """ + assert self._settings is not None return self._settings @settings.setter diff --git a/gns3server/web/web_server.py b/gns3server/web/web_server.py index e955cdd6..bccb3083 100644 --- a/gns3server/web/web_server.py +++ b/gns3server/web/web_server.py @@ -249,13 +249,6 @@ class WebServer: time.sleep(1) # this is to prevent too many request to slow down the server log.debug("UDP server discovery stopped") - @asyncio.coroutine - def _on_startup(self, *args): - """ - Called when the HTTP server start - """ - yield from Controller.instance().start() - def run(self): """ Starts the server. @@ -295,9 +288,8 @@ class WebServer: for key, val in os.environ.items(): log.debug("ENV %s=%s", key, val) + self._loop.run_until_complete(Controller.instance().start()) self._app = aiohttp.web.Application() - # Background task started with the server - self._app.on_startup.append(self._on_startup) # Allow CORS for this domains cors = aiohttp_cors.setup(self._app, defaults={ diff --git a/tests/conftest.py b/tests/conftest.py index 3b810fc5..2ecd7764 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -179,6 +179,7 @@ def controller(tmpdir, controller_config_path): Controller._instance = None controller = Controller.instance() controller._config_file = controller_config_path + controller._settings = {} return controller diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 3ae347a2..80f0a00b 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -60,23 +60,8 @@ def test_load_controller_settings(controller, controller_config_path, async_run) data["gns3vm"] = {"vmname": "Test VM"} with open(controller_config_path, "w+") as f: json.dump(data, f) - async_run(controller._load_controller_settings()) + assert len(async_run(controller._load_controller_settings())) == 1 assert controller.settings["IOU"] - assert controller.computes["test1"].__json__() == { - "compute_id": "test1", - "connected": False, - "host": "localhost", - "port": 8000, - "protocol": "http", - "user": "admin", - "name": "http://admin@localhost:8000", - "cpu_usage_percent": None, - "memory_usage_percent": None, - "capabilities": { - "version": None, - "node_types": [] - } - } assert controller.gns3vm.settings["vmname"] == "Test VM"