Replace snapshots by import / export

Fix #1042
This commit is contained in:
Julien Duponchelle committed 2016-07-26 19:52:58 +02:00
1 parent 814fd1fcfb
commit 68eca6c111
14 files changed
+606 -18

No files matched your search

+3 -3
View File
@@ -296,9 +296,9 @@ class Controller:
topo_data.pop("type")
if topo_data["project_id"] in self._projects:
project = self._projects[topo_data["project_id"]]
else:
project = yield from self.add_project(path=os.path.dirname(path), status="closed", filename=os.path.basename(path), **topo_data)
self.remove_project(self._projects[topo_data["project_id"]])
project = yield from self.add_project(path=os.path.dirname(path), status="closed", filename=os.path.basename(path), **topo_data)
if load:
yield from project.open()
return project
+4
View File
@@ -91,6 +91,10 @@ def _filter_files(path):
:returns: True if file should not be included in the final archive
"""
s = os.path.normpath(path).split(os.path.sep)
if path.endswith("snapshots"):
return True
try:
i = s.index("project-files")
if s[i + 1] in ("tmp", "captures", "snapshots"):
+11 -6
View File
@@ -43,7 +43,7 @@ def import_project(controller, project_id, stream, location=None, name=None, kee
:param controller: GNS3 Controller
:param project_id: ID of the project to import
:param stream: A io.BytesIO of the zipfile
:param location: Parent directory for the project if None put in the default directory
:param location: Directory for the project if None put in the default directory
:param name: Wanted project name, generate one from the .gns3 if None
:param keep_compute_id: If true do not touch the compute id
:returns: Project
@@ -53,11 +53,16 @@ def import_project(controller, project_id, stream, location=None, name=None, kee
try:
topology = json.loads(myzip.read("project.gns3").decode())
# If the project name is already used we generate a new one
if name:
project_name = controller.get_free_project_name(name)
# We import the project on top of an existing project (snapshots)
if topology["project_id"] == project_id:
project_name = topology["name"]
else:
project_name = controller.get_free_project_name(topology["name"])
# If the project name is already used we generate a new one
if name:
project_name = controller.get_free_project_name(name)
else:
project_name = controller.get_free_project_name(topology["name"])
except KeyError:
raise aiohttp.web.HTTPConflict(text="Can't import topology the .gns3 is corrupted or missing")
@@ -66,7 +71,7 @@ def import_project(controller, project_id, stream, location=None, name=None, kee
else:
projects_path = controller.projects_directory()
path = os.path.join(projects_path, project_name)
os.makedirs(path)
os.makedirs(path, exist_ok=True)
myzip.extractall(path)
topology = load_topology(os.path.join(path, "project.gns3"))
+72 -3
View File
@@ -26,6 +26,7 @@ import tempfile
from uuid import UUID, uuid4
from .node import Node
from .snapshot import Snapshot
from .drawing import Drawing
from .topology import project_to_topology, load_topology
from .udp_link import UDPLink
@@ -105,6 +106,15 @@ class Project:
self._nodes = {}
self._links = {}
self._drawings = {}
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
# Create the project on demand on the compute node
self._project_created_on_compute = set()
@@ -378,12 +388,62 @@ class Project:
"""
return self._links
@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)
@asyncio.coroutine
def close(self):
for compute in self._project_created_on_compute:
yield from compute.post("/projects/{}/close".format(self._id))
self._cleanPictures()
self.reset()
self._status = "closed"
def _cleanPictures(self):
@@ -405,10 +465,19 @@ class Project:
@asyncio.coroutine
def delete(self):
yield from self.close()
for compute in self._project_created_on_compute:
yield from compute.delete("/projects/{}".format(self._id))
yield from self.delete_on_computes()
shutil.rmtree(self.path, ignore_errors=True)
@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()
@classmethod
def _get_default_project_directory(cls):
"""
+93
View File
@@ -0,0 +1,93 @@
#!/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/>.
import os
import uuid
import shutil
import asyncio
from datetime import datetime, timezone
from .import_project import import_project
# The string use to extract the date from the filename
FILENAME_TIME_FORMAT = "%d%m%y_%H%M%S"
class Snapshot:
"""
A snapshot object
"""
def __init__(self, project, name=None, filename=None):
assert filename or name, "You need to pass a name or a filename"
self._id = str(uuid.uuid4()) # We don't need to keep id between project loading because they are use only as key for operation like delete, update.. but have no impact on disk
self._project = project
if name:
self._name = name
self._created_at = datetime.now().timestamp()
filename = self._name + "_" + datetime.utcfromtimestamp(self._created_at).replace(tzinfo=None).strftime(FILENAME_TIME_FORMAT) + ".gns3project"
else:
self._name = filename.split("_")[0]
datestring = filename.replace(self._name + "_", "").split(".")[0]
try:
self._created_at = datetime.strptime(datestring, FILENAME_TIME_FORMAT).replace(tzinfo=timezone.utc).timestamp()
except ValueError:
self._created_at = datetime.utcnow().timestamp()
self._path = os.path.join(project.path, "snapshots", filename)
@property
def id(self):
return self._id
@property
def name(self):
return self._name
@property
def path(self):
return self._path
@property
def created_at(self):
return int(self._created_at)
@asyncio.coroutine
def restore(self):
"""
Restore the snapshot
"""
yield from self._project.delete_on_computes()
yield from self._project.close()
shutil.rmtree(os.path.join(self._project.path, "project-files"))
with open(self._path, "rb") as f:
project = yield from import_project(self._project.controller, self._project.id, f, location=self._project.path)
yield from project.open()
return project
def __json__(self):
return {
"snapshot_id": self._id,
"name": self._name,
"created_at": int(self._created_at),
"project_id": self._project.id
}