mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
7 files changed
+166
-20
No files matched your search
@@ -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]
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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 {
|
||||
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user