From 48e71617d60bb808c2e8f70c3699121d2af69bfc Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Thu, 21 Apr 2016 17:27:49 +0200 Subject: [PATCH] Store capture in a temporary directory on compute node --- gns3server/compute/project.py | 24 +++++++++++++++++++++--- tests/compute/test_project.py | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/gns3server/compute/project.py b/gns3server/compute/project.py index 787d16dd..67c6c610 100644 --- a/gns3server/compute/project.py +++ b/gns3server/compute/project.py @@ -70,6 +70,12 @@ class Project: raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) self.path = path + try: + if os.path.exists(self.tmp_working_directory()): + shutil.rmtree(self.tmp_working_directory()) + except OSError: + raise aiohttp.web.HTTPInternalServerError(text="Could not clean project directory: {}".format(e)) + log.info("Project {id} with path '{path}' created".format(path=self._path, id=self._id)) def __json__(self): @@ -274,14 +280,20 @@ class Project: raise aiohttp.web.HTTPInternalServerError(text="Could not create the VM working directory: {}".format(e)) return workdir + def tmp_working_directory(self): + """ + A temporary directory. Will be clean at project open and close + """ + return os.path.join(self._path, "project-files", "tmp") + def capture_working_directory(self): """ - Returns a working directory where to store packet capture files. + Returns a working directory where to temporary store packet capture files. :returns: path to the directory """ - workdir = os.path.join(self._path, "project-files", "captures") + workdir = os.path.join(self._path, "project-files", "tmp", "captures") try: os.makedirs(workdir, exist_ok=True) except OSError as e: @@ -329,6 +341,12 @@ class Project: for module in self.compute(): yield from module.instance().project_closed(self) + try: + if os.path.exists(self.tmp_working_directory()): + shutil.rmtree(self.tmp_working_directory()) + except OSError: + pass + @asyncio.coroutine def _close_and_clean(self, cleanup): """ @@ -484,7 +502,7 @@ class Project: for root, dirs, files in os.walk(self._path, topdown=True): # Remove snapshots and capture if os.path.split(root)[-1:][0] == "project-files": - dirs[:] = [d for d in dirs if d not in ("snapshots", "captures")] + dirs[:] = [d for d in dirs if d not in ("snapshots", "tmp")] # Ignore log files and OS noise files = [f for f in files if not f.endswith('_log.txt') and not f.endswith('.log') and f != '.DS_Store'] diff --git a/tests/compute/test_project.py b/tests/compute/test_project.py index bc43d489..d7254fef 100644 --- a/tests/compute/test_project.py +++ b/tests/compute/test_project.py @@ -51,6 +51,22 @@ def test_affect_uuid(): assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f' +def test_clean_tmp_directory(async_run): + """ + The tmp directory should be clean at project open and close + """ + + p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f') + path = p.tmp_working_directory() + os.makedirs(path) + async_run(p.close()) + assert not os.path.exists(path) + + os.makedirs(path) + p = Project(project_id='00010203-0405-0607-0809-0a0b0c0d0e0f') + assert not os.path.exists(path) + + def test_path(tmpdir): directory = Config.instance().get_section_config("Server").get("project_directory")