From 98f04365b7ec2b2bbb044cb3d403f5bbc2ad5c63 Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 14 Nov 2018 16:24:30 +0800 Subject: [PATCH] Remove generic controller settings API endpoint. --- conf/gns3_server.conf | 2 - gns3server/compute/iou/iou_vm.py | 22 ++++++- gns3server/controller/__init__.py | 58 +++++++------------ gns3server/controller/appliance.py | 5 +- gns3server/controller/node.py | 19 +++--- .../api/controller/appliance_handler.py | 5 ++ .../api/controller/gns3_vm_handler.py | 2 - .../handlers/api/controller/server_handler.py | 53 ++++++++--------- gns3server/schemas/appliance.py | 7 ++- gns3server/schemas/iou.py | 4 ++ .../schemas/iou_license.py | 34 +++++------ gns3server/schemas/label.py | 6 +- tests/conftest.py | 2 +- tests/controller/test_controller.py | 11 +--- tests/controller/test_node.py | 8 +-- 15 files changed, 120 insertions(+), 118 deletions(-) rename tests/handlers/api/controller/test_settings.py => gns3server/schemas/iou_license.py (54%) diff --git a/conf/gns3_server.conf b/conf/gns3_server.conf index bcf188a1..ffab4ec8 100644 --- a/conf/gns3_server.conf +++ b/conf/gns3_server.conf @@ -61,8 +61,6 @@ sparse_memory_support = True ghost_ios_support = True [IOU] -; iouyap executable path, default: search in PATH -;iouyap_path = iouyap ; Path of your .iourc file. If not provided, the file is searched in $HOME/.iourc iourc_path = /home/gns3/.iourc ; Validate if the iourc license file is correct. If you turn this off and your licence is invalid IOU will not start and no errors will be shown. diff --git a/gns3server/compute/iou/iou_vm.py b/gns3server/compute/iou/iou_vm.py index 7f22d055..170d3560 100644 --- a/gns3server/compute/iou/iou_vm.py +++ b/gns3server/compute/iou/iou_vm.py @@ -76,6 +76,7 @@ class IOUVM(BaseNode): self._started = False self._nvram_watcher = None self._path = self.manager.get_abs_image_path(path) + self._license_check = True # IOU settings self._ethernet_adapters = [] @@ -358,6 +359,16 @@ class IOUVM(BaseNode): except OSError as e: raise IOUError("Could not write the iourc file {}: {}".format(path, e)) + @property + def license_check(self): + + return self._license_check + + @license_check.setter + def license_check(self, value): + + self._license_check = value + async def _library_check(self): """ Checks for missing shared library dependencies in the IOU image. @@ -379,11 +390,18 @@ class IOUVM(BaseNode): """ Checks for a valid IOU key in the iourc file (paranoid mode). """ + + # license check is sent by the controller + if self.license_check is False: + return + try: - license_check = self._config().getboolean("license_check", True) + # we allow license check to be disabled server wide + server_wide_license_check = self._config().getboolean("license_check", True) except ValueError: raise IOUError("Invalid licence check setting") - if license_check is False: + + if server_wide_license_check is False: return config = configparser.ConfigParser() diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 242f80a9..da316b52 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -54,9 +54,9 @@ class Controller: self._notification = Notification(self) self.gns3vm = GNS3VM(self) self.symbols = Symbols() - - # FIXME: store settings shared by the different GUI will be replace by dedicated API later - self._settings = None + self._iou_license_settings = {"iourc_content": "", + "license_check": True} + self._config_loaded = False self._appliances = {} self._appliance_templates = {} self._appliance_templates_etag = None @@ -150,6 +150,7 @@ class Controller: raise aiohttp.web.HTTPConflict(text="Cannot create new appliance: key '{}' is missing for appliance ID '{}'".format(e, appliance_id)) self._appliances[appliance.id] = appliance self.save() + self.notification.controller_emit("appliance.created", appliance.__json__()) return appliance def get_appliance(self, appliance_id): @@ -173,13 +174,12 @@ class Controller: :param appliance_id: appliance identifier """ - appliance = self._appliances.get(appliance_id) - if not appliance: - raise aiohttp.web.HTTPNotFound(text="Appliance ID {} doesn't exist".format(appliance_id)) + appliance = self.get_appliance(appliance_id) if appliance.builtin: raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is a builtin".format(appliance_id)) self._appliances.pop(appliance_id) self.save() + self.notification.controller_emit("appliance.deleted", appliance.__json__()) def load_appliances(self): @@ -231,7 +231,7 @@ class Controller: user=server_config.get("user", ""), password=server_config.get("password", ""), force=True) - except aiohttp.web.HTTPConflict as e: + except aiohttp.web.HTTPConflict: log.fatal("Cannot access to the local server, make sure something else is not running on the TCP port {}".format(port)) sys.exit(1) for c in computes: @@ -276,14 +276,13 @@ 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: + if self._config_loaded is False: return controller_settings = {"computes": [], - "settings": self._settings, "appliances": [], "gns3vm": self.gns3vm.__json__(), + "iou_license": self._iou_license_settings, "appliance_templates_etag": self._appliance_templates_etag, "version": __version__} @@ -321,14 +320,8 @@ class Controller: controller_settings = json.load(f) except (OSError, ValueError) as e: log.critical("Cannot load configuration file '{}': {}".format(self._config_file, e)) - self._settings = {} return [] - if "settings" in controller_settings and controller_settings["settings"] is not None: - self._settings = controller_settings["settings"] - else: - self._settings = {} - # load the appliances if "appliances" in controller_settings: for appliance_settings in controller_settings["appliances"]: @@ -345,9 +338,14 @@ class Controller: if "gns3vm" in controller_settings: self.gns3vm.settings = controller_settings["gns3vm"] + # load the IOU license settings + if "iou_license" in controller_settings: + self._iou_license_settings = controller_settings["iou_license"] + self._appliance_templates_etag = controller_settings.get("appliance_templates_etag") self.load_appliance_templates() self.load_appliances() + self._config_loaded = True return controller_settings.get("computes", []) async def load_projects(self): @@ -528,26 +526,6 @@ class Controller: log.warning("Cannot load appliance template {} ('{}'): missing key {}".format(vm["appliance_id"], vm.get("name", "unknown"), e)) continue - self._settings = {} - - @property - def settings(self): - """ - Store settings shared by the different GUI will be replace by dedicated API later. Dictionnary - """ - - return self._settings - - @settings.setter - def settings(self, val): - - self._settings = val - self._settings["modification_uuid"] = str(uuid.uuid4()) # We add a modification id to the settings to help the gui to detect changes - self.save() - self.load_appliance_templates() - self.load_appliances() - self.notification.controller_emit("settings.updated", val) - async def add_compute(self, compute_id=None, name=None, force=False, connect=True, **kwargs): """ Add a server to the dictionary of compute servers controlled by this controller @@ -783,6 +761,14 @@ class Controller: return self._appliances + @property + def iou_license(self): + """ + :returns: The dictionary of IOU license settings + """ + + return self._iou_license_settings + def projects_directory(self): server_config = Config.instance().get_section_config("Server") diff --git a/gns3server/controller/appliance.py b/gns3server/controller/appliance.py index 5616bf77..ecc304ce 100644 --- a/gns3server/controller/appliance.py +++ b/gns3server/controller/appliance.py @@ -96,8 +96,11 @@ class Appliance: def update(self, **kwargs): - #TODO: do not update appliance_id, builtin or appliance_type self._settings.update(kwargs) + from gns3server.controller import Controller + controller = Controller.instance() + controller.notification.controller_emit("appliance.updated", self.__json__()) + controller.save() def __json__(self): """ diff --git a/gns3server/controller/node.py b/gns3server/controller/node.py index 95227606..2e9047bc 100644 --- a/gns3server/controller/node.py +++ b/gns3server/controller/node.py @@ -271,16 +271,17 @@ class Node: if self._label is None: # Apply to label user style or default try: - style = qt_font_to_style(self._project.controller.settings["GraphicsView"]["default_label_font"], - self._project.controller.settings["GraphicsView"]["default_label_color"]) + style = None # FIXME: allow configuration of default label font & color on controller + #style = qt_font_to_style(self._project.controller.settings["GraphicsView"]["default_label_font"], + # self._project.controller.settings["GraphicsView"]["default_label_color"]) except KeyError: - style = "font-size: 10;font-familly: Verdana" + style = "font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;" self._label = { "y": round(self._height / 2 + 10) * -1, "text": html.escape(self._name), - "style": style, - "x": None, # None: mean the client should center it + "style": style, # None: means the client will apply its default style + "x": None, # None: means the client should center it "rotation": 0 } @@ -483,11 +484,11 @@ class Node: try: # For IOU we need to send the licence everytime if self.node_type == "iou": - try: - licence = self._project.controller.settings["IOU"]["iourc_content"] - except KeyError: + license_check = self._project.controller.iou_license.get("license_check", True) + iourc_content = self._project.controller.iou_license.get("iourc_content", None) + if license_check and not iourc_content: raise aiohttp.web.HTTPConflict(text="IOU licence is not configured") - await self.post("/start", timeout=240, data={"iourc_content": licence}) + await self.post("/start", timeout=240, data={"license_check": license_check, "iourc_content": iourc_content}) else: await self.post("/start", data=data, timeout=240) except asyncio.TimeoutError: diff --git a/gns3server/handlers/api/controller/appliance_handler.py b/gns3server/handlers/api/controller/appliance_handler.py index 06e6f56c..4c3115f7 100644 --- a/gns3server/handlers/api/controller/appliance_handler.py +++ b/gns3server/handlers/api/controller/appliance_handler.py @@ -103,6 +103,11 @@ class ApplianceHandler: controller = Controller.instance() appliance = controller.get_appliance(request.match_info["appliance_id"]) + # Ignore these because we only use them when creating a appliance + request.json.pop("appliance_id", None) + request.json.pop("appliance_type", None) + request.json.pop("compute_id", None) + request.json.pop("builtin", None) appliance.update(**request.json) response.set_status(200) response.json(appliance) diff --git a/gns3server/handlers/api/controller/gns3_vm_handler.py b/gns3server/handlers/api/controller/gns3_vm_handler.py index 5371e3d0..d1c56b6a 100644 --- a/gns3server/handlers/api/controller/gns3_vm_handler.py +++ b/gns3server/handlers/api/controller/gns3_vm_handler.py @@ -15,12 +15,10 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from aiohttp.web import HTTPConflict from gns3server.web.route import Route from gns3server.controller import Controller from gns3server.schemas.gns3vm import GNS3VM_SETTINGS_SCHEMA - import logging log = logging.getLogger(__name__) diff --git a/gns3server/handlers/api/controller/server_handler.py b/gns3server/handlers/api/controller/server_handler.py index a7422816..ae1160c4 100644 --- a/gns3server/handlers/api/controller/server_handler.py +++ b/gns3server/handlers/api/controller/server_handler.py @@ -19,6 +19,7 @@ from gns3server.web.route import Route from gns3server.config import Config from gns3server.controller import Controller from gns3server.schemas.version import VERSION_SCHEMA +from gns3server.schemas.iou_license import IOU_LICENSE_SETTINGS_SCHEMA from gns3server.version import __version__ from aiohttp.web import HTTPConflict, HTTPForbidden @@ -102,37 +103,31 @@ class ServerHandler: response.json({"version": __version__}) @Route.get( - r"/settings", - description="Retrieve gui settings from the server. Temporary will we removed in later release") - async def read_settings(request, response): - - settings = None - while True: - # The init of the server could take some times - # we ensure settings are loaded before returning them - settings = Controller.instance().settings - - if settings is not None: - break - await asyncio.sleep(0.5) - response.json(settings) - - @Route.post( - r"/settings", - description="Write gui settings on the server. Temporary will we removed in later releases", + r"/iou_license", + description="Get the IOU license settings", status_codes={ - 201: "Settings saved" + 200: "IOU license settings returned" + }, + output_schema=IOU_LICENSE_SETTINGS_SCHEMA) + def show(request, response): + + response.json(Controller.instance().iou_license) + + @Route.put( + r"/iou_license", + description="Update the IOU license settings", + input_schema=IOU_LICENSE_SETTINGS_SCHEMA, + output_schema=IOU_LICENSE_SETTINGS_SCHEMA, + status_codes={ + 201: "IOU license settings updated" }) - def write_settings(request, response): - controller = Controller.instance() - if controller.settings is None: # Server is not loaded ignore settings update to prevent buggy client sync issue - return - try: - controller.settings = request.json - #controller.save() - except (OSError, PermissionError) as e: - raise HTTPConflict(text="Can't save the settings {}".format(str(e))) - response.json(controller.settings) + async def update(request, response): + + controller = Controller().instance() + iou_license = controller.iou_license + iou_license.update(request.json) + controller.save() + response.json(iou_license) response.set_status(201) @Route.post( diff --git a/gns3server/schemas/appliance.py b/gns3server/schemas/appliance.py index 2cbb9ea8..408e33c4 100644 --- a/gns3server/schemas/appliance.py +++ b/gns3server/schemas/appliance.py @@ -58,13 +58,16 @@ APPLIANCE_OBJECT_SCHEMA = { } APPLIANCE_CREATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) + +# create schema # these properties are not required to create an appliance APPLIANCE_CREATE_SCHEMA["required"].remove("appliance_id") APPLIANCE_CREATE_SCHEMA["required"].remove("compute_id") APPLIANCE_CREATE_SCHEMA["required"].remove("default_name_format") APPLIANCE_CREATE_SCHEMA["required"].remove("symbol") -APPLIANCE_UPDATE_SCHEMA = copy.deepcopy(APPLIANCE_CREATE_SCHEMA) -#APPLIANCE_UPDATE_SCHEMA["additionalProperties"] = False + +# update schema +APPLIANCE_UPDATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) del APPLIANCE_UPDATE_SCHEMA["required"] APPLIANCE_USAGE_SCHEMA = { diff --git a/gns3server/schemas/iou.py b/gns3server/schemas/iou.py index 3243c890..8bad7a9e 100644 --- a/gns3server/schemas/iou.py +++ b/gns3server/schemas/iou.py @@ -104,6 +104,10 @@ IOU_START_SCHEMA = { "iourc_content": { "description": "Content of the iourc file. Ignored if Null", "type": ["string", "null"] + }, + "license_check": { + "description": "Whether the license should be checked", + "type": "boolean" } } } diff --git a/tests/handlers/api/controller/test_settings.py b/gns3server/schemas/iou_license.py similarity index 54% rename from tests/handlers/api/controller/test_settings.py rename to gns3server/schemas/iou_license.py index a87df472..4b2262cf 100644 --- a/tests/handlers/api/controller/test_settings.py +++ b/gns3server/schemas/iou_license.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2015 GNS3 Technologies Inc. +# Copyright (C) 2018 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 @@ -15,19 +15,19 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -""" -This test suite check /version endpoint -It's also used for unittest the HTTP implementation. -""" - -from gns3server.config import Config - - -def test_settings(http_controller): - query = {"test": True} - response = http_controller.post('/settings', query, example=True) - assert response.status == 201 - response = http_controller.get('/settings', example=True) - assert response.status == 200 - assert response.json["test"] is True - assert response.json["modification_uuid"] is not None +IOU_LICENSE_SETTINGS_SCHEMA = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "IOU license", + "type": "object", + "properties": { + "iourc_content": { + "type": "string", + "description": "Content of iourc file" + }, + "license_check": { + "type": "boolean", + "description": "Whether the license must be checked or not", + }, + }, + "additionalProperties": False +} diff --git a/gns3server/schemas/label.py b/gns3server/schemas/label.py index eae97417..eb5c2de1 100644 --- a/gns3server/schemas/label.py +++ b/gns3server/schemas/label.py @@ -20,11 +20,11 @@ LABEL_OBJECT_SCHEMA = { "properties": { "text": {"type": "string"}, "style": { - "description": "SVG style attribute", - "type": "string" + "description": "SVG style attribute. Apply default style if null", + "type": ["string", "null"] }, "x": { - "description": "Relative X position of the label. If null center it", + "description": "Relative X position of the label. Center it if null", "type": ["integer", "null"] }, "y": { diff --git a/tests/conftest.py b/tests/conftest.py index bb24dca5..2c4260c5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -201,7 +201,7 @@ def controller(tmpdir, controller_config_path): Controller._instance = None controller = Controller.instance() controller._config_file = controller_config_path - controller._settings = {} + controller._config_loaded = True return controller diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 18e87fd6..0d99a534 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -35,7 +35,7 @@ def test_save(controller, controller_config_path): data = json.load(f) assert data["computes"] == [] assert data["version"] == __version__ - assert data["settings"] == {} + assert data["iou_license"] == controller.iou_license assert data["gns3vm"] == controller.gns3vm.__json__() @@ -53,12 +53,10 @@ def test_load_controller_settings(controller, controller_config_path, async_run) "compute_id": "test1" } ] - data["settings"] = {"IOU": {"test": True}} data["gns3vm"] = {"vmname": "Test VM"} with open(controller_config_path, "w+") as f: json.dump(data, f) assert len(async_run(controller._load_controller_settings())) == 1 - assert controller.settings["IOU"] assert controller.gns3vm.settings["vmname"] == "Test VM" @@ -199,13 +197,6 @@ def test_import_remote_gns3vm_1_x(controller, controller_config_path, async_run) assert controller.gns3vm.settings["vmname"] == "http://127.0.0.1:3081" -def test_settings(controller): - controller._notification = MagicMock() - controller.settings = {"a": 1} - controller._notification.controller_emit.assert_called_with("settings.updated", controller.settings) - assert controller.settings["modification_uuid"] is not None - - def test_load_projects(controller, projects_dir, async_run): controller.save() diff --git a/tests/controller/test_node.py b/tests/controller/test_node.py index a80bc9a1..715cdbbf 100644 --- a/tests/controller/test_node.py +++ b/tests/controller/test_node.py @@ -269,7 +269,7 @@ def test_symbol(node, symbols_dir): assert node.height == 71 assert node.label["x"] is None assert node.label["y"] == -40 - assert node.label["style"] == "font-size: 10;font-familly: Verdana" + assert node.label["style"] == None#"font-family: TypeWriter;font-size: 10.0;font-weight: bold;fill: #000000;fill-opacity: 1.0;" shutil.copy(os.path.join("gns3server", "symbols", "cloud.svg"), os.path.join(symbols_dir, "cloud2.svg")) node.symbol = "cloud2.svg" @@ -298,7 +298,7 @@ def test_label_with_default_label_font(node): node._label = None node.symbol = ":/symbols/dslam.svg" - assert node.label["style"] == "font-family: TypeWriter;font-size: 10;font-weight: bold;fill: #ff0000;fill-opacity: 1.0;" + assert node.label["style"] == None #"font-family: TypeWriter;font-size: 10;font-weight: bold;fill: #ff0000;fill-opacity: 1.0;" def test_update(node, compute, project, async_run, controller): @@ -405,9 +405,9 @@ def test_start_iou(compute, project, async_run, controller): with pytest.raises(aiohttp.web.HTTPConflict): async_run(node.start()) - controller.settings["IOU"] = {"iourc_content": "aa"} + controller._iou_license_settings = {"license_check": True, "iourc_content": "aa"} async_run(node.start()) - compute.post.assert_called_with("/projects/{}/iou/nodes/{}/start".format(node.project.id, node.id), timeout=240, data={"iourc_content": "aa"}) + compute.post.assert_called_with("/projects/{}/iou/nodes/{}/start".format(node.project.id, node.id), timeout=240, data={"license_check": True, "iourc_content": "aa"}) def test_stop(node, compute, project, async_run):