Store capture in a temporary directory on compute node

This commit is contained in:
Julien Duponchelle 2016-04-21 17:27:49 +02:00
parent 30f05c6190
commit 48e71617d6
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
2 changed files with 37 additions and 3 deletions

View File

@ -70,6 +70,12 @@ class Project:
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
self.path = path 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)) log.info("Project {id} with path '{path}' created".format(path=self._path, id=self._id))
def __json__(self): def __json__(self):
@ -274,14 +280,20 @@ class Project:
raise aiohttp.web.HTTPInternalServerError(text="Could not create the VM working directory: {}".format(e)) raise aiohttp.web.HTTPInternalServerError(text="Could not create the VM working directory: {}".format(e))
return workdir 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): 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 :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: try:
os.makedirs(workdir, exist_ok=True) os.makedirs(workdir, exist_ok=True)
except OSError as e: except OSError as e:
@ -329,6 +341,12 @@ class Project:
for module in self.compute(): for module in self.compute():
yield from module.instance().project_closed(self) 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 @asyncio.coroutine
def _close_and_clean(self, cleanup): def _close_and_clean(self, cleanup):
""" """
@ -484,7 +502,7 @@ class Project:
for root, dirs, files in os.walk(self._path, topdown=True): for root, dirs, files in os.walk(self._path, topdown=True):
# Remove snapshots and capture # Remove snapshots and capture
if os.path.split(root)[-1:][0] == "project-files": 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 # 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'] files = [f for f in files if not f.endswith('_log.txt') and not f.endswith('.log') and f != '.DS_Store']

View File

@ -51,6 +51,22 @@ def test_affect_uuid():
assert p.id == '00010203-0405-0607-0809-0a0b0c0d0e0f' 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): def test_path(tmpdir):
directory = Config.instance().get_section_config("Server").get("project_directory") directory = Config.instance().get_section_config("Server").get("project_directory")