Import with images

This commit is contained in:
Julien Duponchelle committed 2016-07-21 17:55:37 +02:00
1 parent 0f85fbd5f2
commit 487e99bea5
4 files changed
+65 -7

No files matched your search

+10 -1
View File
@@ -116,6 +116,15 @@ class Controller:
except OSError as e:
log.error(str(e))
def images_path(self):
"""
Get the image storage directory
"""
server_config = Config.instance().get_section_config("Server")
images_path = os.path.expanduser(server_config.get("images_path", "~/GNS3/projects"))
os.makedirs(images_path, exist_ok=True)
return images_path
@asyncio.coroutine
def _import_gns3_gui_conf(self):
"""
@@ -290,7 +299,7 @@ class Controller:
"""
Generate a free project name base on the base name
"""
names = [ p.name for p in self._projects.values() ]
names = [p.name for p in self._projects.values()]
if base_name not in names:
return base_name
i = 1
+22 -2
View File
@@ -18,6 +18,7 @@
import os
import json
import uuid
import shutil
import asyncio
import zipfile
import aiohttp
@@ -29,8 +30,9 @@ from ..config import Config
Handle the import of project from a .gns3project
"""
@asyncio.coroutine
def import_project(controller, project_id, stream, gns3vm=True):
def import_project(controller, project_id, stream):
"""
Import a project contain in a zip file
@@ -64,6 +66,24 @@ def import_project(controller, project_id, stream, gns3vm=True):
json.dump(topology, f, indent=4)
os.remove(os.path.join(path, "project.gns3"))
project = yield from controller.load_project(dot_gns3_path)
if os.path.exists(os.path.join(path, "images")):
_import_images(controller, path)
project = yield from controller.load_project(dot_gns3_path, load=False)
return project
def _import_images(controller, path):
"""
Copy images to the images directory or delete them if they
already exists.
"""
image_dir = controller.images_path()
root = os.path.join(path, "images")
for (dirpath, dirnames, filenames) in os.walk(root):
for filename in filenames:
path = os.path.join(dirpath, filename)
dst = os.path.join(image_dir, os.path.relpath(path, root))
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.move(path, dst)
@@ -279,7 +279,7 @@ class ProjectHandler:
if not packet:
break
temp.write(packet)
project = yield from import_project(controller, request.match_info["project_id"], temp, gns3vm=bool(int(request.GET.get("gns3vm", "1"))))
project = yield from import_project(controller, request.match_info["project_id"], temp)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e))
@@ -367,5 +367,3 @@ class ProjectHandler:
raise aiohttp.web.HTTPNotFound()
except PermissionError:
raise aiohttp.web.HTTPForbidden()