From f6561bf684b3ef195379369cd73ca23164c44ae8 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Mon, 10 Nov 2014 13:50:17 -0700 Subject: [PATCH] Automatically extract IOS configs when a project is closed. --- gns3server/modules/dynamips/__init__.py | 11 +++++++- gns3server/modules/dynamips/backends/vm.py | 26 +----------------- gns3server/modules/dynamips/nodes/router.py | 30 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/gns3server/modules/dynamips/__init__.py b/gns3server/modules/dynamips/__init__.py index 897a1b13..a69518d2 100644 --- a/gns3server/modules/dynamips/__init__.py +++ b/gns3server/modules/dynamips/__init__.py @@ -154,12 +154,16 @@ class Dynamips(IModule): if not sys.platform.startswith("win32"): self._callback.stop() + # automatically save configs for all router instances + for router_id in self._routers: + router = self._routers[router_id] + router.save_configs() + # stop all Dynamips hypervisors if self._hypervisor_manager: self._hypervisor_manager.stop_all_hypervisors() self.delete_dynamips_files() - IModule.stop(self, signum) # this will stop the I/O loop def _check_hypervisors(self): @@ -225,6 +229,11 @@ class Dynamips(IModule): :param request: JSON request (not used) """ + # automatically save configs for all router instances + for router_id in self._routers: + router = self._routers[router_id] + router.save_configs() + # stop all Dynamips hypervisors if self._hypervisor_manager: self._hypervisor_manager.stop_all_hypervisors() diff --git a/gns3server/modules/dynamips/backends/vm.py b/gns3server/modules/dynamips/backends/vm.py index 0b690b63..da9c8eaf 100644 --- a/gns3server/modules/dynamips/backends/vm.py +++ b/gns3server/modules/dynamips/backends/vm.py @@ -608,31 +608,7 @@ class VM(object): return try: - if router.startup_config or router.private_config: - - startup_config_base64, private_config_base64 = router.extract_config() - if startup_config_base64: - try: - config = base64.decodestring(startup_config_base64.encode("utf-8")).decode("utf-8") - config = "!\n" + config.replace("\r", "") - config_path = os.path.join(router.hypervisor.working_dir, router.startup_config) - with open(config_path, "w") as f: - log.info("saving startup-config to {}".format(router.startup_config)) - f.write(config) - except OSError as e: - raise DynamipsError("Could not save the startup configuration {}: {}".format(config_path, e)) - - if private_config_base64: - try: - config = base64.decodestring(private_config_base64.encode("utf-8")).decode("utf-8") - config = "!\n" + config.replace("\r", "") - config_path = os.path.join(router.hypervisor.working_dir, router.private_config) - with open(config_path, "w") as f: - log.info("saving private-config to {}".format(router.private_config)) - f.write(config) - except OSError as e: - raise DynamipsError("Could not save the private configuration {}: {}".format(config_path, e)) - + router.save_configs() except DynamipsError as e: log.warn("could not save config to {}: {}".format(router.startup_config, e)) diff --git a/gns3server/modules/dynamips/nodes/router.py b/gns3server/modules/dynamips/nodes/router.py index ba1ca522..ea32ac12 100644 --- a/gns3server/modules/dynamips/nodes/router.py +++ b/gns3server/modules/dynamips/nodes/router.py @@ -26,6 +26,7 @@ from ...attic import find_unused_port import time import sys import os +import base64 import logging log = logging.getLogger(__name__) @@ -598,6 +599,35 @@ class Router(object): log.info("router {name} [id={id}]: new private-config pushed".format(name=self._name, id=self._id)) + def save_configs(self): + """ + Saves the startup-config and private-config to files. + """ + + if self.startup_config or self.private_config: + startup_config_base64, private_config_base64 = self.extract_config() + if startup_config_base64: + try: + config = base64.decodebytes(startup_config_base64.encode("utf-8")).decode("utf-8") + config = "!\n" + config.replace("\r", "") + config_path = os.path.join(self.hypervisor.working_dir, self.startup_config) + with open(config_path, "w") as f: + log.info("saving startup-config to {}".format(self.startup_config)) + f.write(config) + except OSError as e: + raise DynamipsError("Could not save the startup configuration {}: {}".format(config_path, e)) + + if private_config_base64: + try: + config = base64.decodebytes(private_config_base64.encode("utf-8")).decode("utf-8") + config = "!\n" + config.replace("\r", "") + config_path = os.path.join(self.hypervisor.working_dir, self.private_config) + with open(config_path, "w") as f: + log.info("saving private-config to {}".format(self.private_config)) + f.write(config) + except OSError as e: + raise DynamipsError("Could not save the private configuration {}: {}".format(config_path, e)) + @property def ram(self): """