From 08b2dc6369baa22a142118c6a0bb984c34762f21 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Thu, 22 Jan 2015 11:49:22 +0100 Subject: [PATCH] Cleanup VMS when leaving --- gns3server/modules/base_manager.py | 7 +++++++ gns3server/modules/base_vm.py | 10 +++++++++- gns3server/modules/vpcs/vpcs_vm.py | 3 --- gns3server/server.py | 11 +++++------ 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index 992a1b29..33206f37 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -49,6 +49,13 @@ class BaseManager: cls._instance = cls() return cls._instance + def __del__(self): + self.destroy() + + def destroy(): + """Cleanup the VMS. Call this before closing the server""" + cls._instance() + @property def module_name(self): """ diff --git a/gns3server/modules/base_vm.py b/gns3server/modules/base_vm.py index cd6a7aa4..4f13ca04 100644 --- a/gns3server/modules/base_vm.py +++ b/gns3server/modules/base_vm.py @@ -32,7 +32,8 @@ class BaseVM: name=self.name, uuid=self.uuid)) - # TODO: When delete release console ports + def __del__(self): + self.destroy() @property def project(self): @@ -118,3 +119,10 @@ class BaseVM: """ raise NotImplementedError + + def destroy(self): + """ + Destroy the VM process. + """ + + raise NotImplementedError diff --git a/gns3server/modules/vpcs/vpcs_vm.py b/gns3server/modules/vpcs/vpcs_vm.py index d7ef3d3c..fa80e5e9 100644 --- a/gns3server/modules/vpcs/vpcs_vm.py +++ b/gns3server/modules/vpcs/vpcs_vm.py @@ -81,9 +81,6 @@ class VPCSVM(BaseVM): else: self._console = self._manager.port_manager.get_free_console_port() - def __del__(self): - self.destroy() - def destroy(self): self._kill_process() if self._console: diff --git a/gns3server/server.py b/gns3server/server.py index a776a71e..4161a917 100644 --- a/gns3server/server.py +++ b/gns3server/server.py @@ -77,7 +77,10 @@ class Server: Cleanup the modules (shutdown running emulators etc.) """ - # TODO: clean everything from here + for module in MODULES: + log.debug("Unloading module {}".format(module.__name__)) + m = module.instance() + m.destroy() self._loop.stop() def _signal_handling(self): @@ -152,8 +155,4 @@ class Server: # FIXME: remove it in production or in tests self._loop.call_later(1, self._reload_hook) - try: - self._loop.run_forever() - except KeyboardInterrupt: - log.info("\nExiting...") - self._cleanup() + self._loop.run_forever()