Skip to content

Commit

Permalink
Merge 6437cc5 into bd9ea11
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbyers committed Jan 18, 2019
2 parents bd9ea11 + 6437cc5 commit ca68713
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions napalm/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,16 @@ def __init__(self, hostname, username, password, timeout=60, optional_args=None)
self._dest_file_system = optional_args.get("dest_file_system", None)
self.auto_rollback_on_error = optional_args.get("auto_rollback_on_error", True)

# Control automatic toggling of 'file prompt quiet' for file operations
# Control automatic execution of 'file prompt quiet' for file operations
self.auto_file_prompt = optional_args.get("auto_file_prompt", True)

# Track whether 'file prompt quiet' has been changed by NAPALM.
if self.auto_file_prompt:
self.auto_prompt_changed = False
else:
# If auto_file_prompt is disabled, then prompting must be disabled in the config
self.auto_prompt_changed = None

self.netmiko_optional_args = netmiko_args(optional_args)

# Set the default port if not set
Expand Down Expand Up @@ -157,7 +164,12 @@ def _discover_file_system(self):
raise CommandErrorException(msg)

def close(self):
"""Close the connection to the device."""
"""Close the connection to the device and do the necessary cleanup."""

# Return file prompt quiet to the original state
if self.auto_file_prompt and self.auto_prompt_changed is True:
self.device.send_config_set(["no file prompt quiet"])
self.auto_prompt_changed = False
self._netmiko_close()

def _send_command(self, command):
Expand Down Expand Up @@ -412,32 +424,17 @@ def compare_config(self):
return diff.strip()

def _file_prompt_quiet(f):
"""Decorator to toggle 'file prompt quiet' around methods that perform file operations."""
"""Decorator to toggle 'file prompt quiet' for methods that perform file operations."""

@functools.wraps(f)
def wrapper(self, *args, **kwargs):
# only toggle config if 'auto_file_prompt' is true
if self.auto_file_prompt:
# Only execute config change if allowed and not already done
if self.auto_file_prompt and self.auto_prompt_changed is False:
# disable file operation prompts
self.device.send_config_set(["file prompt quiet"])
self.auto_prompt_changed = True
# call wrapped function
retval = f(self, *args, **kwargs)
# re-enable prompts
self.device.send_config_set(["no file prompt quiet"])
else:
# check if the command is already in the running-config
cmd = "file prompt quiet"
show_cmd = "show running-config | inc {}".format(cmd)
output = self.device.send_command_expect(show_cmd)
if cmd in output:
# call wrapped function
retval = f(self, *args, **kwargs)
else:
msg = (
"on-device file operations require prompts to be disabled. "
"Configure 'file prompt quiet' or set 'auto_file_prompt=True'"
)
raise CommandErrorException(msg)
return retval

return wrapper
Expand Down

0 comments on commit ca68713

Please sign in to comment.