From a797038aeb4de4a8331914c16ea4b195561daabc Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 24 May 2016 17:54:08 +0200 Subject: [PATCH] Drop code for temporary projects Fix https://github.com/GNS3/gns3-gui/issues/982 --- gns3server/compute/project.py | 71 +------------------ gns3server/compute/project_manager.py | 4 +- gns3server/controller/__init__.py | 15 ++-- gns3server/controller/project.py | 11 +-- .../handlers/api/compute/project_handler.py | 6 +- .../api/controller/project_handler.py | 3 +- gns3server/run.py | 3 - gns3server/schemas/project.py | 16 +---- tests/compute/test_project.py | 62 +--------------- tests/controller/test_project.py | 9 ++- tests/handlers/api/compute/test_project.py | 45 +----------- tests/handlers/api/controller/test_project.py | 4 +- 12 files changed, 25 insertions(+), 224 deletions(-) diff --git a/gns3server/compute/project.py b/gns3server/compute/project.py index 3619576e..f6b01fcc 100644 --- a/gns3server/compute/project.py +++ b/gns3server/compute/project.py @@ -44,10 +44,9 @@ class Project: :param project_id: force project identifier (None by default auto generate an UUID) :param path: path of the project. (None use the standard directory) - :param temporary: boolean to tell if the project is a temporary project (destroy when closed) """ - def __init__(self, name=None, project_id=None, path=None, temporary=False): + def __init__(self, name=None, project_id=None, path=None): self._name = name try: @@ -58,7 +57,6 @@ class Project: self._nodes = set() self._nodes_to_destroy = set() - self.temporary = temporary self._used_tcp_ports = set() self._used_udp_ports = set() @@ -83,8 +81,7 @@ class Project: return { "name": self._name, - "project_id": self._id, - "temporary": self._temporary + "project_id": self._id } def _config(self): @@ -114,19 +111,6 @@ class Project: raise aiohttp.web.HTTPForbidden(text="You are not allowed to modify the project directory path") self._path = path - self._update_temporary_file() - - @asyncio.coroutine - def clean_old_path(self, old_path): - """ - Called after a project location change. All the compute should - have been notified before - """ - if self._temporary: - try: - yield from wait_run_in_executor(shutil.rmtree, old_path) - except OSError as e: - log.warn("Can't remove temporary directory {}: {}".format(old_path, e)) @property def name(self): @@ -145,20 +129,6 @@ class Project: return self._nodes - @property - def temporary(self): - - return self._temporary - - @temporary.setter - def temporary(self, temporary): - - if hasattr(self, 'temporary') and temporary == self._temporary: - return - - self._temporary = temporary - self._update_temporary_file() - def record_tcp_port(self, port): """ Associate a reserved TCP port number with this project. @@ -199,27 +169,6 @@ class Project: if port in self._used_udp_ports: self._used_udp_ports.remove(port) - def _update_temporary_file(self): - """ - Update the .gns3_temporary file in order to reflect the current project status. - """ - - if not hasattr(self, "_path"): - return - - if self._temporary: - try: - with open(os.path.join(self._path, ".gns3_temporary"), 'w+') as f: - f.write("1") - except OSError as e: - raise aiohttp.web.HTTPInternalServerError(text="Could not create temporary project: {}".format(e)) - else: - if os.path.exists(os.path.join(self._path, ".gns3_temporary")): - try: - os.remove(os.path.join(self._path, ".gns3_temporary")) - except OSError as e: - raise aiohttp.web.HTTPInternalServerError(text="Could not mark project as no longer temporary: {}".format(e)) - def module_working_directory(self, module_name): """ Returns a working directory for the module @@ -319,7 +268,7 @@ class Project: for module in self.compute(): yield from module.instance().project_closing(self) - yield from self._close_and_clean(self._temporary) + yield from self._close_and_clean(False) for module in self.compute(): yield from module.instance().project_closed(self) @@ -395,20 +344,6 @@ class Project: for module in self.compute(): yield from module.instance().project_closed(self) - @classmethod - def clean_project_directory(cls): - """ - At startup drop old temporary project. After a crash for example - """ - - directory = get_default_project_directory() - if os.path.exists(directory): - for project in os.listdir(directory): - path = os.path.join(directory, project) - if os.path.exists(os.path.join(path, ".gns3_temporary")): - log.warning("Purge old temporary project {}".format(project)) - shutil.rmtree(path) - def compute(self): """ Returns all loaded modules from compute. diff --git a/gns3server/compute/project_manager.py b/gns3server/compute/project_manager.py index 4d702dfa..cce5af26 100644 --- a/gns3server/compute/project_manager.py +++ b/gns3server/compute/project_manager.py @@ -70,7 +70,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, name=None, project_id=None, path=None, temporary=False): + def create_project(self, name=None, project_id=None, path=None): """ Create a project and keep a references to it in project manager. @@ -79,7 +79,7 @@ class ProjectManager: if project_id is not None and project_id in self._projects: return self._projects[project_id] - project = Project(name=name, project_id=project_id, path=path, temporary=temporary) + project = Project(name=name, project_id=project_id, path=path) self._projects[project.id] = project return project diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 0b343fb5..6914c1fe 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -46,16 +46,14 @@ class Controller: self._config_file = os.path.join(config_path, "gns3_controller.conf") server_config = Config.instance().get_section_config("Server") - print(server_config) if server_config.getboolean("local", False) is True: - print("MMOOOOOOONKEYYYYYY") self._computes["local"] = Compute(compute_id="local", - controller=self, - protocol=server_config.get("protocol", "http"), - host=server_config.get("host", "localhost"), - port=server_config.getint("port", 3080), - user=server_config.get("user", ""), - password=server_config.get("password", "")) + controller=self, + protocol=server_config.get("protocol", "http"), + host=server_config.get("host", "localhost"), + port=server_config.getint("port", 3080), + user=server_config.get("user", ""), + password=server_config.get("password", "")) def save(self): """ @@ -107,7 +105,6 @@ class Controller: """ if compute_id not in self._computes: - # We disallow to create from the outside the if compute_id == 'local': return None diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index cc16be51..ab36ee67 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -34,10 +34,9 @@ class Project: :param project_id: force project identifier (None by default auto generate an UUID) :param path: path of the project. (None use the standard directory) - :param temporary: boolean to tell if the project is a temporary project (destroy when closed) """ - def __init__(self, name=None, project_id=None, path=None, temporary=False, controller=None): + def __init__(self, name=None, project_id=None, path=None, controller=None): self._controller = controller self._name = name @@ -54,7 +53,6 @@ class Project: path = os.path.join(get_default_project_directory(), self._id) self.path = path - self._temporary = temporary self._computes = set() self._nodes = {} self._links = {} @@ -74,10 +72,6 @@ class Project: def id(self): return self._id - @property - def temporary(self): - return self._temporary - @property def path(self): return self._path @@ -126,14 +120,12 @@ class Project: yield from compute.post("/projects", data={ "name": self._name, "project_id": self._id, - "temporary": self._temporary, "path": self._path }) else: yield from compute.post("/projects", data={ "name": self._name, "project_id": self._id, - "temporary": self._temporary }) self._project_created_on_compute.add(compute) @@ -235,6 +227,5 @@ class Project: return { "name": self._name, "project_id": self._id, - "temporary": self._temporary, "path": self._path } diff --git a/gns3server/handlers/api/compute/project_handler.py b/gns3server/handlers/api/compute/project_handler.py index 27528b2b..904869cd 100644 --- a/gns3server/handlers/api/compute/project_handler.py +++ b/gns3server/handlers/api/compute/project_handler.py @@ -73,8 +73,7 @@ class ProjectHandler: 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) + project_id=request.json.get("project_id") ) response.set_status(201) response.json(p) @@ -120,9 +119,6 @@ class ProjectHandler: project.path = project_path for module in MODULES: yield from module.instance().project_moved(project) - yield from project.clean_old_path(old_path) - # Very important: we need to remove temporary flag after moving the project - project.temporary = request.json.get("temporary", project.temporary) response.json(project) @Route.post( diff --git a/gns3server/handlers/api/controller/project_handler.py b/gns3server/handlers/api/controller/project_handler.py index 0440a64b..efe4f605 100644 --- a/gns3server/handlers/api/controller/project_handler.py +++ b/gns3server/handlers/api/controller/project_handler.py @@ -47,8 +47,7 @@ class ProjectHandler: controller = Controller.instance() project = yield from controller.add_project(name=request.json.get("name"), path=request.json.get("path"), - project_id=request.json.get("project_id"), - temporary=request.json.get("temporary", False)) + project_id=request.json.get("project_id")) response.set_status(201) response.json(project) diff --git a/gns3server/run.py b/gns3server/run.py index 2bfb0944..5a8b4c1d 100644 --- a/gns3server/run.py +++ b/gns3server/run.py @@ -31,7 +31,6 @@ from gns3server.web.web_server import WebServer from gns3server.web.logger import init_logger from gns3server.version import __version__ from gns3server.config import Config -from gns3server.compute.project import Project from gns3server.crash_report import CrashReport @@ -224,8 +223,6 @@ def run(): log.critical("The current working directory doesn't exist") return - Project.clean_project_directory() - CrashReport.instance() host = server_config["host"] port = int(server_config["port"]) diff --git a/gns3server/schemas/project.py b/gns3server/schemas/project.py index bd0b9751..e6462182 100644 --- a/gns3server/schemas/project.py +++ b/gns3server/schemas/project.py @@ -37,11 +37,7 @@ PROJECT_CREATE_SCHEMA = { "minLength": 36, "maxLength": 36, "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" - }, - "temporary": { - "description": "Whether the project is a temporary project or not", - "type": "boolean" - }, + } }, "additionalProperties": False, } @@ -56,10 +52,6 @@ PROJECT_UPDATE_SCHEMA = { "type": ["string", "null"], "minLength": 1 }, - "temporary": { - "description": "Whether the project is a temporary project or not", - "type": "boolean" - }, "path": { "description": "Path of the project on the server (work only with --local)", "type": ["string", "null"] @@ -85,10 +77,6 @@ PROJECT_OBJECT_SCHEMA = { "maxLength": 36, "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" }, - "temporary": { - "description": "Whether the project is a temporary project or not", - "type": "boolean" - }, "path": { "description": "Project directory", "type": ["string", "null"], @@ -96,7 +84,7 @@ PROJECT_OBJECT_SCHEMA = { } }, "additionalProperties": False, - "required": ["project_id", "temporary"] + "required": ["project_id"] } PROJECT_LIST_SCHEMA = { diff --git a/tests/compute/test_project.py b/tests/compute/test_project.py index 5cf1c7fd..1ee6f1fb 100644 --- a/tests/compute/test_project.py +++ b/tests/compute/test_project.py @@ -76,7 +76,6 @@ def test_path(tmpdir): p = Project(project_id=str(uuid4())) assert p.path == os.path.join(directory, p.id) assert os.path.exists(os.path.join(directory, p.id)) - assert not os.path.exists(os.path.join(p.path, ".gns3_temporary")) def test_init_path(tmpdir): @@ -86,31 +85,6 @@ def test_init_path(tmpdir): assert p.path == str(tmpdir) -def test_changing_path_temporary_flag(tmpdir): - - with patch("gns3server.compute.project.Project.is_local", return_value=True): - p = Project(temporary=True, project_id=str(uuid4())) - assert os.path.exists(p.path) - original_path = p.path - assert os.path.exists(os.path.join(p.path, ".gns3_temporary")) - - p.path = str(tmpdir) - - -def test_temporary_path(): - p = Project(temporary=True, project_id=str(uuid4())) - assert os.path.exists(p.path) - assert os.path.exists(os.path.join(p.path, ".gns3_temporary")) - - -def test_remove_temporary_flag(): - p = Project(temporary=True, project_id=str(uuid4())) - assert os.path.exists(p.path) - assert os.path.exists(os.path.join(p.path, ".gns3_temporary")) - p.temporary = False - assert not os.path.exists(os.path.join(p.path, ".gns3_temporary")) - - def test_changing_path_not_allowed(tmpdir): with patch("gns3server.compute.project.Project.is_local", return_value=False): with pytest.raises(aiohttp.web.HTTPForbidden): @@ -120,7 +94,7 @@ def test_changing_path_not_allowed(tmpdir): def test_json(tmpdir): p = Project(project_id=str(uuid4())) - assert p.__json__() == {"name": p.name, "project_id": p.id, "temporary": False} + assert p.__json__() == {"name": p.name, "project_id": p.id} def test_node_working_directory(tmpdir, node): @@ -201,40 +175,6 @@ def test_project_close(loop, node, project): assert node.id not in node.manager._nodes -def test_project_close_temporary_project(loop, manager): - """A temporary project is deleted when closed""" - - project = Project(temporary=True, project_id=str(uuid4())) - directory = project.path - assert os.path.exists(directory) - loop.run_until_complete(asyncio.async(project.close())) - assert os.path.exists(directory) is False - -def test_clean_project_directory(tmpdir): - - # A non anonymous project with uuid. - project1 = tmpdir / uuid4() - project1.mkdir() - - # A non anonymous project. - oldproject = tmpdir / uuid4() - oldproject.mkdir() - - # an anonymous project - project2 = tmpdir / uuid4() - project2.mkdir() - tmp = (project2 / ".gns3_temporary") - with open(str(tmp), 'w+') as f: - f.write("1") - - with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}): - Project.clean_project_directory() - - assert os.path.exists(str(project1)) - assert os.path.exists(str(oldproject)) - assert not os.path.exists(str(project2)) - - def test_list_files(tmpdir, loop): with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}): diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index ee03850c..14fa4d7d 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -27,9 +27,10 @@ from uuid import uuid4 from gns3server.controller.project import Project from gns3server.config import Config + @pytest.fixture def project(controller): - return Project(controller=controller) + return Project(controller=controller) def test_affect_uuid(): @@ -42,7 +43,7 @@ def test_affect_uuid(): def test_json(tmpdir): p = Project() - assert p.__json__() == {"name": p.name, "project_id": p.id, "temporary": False, "path": p.path} + assert p.__json__() == {"name": p.name, "project_id": p.id, "path": p.path} def test_path(tmpdir): @@ -99,7 +100,6 @@ def test_add_node_local(async_run, controller): compute.post.assert_any_call('/projects', data={ "name": project._name, "project_id": project._id, - "temporary": project._temporary, "path": project._path }) compute.post.assert_any_call('/projects/{}/vpcs/nodes'.format(project.id), @@ -127,8 +127,7 @@ def test_add_node_non_local(async_run, controller): compute.post.assert_any_call('/projects', data={ "name": project._name, - "project_id": project._id, - "temporary": project._temporary + "project_id": project._id }) compute.post.assert_any_call('/projects/{}/vpcs/nodes'.format(project.id), data={'node_id': node.id, diff --git a/tests/handlers/api/compute/test_project.py b/tests/handlers/api/compute/test_project.py index 1b8ea231..6420bbcd 100644 --- a/tests/handlers/api/compute/test_project.py +++ b/tests/handlers/api/compute/test_project.py @@ -44,16 +44,6 @@ def test_create_project_without_dir(http_compute): response = http_compute.post("/projects", query, example=True) assert response.status == 201 assert response.json["project_id"] == "10010203-0405-0607-0809-0a0b0c0d0e0f" - assert response.json["temporary"] is False - assert response.json["name"] == "test" - - -def test_create_temporary_project(http_compute): - query = {"name": "test", "temporary": True, "project_id": "20010203-0405-0607-0809-0a0b0c0d0e0f"} - response = http_compute.post("/projects", query) - assert response.status == 201 - assert response.json["project_id"] == "20010203-0405-0607-0809-0a0b0c0d0e0f" - assert response.json["temporary"] is True assert response.json["name"] == "test" @@ -66,13 +56,12 @@ def test_create_project_with_uuid(http_compute): def test_show_project(http_compute): - query = {"name": "test", "project_id": "40010203-0405-0607-0809-0a0b0c0d0e02", "temporary": False} + query = {"name": "test", "project_id": "40010203-0405-0607-0809-0a0b0c0d0e02"} response = http_compute.post("/projects", query) assert response.status == 201 response = http_compute.get("/projects/40010203-0405-0607-0809-0a0b0c0d0e02", example=True) - assert len(response.json.keys()) == 3 + assert len(response.json.keys()) == 2 assert response.json["project_id"] == "40010203-0405-0607-0809-0a0b0c0d0e02" - assert response.json["temporary"] is False assert response.json["name"] == "test" @@ -97,35 +86,7 @@ def test_list_projects(http_compute): assert "51010203-0405-0607-0809-0a0b0c0d0e0f" in [p["project_id"] for p in response.json] -def test_update_temporary_project(http_compute): - query = {"name": "test", "temporary": True, "project_id": "60010203-0405-0607-0809-0a0b0c0d0e0b"} - response = http_compute.post("/projects", query) - assert response.status == 201 - query = {"name": "test", "temporary": False} - response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True) - assert response.status == 200 - assert response.json["temporary"] is False - - -def test_update_path_project_temporary(http_compute, tmpdir): - - os.makedirs(str(tmpdir / "a")) - os.makedirs(str(tmpdir / "b")) - - with patch("gns3server.compute.project.Project.is_local", return_value=True): - response = http_compute.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "temporary": True, "project_id": "70010203-0405-0607-0809-0a0b0c0d0e0b"}) - assert response.status == 201 - assert response.json["name"] == "first_name" - query = {"name": "second_name", "path": str(tmpdir / "b")} - response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True) - assert response.status == 200 - assert response.json["name"] == "second_name" - - assert not os.path.exists(str(tmpdir / "a")) - assert os.path.exists(str(tmpdir / "b")) - - -def test_update_path_project_non_temporary(http_compute, tmpdir): +def test_update_path_project(http_compute, tmpdir): os.makedirs(str(tmpdir / "a")) os.makedirs(str(tmpdir / "b")) diff --git a/tests/handlers/api/controller/test_project.py b/tests/handlers/api/controller/test_project.py index 5b5b9c9e..2bb7a540 100644 --- a/tests/handlers/api/controller/test_project.py +++ b/tests/handlers/api/controller/test_project.py @@ -54,16 +54,14 @@ def test_create_project_without_dir(http_controller): response = http_controller.post("/projects", query, example=True) assert response.status == 201 assert response.json["project_id"] == "10010203-0405-0607-0809-0a0b0c0d0e0f" - assert response.json["temporary"] is False assert response.json["name"] == "test" def test_create_temporary_project(http_controller): - query = {"name": "test", "temporary": True, "project_id": "20010203-0405-0607-0809-0a0b0c0d0e0f"} + query = {"name": "test", "project_id": "20010203-0405-0607-0809-0a0b0c0d0e0f"} response = http_controller.post("/projects", query) assert response.status == 201 assert response.json["project_id"] == "20010203-0405-0607-0809-0a0b0c0d0e0f" - assert response.json["temporary"] is True assert response.json["name"] == "test"