Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
+ self._BACKUP_FILENAME)
self._new_config = f.read()
cfg = [x for x in self._new_config.split("\n") if x is not ""]
output_loadcmd = self.device.send_config_set(cfg)
match_setfailed = re.findall("Delete failed", output_loadcmd)
match_delfailed = re.findall("Set failed", output_loadcmd)
if match_setfailed or match_delfailed:
raise MergeConfigException("Failed merge config: "
+ output_loadcmd)
else:
raise MergeConfigException("config file is not found")
elif config is not None:
self._new_config = config
else:
raise MergeConfigException("no configuration found")
elif "%Please turn config archive on" in output:
msg = "napalm-ios replace() requires Cisco 'archive' feature to be enabled."
raise ReplaceConfigException(msg)
else:
# Merge operation
filename = self.merge_cfg
cfg_file = self._gen_full_path(filename)
if not self._check_file_exists(cfg_file):
raise MergeConfigException("Merge source config file does not exist")
cmd = "copy {} running-config".format(cfg_file)
output = self._commit_handler(cmd)
if "Invalid input detected" in output:
self.rollback()
err_header = "Configuration merge failed; automatic rollback attempted"
merge_error = "{0}:\n{1}".format(err_header, output)
raise MergeConfigException(merge_error)
# After a commit - we no longer know whether this is configured or not.
self.prompt_quiet_configured = None
# Save config to startup (both replace and merge)
output += self.device.save_config()
def load_merge_candidate(self, filename=None, config=None):
"""
SCP file to remote device.
Merge configuration in: copy running-config
"""
self.config_replace = False
return_status, msg = self._load_candidate_wrapper(
source_file=filename,
source_config=config,
dest_file=self.merge_cfg,
file_system=self.dest_file_system,
)
if not return_status:
raise MergeConfigException(msg)
output = ''
try:
output += self.device.send_command('system-view', expect_string=r'\[.+\]')
for command in commands:
output += self.device.send_command(command, expect_string=r'\[.+\]')
if self.device.check_config_mode():
check_error = re.search("error", output, re.IGNORECASE)
if check_error is not None:
return_log = self.device.send_command('return', expect_string=r'[<\[].+[>\]]')
if 'Uncommitted configurations' in return_log:
# Discard uncommitted configuration
return_log += self.device.send_command('n', expect_string=r'<.+>')
output += return_log
raise MergeConfigException('Error while applying config!')
output += self.device.send_command('commit', expect_string=r'\[.+\]')
output += self.device.send_command('return', expect_string=r'<.+>')
else:
raise MergeConfigException('Not in configuration mode.')
except Exception as e:
msg = str(e) + '\nconfiguration output: ' + output
raise MergeConfigException(msg)
commands = self._multiline_convert(commands, start=start, depth=depth)
commands = self._mode_comment_convert(commands)
try:
if self.eos_autoComplete is not None:
self.device.run_commands(commands, autoComplete=self.eos_autoComplete)
else:
self.device.run_commands(commands)
except pyeapi.eapilib.CommandError as e:
self.discard_config()
msg = str(e)
if replace:
raise ReplaceConfigException(msg)
else:
raise MergeConfigException(msg)
def _commit_merge(self):
try:
output = self._send_config(self.merge_candidate)
if output and "Invalid command" in output:
raise MergeConfigException("Error while applying config!")
except Exception as e:
self.changed = True
self.rollback()
err_header = "Configuration merge failed; automatic rollback attempted"
merge_error = "{0}:\n{1}".format(err_header, repr(str(e)))
raise MergeConfigException(merge_error)
self.changed = True
# clear the merge buffer
self.merge_candidate = ""
for command in commands:
output += self.device.send_command(command, expect_string=r'\[.+\]')
if self.device.check_config_mode():
check_error = re.search("error", output, re.IGNORECASE)
if check_error is not None:
return_log = self.device.send_command('return', expect_string=r'[<\[].+[>\]]')
if 'Uncommitted configurations' in return_log:
# Discard uncommitted configuration
return_log += self.device.send_command('n', expect_string=r'<.+>')
output += return_log
raise MergeConfigException('Error while applying config!')
output += self.device.send_command('commit', expect_string=r'\[.+\]')
output += self.device.send_command('return', expect_string=r'<.+>')
else:
raise MergeConfigException('Not in configuration mode.')
except Exception as e:
msg = str(e) + '\nconfiguration output: ' + output
raise MergeConfigException(msg)
("original configuration has been successfully restored" in output)
or ("error" in output.lower())
or ("not a valid config file" in output.lower())
or ("failed" in output.lower())
):
msg = "Candidate config could not be applied\n{}".format(output)
raise ReplaceConfigException(msg)
elif "%Please turn config archive on" in output:
msg = "napalm-ios replace() requires Cisco 'archive' feature to be enabled."
raise ReplaceConfigException(msg)
else:
# Merge operation
filename = self.merge_cfg
cfg_file = self._gen_full_path(filename)
if not self._check_file_exists(cfg_file):
raise MergeConfigException("Merge source config file does not exist")
cmd = "copy {} running-config".format(cfg_file)
output = self._commit_handler(cmd)
if "Invalid input detected" in output:
self.rollback()
err_header = "Configuration merge failed; automatic rollback attempted"
merge_error = "{0}:\n{1}".format(err_header, output)
raise MergeConfigException(merge_error)
# After a commit - we no longer know whether this is configured or not.
self.prompt_quiet_configured = None
# Save config to startup (both replace and merge)
output += self.device.save_config()