From babdfd5086461312cfebc960775a9bf9812806b1 Mon Sep 17 00:00:00 2001 From: grossmj Date: Mon, 19 May 2014 13:14:57 -0600 Subject: [PATCH] Amend device configs when renaming. --- gns3server/modules/dynamips/nodes/router.py | 44 +++++++++++++++++++++ gns3server/modules/iou/__init__.py | 2 +- gns3server/modules/iou/iou_device.py | 15 ++++++- gns3server/modules/vpcs/__init__.py | 2 +- gns3server/modules/vpcs/vpcs_device.py | 19 +++++++-- 5 files changed, 76 insertions(+), 6 deletions(-) diff --git a/gns3server/modules/dynamips/nodes/router.py b/gns3server/modules/dynamips/nodes/router.py index e2ce440d..554cd729 100644 --- a/gns3server/modules/dynamips/nodes/router.py +++ b/gns3server/modules/dynamips/nodes/router.py @@ -225,6 +225,38 @@ class Router(object): if new_name in self._allocated_names: raise DynamipsError('Name "{}" is already used by another router'.format(new_name)) + if self._startup_config: + # change the hostname in the startup-config + startup_config_path = os.path.join(self.hypervisor.working_dir, "configs", "{}.cfg".format(self.name)) + if os.path.isfile(startup_config_path): + try: + with open(startup_config_path, "r+") as f: + old_config = f.read() + new_config = old_config.replace(self.name, new_name) + f.seek(0) + f.write(new_config) + new_startup_config_path = os.path.join(os.path.dirname(startup_config_path), "{}.cfg".format(new_name)) + os.rename(startup_config_path, new_startup_config_path) + except OSError as e: + raise DynamipsError("Could not amend the configuration {}: {}".format(startup_config_path, e)) + self.set_config(new_startup_config_path) + + if self._private_config: + # change the hostname in the startup-config + private_config_path = os.path.join(self.hypervisor.working_dir, "configs", "{}-private.cfg".format(self.name)) + if os.path.isfile(private_config_path): + try: + with open(private_config_path, "r+") as f: + old_config = f.read() + new_config = old_config.replace(self.name, new_name) + f.seek(0) + f.write(new_config) + new_private_config_path = os.path.join(os.path.dirname(private_config_path), "{}-private.cfg".format(new_name)) + os.rename(private_config_path, new_private_config_path) + except OSError as e: + raise DynamipsError("Could not amend the configuration {}: {}".format(private_config_path, e)) + self.set_config(self.startup_config, new_private_config_path) + new_name_no_quotes = new_name new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces self._hypervisor.send("vm rename {name} {new_name}".format(name=self._name, @@ -300,6 +332,18 @@ class Router(object): self._hypervisor.send("vm clean_delete {}".format(self._name)) self._hypervisor.devices.remove(self) + if self._startup_config: + # delete the startup-config + startup_config_path = os.path.join(self.hypervisor.working_dir, "configs", "{}.cfg".format(self.name)) + if os.path.isfile(startup_config_path): + os.remove(startup_config_path) + + if self._private_config: + # delete the private-config + private_config_path = os.path.join(self.hypervisor.working_dir, "configs", "{}-private.cfg".format(self.name)) + if os.path.isfile(private_config_path): + os.remove(private_config_path) + log.info("router {name} [id={id}] has been deleted (including associated files)".format(name=self._name, id=self._id)) self._allocated_names.remove(self.name) if self.console: diff --git a/gns3server/modules/iou/__init__.py b/gns3server/modules/iou/__init__.py index b945c4ba..4a3a295a 100644 --- a/gns3server/modules/iou/__init__.py +++ b/gns3server/modules/iou/__init__.py @@ -437,7 +437,7 @@ class IOU(IModule): except OSError as e: raise IOUError("Could not save the configuration from {} to {}: {}".format(request["startup_config"], config_path, e)) elif not os.path.isfile(config_path): - raise IOUError("Startup-config {} could not be found on this server".format(config_path)) + raise IOUError("Startup-config {} could not be found on this server".format(request["startup_config"])) except IOUError as e: self.send_custom_error(str(e)) return diff --git a/gns3server/modules/iou/iou_device.py b/gns3server/modules/iou/iou_device.py index bb126535..fc7ec837 100644 --- a/gns3server/modules/iou/iou_device.py +++ b/gns3server/modules/iou/iou_device.py @@ -177,10 +177,23 @@ class IOUDevice(object): :param new_name: name """ - self._name = new_name + if self._startup_config: + # update the startup-config + config_path = os.path.join(self.working_dir, "startup-config") + if os.path.isfile(config_path): + try: + with open(config_path, "r+") as f: + old_config = f.read() + new_config = old_config.replace(self._name, new_name) + f.seek(0) + f.write(new_config) + except OSError as e: + raise IOUError("Could not amend the configuration {}: {}".format(config_path, e)) + log.info("IOU {name} [id={id}]: renamed to {new_name}".format(name=self._name, id=self._id, new_name=new_name)) + self._name = new_name @property def path(self): diff --git a/gns3server/modules/vpcs/__init__.py b/gns3server/modules/vpcs/__init__.py index 294fe2ec..585f9abd 100644 --- a/gns3server/modules/vpcs/__init__.py +++ b/gns3server/modules/vpcs/__init__.py @@ -348,7 +348,7 @@ class VPCS(IModule): except OSError as e: raise VPCSError("Could not save the configuration from {} to {}: {}".format(request["script_file"], config_path, e)) elif not os.path.isfile(config_path): - raise VPCSError("Startup-config {} could not be found on this server".format(config_path)) + raise VPCSError("Startup-config {} could not be found on this server".format(request["script_file"])) except VPCSError as e: self.send_custom_error(str(e)) return diff --git a/gns3server/modules/vpcs/vpcs_device.py b/gns3server/modules/vpcs/vpcs_device.py index f95cb9d0..5e3263bb 100644 --- a/gns3server/modules/vpcs/vpcs_device.py +++ b/gns3server/modules/vpcs/vpcs_device.py @@ -158,10 +158,23 @@ class VPCSDevice(object): :param new_name: name """ - self._name = new_name + if self._script_file: + # update the startup.vpc + config_path = os.path.join(self.working_dir, "startup.vpc") + if os.path.isfile(config_path): + try: + with open(config_path, "r+") as f: + old_config = f.read() + new_config = old_config.replace(self._name, new_name) + f.seek(0) + f.write(new_config) + except OSError as e: + raise VPCSError("Could not amend the configuration {}: {}".format(config_path, e)) + log.info("VPCS {name} [id={id}]: renamed to {new_name}".format(name=self._name, id=self._id, new_name=new_name)) + self._name = new_name @property def path(self): @@ -288,8 +301,8 @@ class VPCSDevice(object): error=e)) return - log.info("VPCS device {name} [id={id}] has been deleted".format(name=self._name, - id=self._id)) + log.info("VPCS device {name} [id={id}] has been deleted (including associated files)".format(name=self._name, + id=self._id)) @property def started(self):