Skip to content

Commit

Permalink
Closes #2005: Change rollback behavior (#2006)
Browse files Browse the repository at this point in the history
  • Loading branch information
decoupca authored Mar 26, 2024
1 parent f0a69b4 commit 800e8d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
33 changes: 29 additions & 4 deletions napalm/nxos/nxos.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@ def _send_command(
) -> Dict[str, Union[str, Dict[str, Any]]]:
raise NotImplementedError

def _check_file_exists(self, cfg_file: str) -> bool:
"""
Check that the file exists on remote device using full path.
cfg_file can be a full path, e.g.: bootflash:rollback_config.txt
or just a filename, e.g.: rollback_config.txt
For example
# dir rollback_config.txt
71803 Sep 06 14:13:33 2023 rollback_config.txt
Usage for bootflash://sup-local
6211682304 bytes used
110314684416 bytes free
116526366720 bytes total
"""
cmd = f"dir {cfg_file}"
output = self._send_command(command=cmd, raw_text=True)
if "No such file or directory" in output:
return False
else:
return True

def _commit_merge(self) -> None:
try:
output = self._send_config(self.merge_candidate)
Expand Down Expand Up @@ -918,10 +941,12 @@ def _load_cfg_from_checkpoint(self) -> None:

def rollback(self) -> None:
assert isinstance(self.device, NXOSDevice)
if self.changed:
self.device.rollback(self.rollback_cfg)
self._copy_run_start()
self.changed = False
if not self._check_file_exists(cfg_file=self.rollback_cfg):
msg = f"Rollback file '{self.rollback_cfg}' does not exist on device."
raise ReplaceConfigException(msg)
self.device.rollback(self.rollback_cfg)
self._copy_run_start()
self.changed = False

def get_facts(self) -> models.FactsDict:
facts: models.FactsDict = {} # type: ignore
Expand Down
33 changes: 18 additions & 15 deletions napalm/nxos_ssh/nxos_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,21 +553,24 @@ def _load_cfg_from_checkpoint(self):
raise ReplaceConfigException(msg)

def rollback(self):
if self.changed:
commands = [
"terminal dont-ask",
"rollback running-config file {}".format(self.rollback_cfg),
"no terminal dont-ask",
]
result = self._send_command_list(
commands, expect_string=r"[#>]", read_timeout=90
)
if "completed" not in result.lower():
raise ReplaceConfigException(result)
# If hostname changes ensure Netmiko state is updated properly
self._netmiko_device.set_base_prompt()
self._copy_run_start()
self.changed = False
if not self._check_file_exists(self.rollback_cfg):
msg = f"Rollback file '{self.rollback_cfg}' does not exist on device."
raise ReplaceConfigException(msg)

commands = [
"terminal dont-ask",
"rollback running-config file {}".format(self.rollback_cfg),
"no terminal dont-ask",
]
result = self._send_command_list(
commands, expect_string=r"[#>]", read_timeout=90
)
if "completed" not in result.lower():
raise ReplaceConfigException(result)
# If hostname changes ensure Netmiko state is updated properly
self._netmiko_device.set_base_prompt()
self._copy_run_start()
self.changed = False

def _apply_key_map(self, key_map, table):
new_dict = {}
Expand Down

0 comments on commit 800e8d7

Please sign in to comment.