1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 11:18:11 +00:00

Fix GNS3VM settings are lost at startup

Fix #926
This commit is contained in:
Julien Duponchelle 2017-03-07 15:36:35 +01:00
parent d89ea83746
commit 7b642552dd
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
4 changed files with 21 additions and 34 deletions

View File

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

View File

@ -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={

View File

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

View File

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