Remove generic controller settings API endpoint.

This commit is contained in:
grossmj 2018-11-14 16:24:30 +08:00
parent cebb56387a
commit 98f04365b7
15 changed files with 120 additions and 118 deletions

View File

@ -61,8 +61,6 @@ sparse_memory_support = True
ghost_ios_support = True ghost_ios_support = True
[IOU] [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 ; Path of your .iourc file. If not provided, the file is searched in $HOME/.iourc
iourc_path = /home/gns3/.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. ; 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.

View File

@ -76,6 +76,7 @@ class IOUVM(BaseNode):
self._started = False self._started = False
self._nvram_watcher = None self._nvram_watcher = None
self._path = self.manager.get_abs_image_path(path) self._path = self.manager.get_abs_image_path(path)
self._license_check = True
# IOU settings # IOU settings
self._ethernet_adapters = [] self._ethernet_adapters = []
@ -358,6 +359,16 @@ class IOUVM(BaseNode):
except OSError as e: except OSError as e:
raise IOUError("Could not write the iourc file {}: {}".format(path, 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): async def _library_check(self):
""" """
Checks for missing shared library dependencies in the IOU image. 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). 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: 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: except ValueError:
raise IOUError("Invalid licence check setting") raise IOUError("Invalid licence check setting")
if license_check is False:
if server_wide_license_check is False:
return return
config = configparser.ConfigParser() config = configparser.ConfigParser()

View File

@ -54,9 +54,9 @@ class Controller:
self._notification = Notification(self) self._notification = Notification(self)
self.gns3vm = GNS3VM(self) self.gns3vm = GNS3VM(self)
self.symbols = Symbols() self.symbols = Symbols()
self._iou_license_settings = {"iourc_content": "",
# FIXME: store settings shared by the different GUI will be replace by dedicated API later "license_check": True}
self._settings = None self._config_loaded = False
self._appliances = {} self._appliances = {}
self._appliance_templates = {} self._appliance_templates = {}
self._appliance_templates_etag = None 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)) 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._appliances[appliance.id] = appliance
self.save() self.save()
self.notification.controller_emit("appliance.created", appliance.__json__())
return appliance return appliance
def get_appliance(self, appliance_id): def get_appliance(self, appliance_id):
@ -173,13 +174,12 @@ class Controller:
:param appliance_id: appliance identifier :param appliance_id: appliance identifier
""" """
appliance = self._appliances.get(appliance_id) appliance = self.get_appliance(appliance_id)
if not appliance:
raise aiohttp.web.HTTPNotFound(text="Appliance ID {} doesn't exist".format(appliance_id))
if appliance.builtin: if appliance.builtin:
raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is a builtin".format(appliance_id)) raise aiohttp.web.HTTPConflict(text="Appliance ID {} cannot be deleted because it is a builtin".format(appliance_id))
self._appliances.pop(appliance_id) self._appliances.pop(appliance_id)
self.save() self.save()
self.notification.controller_emit("appliance.deleted", appliance.__json__())
def load_appliances(self): def load_appliances(self):
@ -231,7 +231,7 @@ class Controller:
user=server_config.get("user", ""), user=server_config.get("user", ""),
password=server_config.get("password", ""), password=server_config.get("password", ""),
force=True) 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)) log.fatal("Cannot access to the local server, make sure something else is not running on the TCP port {}".format(port))
sys.exit(1) sys.exit(1)
for c in computes: for c in computes:
@ -276,14 +276,13 @@ class Controller:
Save the controller configuration on disk Save the controller configuration on disk
""" """
# We don't save during the loading otherwise we could lost stuff if self._config_loaded is False:
if self._settings is None:
return return
controller_settings = {"computes": [], controller_settings = {"computes": [],
"settings": self._settings,
"appliances": [], "appliances": [],
"gns3vm": self.gns3vm.__json__(), "gns3vm": self.gns3vm.__json__(),
"iou_license": self._iou_license_settings,
"appliance_templates_etag": self._appliance_templates_etag, "appliance_templates_etag": self._appliance_templates_etag,
"version": __version__} "version": __version__}
@ -321,14 +320,8 @@ class Controller:
controller_settings = json.load(f) controller_settings = json.load(f)
except (OSError, ValueError) as e: except (OSError, ValueError) as e:
log.critical("Cannot load configuration file '{}': {}".format(self._config_file, e)) log.critical("Cannot load configuration file '{}': {}".format(self._config_file, e))
self._settings = {}
return [] return []
if "settings" in controller_settings and controller_settings["settings"] is not None:
self._settings = controller_settings["settings"]
else:
self._settings = {}
# load the appliances # load the appliances
if "appliances" in controller_settings: if "appliances" in controller_settings:
for appliance_settings in controller_settings["appliances"]: for appliance_settings in controller_settings["appliances"]:
@ -345,9 +338,14 @@ class Controller:
if "gns3vm" in controller_settings: if "gns3vm" in controller_settings:
self.gns3vm.settings = controller_settings["gns3vm"] 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._appliance_templates_etag = controller_settings.get("appliance_templates_etag")
self.load_appliance_templates() self.load_appliance_templates()
self.load_appliances() self.load_appliances()
self._config_loaded = True
return controller_settings.get("computes", []) return controller_settings.get("computes", [])
async def load_projects(self): 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)) log.warning("Cannot load appliance template {} ('{}'): missing key {}".format(vm["appliance_id"], vm.get("name", "unknown"), e))
continue 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): 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 Add a server to the dictionary of compute servers controlled by this controller
@ -783,6 +761,14 @@ class Controller:
return self._appliances return self._appliances
@property
def iou_license(self):
"""
:returns: The dictionary of IOU license settings
"""
return self._iou_license_settings
def projects_directory(self): def projects_directory(self):
server_config = Config.instance().get_section_config("Server") server_config = Config.instance().get_section_config("Server")

