1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 03:08:14 +00:00

Amend device configs when renaming.

This commit is contained in:
grossmj 2014-05-19 13:14:57 -06:00
parent 7182e59892
commit babdfd5086
5 changed files with 76 additions and 6 deletions

View File

@ -225,6 +225,38 @@ class Router(object):
if new_name in self._allocated_names: if new_name in self._allocated_names:
raise DynamipsError('Name "{}" is already used by another router'.format(new_name)) 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_no_quotes = new_name
new_name = '"' + new_name + '"' # put the new name into quotes to protect spaces 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, 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.send("vm clean_delete {}".format(self._name))
self._hypervisor.devices.remove(self) 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)) 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) self._allocated_names.remove(self.name)
if self.console: if self.console:

View File

@ -437,7 +437,7 @@ class IOU(IModule):
except OSError as e: except OSError as e:
raise IOUError("Could not save the configuration from {} to {}: {}".format(request["startup_config"], config_path, e)) raise IOUError("Could not save the configuration from {} to {}: {}".format(request["startup_config"], config_path, e))
elif not os.path.isfile(config_path): 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: except IOUError as e:
self.send_custom_error(str(e)) self.send_custom_error(str(e))
return return

View File

@ -177,10 +177,23 @@ class IOUDevice(object):
:param new_name: name :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, log.info("IOU {name} [id={id}]: renamed to {new_name}".format(name=self._name,
id=self._id, id=self._id,
new_name=new_name)) new_name=new_name))
self._name = new_name
@property @property
def path(self): def path(self):

View File

@ -348,7 +348,7 @@ class VPCS(IModule):
except OSError as e: except OSError as e:
raise VPCSError("Could not save the configuration from {} to {}: {}".format(request["script_file"], config_path, e)) raise VPCSError("Could not save the configuration from {} to {}: {}".format(request["script_file"], config_path, e))
elif not os.path.isfile(config_path): 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: except VPCSError as e:
self.send_custom_error(str(e)) self.send_custom_error(str(e))
return return

View File

@ -158,10 +158,23 @@ class VPCSDevice(object):
:param new_name: name :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, log.info("VPCS {name} [id={id}]: renamed to {new_name}".format(name=self._name,
id=self._id, id=self._id,
new_name=new_name)) new_name=new_name))
self._name = new_name
@property @property
def path(self): def path(self):
@ -288,8 +301,8 @@ class VPCSDevice(object):
error=e)) error=e))
return return
log.info("VPCS device {name} [id={id}] has been deleted".format(name=self._name, log.info("VPCS device {name} [id={id}] has been deleted (including associated files)".format(name=self._name,
id=self._id)) id=self._id))
@property @property
def started(self): def started(self):