The server has now 2 notification streams

* A new one for controller related events (compute, appliance templates etc.)
* The existing one for project related events (links, nodes etc.)
This commit is contained in:
grossmj committed 2018-08-16 21:31:57 +07:00
1 parent dac3ba19b7
commit 7f72b90b0e
364 files changed
+12364 -9923

No files matched your search

+4 -4
View File
@@ -429,7 +429,7 @@ class Controller:
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_appliances()
self.notification.emit("settings.updated", val)
self.notification.controller_emit("settings.updated", val)
@asyncio.coroutine
def add_compute(self, compute_id=None, name=None, force=False, connect=True, **kwargs):
@@ -463,12 +463,12 @@ class Controller:
self.save()
if connect:
yield from compute.connect()
self.notification.emit("compute.created", compute.__json__())
self.notification.controller_emit("compute.created", compute.__json__())
return compute
else:
if connect:
yield from self._computes[compute_id].connect()
self.notification.emit("compute.updated", self._computes[compute_id].__json__())
self.notification.controller_emit("compute.updated", self._computes[compute_id].__json__())
return self._computes[compute_id]
@asyncio.coroutine
@@ -509,7 +509,7 @@ class Controller:
yield from compute.close()
del self._computes[compute_id]
self.save()
self.notification.emit("compute.deleted", compute.__json__())
self.notification.controller_emit("compute.deleted", compute.__json__())
@property
def notification(self):
+4 -4
View File
@@ -158,7 +158,7 @@ class Compute:
if self._http_session:
self._http_session.close()
self._connected = False
self._controller.notification.emit("compute.updated", self.__json__())
self._controller.notification.controller_emit("compute.updated", self.__json__())
self._controller.save()
@asyncio.coroutine
@@ -442,7 +442,7 @@ class Compute:
self._notifications = asyncio.gather(self._connect_notification())
self._connected = True
self._connection_failure = 0
self._controller.notification.emit("compute.updated", self.__json__())
self._controller.notification.controller_emit("compute.updated", self.__json__())
@asyncio.coroutine
def _connect_notification(self):
@@ -468,7 +468,7 @@ class Compute:
if action == "ping":
self._cpu_usage_percent = event["cpu_usage_percent"]
self._memory_usage_percent = event["memory_usage_percent"]
self._controller.notification.emit("compute.updated", self.__json__())
self._controller.notification.controller_emit("compute.updated", self.__json__())
else:
yield from self._controller.notification.dispatch(action, event, compute_id=self.id)
if self._ws:
@@ -480,7 +480,7 @@ class Compute:
self._ws = None
self._cpu_usage_percent = None
self._memory_usage_percent = None
self._controller.notification.emit("compute.updated", self.__json__())
self._controller.notification.controller_emit("compute.updated", self.__json__())
def _getUrl(self, path):
host = self._host
+1 -1
View File
@@ -186,7 +186,7 @@ class Drawing:
data = self.__json__()
if not svg_changed:
del data["svg"]
self._project.controller.notification.emit("drawing.updated", data)
self._project.controller.notification.project_emit("drawing.updated", data)
self._project.dump()
def __json__(self, topology_dump=False):
+1 -1
View File
@@ -72,7 +72,7 @@ def export_project(project, temporary_dir, include_images=False, keep_compute_id
except OSError as e:
msg = "Could not export file {}: {}".format(path, e)
log.warning(msg)
project.controller.notification.emit("log.warning", {"message": msg})
project.controller.notification.project_emit("log.warning", {"message": msg})
continue
# ignore the .gns3 file
if file.endswith(".gns3"):
+1 -1
View File
@@ -367,7 +367,7 @@ class GNS3VM:
if vm_network.compare_networks(compute_network) != 0:
msg = "The GNS3 VM ({}) is not on the same network as the {} server ({}), please make sure the local server binding is in the same network as the GNS3 VM".format(
vm_network, compute_id, compute_network)
self._controller.notification.emit("log.warning", {"message": msg})
self._controller.notification.controller_emit("log.warning", {"message": msg})
except ComputeError as e:
log.warning("Could not check the VM is in the same subnet as the local server: {}".format(e))
+10 -10
View File
@@ -174,7 +174,7 @@ class Link:
self._filters = new_filters
if self._created:
yield from self.update()
self._project.controller.notification.emit("link.updated", self.__json__())
self._project.controller.notification.project_emit("link.updated", self.__json__())
self._project.dump()
@asyncio.coroutine
@@ -182,7 +182,7 @@ class Link:
if value != self._suspended:
self._suspended = value
yield from self.update()
self._project.controller.notification.emit("link.updated", self.__json__())
self._project.controller.notification.project_emit("link.updated", self.__json__())
self._project.dump()
@property
@@ -214,14 +214,14 @@ class Link:
if node.node_type in ["nat", "cloud"]:
if other_node["node"].node_type in ["nat", "cloud"]:
raise aiohttp.web.HTTPConflict(text="It's not allowed to connect a {} to a {}".format(other_node["node"].node_type, node.node_type))
raise aiohttp.web.HTTPConflict(text="Connecting a {} to a {} is not allowed".format(other_node["node"].node_type, node.node_type))
# Check if user is not connecting serial => ethernet
other_port = other_node["node"].get_port(other_node["adapter_number"], other_node["port_number"])
if other_port is None:
raise aiohttp.web.HTTPNotFound(text="Port {}/{} for {} not found".format(other_node["adapter_number"], other_node["port_number"], other_node["node"].name))
if port.link_type != other_port.link_type:
raise aiohttp.web.HTTPConflict(text="It's not allowed to connect a {} to a {}".format(other_port.link_type, port.link_type))
raise aiohttp.web.HTTPConflict(text="Connecting a {} interface to a {} interface is not allowed".format(other_port.link_type, port.link_type))
if label is None:
label = {
@@ -246,7 +246,7 @@ class Link:
n["node"].add_link(self)
n["port"].link = self
self._created = True
self._project.controller.notification.emit("link.created", self.__json__())
self._project.controller.notification.project_emit("link.created", self.__json__())
if dump:
self._project.dump()
@@ -260,7 +260,7 @@ class Link:
label = node_data.get("label")
if label:
port["label"] = label
self._project.controller.notification.emit("link.updated", self.__json__())
self._project.controller.notification.project_emit("link.updated", self.__json__())
self._project.dump()
@asyncio.coroutine
@@ -300,7 +300,7 @@ class Link:
self._capturing = True
self._capture_file_name = capture_file_name
self._streaming_pcap = asyncio_ensure_future(self._start_streaming_pcap())
self._project.controller.notification.emit("link.updated", self.__json__())
self._project.controller.notification.project_emit("link.updated", self.__json__())
@asyncio.coroutine
def _start_streaming_pcap(self):
@@ -320,8 +320,8 @@ class Link:
error_msg = "Could not stream PCAP file: error {}: {}".format(e.status, e.text)
log.error(error_msg)
self._capturing = False
self._project.notification.emit("log.error", {"message": error_msg})
self._project.controller.notification.emit("link.updated", self.__json__())
self._project.notification.project_emit("log.error", {"message": error_msg})
self._project.controller.notification.project_emit("link.updated", self.__json__())
with stream_content as stream:
try:
@@ -345,7 +345,7 @@ class Link:
"""
self._capturing = False
self._project.controller.notification.emit("link.updated", self.__json__())
self._project.controller.notification.project_emit("link.updated", self.__json__())
@asyncio.coroutine
def _read_pcap_from_source(self):
+3 -3
View File
@@ -407,7 +407,7 @@ class Node:
yield from self.parse_node_response(response.json)
elif old_json != self.__json__():
# We send notif only if object has changed
self.project.controller.notification.emit("node.updated", self.__json__())
self.project.controller.notification.project_emit("node.updated", self.__json__())
self.project.dump()
@asyncio.coroutine
@@ -575,13 +575,13 @@ class Node:
for directory in images_directories(type):
image = os.path.join(directory, img)
if os.path.exists(image):
self.project.controller.notification.emit("log.info", {"message": "Uploading missing image {}".format(img)})
self.project.controller.notification.project_emit("log.info", {"message": "Uploading missing image {}".format(img)})
try:
with open(image, 'rb') as f:
yield from self._compute.post("/{}/images/{}".format(self._node_type, os.path.basename(img)), data=f, timeout=None)
except OSError as e:
raise aiohttp.web.HTTPConflict(text="Can't upload {}: {}".format(image, str(e)))
self.project.controller.notification.emit("log.info", {"message": "Upload finished for {}".format(img)})
self.project.controller.notification.project_emit("log.info", {"message": "Upload finished for {}".format(img)})
return True
return False
+50 -14
View File
@@ -30,27 +30,63 @@ class Notification:
def __init__(self, controller):
self._controller = controller
self._listeners = {}
self._project_listeners = {}
self._controller_listeners = []
@contextmanager
def queue(self, project):
def project_queue(self, project):
"""
Get a queue of notifications
Use it with Python with
"""
queue = NotificationQueue()
self._listeners.setdefault(project.id, set())
self._listeners[project.id].add(queue)
self._project_listeners.setdefault(project.id, set())
self._project_listeners[project.id].add(queue)
yield queue
self._listeners[project.id].remove(queue)
self._project_listeners[project.id].remove(queue)
@contextmanager
def controller_queue(self):
"""
Get a queue of notifications
Use it with Python with
"""
queue = NotificationQueue()
self._controller_listeners.append(queue)
yield queue
self._controller_listeners.remove(queue)
def controller_emit(self, action, event):
"""
Send a notification to clients connected to the controller stream
:param action: Action name
:param event: Event to send
"""
# If use in tests for documentation we save a sample
if os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
os.makedirs("docs/api/notifications", exist_ok=True)
try:
import json
data = json.dumps(event, indent=4, sort_keys=True)
if "MagicMock" not in data:
with open(os.path.join("docs/api/notifications", action + ".json"), 'w+') as f:
f.write(data)
except TypeError: # If we receive a mock as an event it will raise TypeError when using json dump
pass
for controller_listener in self._controller_listeners:
controller_listener.put_nowait((action, event, {}))
def project_has_listeners(self, project):
"""
:param project_id: Project object
:returns: True if client listen this project
"""
return project.id in self._listeners and len(self._listeners[project.id]) > 0
return project.id in self._project_listeners and len(self._project_listeners[project.id]) > 0
@asyncio.coroutine
def dispatch(self, action, event, compute_id):
@@ -69,16 +105,16 @@ class Notification:
node = project.get_node(event["node_id"])
yield from node.parse_node_response(event)
self.emit("node.updated", node.__json__())
self.project_emit("node.updated", node.__json__())
except (aiohttp.web.HTTPNotFound, aiohttp.web.HTTPForbidden): # Project closing
return
elif action == "ping":
event["compute_id"] = compute_id
self.emit(action, event)
self.project_emit(action, event)
else:
self.emit(action, event)
self.project_emit(action, event)
def emit(self, action, event):
def project_emit(self, action, event):
"""
Send a notification to clients scoped by projects
@@ -101,7 +137,7 @@ class Notification:
if "project_id" in event:
self._send_event_to_project(event["project_id"], action, event)
else:
self._send_event_to_all(action, event)
self._send_event_to_all_projects(action, event)
def _send_event_to_project(self, project_id, action, event):
"""
@@ -113,13 +149,13 @@ class Notification:
:param event: Event to send
"""
try:
project_listeners = self._listeners[project_id]
project_listeners = self._project_listeners[project_id]
except KeyError:
return
for listener in project_listeners:
listener.put_nowait((action, event, {}))
def _send_event_to_all(self, action, event):
def _send_event_to_all_projects(self, action, event):
"""
Send an event to all the client listening for notifications on all
projects
@@ -127,6 +163,6 @@ class Notification:
:param action: Action name
:param event: Event to send
"""
for project_listeners in self._listeners.values():
for project_listeners in self._project_listeners.values():
for listener in project_listeners:
listener.put_nowait((action, event, {}))
+7 -7
View File
@@ -135,7 +135,7 @@ class Project:
# We send notif only if object has changed
if old_json != self.__json__():
self.controller.notification.emit("project.updated", self.__json__())
self.controller.notification.project_emit("project.updated", self.__json__())
self.dump()
# update on computes
@@ -519,7 +519,7 @@ class Project:
self._project_created_on_compute.add(compute)
yield from node.create()
self._nodes[node.id] = node
self.controller.notification.emit("node.created", node.__json__())
self.controller.notification.project_emit("node.created", node.__json__())
if dump:
self.dump()
return node
@@ -545,7 +545,7 @@ class Project:
del self._nodes[node.id]
yield from node.destroy()
self.dump()
self.controller.notification.emit("node.deleted", node.__json__())
self.controller.notification.project_emit("node.deleted", node.__json__())
@open_required
def get_node(self, node_id):
@@ -611,7 +611,7 @@ class Project:
if drawing_id not in self._drawings:
drawing = Drawing(self, drawing_id=drawing_id, **kwargs)
self._drawings[drawing.id] = drawing
self.controller.notification.emit("drawing.created", drawing.__json__())
self.controller.notification.project_emit("drawing.created", drawing.__json__())
if dump:
self.dump()
return drawing
@@ -633,7 +633,7 @@ class Project:
drawing = self.get_drawing(drawing_id)
del self._drawings[drawing.id]
self.dump()
self.controller.notification.emit("drawing.deleted", drawing.__json__())
self.controller.notification.project_emit("drawing.deleted", drawing.__json__())
@open_required
@asyncio.coroutine
@@ -662,7 +662,7 @@ class Project:
if force_delete is False:
raise
self.dump()
self.controller.notification.emit("link.deleted", link.__json__())
self.controller.notification.project_emit("link.deleted", link.__json__())
@open_required
def get_link(self, link_id):
@@ -737,7 +737,7 @@ class Project:
self._cleanPictures()
self._status = "closed"
if not ignore_notification:
self.controller.notification.emit("project.closed", self.__json__())
self.controller.notification.project_emit("project.closed", self.__json__())
self.reset()
def _cleanPictures(self):
+1 -1
View File
@@ -124,7 +124,7 @@ class Snapshot:
except (OSError, PermissionError) as e:
raise aiohttp.web.HTTPConflict(text=str(e))
yield from project.open()
self._project.controller.notification.emit("snapshot.restored", self.__json__())
self._project.controller.notification.project_emit("snapshot.restored", self.__json__())
return self._project
def __json__(self):