View File

@ -96,8 +96,11 @@ class Appliance:
def update(self, **kwargs): def update(self, **kwargs):
#TODO: do not update appliance_id, builtin or appliance_type
self._settings.update(kwargs) 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): def __json__(self):
""" """

View File

@ -271,16 +271,17 @@ class Node:
if self._label is None: if self._label is None:
# Apply to label user style or default # Apply to label user style or default
try: try:
style = qt_font_to_style(self._project.controller.settings["GraphicsView"]["default_label_font"], style = None # FIXME: allow configuration of default label font & color on controller
self._project.controller.settings["GraphicsView"]["default_label_color"]) #style = qt_font_to_style(self._project.controller.settings["GraphicsView"]["default_label_font"],
# self._project.controller.settings["GraphicsView"]["default_label_color"])
except KeyError: 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 = { self._label = {
"y": round(self._height / 2 + 10) * -1, "y": round(self._height / 2 + 10) * -1,
"text": html.escape(self._name), "text": html.escape(self._name),
"style": style, "style": style, # None: means the client will apply its default style
"x": None, # None: mean the client should center it "x": None, # None: means the client should center it
"rotation": 0 "rotation": 0
} }
@ -483,11 +484,11 @@ class Node:
try: try:
# For IOU we need to send the licence everytime # For IOU we need to send the licence everytime
if self.node_type == "iou": if self.node_type == "iou":
try: license_check = self._project.controller.iou_license.get("license_check", True)
licence = self._project.controller.settings["IOU"]["iourc_content"] iourc_content = self._project.controller.iou_license.get("iourc_content", None)
except KeyError: if license_check and not iourc_content:
raise aiohttp.web.HTTPConflict(text="IOU licence is not configured") 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: else:
await self.post("/start", data=data, timeout=240) await self.post("/start", data=data, timeout=240)
except asyncio.TimeoutError: except asyncio.TimeoutError:

View File

@ -103,6 +103,11 @@ class ApplianceHandler:
controller = Controller.instance() controller = Controller.instance()
appliance = controller.get_appliance(request.match_info["appliance_id"]) 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) appliance.update(**request.json)
response.set_status(200) response.set_status(200)
response.json(appliance) response.json(appliance)

View File

