Merge 2.1 into 2.2 branch

This commit is contained in:
grossmj committed 2018-03-12 13:38:50 +07:00
1 parent 2a5f3221b9
commit 658aa4bae9
69 files changed
+846 -448

No files matched your search

+17 -2
View File
@@ -82,7 +82,8 @@ class Controller:
if appliance.status != 'broken':
self._appliance_templates[appliance.id] = appliance
except (ValueError, OSError, KeyError) as e:
log.warning("Can't load %s: %s", path, str(e))
log.warning("Cannot load appliance template file '%s': %s", path, str(e))
continue
self._appliances = {}
vms = []
@@ -122,13 +123,27 @@ class Controller:
for prop in vm.copy():
if prop in ["enable_remote_console", "use_ubridge"]:
del vm[prop]
# remove deprecated default_symbol and hover_symbol
# and set symbol if not present
deprecated = ["default_symbol", "hover_symbol"]
if len([prop for prop in vm.keys() if prop in deprecated]) > 0:
if "default_symbol" in vm.keys():
del vm["default_symbol"]
if "hover_symbol" in vm.keys():
del vm["hover_symbol"]
if "symbol" not in vm.keys():
vm["symbol"] = ":/symbols/computer.svg"
vm.setdefault("appliance_id", str(uuid.uuid4()))
try:
appliance = Appliance(vm["appliance_id"], vm)
appliance.__json__() # Check if loaded without error
self._appliances[appliance.id] = appliance
except KeyError as e:
# appliance data is not complete (missing name or type)
log.warning("Could not load appliance template {} ('{}'): {}".format(vm["appliance_id"], vm.get("name", "unknown"), e))
log.warning("Cannot load appliance template {} ('{}'): missing key {}".format(vm["appliance_id"], vm.get("name", "unknown"), e))
continue
# Add builtins
+3 -1
View File
@@ -44,7 +44,9 @@ class Appliance:
# Version of the gui before 2.1 use linked_base
# and the server linked_clone
if "linked_base" in self._data:
self._data["linked_clone"] = self._data.pop("linked_base")
linked_base = self._data.pop("linked_base")
if "linked_clone" not in self._data:
self._data["linked_clone"] = linked_base
if data["node_type"] == "iou" and "image" in data:
del self._data["image"]
self._builtin = builtin
+1 -1
View File
@@ -118,7 +118,7 @@ class Drawing:
file_path = os.path.join(self._project.pictures_directory, filename)
if not os.path.exists(file_path):
with open(file_path, "wb+") as f:
with open(file_path, "wb") as f:
f.write(data)
value = filename
+5 -5
View File
@@ -64,7 +64,6 @@ def export_project(project, temporary_dir, include_images=False, keep_compute_id
for root, dirs, files in os.walk(project._path, topdown=True):
files = [f for f in files if not _filter_files(os.path.join(root, f))]
for file in files:
path = os.path.join(root, file)
# Try open the file
@@ -162,9 +161,12 @@ def _export_project_file(project, path, z, include_images, keep_compute_id, allo
if "properties" in node and node["node_type"] != "docker":
for prop, value in node["properties"].items():
if not prop.endswith("image"):
continue
if node["node_type"] == "iou":
if not prop == "path":
continue
elif not prop.endswith("image"):
continue
if value is None or value.strip() == '':
continue
@@ -214,7 +216,6 @@ def _export_local_images(project, image, z):
continue
directory = os.path.split(img_directory)[-1:][0]
if os.path.exists(image):
path = image
else:
@@ -265,4 +266,3 @@ def _export_remote_images(project, compute_id, image_type, image, project_zipfil
arcname = os.path.join("images", image_type, image)
log.info("Saved {}".format(arcname))
project_zipfile.write(temp_path, arcname=arcname, compress_type=zipfile.ZIP_DEFLATED)
+27 -10
View File
@@ -173,12 +173,16 @@ class Link:
self._filters = new_filters
if self._created:
yield from self.update()
self._project.controller.notification.emit("link.updated", self.__json__())
self._project.dump()
@asyncio.coroutine
def update_suspend(self, value):
if value != self._suspend:
self._suspend = value
yield from self.update()
self._project.controller.notification.emit("link.updated", self.__json__())
self._project.dump()
@property
def created(self):
@@ -196,6 +200,8 @@ class Link:
"""
port = node.get_port(adapter_number, port_number)
if port is None:
raise aiohttp.web.HTTPNotFound(text="Port {}/{} for {} not found".format(adapter_number, port_number, node.name))
if port.link is not None:
raise aiohttp.web.HTTPConflict(text="Port is already used")
@@ -211,6 +217,8 @@ class Link:
# 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))
@@ -299,6 +307,12 @@ class Link:
Dump a pcap file on disk
"""
if os.path.exists(self.capture_file_path):
try:
os.remove(self.capture_file_path)
except OSError as e:
raise aiohttp.web.HTTPConflict(text="Could not delete old capture file '{}': {}".format(self.capture_file_path, e))
try:
stream_content = yield from self.read_pcap_from_source()
except aiohttp.web.HTTPException as e:
@@ -309,16 +323,19 @@ class Link:
self._project.controller.notification.emit("link.updated", self.__json__())
with stream_content as stream:
with open(self.capture_file_path, "wb+") as f:
while self._capturing:
# We read 1 bytes by 1 otherwise the remaining data is not read if the traffic stops
data = yield from stream.read(1)
if data:
f.write(data)
# Flush to disk otherwise the live is not really live
f.flush()
else:
break
try:
with open(self.capture_file_path, "wb") as f:
while self._capturing:
# We read 1 bytes by 1 otherwise the remaining data is not read if the traffic stops
data = yield from stream.read(1)
if data:
f.write(data)
# Flush to disk otherwise the live is not really live
f.flush()
else:
break
except OSError as e:
raise aiohttp.web.HTTPConflict(text="Could not write capture file '{}': {}".format(self.capture_file_path, e))
@asyncio.coroutine
def stop_capture(self):
+2 -2
View File
@@ -574,12 +574,12 @@ class Node:
def get_port(self, adapter_number, port_number):
"""
Return the port for this adapter_number and port_number
or raise an HTTPNotFound
or returns None if the port is not found
"""
for port in self.ports:
if port.adapter_number == adapter_number and port.port_number == port_number:
return port
raise aiohttp.web.HTTPNotFound(text="Port {}/{} for {} not found".format(adapter_number, port_number, self.name))
return None
def _list_ports(self):
"""
+31 -34
View File
@@ -39,7 +39,6 @@ from ..utils.asyncio.pool import Pool
from ..utils.asyncio import locked_coroutine, asyncio_ensure_future
from .export_project import export_project
from .import_project import import_project
from ..compute.iou.utils.application_id import get_next_application_id
import logging
log = logging.getLogger(__name__)
@@ -86,7 +85,6 @@ class Project:
self._show_grid = show_grid
self._show_interface_labels = show_interface_labels
self._loading = False
self._add_node_lock = asyncio.Lock()
# Disallow overwrite of existing project
if project_id is None and path is not None:
@@ -440,34 +438,27 @@ class Project:
if node_id in self._nodes:
return self._nodes[node_id]
with (yield from self._add_node_lock):
# wait for a node to be completely created before adding a new one
# this is important otherwise we allocate the same application ID
# when creating multiple IOU node at the same time
if node_type == "iou" and 'application_id' not in kwargs.keys():
kwargs['application_id'] = get_next_application_id(self._nodes.values())
node = Node(self, compute, name, node_id=node_id, node_type=node_type, **kwargs)
if compute not in self._project_created_on_compute:
# For a local server we send the project path
if compute.id == "local":
yield from compute.post("/projects", data={
"name": self._name,
"project_id": self._id,
"path": self._path
})
else:
yield from compute.post("/projects", data={
"name": self._name,
"project_id": self._id,
})
node = Node(self, compute, name, node_id=node_id, node_type=node_type, **kwargs)
if compute not in self._project_created_on_compute:
# For a local server we send the project path
if compute.id == "local":
yield from compute.post("/projects", data={
"name": self._name,
"project_id": self._id,
"path": self._path
})
else:
yield from compute.post("/projects", data={
"name": self._name,
"project_id": self._id,
})
self._project_created_on_compute.add(compute)
yield from node.create()
self._nodes[node.id] = node
self.controller.notification.emit("node.created", node.__json__())
if dump:
self.dump()
self._project_created_on_compute.add(compute)
yield from node.create()
self._nodes[node.id] = node
self.controller.notification.emit("node.created", node.__json__())
if dump:
self.dump()
return node
@locked_coroutine
@@ -667,9 +658,12 @@ class Project:
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)
try:
with open(snapshot.path, "wb") as f:
for data in zipstream:
f.write(data)
except OSError as e:
raise aiohttp.web.HTTPConflict(text="Could not write snapshot file '{}': {}".format(snapshot.path, e))
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
@@ -826,8 +820,11 @@ class Project:
for node_link in link_data["nodes"]:
node = self.get_node(node_link["node_id"])
port = node.get_port(node_link["adapter_number"], node_link["port_number"])
if port is None:
log.warning("Port {}/{} for {} not found".format(node_link["adapter_number"], node_link["port_number"], node.name))
continue
if port.link is not None:
# the node port is already attached to another link
log.warning("Port {}/{} is already connected to link ID {}".format(node_link["adapter_number"], node_link["port_number"], port.link.id))
continue
yield from link.add_node(node, node_link["adapter_number"], node_link["port_number"], label=node_link.get("label"), dump=False)
if len(link.nodes) != 2:
@@ -899,7 +896,7 @@ class Project:
try:
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:
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: