Skip to content

Commit

Permalink
Closes #2005: Change rollback behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
decoupca committed Sep 6, 2023
1 parent 480cc91 commit 9d6d060
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
34 changes: 30 additions & 4 deletions napalm/nxos/nxos.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,30 @@ 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
elif cfg_file in output:
return True
return False

def _commit_merge(self) -> None:
try:
output = self._send_config(self.merge_candidate)
Expand Down Expand Up @@ -918,10 +942,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=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 @@ -544,21 +544,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=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 9d6d060

Please sign in to comment.