2016-03-10 09:32:07 +00:00
|
|
|
#!/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/>.
|
|
|
|
|
2016-04-26 12:34:49 +00:00
|
|
|
import os
|
2016-06-14 10:04:23 +00:00
|
|
|
import json
|
2016-07-25 12:47:37 +00:00
|
|
|
import uuid
|
|
|
|
import shutil
|
2016-03-10 09:32:07 +00:00
|
|
|
import asyncio
|
2016-03-11 15:51:35 +00:00
|
|
|
import aiohttp
|
2016-07-25 12:47:37 +00:00
|
|
|
import tempfile
|
2016-05-11 17:35:36 +00:00
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
from uuid import UUID, uuid4
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
from .node import Node
|
2016-07-26 08:32:43 +00:00
|
|
|
from .snapshot import Snapshot
|
2016-06-23 09:17:23 +00:00
|
|
|
from .drawing import Drawing
|
2016-06-14 14:57:13 +00:00
|
|
|
from .topology import project_to_topology, load_topology
|
2016-03-14 15:51:47 +00:00
|
|
|
from .udp_link import UDPLink
|
2016-04-26 12:34:49 +00:00
|
|
|
from ..config import Config
|
2016-05-11 16:42:55 +00:00
|
|
|
from ..utils.path import check_path_allowed, get_default_project_directory
|
2016-08-16 13:45:06 +00:00
|
|
|
from ..utils.asyncio.pool import Pool
|
2016-07-25 12:47:37 +00:00
|
|
|
from .export_project import export_project
|
|
|
|
from .import_project import import_project
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2016-06-14 14:57:13 +00:00
|
|
|
|
2016-06-14 10:04:23 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
def open_required(func):
|
|
|
|
"""
|
|
|
|
Use this decorator to raise an error if the project is not opened
|
|
|
|
"""
|
|
|
|
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
if self._status == "closed":
|
|
|
|
raise aiohttp.web.HTTPForbidden(text="The project is not opened")
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
class Project:
|
|
|
|
"""
|
2016-05-14 00:48:10 +00:00
|
|
|
A project inside a controller
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
:param project_id: force project identifier (None by default auto generate an UUID)
|
|
|
|
:param path: path of the project. (None use the standard directory)
|
2016-06-14 21:08:30 +00:00
|
|
|
:param status: Status of the project (opened / closed)
|
2016-03-10 09:32:07 +00:00
|
|
|
"""
|
|
|
|
|
2016-09-20 10:46:39 +00:00
|
|
|
def __init__(self, name=None, project_id=None, path=None, controller=None, status="opened",
|
|
|
|
filename=None, auto_start=False, auto_open=False, auto_close=True,
|
|
|
|
scene_height=1000, scene_width=2000):
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-05-18 16:37:18 +00:00
|
|
|
self._controller = controller
|
2016-07-11 13:36:52 +00:00
|
|
|
assert name is not None
|
2016-03-10 09:32:07 +00:00
|
|
|
self._name = name
|
2016-08-16 13:45:06 +00:00
|
|
|
self._auto_start = auto_start
|
|
|
|
self._auto_close = auto_close
|
|
|
|
self._auto_open = auto_open
|
2016-06-14 21:08:30 +00:00
|
|
|
self._status = status
|
2016-09-20 10:46:39 +00:00
|
|
|
self._scene_height = scene_height
|
|
|
|
self._scene_width = scene_width
|
2016-07-18 19:30:30 +00:00
|
|
|
|
|
|
|
# Disallow overwrite of existing project
|
|
|
|
if project_id is None and path is not None:
|
|
|
|
if os.path.exists(path):
|
|
|
|
raise aiohttp.web.HTTPForbidden(text="The path {} already exist.".format(path))
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
if project_id is None:
|
|
|
|
self._id = str(uuid4())
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
UUID(project_id, version=4)
|
|
|
|
except ValueError:
|
|
|
|
raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(project_id))
|
|
|
|
self._id = project_id
|
2016-04-26 12:34:49 +00:00
|
|
|
|
|
|
|
if path is None:
|
2016-05-11 16:42:55 +00:00
|
|
|
path = os.path.join(get_default_project_directory(), self._id)
|
2016-04-26 12:34:49 +00:00
|
|
|
self.path = path
|
2016-06-16 14:57:54 +00:00
|
|
|
|
2016-07-11 13:36:52 +00:00
|
|
|
if filename is not None:
|
2016-06-16 14:57:54 +00:00
|
|
|
self._filename = filename
|
|
|
|
else:
|
|
|
|
self._filename = self.name + ".gns3"
|
2016-07-25 12:47:37 +00:00
|
|
|
|
2016-06-15 13:12:38 +00:00
|
|
|
self.reset()
|
2016-04-26 12:34:49 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
# At project creation we write an empty .gns3
|
|
|
|
if not os.path.exists(self._topology_file()):
|
|
|
|
self.dump()
|
|
|
|
|
2016-08-15 14:44:09 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def update(self, **kwargs):
|
|
|
|
"""
|
2016-08-16 13:45:06 +00:00
|
|
|
Update the project
|
|
|
|
:param kwargs: Project properties
|
2016-08-15 14:44:09 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
old_json = self.__json__()
|
|
|
|
|
|
|
|
for prop in kwargs:
|
|
|
|
setattr(self, prop, kwargs[prop])
|
|
|
|
|
|
|
|
# We send notif only if object has changed
|
|
|
|
if old_json != self.__json__():
|
|
|
|
self.controller.notification.emit("project.updated", self.__json__())
|
|
|
|
self.dump()
|
|
|
|
|
2016-06-15 13:12:38 +00:00
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
Called when open/close a project. Cleanup internal stuff
|
|
|
|
"""
|
2016-06-11 23:31:30 +00:00
|
|
|
self._allocated_node_names = set()
|
2016-05-11 17:35:36 +00:00
|
|
|
self._nodes = {}
|
2016-03-11 15:51:35 +00:00
|
|
|
self._links = {}
|
2016-06-23 09:17:23 +00:00
|
|
|
self._drawings = {}
|
2016-07-26 08:32:43 +00:00
|
|
|
self._snapshots = {}
|
|
|
|
|
|
|
|
# List the available snapshots
|
|
|
|
snapshot_dir = os.path.join(self.path, "snapshots")
|
|
|
|
if os.path.exists(snapshot_dir):
|
|
|
|
for snap in os.listdir(snapshot_dir):
|
|
|
|
if snap.endswith(".gns3project"):
|
|
|
|
snapshot = Snapshot(self, filename=snap)
|
|
|
|
self._snapshots[snapshot.id] = snapshot
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-05-11 08:56:43 +00:00
|
|
|
# Create the project on demand on the compute node
|
|
|
|
self._project_created_on_compute = set()
|
|
|
|
|
2016-09-20 10:46:39 +00:00
|
|
|
@property
|
|
|
|
def scene_height(self):
|
|
|
|
return self._scene_height
|
|
|
|
|
|
|
|
@scene_height.setter
|
|
|
|
def scene_height(self, val):
|
|
|
|
"""
|
|
|
|
Height of the drawing area
|
|
|
|
"""
|
|
|
|
self._scene_height = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def scene_width(self):
|
|
|
|
return self._scene_width
|
|
|
|
|
|
|
|
@scene_width.setter
|
|
|
|
def scene_width(self, val):
|
|
|
|
"""
|
|
|
|
Width of the drawing area
|
|
|
|
"""
|
|
|
|
self._scene_width = val
|
|
|
|
|
2016-07-07 10:10:42 +00:00
|
|
|
@property
|
|
|
|
def auto_start(self):
|
2016-08-15 18:51:59 +00:00
|
|
|
"""
|
|
|
|
Should project auto start when opened
|
|
|
|
"""
|
2016-07-07 10:10:42 +00:00
|
|
|
return self._auto_start
|
|
|
|
|
2016-08-15 18:51:59 +00:00
|
|
|
@auto_start.setter
|
|
|
|
def auto_start(self, val):
|
|
|
|
self._auto_start = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auto_close(self):
|
|
|
|
"""
|
|
|
|
Should project automaticaly closed when client
|
|
|
|
stop listening for notification
|
|
|
|
"""
|
|
|
|
return self._auto_close
|
|
|
|
|
|
|
|
@auto_close.setter
|
|
|
|
def auto_close(self, val):
|
|
|
|
self._auto_close = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auto_open(self):
|
|
|
|
return self._auto_open
|
|
|
|
|
|
|
|
@auto_open.setter
|
|
|
|
def auto_open(self, val):
|
|
|
|
self._auto_open = val
|
|
|
|
|
2016-05-18 16:37:18 +00:00
|
|
|
@property
|
|
|
|
def controller(self):
|
|
|
|
return self._controller
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
|
2016-08-15 14:44:09 +00:00
|
|
|
@name.setter
|
|
|
|
def name(self, val):
|
|
|
|
self._name = val
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self._path
|
|
|
|
|
2016-06-14 21:08:30 +00:00
|
|
|
@property
|
|
|
|
def status(self):
|
|
|
|
return self._status
|
|
|
|
|
2016-04-26 12:34:49 +00:00
|
|
|
@path.setter
|
|
|
|
def path(self, path):
|
2016-05-11 13:59:32 +00:00
|
|
|
check_path_allowed(path)
|
2016-04-26 12:34:49 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
|
|
|
|
|
|
|
|
if '"' in path:
|
2016-05-14 00:48:10 +00:00
|
|
|
raise aiohttp.web.HTTPForbidden(text="You are not allowed to use \" in the project directory path. Not supported by Dynamips.")
|
2016-04-26 12:34:49 +00:00
|
|
|
|
|
|
|
self._path = path
|
|
|
|
|
|
|
|
def _config(self):
|
|
|
|
return Config.instance().get_section_config("Server")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def captures_directory(self):
|
|
|
|
"""
|
2016-07-13 14:13:14 +00:00
|
|
|
Location of the captures files
|
2016-04-26 12:34:49 +00:00
|
|
|
"""
|
|
|
|
path = os.path.join(self._path, "project-files", "captures")
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
return path
|
|
|
|
|
2016-07-13 14:13:14 +00:00
|
|
|
@property
|
|
|
|
def pictures_directory(self):
|
|
|
|
"""
|
|
|
|
Location of the images files
|
|
|
|
"""
|
|
|
|
path = os.path.join(self._path, "project-files", "images")
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
return path
|
|
|
|
|
2016-06-14 10:04:23 +00:00
|
|
|
@property
|
|
|
|
def computes(self):
|
|
|
|
"""
|
2016-07-21 18:17:36 +00:00
|
|
|
:return: List of computes used by the project
|
2016-06-14 10:04:23 +00:00
|
|
|
"""
|
2016-07-21 18:17:36 +00:00
|
|
|
return self._project_created_on_compute
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-07-18 17:30:38 +00:00
|
|
|
def remove_allocated_node_name(self, name):
|
2016-06-11 23:31:30 +00:00
|
|
|
"""
|
2016-07-18 17:30:38 +00:00
|
|
|
Removes an allocated node name
|
|
|
|
|
|
|
|
:param name: allocated node name
|
|
|
|
"""
|
|
|
|
|
|
|
|
if name in self._allocated_node_names:
|
|
|
|
self._allocated_node_names.remove(name)
|
2016-06-11 23:31:30 +00:00
|
|
|
|
2016-07-18 17:30:38 +00:00
|
|
|
def update_allocated_node_name(self, base_name):
|
|
|
|
"""
|
|
|
|
Updates a node name or generate a new if no node
|
|
|
|
name is available.
|
2016-06-11 23:31:30 +00:00
|
|
|
|
2016-07-18 17:30:38 +00:00
|
|
|
:param base_name: new node base name
|
2016-06-11 23:31:30 +00:00
|
|
|
"""
|
|
|
|
|
2016-07-18 17:30:38 +00:00
|
|
|
if base_name is None:
|
|
|
|
return None
|
|
|
|
self.remove_allocated_node_name(base_name)
|
2016-06-11 23:31:30 +00:00
|
|
|
if '{0}' in base_name or '{id}' in base_name:
|
|
|
|
# base name is a template, replace {0} or {id} by an unique identifier
|
|
|
|
for number in range(1, 1000000):
|
|
|
|
name = base_name.format(number, id=number)
|
|
|
|
if name not in self._allocated_node_names:
|
|
|
|
self._allocated_node_names.add(name)
|
|
|
|
return name
|
|
|
|
else:
|
|
|
|
if base_name not in self._allocated_node_names:
|
2016-07-18 17:30:38 +00:00
|
|
|
self._allocated_node_names.add(base_name)
|
2016-06-11 23:31:30 +00:00
|
|
|
return base_name
|
|
|
|
# base name is not unique, let's find a unique name by appending a number
|
|
|
|
for number in range(1, 1000000):
|
|
|
|
name = base_name + str(number)
|
|
|
|
if name not in self._allocated_node_names:
|
|
|
|
self._allocated_node_names.add(name)
|
|
|
|
return name
|
2016-07-18 17:30:38 +00:00
|
|
|
raise aiohttp.web.HTTPConflict(text="A node name could not be allocated (node limit reached?)")
|
2016-06-11 23:31:30 +00:00
|
|
|
|
|
|
|
def has_allocated_node_name(self, name):
|
|
|
|
"""
|
|
|
|
Returns either a node name is already allocated or not.
|
|
|
|
|
|
|
|
:param name: node name
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
|
|
|
|
if name in self._allocated_node_names:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def update_node_name(self, node, new_name):
|
|
|
|
|
|
|
|
if new_name and node.name != new_name:
|
2016-07-18 17:30:38 +00:00
|
|
|
return self.update_allocated_node_name(new_name)
|
|
|
|
return new_name
|
2016-06-11 23:31:30 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-03-10 20:51:29 +00:00
|
|
|
@asyncio.coroutine
|
2016-06-11 23:31:30 +00:00
|
|
|
def add_node(self, compute, name, node_id, **kwargs):
|
2016-03-10 20:51:29 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Create a node or return an existing node
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
:param kwargs: See the documentation of node
|
2016-03-10 20:51:29 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
if node_id not in self._nodes:
|
2016-06-11 23:31:30 +00:00
|
|
|
node = Node(self, compute, name, node_id=node_id, **kwargs)
|
2016-05-11 08:56:43 +00:00
|
|
|
if compute not in self._project_created_on_compute:
|
2016-05-16 16:17:24 +00:00
|
|
|
# For a local server we send the project path
|
|
|
|
if compute.id == "local":
|
2016-05-16 19:12:32 +00:00
|
|
|
yield from compute.post("/projects", data={
|
2016-05-16 16:17:24 +00:00
|
|
|
"name": self._name,
|
|
|
|
"project_id": self._id,
|
|
|
|
"path": self._path
|
|
|
|
})
|
|
|
|
else:
|
2016-05-16 19:12:32 +00:00
|
|
|
yield from compute.post("/projects", data={
|
2016-05-16 16:17:24 +00:00
|
|
|
"name": self._name,
|
|
|
|
"project_id": self._id,
|
|
|
|
})
|
|
|
|
|
2016-05-11 08:56:43 +00:00
|
|
|
self._project_created_on_compute.add(compute)
|
2016-05-11 17:35:36 +00:00
|
|
|
yield from node.create()
|
|
|
|
self._nodes[node.id] = node
|
2016-05-18 16:37:18 +00:00
|
|
|
self.controller.notification.emit("node.created", node.__json__())
|
2016-06-14 10:04:23 +00:00
|
|
|
self.dump()
|
2016-05-11 17:35:36 +00:00
|
|
|
return node
|
|
|
|
return self._nodes[node_id]
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-05-17 11:11:43 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def delete_node(self, node_id):
|
2016-06-11 23:31:30 +00:00
|
|
|
|
2016-05-17 11:11:43 +00:00
|
|
|
node = self.get_node(node_id)
|
2016-07-05 14:07:05 +00:00
|
|
|
|
|
|
|
for link in list(self._links.values()):
|
|
|
|
if node in link.nodes:
|
|
|
|
yield from self.delete_link(link.id)
|
|
|
|
|
2016-06-11 23:31:30 +00:00
|
|
|
self.remove_allocated_node_name(node.name)
|
2016-05-17 11:11:43 +00:00
|
|
|
del self._nodes[node.id]
|
2016-05-18 16:37:18 +00:00
|
|
|
yield from node.destroy()
|
2016-06-14 10:04:23 +00:00
|
|
|
self.dump()
|
2016-05-18 16:37:18 +00:00
|
|
|
self.controller.notification.emit("node.deleted", node.__json__())
|
2016-05-17 11:11:43 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-05-11 17:35:36 +00:00
|
|
|
def get_node(self, node_id):
|
2016-03-11 15:51:35 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Return the node or raise a 404 if the node is unknown
|
2016-03-11 15:51:35 +00:00
|
|
|
"""
|
|
|
|
try:
|
2016-05-11 17:35:36 +00:00
|
|
|
return self._nodes[node_id]
|
2016-03-11 15:51:35 +00:00
|
|
|
except KeyError:
|
2016-05-11 17:35:36 +00:00
|
|
|
raise aiohttp.web.HTTPNotFound(text="Node ID {} doesn't exist".format(node_id))
|
2016-03-11 15:51:35 +00:00
|
|
|
|
2016-03-15 10:32:10 +00:00
|
|
|
@property
|
2016-05-11 17:35:36 +00:00
|
|
|
def nodes(self):
|
2016-03-15 10:32:10 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
:returns: Dictionary of the nodes
|
2016-03-15 10:32:10 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
return self._nodes
|
2016-03-15 10:32:10 +00:00
|
|
|
|
2016-06-20 16:45:31 +00:00
|
|
|
@property
|
2016-06-23 09:17:23 +00:00
|
|
|
def drawings(self):
|
2016-06-20 16:45:31 +00:00
|
|
|
"""
|
2016-06-23 09:17:23 +00:00
|
|
|
:returns: Dictionary of the drawings
|
2016-06-20 16:45:31 +00:00
|
|
|
"""
|
2016-06-23 09:17:23 +00:00
|
|
|
return self._drawings
|
2016-06-20 16:45:31 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-06-20 16:45:31 +00:00
|
|
|
@asyncio.coroutine
|
2016-06-23 09:17:23 +00:00
|
|
|
def add_drawing(self, drawing_id=None, **kwargs):
|
2016-06-20 16:45:31 +00:00
|
|
|
"""
|
2016-06-23 09:17:23 +00:00
|
|
|
Create an drawing or return an existing drawing
|
2016-06-20 16:45:31 +00:00
|
|
|
|
2016-06-23 09:17:23 +00:00
|
|
|
:param kwargs: See the documentation of drawing
|
2016-06-20 16:45:31 +00:00
|
|
|
"""
|
2016-06-23 09:17:23 +00:00
|
|
|
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__())
|
2016-06-20 16:45:31 +00:00
|
|
|
self.dump()
|
2016-06-23 09:17:23 +00:00
|
|
|
return drawing
|
|
|
|
return self._drawings[drawing_id]
|
2016-06-20 16:45:31 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-06-23 09:17:23 +00:00
|
|
|
def get_drawing(self, drawing_id):
|
2016-06-20 16:45:31 +00:00
|
|
|
"""
|
2016-06-23 09:17:23 +00:00
|
|
|
Return the Drawing or raise a 404 if the drawing is unknown
|
2016-06-20 16:45:31 +00:00
|
|
|
"""
|
|
|
|
try:
|
2016-06-23 09:17:23 +00:00
|
|
|
return self._drawings[drawing_id]
|
2016-06-20 16:45:31 +00:00
|
|
|
except KeyError:
|
2016-06-23 09:17:23 +00:00
|
|
|
raise aiohttp.web.HTTPNotFound(text="Drawing ID {} doesn't exist".format(drawing_id))
|
2016-06-20 16:45:31 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-06-20 16:45:31 +00:00
|
|
|
@asyncio.coroutine
|
2016-06-23 09:17:23 +00:00
|
|
|
def delete_drawing(self, drawing_id):
|
|
|
|
drawing = self.get_drawing(drawing_id)
|
|
|
|
del self._drawings[drawing.id]
|
2016-06-20 16:45:31 +00:00
|
|
|
self.dump()
|
2016-06-23 09:17:23 +00:00
|
|
|
self.controller.notification.emit("drawing.deleted", drawing.__json__())
|
2016-06-20 16:45:31 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-03-11 15:51:35 +00:00
|
|
|
@asyncio.coroutine
|
2016-06-14 14:57:13 +00:00
|
|
|
def add_link(self, link_id=None):
|
2016-03-11 15:51:35 +00:00
|
|
|
"""
|
|
|
|
Create a link. By default the link is empty
|
|
|
|
"""
|
2016-06-14 14:57:13 +00:00
|
|
|
if link_id and link_id in self._links:
|
|
|
|
return self._links[link.id]
|
|
|
|
link = UDPLink(self, link_id=link_id)
|
2016-03-11 15:51:35 +00:00
|
|
|
self._links[link.id] = link
|
2016-06-14 10:04:23 +00:00
|
|
|
self.dump()
|
2016-03-11 15:51:35 +00:00
|
|
|
return link
|
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-05-18 16:37:18 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def delete_link(self, link_id):
|
|
|
|
link = self.get_link(link_id)
|
|
|
|
del self._links[link.id]
|
|
|
|
yield from link.delete()
|
2016-06-14 10:04:23 +00:00
|
|
|
self.dump()
|
2016-05-18 16:37:18 +00:00
|
|
|
self.controller.notification.emit("link.deleted", link.__json__())
|
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
2016-05-11 17:35:36 +00:00
|
|
|
def get_link(self, link_id):
|
2016-03-11 15:51:35 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Return the Link or raise a 404 if the link is unknown
|
2016-03-11 15:51:35 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self._links[link_id]
|
|
|
|
except KeyError:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="Link ID {} doesn't exist".format(link_id))
|
|
|
|
|
2016-03-15 10:32:10 +00:00
|
|
|
@property
|
|
|
|
def links(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
:returns: Dictionary of the Links
|
2016-03-15 10:32:10 +00:00
|
|
|
"""
|
|
|
|
return self._links
|
|
|
|
|
2016-07-26 08:32:43 +00:00
|
|
|
@property
|
|
|
|
def snapshots(self):
|
|
|
|
"""
|
|
|
|
:returns: Dictionary of snapshots
|
|
|
|
"""
|
|
|
|
return self._snapshots
|
|
|
|
|
|
|
|
@open_required
|
|
|
|
def get_snapshot(self, snapshot_id):
|
|
|
|
"""
|
|
|
|
Return the snapshot or raise a 404 if the snapshot is unknown
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self._snapshots[snapshot_id]
|
|
|
|
except KeyError:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="Snapshot ID {} doesn't exist".format(snapshot_id))
|
|
|
|
|
|
|
|
@open_required
|
|
|
|
@asyncio.coroutine
|
|
|
|
def snapshot(self, name):
|
|
|
|
"""
|
|
|
|
Snapshot the project
|
|
|
|
|
|
|
|
:param name: Name of the snapshot
|
|
|
|
"""
|
|
|
|
|
|
|
|
snapshot = Snapshot(self, name=name)
|
|
|
|
try:
|
|
|
|
if os.path.exists(snapshot.path):
|
|
|
|
raise aiohttp.web_exceptions.HTTPConflict(text="The snapshot {} already exist".format(name))
|
|
|
|
|
|
|
|
os.makedirs(os.path.join(self.path, "snapshots"), exist_ok=True)
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
zipstream = yield from export_project(self, tmpdir, keep_compute_id=True, allow_all_nodes=True)
|
|
|
|
with open(snapshot.path, "wb+") as f:
|
|
|
|
for data in zipstream:
|
|
|
|
f.write(data)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
|
|
|
|
|
|
|
|
self._snapshots[snapshot.id] = snapshot
|
|
|
|
return snapshot
|
|
|
|
|
|
|
|
@open_required
|
|
|
|
@asyncio.coroutine
|
|
|
|
def delete_snapshot(self, snapshot_id):
|
|
|
|
snapshot = self.get_snapshot(snapshot_id)
|
|
|
|
del self._snapshots[snapshot.id]
|
|
|
|
os.remove(snapshot.path)
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
@asyncio.coroutine
|
2016-08-15 11:30:02 +00:00
|
|
|
def close(self, ignore_notification=False):
|
2016-08-16 13:45:06 +00:00
|
|
|
yield from self.stop_all()
|
2016-05-16 19:59:40 +00:00
|
|
|
for compute in self._project_created_on_compute:
|
2016-08-26 12:09:18 +00:00
|
|
|
try:
|
|
|
|
yield from compute.post("/projects/{}/close".format(self._id))
|
|
|
|
# We don't care if a compute is down at this step
|
2016-08-29 16:59:13 +00:00
|
|
|
except (aiohttp.errors.ClientOSError, aiohttp.web.HTTPNotFound, aiohttp.web.HTTPConflict):
|
2016-08-26 12:09:18 +00:00
|
|
|
pass
|
2016-07-13 14:13:14 +00:00
|
|
|
self._cleanPictures()
|
2016-06-15 13:12:38 +00:00
|
|
|
self._status = "closed"
|
2016-08-15 11:30:02 +00:00
|
|
|
if not ignore_notification:
|
2016-08-15 14:44:09 +00:00
|
|
|
self.controller.notification.emit("project.closed", self.__json__())
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-07-13 14:13:14 +00:00
|
|
|
def _cleanPictures(self):
|
|
|
|
"""
|
|
|
|
Delete unused images
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
pictures = set(os.listdir(self.pictures_directory))
|
|
|
|
for drawing in self._drawings.values():
|
2016-09-02 09:46:08 +00:00
|
|
|
try:
|
|
|
|
pictures.remove(drawing.ressource_filename)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2016-07-13 14:13:14 +00:00
|
|
|
|
|
|
|
for pict in pictures:
|
|
|
|
os.remove(os.path.join(self.pictures_directory, pict))
|
|
|
|
except OSError as e:
|
|
|
|
log.warning(str(e))
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def delete(self):
|
2016-08-16 11:30:10 +00:00
|
|
|
if self._status == "opened":
|
|
|
|
yield from self.close()
|
2016-07-26 08:32:43 +00:00
|
|
|
yield from self.delete_on_computes()
|
2016-08-31 15:24:19 +00:00
|
|
|
shutil.rmtree(self.path)
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-07-26 08:32:43 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def delete_on_computes(self):
|
|
|
|
"""
|
|
|
|
Delete the project on computes but not on controller
|
|
|
|
"""
|
|
|
|
for compute in self._project_created_on_compute:
|
|
|
|
if compute.id != "local":
|
|
|
|
yield from compute.delete("/projects/{}".format(self._id))
|
|
|
|
self._project_created_on_compute = set()
|
|
|
|
|
2016-04-26 12:34:49 +00:00
|
|
|
@classmethod
|
|
|
|
def _get_default_project_directory(cls):
|
|
|
|
"""
|
|
|
|
Return the default location for the project directory
|
|
|
|
depending of the operating system
|
|
|
|
"""
|
|
|
|
|
|
|
|
server_config = Config.instance().get_section_config("Server")
|
|
|
|
path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))
|
|
|
|
path = os.path.normpath(path)
|
|
|
|
try:
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
|
|
|
|
return path
|
|
|
|
|
2016-06-15 16:22:11 +00:00
|
|
|
def _topology_file(self):
|
2016-06-16 14:57:54 +00:00
|
|
|
return os.path.join(self.path, self._filename)
|
2016-06-14 14:57:13 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-06-15 13:12:38 +00:00
|
|
|
def open(self):
|
2016-06-14 14:57:13 +00:00
|
|
|
"""
|
|
|
|
Load topology elements
|
|
|
|
"""
|
2016-06-17 15:50:06 +00:00
|
|
|
if self._status == "opened":
|
|
|
|
return
|
|
|
|
|
2016-06-15 13:12:38 +00:00
|
|
|
self.reset()
|
2016-07-25 12:47:37 +00:00
|
|
|
self._status = "opened"
|
|
|
|
|
2016-06-14 14:57:13 +00:00
|
|
|
path = self._topology_file()
|
2016-09-05 15:28:49 +00:00
|
|
|
if not os.path.exists(path):
|
|
|
|
return
|
|
|
|
shutil.copy(path, path + ".backup")
|
|
|
|
try:
|
2016-06-15 13:12:38 +00:00
|
|
|
topology = load_topology(path)["topology"]
|
2016-06-20 16:45:31 +00:00
|
|
|
for compute in topology.get("computes", []):
|
2016-06-15 13:12:38 +00:00
|
|
|
yield from self.controller.add_compute(**compute)
|
2016-06-20 16:45:31 +00:00
|
|
|
for node in topology.get("nodes", []):
|
2016-06-15 13:12:38 +00:00
|
|
|
compute = self.controller.get_compute(node.pop("compute_id"))
|
|
|
|
name = node.pop("name")
|
|
|
|
node_id = node.pop("node_id")
|
|
|
|
yield from self.add_node(compute, name, node_id, **node)
|
2016-06-20 16:45:31 +00:00
|
|
|
for link_data in topology.get("links", []):
|
2016-06-15 13:12:38 +00:00
|
|
|
link = yield from self.add_link(link_id=link_data["link_id"])
|
|
|
|
for node_link in link_data["nodes"]:
|
|
|
|
node = self.get_node(node_link["node_id"])
|
2016-07-01 19:33:39 +00:00
|
|
|
yield from link.add_node(node, node_link["adapter_number"], node_link["port_number"], label=node_link.get("label"))
|
2016-06-23 09:17:23 +00:00
|
|
|
for drawing_data in topology.get("drawings", []):
|
|
|
|
drawing = yield from self.add_drawing(**drawing_data)
|
2016-07-25 12:47:37 +00:00
|
|
|
|
2016-09-05 15:28:49 +00:00
|
|
|
# We catch all error to be able to rollback the .gns3 to the previous state
|
|
|
|
except Exception as e:
|
|
|
|
for compute in self._project_created_on_compute:
|
|
|
|
try:
|
|
|
|
yield from compute.post("/projects/{}/close".format(self._id))
|
|
|
|
# We don't care if a compute is down at this step
|
|
|
|
except (aiohttp.errors.ClientOSError, aiohttp.web.HTTPNotFound, aiohttp.web.HTTPConflict):
|
|
|
|
pass
|
|
|
|
shutil.copy(path + ".backup", path)
|
|
|
|
self._status = "closed"
|
|
|
|
raise e
|
|
|
|
os.remove(path + ".backup")
|
|
|
|
|
|
|
|
# Should we start the nodes when project is open
|
|
|
|
if self._auto_start:
|
|
|
|
yield from self.start_all()
|
2016-08-16 13:45:06 +00:00
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@open_required
|
|
|
|
@asyncio.coroutine
|
|
|
|
def duplicate(self, name=None, location=None):
|
|
|
|
"""
|
|
|
|
Duplicate a project
|
|
|
|
|
|
|
|
It's the save as feature of the 1.X. It's implemented on top of the
|
|
|
|
export / import features. It will generate a gns3p and reimport it.
|
|
|
|
It's a little slower but we have only one implementation to maintain.
|
|
|
|
|
|
|
|
:param name: Name of the new project. A new one will be generated in case of conflicts
|
|
|
|
:param location: Parent directory of the new project
|
|
|
|
"""
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
zipstream = yield from export_project(self, tmpdir, keep_compute_id=True, allow_all_nodes=True)
|
|
|
|
with open(os.path.join(tmpdir, "project.gns3p"), "wb+") as f:
|
|
|
|
for data in zipstream:
|
|
|
|
f.write(data)
|
|
|
|
with open(os.path.join(tmpdir, "project.gns3p"), "rb") as f:
|
|
|
|
project = yield from import_project(self._controller, str(uuid.uuid4()), f, location=location, name=name, keep_compute_id=True)
|
|
|
|
return project
|
2016-06-14 14:57:13 +00:00
|
|
|
|
2016-07-21 07:45:02 +00:00
|
|
|
def is_running(self):
|
|
|
|
"""
|
|
|
|
If a node is started or paused return True
|
|
|
|
"""
|
|
|
|
for node in self._nodes.values():
|
2016-09-15 16:48:14 +00:00
|
|
|
# Some node type are always running we ignore them
|
|
|
|
if node.status != "stopped" and node.node_type in ("qemu", "docker", "dynamips", "vpcs", "vmware", "virtualbox", "iou"):
|
2016-07-21 07:45:02 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-06-14 10:04:23 +00:00
|
|
|
def dump(self):
|
|
|
|
"""
|
|
|
|
Dump topology to disk
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
topo = project_to_topology(self)
|
2016-06-14 14:57:13 +00:00
|
|
|
path = self._topology_file()
|
|
|
|
log.debug("Write %s", path)
|
2016-09-18 20:23:52 +00:00
|
|
|
with open(path + ".tmp", "w+", encoding="utf-8") as f:
|
2016-06-14 10:04:23 +00:00
|
|
|
json.dump(topo, f, indent=4, sort_keys=True)
|
2016-06-17 15:13:36 +00:00
|
|
|
shutil.move(path + ".tmp", path)
|
2016-06-14 10:04:23 +00:00
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not write topology: {}".format(e))
|
|
|
|
|
2016-08-16 13:45:06 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def start_all(self):
|
|
|
|
"""
|
|
|
|
Start all nodes
|
|
|
|
"""
|
|
|
|
pool = Pool(concurrency=3)
|
|
|
|
for node in self.nodes.values():
|
|
|
|
pool.append(node.start)
|
|
|
|
yield from pool.join()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def stop_all(self):
|
|
|
|
"""
|
|
|
|
Stop all nodes
|
|
|
|
"""
|
|
|
|
pool = Pool(concurrency=3)
|
|
|
|
for node in self.nodes.values():
|
|
|
|
pool.append(node.stop)
|
|
|
|
yield from pool.join()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def suspend_all(self):
|
|
|
|
"""
|
|
|
|
Suspend all nodes
|
|
|
|
"""
|
|
|
|
pool = Pool(concurrency=3)
|
|
|
|
for node in self.nodes.values():
|
|
|
|
pool.append(node.suspend)
|
|
|
|
yield from pool.join()
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
def __json__(self):
|
|
|
|
|
|
|
|
return {
|
|
|
|
"name": self._name,
|
|
|
|
"project_id": self._id,
|
2016-06-14 21:08:30 +00:00
|
|
|
"path": self._path,
|
2016-06-16 14:57:54 +00:00
|
|
|
"filename": self._filename,
|
2016-08-15 18:51:59 +00:00
|
|
|
"status": self._status,
|
|
|
|
"auto_start": self._auto_start,
|
|
|
|
"auto_close": self._auto_close,
|
2016-09-20 10:46:39 +00:00
|
|
|
"auto_open": self._auto_open,
|
|
|
|
"scene_height": self._scene_height,
|
|
|
|
"scene_width": self._scene_width
|
2016-03-10 09:32:07 +00:00
|
|
|
}
|
2016-06-20 16:45:31 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<gns3server.controller.Project {} {}>".format(self._name, self._id)
|