Update hypervisors working dir when the project is moved.

This commit is contained in:
Jeremy 2015-02-27 16:51:17 -07:00
parent b55719186e
commit 0e8b8fa66f
4 changed files with 40 additions and 10 deletions

View File

@ -81,7 +81,11 @@ class ProjectHandler:
pm = ProjectManager.instance() pm = ProjectManager.instance()
project = pm.get_project(request.match_info["project_id"]) project = pm.get_project(request.match_info["project_id"])
project.temporary = request.json.get("temporary", project.temporary) project.temporary = request.json.get("temporary", project.temporary)
project.path = request.json.get("path", project.path) project_path = request.json.get("path", project.path)
if project_path != project.path:
project.path = project_path
for module in MODULES:
yield from module.instance().project_moved(project)
response.json(project) response.json(project)
@classmethod @classmethod
@ -119,7 +123,7 @@ class ProjectHandler:
project = pm.get_project(request.match_info["project_id"]) project = pm.get_project(request.match_info["project_id"])
yield from project.close() yield from project.close()
for module in MODULES: for module in MODULES:
yield from module.instance().project_closed(project.path) yield from module.instance().project_closed(project)
pm.remove_project(project.id) pm.remove_project(project.id)
response.set_status(204) response.set_status(204)

View File

@ -233,11 +233,21 @@ class BaseManager:
return vm return vm
@asyncio.coroutine @asyncio.coroutine
def project_closed(self, project_dir): def project_closed(self, project):
""" """
Called when a project is closed. Called when a project is closed.
:param project_dir: project directory :param project: Project instance
"""
pass
@asyncio.coroutine
def project_moved(self, project):
"""
Called when a project is moved
:param project: project instance
""" """
pass pass

View File

@ -133,17 +133,18 @@ class Dynamips(BaseManager):
continue continue
@asyncio.coroutine @asyncio.coroutine
def project_closed(self, project_dir): def project_closed(self, project):
""" """
Called when a project is closed. Called when a project is closed.
:param project_dir: project directory :param project: Project instance
""" """
# delete the Dynamips devices # delete the Dynamips devices corresponding to the project
tasks = [] tasks = []
for device in self._devices.values(): for device in self._devices.values():
tasks.append(asyncio.async(device.delete())) if device.project.id == project.id:
tasks.append(asyncio.async(device.delete()))
if tasks: if tasks:
done, _ = yield from asyncio.wait(tasks) done, _ = yield from asyncio.wait(tasks)
@ -154,7 +155,7 @@ class Dynamips(BaseManager):
log.error("Could not delete device {}".format(e), exc_info=1) log.error("Could not delete device {}".format(e), exc_info=1)
# delete useless files # delete useless files
project_dir = os.path.join(project_dir, 'project-files', self.module_name.lower()) project_dir = project.module_working_directory(self.module_name.lower())
files = glob.glob(os.path.join(project_dir, "*.ghost")) 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, "*_lock"))
files += glob.glob(os.path.join(project_dir, "ilt_*")) files += glob.glob(os.path.join(project_dir, "ilt_*"))
@ -170,6 +171,21 @@ class Dynamips(BaseManager):
log.warn("Could not delete file {}: {}".format(file, e)) log.warn("Could not delete file {}: {}".format(file, e))
continue continue
@asyncio.coroutine
def project_moved(self, project):
"""
Called when a project is moved.
:param project: Project instance
"""
for vm in project.vms:
yield from vm.hypervisor.set_working_dir(project.module_working_directory(self.module_name.lower()))
for device in self._devices.values():
if device.project.id == project.id:
yield from device.hypervisor.set_working_dir(project.module_working_directory(self.module_name.lower()))
@property @property
def dynamips_path(self): def dynamips_path(self):
""" """

View File

@ -146,7 +146,7 @@ class DynamipsHypervisor:
""" """
# encase working_dir in quotes to protect spaces in the path # encase working_dir in quotes to protect spaces in the path
yield from self.send("hypervisor working_dir {}".format('"' + working_dir + '"')) yield from self.send('hypervisor working_dir "{}"'.format(working_dir))
self._working_dir = working_dir self._working_dir = working_dir
log.debug("Working directory set to {}".format(self._working_dir)) log.debug("Working directory set to {}".format(self._working_dir))