@ -15,12 +15,10 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from aiohttp.web import HTTPConflict
from gns3server.web.route import Route from gns3server.web.route import Route
from gns3server.controller import Controller from gns3server.controller import Controller
from gns3server.schemas.gns3vm import GNS3VM_SETTINGS_SCHEMA from gns3server.schemas.gns3vm import GNS3VM_SETTINGS_SCHEMA
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@ -19,6 +19,7 @@ from gns3server.web.route import Route
from gns3server.config import Config from gns3server.config import Config
from gns3server.controller import Controller from gns3server.controller import Controller
from gns3server.schemas.version import VERSION_SCHEMA from gns3server.schemas.version import VERSION_SCHEMA
from gns3server.schemas.iou_license import IOU_LICENSE_SETTINGS_SCHEMA
from gns3server.version import __version__ from gns3server.version import __version__
from aiohttp.web import HTTPConflict, HTTPForbidden from aiohttp.web import HTTPConflict, HTTPForbidden
@ -102,37 +103,31 @@ class ServerHandler:
response.json({"version": __version__}) response.json({"version": __version__})
@Route.get( @Route.get(
r"/settings", r"/iou_license",
description="Retrieve gui settings from the server. Temporary will we removed in later release") description="Get the IOU license settings",
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",
status_codes={ 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): async def update(request, response):
controller = Controller.instance()
if controller.settings is None: # Server is not loaded ignore settings update to prevent buggy client sync issue controller = Controller().instance()
return iou_license = controller.iou_license
try: iou_license.update(request.json)
controller.settings = request.json controller.save()
#controller.save() response.json(iou_license)
except (OSError, PermissionError) as e:
raise HTTPConflict(text="Can't save the settings {}".format(str(e)))
response.json(controller.settings)
response.set_status(201) response.set_status(201)
@Route.post( @Route.post(

View File

@ -58,13 +58,16 @@ APPLIANCE_OBJECT_SCHEMA = {
} }
APPLIANCE_CREATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA) APPLIANCE_CREATE_SCHEMA = copy.deepcopy(APPLIANCE_OBJECT_SCHEMA)
# create schema
# these properties are not required to create an appliance # these properties are not required to create an appliance
APPLIANCE_CREATE_SCHEMA["required"].remove("appliance_id") APPLIANCE_CREATE_SCHEMA["required"].remove("appliance_id")
APPLIANCE_CREATE_SCHEMA["required"].remove("compute_id") APPLIANCE_CREATE_SCHEMA["required"].remove("compute_id")
APPLIANCE_CREATE_SCHEMA["required"].remove("default_name_format") APPLIANCE_CREATE_SCHEMA["required"].remove("default_name_format")
APPLIANCE_CREATE_SCHEMA["required"].remove("symbol") 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"] del APPLIANCE_UPDATE_SCHEMA["required"]
APPLIANCE_USAGE_SCHEMA = { APPLIANCE_USAGE_SCHEMA = {

View File

@ -104,6 +104,10 @@ IOU_START_SCHEMA = {
"iourc_content": { "iourc_content": {
"description": "Content of the iourc file. Ignored if Null", "description": "Content of the iourc file. Ignored if Null",
"type": ["string", "null"] "type": ["string", "null"]
},
"license_check": {
"description": "Whether the license should be checked",
"type": "boolean"
} }
} }
} }

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- 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 # 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 # 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 # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
""" IOU_LICENSE_SETTINGS_SCHEMA = {
This test suite check /version endpoint "$schema": "http://json-schema.org/draft-04/schema#",
It's also used for unittest the HTTP implementation. "description": "IOU license",
""" "type": "object",
"properties": {
from gns3server.config import Config "iourc_content": {
"type": "string",
"description": "Content of iourc file"
def test_settings(http_controller): },
query = {"test": True} "license_check": {
response = http_controller.post('/settings', query, example=True) "type": "boolean",
assert response.status == 201 "description": "Whether the license must be checked or not",
response = http_controller.get('/settings', example=True) },
assert response.status == 200 },
assert response.json["test"] is True "additionalProperties": False
assert response.json["modification_uuid"] is not None }

View File

@ -20,11 +20,11 @@ LABEL_OBJECT_SCHEMA = {
"properties": { "properties": {
"text": {"type": "string"}, "text": {"type": "string"},
"style": { "style": {
"description": "SVG style attribute", "description": "SVG style attribute. Apply default style if null",
"type": "string" "type": ["string", "null"]
}, },
"x": { "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"] "type": ["integer", "null"]
}, },
"y": { "y": {

View File

@ -201,7 +201,7 @@ def controller(tmpdir, controller_config_path):
Controller._instance = None Controller._instance = None
controller = Controller.instance() controller = Controller.instance()
controller._config_file = controller_config_path controller._config_file = controller_config_path
controller._settings = {} controller._config_loaded = True
return controller return controller

View File

@ -35,7 +35,7 @@ def test_save(controller, controller_config_path):
data = json.load(f) data = json.load(f)
assert data["computes"] == [] assert data["computes"] == []
assert data["version"] == __version__ assert data["version"] == __version__
assert data["settings"] == {} assert data["iou_license"] == controller.iou_license
assert data["gns3vm"] == controller.gns3vm.__json__() assert data["gns3vm"] == controller.gns3vm.__json__()
@ -53,12 +53,10 @@ def test_load_controller_settings(controller, controller_config_path, async_run)
"compute_id": "test1" "compute_id": "test1"
} }
] ]
data["settings"] = {"IOU": {"test": True}}
data["gns3vm"] = {"vmname": "Test VM"} data["gns3vm"] = {"vmname": "Test VM"}
with open(controller_config_path, "w+") as f: with open(controller_config_path, "w+") as f:
json.dump(data, f) json.dump(data, f)
assert len(async_run(controller._load_controller_settings())) == 1 assert len(async_run(controller._load_controller_settings())) == 1
assert controller.settings["IOU"]
assert controller.gns3vm.settings["vmname"] == "Test VM" 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" 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): def test_load_projects(controller, projects_dir, async_run):
controller.save() controller.save()

View File

@ -269,7 +269,7 @@ def test_symbol(node, symbols_dir):
assert node.height == 71 assert node.height == 71
assert node.label["x"] is None assert node.label["x"] is None
assert node.label["y"] == -40 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")) shutil.copy(os.path.join("gns3server", "symbols", "cloud.svg"), os.path.join(symbols_dir, "cloud2.svg"))
node.symbol = "cloud2.svg" node.symbol = "cloud2.svg"
@ -298,7 +298,7 @@ def test_label_with_default_label_font(node):
node._label = None node._label = None
node.symbol = ":/symbols/dslam.svg" 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): 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): with pytest.raises(aiohttp.web.HTTPConflict):
async_run(node.start()) async_run(node.start())
controller.settings["IOU"] = {"iourc_content": "aa"} controller._iou_license_settings = {"license_check": True, "iourc_content": "aa"}
async_run(node.start()) 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): def test_stop(node, compute, project, async_run):