From 0e8b8fa66f2969bba85a76696eb6002727687649 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Fri, 27 Feb 2015 16:51:17 -0700 Subject: [PATCH] Update hypervisors working dir when the project is moved. --- gns3server/handlers/api/project_handler.py | 8 ++++-- gns3server/modules/base_manager.py | 14 ++++++++-- gns3server/modules/dynamips/__init__.py | 26 +++++++++++++++---- .../modules/dynamips/dynamips_hypervisor.py | 2 +- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/gns3server/handlers/api/project_handler.py b/gns3server/handlers/api/project_handler.py index f9bd0e72..7477de25 100644 --- a/gns3server/handlers/api/project_handler.py +++ b/gns3server/handlers/api/project_handler.py @@ -81,7 +81,11 @@ class ProjectHandler: pm = ProjectManager.instance() project = pm.get_project(request.match_info["project_id"]) 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) @classmethod @@ -119,7 +123,7 @@ class ProjectHandler: project = pm.get_project(request.match_info["project_id"]) yield from project.close() for module in MODULES: - yield from module.instance().project_closed(project.path) + yield from module.instance().project_closed(project) pm.remove_project(project.id) response.set_status(204) diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index 82d3104d..5c8488f7 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -233,11 +233,21 @@ class BaseManager: return vm @asyncio.coroutine - def project_closed(self, project_dir): + def project_closed(self, project): """ 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 diff --git a/gns3server/modules/dynamips/__init__.py b/gns3server/modules/dynamips/__init__.py index 0e8b648a..0a517936 100644 --- a/gns3server/modules/dynamips/__init__.py +++ b/gns3server/modules/dynamips/__init__.py @@ -133,17 +133,18 @@ class Dynamips(BaseManager): continue @asyncio.coroutine - def project_closed(self, project_dir): + def project_closed(self, project): """ 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 = [] 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: done, _ = yield from asyncio.wait(tasks) @@ -154,7 +155,7 @@ class Dynamips(BaseManager): log.error("Could not delete device {}".format(e), exc_info=1) # 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, "*_lock")) 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)) 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 def dynamips_path(self): """ diff --git a/gns3server/modules/dynamips/dynamips_hypervisor.py b/gns3server/modules/dynamips/dynamips_hypervisor.py index 4895b9fe..863e112a 100644 --- a/gns3server/modules/dynamips/dynamips_hypervisor.py +++ b/gns3server/modules/dynamips/dynamips_hypervisor.py @@ -146,7 +146,7 @@ class DynamipsHypervisor: """ # 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 log.debug("Working directory set to {}".format(self._working_dir))