From bc5b5969eb2fc235e7e7a1662061ab0676359172 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 6 Sep 2016 13:06:20 +0200 Subject: [PATCH] Import GNS3VM settings from 1.5 Fix #643 --- gns3server/controller/__init__.py | 24 +++++- gns3server/controller/gns3vm/__init__.py | 15 ++-- tests/controller/test_controller.py | 94 +++++++++++++++++++++++- 3 files changed, 123 insertions(+), 10 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 82a7ffce..bfa35fff 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -180,7 +180,8 @@ class Controller: if os.path.exists(config_file): with open(config_file) as f: data = json.load(f) - for remote in data.get("Servers", {}).get("remote_servers", []): + server_settings = data.get("Servers", {}) + for remote in server_settings.get("remote_servers", []): yield from self.add_compute( host=remote.get("host", "localhost"), port=remote.get("port", 3080), @@ -189,6 +190,27 @@ class Controller: user=remote.get("user"), password=remote.get("password") ) + if "vm" in server_settings: + vm_settings = server_settings["vm"] + if vm_settings["virtualization"] == "VMware": + engine = "vmware" + vmname = vm_settings.get("vmname", "") + elif vm_settings["virtualization"] == "VirtualBox": + engine = "virtualbox" + vmname = vm_settings.get("vmname", "") + else: + engine = "remote" + # In case of remote server we match the compute with url parameter + for compute in self._computes.values(): + if compute.host == vm_settings.get("remote_vm_host") and compute.port == vm_settings.get("remote_vm_port"): + vmname = compute.name + self.gns3vm.settings = { + "engine": engine, + "enable": vm_settings.get("auto_start", False), + "auto_stop": vm_settings.get("auto_stop", True), + "headless": vm_settings.get("headless", False), + "vmname": vmname + } @property def settings(self): diff --git a/gns3server/controller/gns3vm/__init__.py b/gns3server/controller/gns3vm/__init__.py index 7ced6df7..9eb9e7c6 100644 --- a/gns3server/controller/gns3vm/__init__.py +++ b/gns3server/controller/gns3vm/__init__.py @@ -222,6 +222,7 @@ class GNS3VM: name="GNS3 VM ({})".format(self._current_engine().vmname), host=None, force=True) + @asyncio.coroutine def auto_stop_vm(self): if self.enable and self.auto_stop: @@ -241,13 +242,13 @@ class GNS3VM: engine.vmname = self._settings["vmname"] yield from engine.start() yield from self._controller.add_compute(compute_id="vm", - name="GNS3 VM ({})".format(engine.vmname), - protocol=self.protocol, - host=self.ip_address, - port=self.port, - user=self.user, - password=self.password, - force=True) + name="GNS3 VM ({})".format(engine.vmname), + protocol=self.protocol, + host=self.ip_address, + port=self.port, + user=self.user, + password=self.password, + force=True) @locked_coroutine def _stop(self): diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 87e3cec1..e4457409 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -80,10 +80,10 @@ def test_load(controller, controller_config_path, async_run): assert controller.gns3vm.settings["vmname"] == "Test VM" -def test_import_computes(controller, controller_config_path, async_run): +def test_import_computes_1_x(controller, controller_config_path, async_run): """ At first start the server should import the - computes from the gns3_gui + computes from the gns3_gui 1.X """ gns3_gui_conf = { "Servers": { @@ -115,6 +115,96 @@ def test_import_computes(controller, controller_config_path, async_run): assert compute.password is None +def test_import_gns3vm_1_x(controller, controller_config_path, async_run): + """ + At first start the server should import the + gns3vm settings from the gns3_gui 1.X + """ + gns3_gui_conf = { + "Servers": { + "vm": { + "adjust_local_server_ip": True, + "auto_start": True, + "auto_stop": False, + "headless": True, + "remote_vm_host": "", + "remote_vm_password": "", + "remote_vm_port": 3080, + "remote_vm_protocol": "http", + "remote_vm_url": "", + "remote_vm_user": "", + "virtualization": "VMware", + "vmname": "GNS3 VM", + "vmx_path": "/Users/joe/Documents/Virtual Machines.localized/GNS3 VM.vmwarevm/GNS3 VM.vmx" + } + } + } + config_dir = os.path.dirname(controller_config_path) + os.makedirs(config_dir, exist_ok=True) + with open(os.path.join(config_dir, "gns3_gui.conf"), "w+") as f: + json.dump(gns3_gui_conf, f) + + controller.gns3vm.settings["engine"] = None + async_run(controller.load()) + assert controller.gns3vm.settings["engine"] == "vmware" + assert controller.gns3vm.settings["enable"] + assert controller.gns3vm.settings["headless"] + assert controller.gns3vm.settings["auto_stop"] is False + assert controller.gns3vm.settings["vmname"] == "GNS3 VM" + + +def test_import_remote_gns3vm_1_x(controller, controller_config_path, async_run): + """ + At first start the server should import the + computes and remote GNS3 VM from the gns3_gui 1.X + """ + gns3_gui_conf = { + "Servers": { + "remote_servers": [ + { + "host": "127.0.0.1", + "password": "", + "port": 3080, + "protocol": "http", + "url": "http://127.0.0.1:3080", + "user": "" + }, + { + "host": "127.0.0.1", + "password": "", + "port": 3081, + "protocol": "http", + "url": "http://127.0.0.1:3081", + "user": "" + } + ], + "vm": { + "adjust_local_server_ip": True, + "auto_start": True, + "auto_stop": False, + "headless": True, + "remote_vm_host": "127.0.0.1", + "remote_vm_password": "", + "remote_vm_port": 3081, + "remote_vm_protocol": "http", + "remote_vm_url": "http://127.0.0.1:3081", + "remote_vm_user": "", + "virtualization": "remote", + "vmname": "GNS3 VM", + "vmx_path": "/Users/joe/Documents/Virtual Machines.localized/GNS3 VM.vmwarevm/GNS3 VM.vmx" + } + } + } + config_dir = os.path.dirname(controller_config_path) + os.makedirs(config_dir, exist_ok=True) + with open(os.path.join(config_dir, "gns3_gui.conf"), "w+") as f: + json.dump(gns3_gui_conf, f) + + async_run(controller.load()) + assert controller.gns3vm.settings["engine"] == "remote" + assert controller.gns3vm.settings["vmname"] == "http://127.0.0.1:3081" + + def test_settings(controller): controller._notification = MagicMock() controller.settings = {"a": 1}