From 95766fa30da94214eb8310eef6e5303fea766d7d Mon Sep 17 00:00:00 2001 From: grossmj Date: Sun, 8 Mar 2015 19:13:01 -0600 Subject: [PATCH] Let the server know about the project name and convert old IOU projects on remote servers. --- gns3server/handlers/api/project_handler.py | 2 ++ gns3server/modules/base_manager.py | 25 +++++++++++----------- gns3server/modules/project.py | 22 ++++++++++++++++--- gns3server/modules/project_manager.py | 4 ++-- gns3server/schemas/project.py | 15 +++++++++++++ 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/gns3server/handlers/api/project_handler.py b/gns3server/handlers/api/project_handler.py index 14e40d52..e31d32bc 100644 --- a/gns3server/handlers/api/project_handler.py +++ b/gns3server/handlers/api/project_handler.py @@ -37,6 +37,7 @@ class ProjectHandler: pm = ProjectManager.instance() p = pm.create_project( + name=request.json.get("name"), path=request.json.get("path"), project_id=request.json.get("project_id"), temporary=request.json.get("temporary", False) @@ -81,6 +82,7 @@ class ProjectHandler: pm = ProjectManager.instance() project = pm.get_project(request.match_info["project_id"]) project.temporary = request.json.get("temporary", project.temporary) + project.name = request.json.get("name", project.name) project_path = request.json.get("path", project.path) if project_path != project.path: project.path = project_path diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index 49e9ace7..9c642c7d 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -162,8 +162,7 @@ class BaseManager: """ new_id = str(uuid4()) - project_name = os.path.basename(project.path) - legacy_project_files_path = os.path.join(project.path, "{}-files".format(project_name)) + legacy_project_files_path = os.path.join(project.path, "{}-files".format(project.name)) new_project_files_path = os.path.join(project.path, "project-files") if os.path.exists(legacy_project_files_path) and not os.path.exists(new_project_files_path): # move the project files @@ -175,16 +174,18 @@ class BaseManager: raise aiohttp.web.HTTPInternalServerError(text="Could not move project files directory: {} to {} {}".format(legacy_project_files_path, new_project_files_path, e)) - legacy_iou_dir = os.path.join(project.path, "iou") - new_iou_dir = os.path.join(project.path, "project-files", "iou") - if os.path.exists(legacy_iou_dir) and not os.path.exists(new_iou_dir): - # move the iou dir on remote server - try: - log.info('Moving "{}" to "{}"'.format(legacy_iou_dir, new_iou_dir)) - yield from wait_run_in_executor(shutil.move, legacy_iou_dir, new_iou_dir) - except OSError as e: - raise aiohttp.web.HTTPInternalServerError(text="Could not move IOU directory: {} to {} {}".format(legacy_iou_dir, - new_iou_dir, e)) + if project.is_local() is False: + legacy_remote_iou_project = os.path.join(project.location, project.name, "iou") + new_iou_project_path = os.path.join(project.path, "project-files", "iou") + if os.path.exists(legacy_remote_iou_project) and not os.path.exists(new_iou_project_path): + # move the legacy remote IOU project (remote servers only) + log.info("Converting old remote IOU project...") + try: + log.info('Moving "{}" to "{}"'.format(legacy_remote_iou_project, new_iou_project_path)) + yield from wait_run_in_executor(shutil.move, legacy_remote_iou_project, new_iou_project_path) + except OSError as e: + raise aiohttp.web.HTTPInternalServerError(text="Could not move IOU directory: {} to {} {}".format(legacy_remote_iou_project, + new_iou_project_path, e)) if hasattr(self, "get_legacy_vm_workdir"): # rename old project VM working dir diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index a6a48a66..23c545bf 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -41,8 +41,9 @@ class Project: :param temporary: Boolean the project is a temporary project (destroy when closed) """ - def __init__(self, project_id=None, path=None, location=None, temporary=False): + def __init__(self, name=None, project_id=None, path=None, location=None, temporary=False): + self._name = name if project_id is None: self._id = str(uuid4()) else: @@ -75,6 +76,7 @@ class Project: def __json__(self): return { + "name": self._name, "project_id": self._id, "location": self._location, "temporary": self._temporary, @@ -85,6 +87,10 @@ class Project: return Config.instance().get_section_config("Server") + def is_local(self): + + return self._config().get("local", False) + @classmethod def _get_default_project_directory(cls): """ @@ -113,7 +119,7 @@ class Project: @location.setter def location(self, location): - if location != self._location and self._config().get("local", False) is False: + if location != self._location and self.is_local() is False: raise aiohttp.web.HTTPForbidden(text="You are not allowed to modify the project directory location") self._location = location @@ -127,12 +133,22 @@ class Project: def path(self, path): if hasattr(self, "_path"): - if path != self._path and self._config().get("local", False) is False: + if path != self._path and self.is_local() is False: raise aiohttp.web.HTTPForbidden(text="You are not allowed to modify the project directory location") self._path = path self._update_temporary_file() + @property + def name(self): + + return self._name + + @name.setter + def name(self, name): + + self._name = name + @property def vms(self): diff --git a/gns3server/modules/project_manager.py b/gns3server/modules/project_manager.py index fa34217d..454b3114 100644 --- a/gns3server/modules/project_manager.py +++ b/gns3server/modules/project_manager.py @@ -60,7 +60,7 @@ class ProjectManager: raise aiohttp.web.HTTPNotFound(text="Project ID {} doesn't exist".format(project_id)) return self._projects[project_id] - def create_project(self, project_id=None, path=None, temporary=False): + def create_project(self, name=None, project_id=None, path=None, temporary=False): """ Create a project and keep a references to it in project manager. @@ -71,7 +71,7 @@ class ProjectManager: return self._projects[project_id] # FIXME: should we have an error? #raise aiohttp.web.HTTPConflict(text="Project ID {} is already in use on this server".format(project_id)) - project = Project(project_id=project_id, path=path, temporary=temporary) + project = Project(name=name, project_id=project_id, path=path, temporary=temporary) self._projects[project.id] = project return project diff --git a/gns3server/schemas/project.py b/gns3server/schemas/project.py index 7bd8b723..38bea4c0 100644 --- a/gns3server/schemas/project.py +++ b/gns3server/schemas/project.py @@ -21,6 +21,11 @@ PROJECT_CREATE_SCHEMA = { "description": "Request validation to create a new Project instance", "type": "object", "properties": { + "name": { + "description": "Project name", + "type": ["string", "null"], + "minLength": 1 + }, "path": { "description": "Project directory", "type": ["string", "null"], @@ -46,6 +51,11 @@ PROJECT_UPDATE_SCHEMA = { "description": "Request validation to update a Project instance", "type": "object", "properties": { + "name": { + "description": "Project name", + "type": "string", + "minLength": 1 + }, "temporary": { "description": "If project is a temporary project", "type": "boolean" @@ -63,6 +73,11 @@ PROJECT_OBJECT_SCHEMA = { "description": "Project instance", "type": "object", "properties": { + "name": { + "description": "Project name", + "type": "string", + "minLength": 1 + }, "location": { "description": "Base directory where the project should be created on remote server", "type": "string",