Remove generic controller settings API endpoint.

pull/1455/head
grossmj 6 years ago
parent cebb56387a
commit 98f04365b7

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

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

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

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

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

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

@ -15,12 +15,10 @@
# You should have received a copy of the GNU General Public License
# 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.controller import Controller
from gns3server.schemas.gns3vm import GNS3VM_SETTINGS_SCHEMA
import logging
log = logging.getLogger(__name__)

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

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

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

@ -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 <http://www.gnu.org/licenses/>.
"""
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
}

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

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

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

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

Loading…
Cancel
Save