diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 4d1beea8..73e4adf2 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -305,11 +305,17 @@ class Controller: if base_name not in names: return base_name i = 1 - while "{}-{}".format(base_name, i) in names: + + projects_path = self.projects_directory() + + while True: + new_name = "{}-{}".format(base_name, i) + if new_name not in names and not os.path.exists(os.path.join(projects_path, new_name)): + break i += 1 if i > 1000000: raise aiohttp.web.HTTPConflict(text="A project name could not be allocated (node limit reached?)") - return "{}-{}".format(base_name, i) + return new_name @property def projects(self): @@ -318,6 +324,10 @@ class Controller: """ return self._projects + def projects_directory(self): + server_config = Config.instance().get_section_config("Server") + return os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects")) + @staticmethod def instance(): """ diff --git a/gns3server/controller/compute.py b/gns3server/controller/compute.py index ac98331b..14be2908 100644 --- a/gns3server/controller/compute.py +++ b/gns3server/controller/compute.py @@ -472,4 +472,3 @@ class Compute: path = "/projects/{}/files".format(project.id) res = yield from self.http_query("GET", path, timeout=120) return res.json - diff --git a/gns3server/controller/import_project.py b/gns3server/controller/import_project.py index b9ccf167..0ccd72fe 100644 --- a/gns3server/controller/import_project.py +++ b/gns3server/controller/import_project.py @@ -44,8 +44,7 @@ def import_project(controller, project_id, stream): :param gns3vm: True move Docker, IOU and Qemu to the GNS3 VM :returns: Project """ - server_config = Config.instance().get_section_config("Server") - projects_path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects")) + projects_path = controller.projects_directory() os.makedirs(projects_path, exist_ok=True) with zipfile.ZipFile(stream) as myzip: @@ -108,7 +107,8 @@ def _move_files_to_compute(compute, project_id, directory, files_path): path = os.path.join(dirpath, filename) dst = os.path.relpath(path, directory) yield from _upload_file(compute, project_id, path, dst) - shutil.rmtree(directory) + shutil.rmtree(os.path.join(directory, files_path)) + @asyncio.coroutine def _upload_file(compute, project_id, file_path, path): @@ -118,7 +118,7 @@ def _upload_file(compute, project_id, file_path, path): :param file_path: File path on the controller file system :param path: File path on the remote system relative to project directory """ - path = "/projects/{}/files/path".format(project_id, path.replace("\\", "/")) + path = "/projects/{}/files/{}".format(project_id, path.replace("\\", "/")) with open(file_path, "rb") as f: yield from compute.http_query("POST", path, f, timeout=None) diff --git a/gns3server/handlers/api/compute/project_handler.py b/gns3server/handlers/api/compute/project_handler.py index ce9300dc..d64f96c7 100644 --- a/gns3server/handlers/api/compute/project_handler.py +++ b/gns3server/handlers/api/compute/project_handler.py @@ -349,6 +349,7 @@ class ProjectHandler: response.set_status(200) try: + os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, 'wb+') as f: while True: packet = yield from request.content.read(512)