Garbage collect VM when closing a project

This commit is contained in:
Julien Duponchelle committed 2015-03-02 17:17:28 +01:00
1 parent 66860655b9
commit a9afaa028c
6 files changed
+32 -34

No files matched your search

@@ -121,11 +121,7 @@ class ProjectHandler:
pm = ProjectManager.instance()
project = pm.get_project(request.match_info["project_id"])
for module in MODULES:
yield from module.instance().project_closing(project)
yield from project.close()
for module in MODULES:
yield from module.instance().project_closed(project)
pm.remove_project(project.id)
response.set_status(204)
+3 -1
View File
@@ -255,7 +255,9 @@ class BaseManager:
:param project: Project instance
"""
pass
for vm in project.vms:
if vm.id in self._vms:
del self._vms[vm.id]
@asyncio.coroutine
def project_moved(self, project):
+3 -1
View File
@@ -143,6 +143,7 @@ class Dynamips(BaseManager):
:param project: Project instance
"""
yield from super().project_closing(project)
# delete the Dynamips devices corresponding to the project
tasks = []
for device in self._devices.values():
@@ -165,8 +166,9 @@ class Dynamips(BaseManager):
:param project: Project instance
"""
yield from super().project_closed(project)
# delete useless Dynamips files
project_dir = project.module_working_directory(self.module_name.lower())
project_dir = project.module_working_path(self.module_name.lower())
files = glob.glob(os.path.join(project_dir, "*.ghost"))
files += glob.glob(os.path.join(project_dir, "*_lock"))
files += glob.glob(os.path.join(project_dir, "ilt_*"))
+24 -1
View File
@@ -180,13 +180,21 @@ class Project:
:returns: working directory
"""
workdir = os.path.join(self._path, "project-files", module_name)
workdir = self.module_working_path(module_name)
try:
os.makedirs(workdir, exist_ok=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create module working directory: {}".format(e))
return workdir
def module_working_path(self, module_name):
"""
Return the working direcotory for the module. If you want
to be sure to have the directory on disk take a look on:
module_working_directory
"""
return os.path.join(self._path, "project-files", module_name)
def vm_working_directory(self, vm):
"""
Return a working directory for a specific VM.
@@ -250,7 +258,11 @@ class Project:
def close(self):
"""Close the project, but keep information on disk"""
for module in self.modules():
yield from module.instance().project_closing(self)
yield from self._close_and_clean(self._temporary)
for module in self.modules():
yield from module.instance().project_closed(self)
@asyncio.coroutine
def _close_and_clean(self, cleanup):
@@ -304,7 +316,11 @@ class Project:
def delete(self):
"""Remove project from disk"""
for module in self.modules():
yield from module.instance().project_closing(self)
yield from self._close_and_clean(True)
for module in self.modules():
yield from module.instance().project_closed(self)
@classmethod
def clean_project_directory(cls):
@@ -318,3 +334,10 @@ class Project:
if os.path.exists(os.path.join(path, ".gns3_temporary")):
log.warning("Purge old temporary project {}".format(project))
shutil.rmtree(path)
def modules(self):
"""Return VM modules loaded"""
# We import it at the last time to avoid circular dependencies
from ..modules import MODULES
return MODULES