From 9fd5e4cbc98a734227d402ffba8ba5e5b786c506 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 22 Mar 2017 18:29:08 +0100 Subject: [PATCH] Handle password configuration change on remote servers Fix https://github.com/GNS3/gns3-gui/issues/1942 --- gns3server/config.py | 9 +++++++++ gns3server/controller/__init__.py | 31 +++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/gns3server/config.py b/gns3server/config.py index 8d7ad5f3..0e4b80c4 100644 --- a/gns3server/config.py +++ b/gns3server/config.py @@ -50,6 +50,7 @@ class Config: # Monitor configuration files for changes self._watched_files = {} + self._watch_callback = [] if sys.platform.startswith("win"): @@ -116,6 +117,12 @@ class Config: self.clear() self._watch_config_file() + def listen_for_config_changes(self, callback): + """ + Call the callback when the configuration file change + """ + self._watch_callback.append(callback) + @property def profile(self): """ @@ -143,6 +150,8 @@ class Config: self.read_config() for section in self._override_config: self.set_section_config(section, self._override_config[section]) + for callback in self._watch_callback: + callback() def reload(self): """ diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 26c6d712..a48881fc 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -48,7 +48,7 @@ class Controller: self.symbols = Symbols() # Store settings shared by the different GUI will be replace by dedicated API later self._settings = None - + self._local_server = None self._config_file = os.path.join(Config.instance().config_dir, "gns3_controller.conf") log.info("Load controller configuration file {}".format(self._config_file)) @@ -57,6 +57,7 @@ class Controller: log.info("Start controller") server_config = Config.instance().get_section_config("Server") + Config.instance().listen_for_config_changes(self._update_config) host = server_config.get("host", "localhost") # If console_host is 0.0.0.0 client will use the ip they use @@ -71,15 +72,15 @@ class Controller: computes = yield from self._load_controller_settings() try: - yield from self.add_compute(compute_id="local", - name=name, - protocol=server_config.get("protocol", "http"), - host=host, - console_host=console_host, - port=server_config.getint("port", 3080), - user=server_config.get("user", ""), - password=server_config.get("password", ""), - force=True) + self._local_server = yield from self.add_compute(compute_id="local", + name=name, + protocol=server_config.get("protocol", "http"), + host=host, + console_host=console_host, + port=server_config.getint("port", 3080), + user=server_config.get("user", ""), + password=server_config.get("password", ""), + force=True) except aiohttp.web_exceptions.HTTPConflict as e: log.fatal("Can't acces to the local server, make sure anything else is not running on the same port") sys.exit(1) @@ -92,6 +93,16 @@ class Controller: yield from self.gns3vm.auto_start_vm() yield from self._project_auto_open() + def _update_config(self): + """ + Call this when the server configuration file + change + """ + if self._local_server: + server_config = Config.instance().get_section_config("Server") + self._local_server.user = server_config.get("user") + self._local_server.password = server_config.get("password") + @asyncio.coroutine def stop(self): log.info("Stop controller")