From d815d25bdf3b75cf74371ecb40202686bf0661bd Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 14 Jun 2016 12:04:23 +0200 Subject: [PATCH] Write .gns3 on server Ref https://github.com/GNS3/gns3-gui/issues/1243 --- gns3server/controller/__init__.py | 2 - gns3server/controller/node.py | 7 ++-- gns3server/controller/project.py | 35 +++++++++++++++-- gns3server/controller/topology.py | 49 ++++++++++++++++++++++++ tests/controller/test_node.py | 14 +++++-- tests/controller/test_project.py | 17 +++++---- tests/controller/test_topology.py | 62 +++++++++++++++++++++++++++++++ 7 files changed, 166 insertions(+), 20 deletions(-) create mode 100644 gns3server/controller/topology.py create mode 100644 tests/controller/test_topology.py diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index edf71f26..0fb36ca6 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -170,8 +170,6 @@ class Controller: if project_id not in self._projects: project = Project(project_id=project_id, controller=self, **kwargs) self._projects[project.id] = project - for compute_server in self._computes.values(): - yield from project.add_compute(compute_server) return self._projects[project.id] return self._projects[project_id] diff --git a/gns3server/controller/node.py b/gns3server/controller/node.py index 03d2547a..0016a0de 100644 --- a/gns3server/controller/node.py +++ b/gns3server/controller/node.py @@ -215,7 +215,7 @@ class Node: # We update properties on the compute and wait for the anwser from the compute node if prop == "properties": - compute_properties = kwargs[prop] + compute_properties = kwargs[prop] else: setattr(self, prop, kwargs[prop]) @@ -224,6 +224,7 @@ class Node: data = self._node_data(properties=compute_properties) response = yield from self.put(None, data=data) self.parse_node_response(response.json) + self.project.dump() def parse_node_response(self, response): """ @@ -371,14 +372,14 @@ class Node: def __json__(self): return { - "compute_id": self._compute.id, + "compute_id": str(self._compute.id), "project_id": self._project.id, "node_id": self._id, "node_type": self._node_type, "node_directory": self._node_directory, "name": self._name, "console": self._console, - "console_host": self._compute.host, + "console_host": str(self._compute.host), "console_type": self._console_type, "command_line": self._command_line, "properties": self._properties, diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index d92d1522..379b109b 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -16,6 +16,7 @@ # along with this program. If not, see . import os +import json import asyncio import aiohttp import shutil @@ -23,10 +24,14 @@ import shutil from uuid import UUID, uuid4 from .node import Node +from .topology import project_to_topology from .udp_link import UDPLink from ..config import Config from ..utils.path import check_path_allowed, get_default_project_directory +import logging +log = logging.getLogger(__name__) + class Project: """ @@ -53,7 +58,6 @@ class Project: path = os.path.join(get_default_project_directory(), self._id) self.path = path - self._computes = set() self._allocated_node_names = set() self._nodes = {} self._links = {} @@ -102,9 +106,12 @@ class Project: os.makedirs(path, exist_ok=True) return path - @asyncio.coroutine - def add_compute(self, compute): - self._computes.add(compute) + @property + def computes(self): + """ + :return: Dictonnary of computes used by the project + """ + return self._computes def allocate_node_name(self, base_name): """ @@ -207,6 +214,7 @@ class Project: yield from node.create() self._nodes[node.id] = node self.controller.notification.emit("node.created", node.__json__()) + self.dump() return node return self._nodes[node_id] @@ -217,6 +225,7 @@ class Project: self.remove_allocated_node_name(node.name) del self._nodes[node.id] yield from node.destroy() + self.dump() self.controller.notification.emit("node.deleted", node.__json__()) def get_node(self, node_id): @@ -242,6 +251,7 @@ class Project: """ link = UDPLink(self) self._links[link.id] = link + self.dump() return link @asyncio.coroutine @@ -249,6 +259,7 @@ class Project: link = self.get_link(link_id) del self._links[link.id] yield from link.delete() + self.dump() self.controller.notification.emit("link.deleted", link.__json__()) def get_link(self, link_id): @@ -296,6 +307,22 @@ class Project: raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) return path + def dump(self): + """ + Dump topology to disk + """ + try: + if self.name is None: + filename = "untitled.gns3" + else: + filename = self.name + ".gns3" + topo = project_to_topology(self) + log.debug("Write %s", filename) + with open(os.path.join(self.path, filename), "w+") as f: + json.dump(topo, f, indent=4, sort_keys=True) + except OSError as e: + raise aiohttp.web.HTTPInternalServerError(text="Could not write topology: {}".format(e)) + def __json__(self): return { diff --git a/gns3server/controller/topology.py b/gns3server/controller/topology.py new file mode 100644 index 00000000..7f8bad83 --- /dev/null +++ b/gns3server/controller/topology.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# +# Copyright (C) 2016 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from ..version import __version__ + + +def project_to_topology(project): + """ + :return: A dictionnary with the topology ready to dump to a .gns3 + """ + data = { + "project_id": project.id, + "name": project.name, + "topology": { + "nodes": [], + "links": [], + "computes": [] + }, + "type": "topology", + "revision": 5, + "version": __version__ + } + + computes = set() + for node in project.nodes.values(): + computes.add(node.compute) + data["topology"]["nodes"].append(node.__json__()) + for link in project.links.values(): + data["topology"]["links"].append(link.__json__()) + for compute in computes: + if hasattr(compute, "__json__"): + data["topology"]["computes"].append(compute.__json__()) + print(data) + #TODO: check JSON schema + return data diff --git a/tests/controller/test_node.py b/tests/controller/test_node.py index ba6fdff1..d67a1bfb 100644 --- a/tests/controller/test_node.py +++ b/tests/controller/test_node.py @@ -36,8 +36,12 @@ def compute(): @pytest.fixture -def node(compute, controller): - project = Project(str(uuid.uuid4()), controller=controller) +def project(controller): + return Project(str(uuid.uuid4()), controller=controller) + + +@pytest.fixture +def node(compute, project): node = Node(project, compute, "demo", node_id=str(uuid.uuid4()), node_type="vpcs", @@ -48,14 +52,14 @@ def node(compute, controller): def test_json(node, compute): assert node.__json__() == { - "compute_id": compute.id, + "compute_id": str(compute.id), "project_id": node.project.id, "node_id": node.id, "node_type": node.node_type, "name": "demo", "console": node.console, "console_type": node.console_type, - "console_host": compute.host, + "console_host": str(compute.host), "command_line": None, "node_directory": None, "properties": node.properties, @@ -123,6 +127,7 @@ def test_update(node, compute, project, async_run, controller): response.json = {"console": 2048} compute.put = AsyncioMagicMock(return_value=response) controller._notification = AsyncioMagicMock() + project.dump = MagicMock() async_run(node.update(x=42, console=2048, console_type="vnc", properties={"startup_script": "echo test"}, name="demo")) data = { @@ -136,6 +141,7 @@ def test_update(node, compute, project, async_run, controller): assert node.x == 42 assert node._properties == {"startup_script": "echo test"} controller._notification.emit.assert_called_with("node.updated", node.__json__()) + assert project.dump.called def test_update_properties(node, compute, project, async_run, controller): diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index 2adcd29e..70fd0909 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -74,13 +74,6 @@ def test_captures_directory(tmpdir): assert os.path.exists(p.captures_directory) -def test_add_compute(async_run): - compute = MagicMock() - project = Project() - async_run(project.add_compute(compute)) - assert compute in project._computes - - def test_add_node_local(async_run, controller): """ For a local server we send the project path @@ -225,3 +218,13 @@ def test_delete(async_run, project, controller): async_run(project.delete()) assert not os.path.exists(project.path) + +def test_dump(): + directory = Config.instance().get_section_config("Server").get("projects_path") + + with patch("gns3server.utils.path.get_default_project_directory", return_value=directory): + p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f', name="Test") + p.dump() + with open(os.path.join(directory, p.id, "Test.gns3")) as f: + content = f.read() + assert "00010203-0405-0607-0809-0a0b0c0d0e0f" in content diff --git a/tests/controller/test_topology.py b/tests/controller/test_topology.py new file mode 100644 index 00000000..32db8448 --- /dev/null +++ b/tests/controller/test_topology.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# +# Copyright (C) 2016 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 +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from unittest.mock import MagicMock +from tests.utils import asyncio_patch + +from gns3server.controller.project import Project +from gns3server.controller.compute import Compute +from gns3server.controller.topology import project_to_topology +from gns3server.version import __version__ + + +def test_project_to_topology_empty(tmpdir): + project = Project(name="Test") + topo = project_to_topology(project) + assert topo == { + "project_id": project.id, + "name": "Test", + "revision": 5, + "topology": { + "nodes": [], + "links": [], + "computes": [] + }, + "type": "topology", + "version": __version__ + } + + +def test_basic_topology(tmpdir, async_run, controller): + project = Project(name="Test", controller=controller) + compute = Compute("my_compute", controller) + compute.http_query = MagicMock() + + with asyncio_patch("gns3server.controller.node.Node.create"): + node1 = async_run(project.add_node(compute, "Node 1", "node_1")) + node2 = async_run(project.add_node(compute, "Node 2", "node_2")) + + link = async_run(project.add_link()) + async_run(link.add_node(node1, 0, 0)) + async_run(link.add_node(node2, 0, 0)) + + topo = project_to_topology(project) + assert len(topo["topology"]["nodes"]) == 2 + assert node1.__json__() in topo["topology"]["nodes"] + assert topo["topology"]["links"][0] == link.__json__() + assert topo["topology"]["computes"][0] == compute.__json__() +