def load_merge_candidate(self, filename=None, config=None): """ Only configuration in set-format is supported with load_merge_candidate. """ if filename is not None: if os.path.exists(filename) is True: with open(filename) as f: self.device.send_command("cp " + self._BOOT_FILENAME + " " + 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")
def _commit_merge(self): cmd = self._bnc_cmd(self.filename_candidate, "merge", "rollback-on-error") self._send_rpc(cmd) if self.error.getvalue(): self.discard_config() raise MergeConfigException(self.error.getvalue())
def _get_file_content(self, filename): try: with open(filename, 'r') as f: content = f.read() except IOError: raise MergeConfigException('Error while opening {0}. Make sure ' 'filename is correct.'.format(filename)) return content
def commit_config(self): try: self._execute_command_with_vdom( 'execute backup config flash commit_with_napalm') self.device.commit() self.discard_config() except FailedCommit as e: if self.config_replace: raise ReplaceConfigException(e.message) else: raise MergeConfigException(e.message)
def commit_config(self): """Implementation of NAPALM method commit_config.""" if self.loaded: if self.replace: try: self.device.commit_replace_config() except Exception as e: self.device.rollback() raise ReplaceConfigException(str(e)) else: try: self.device.commit_config() except Exception as e: self.device.rollback() raise MergeConfigException(str(e)) else: raise MergeConfigException('No config loaded.') self.changed = True self.loaded = False
def load_merge_candidate(self, filename=None, config=None): self.config_replace = False if config: raise NotImplementedError if filename: self.filename = os.path.basename(filename) try: self._upload_scf(filename) except Exception as err: raise MergeConfigException('{}'.format(err))
def load_merge_candidate(self, filename=None, config=None): self.config_replace = False self.device.candidate_config = FortiConfig('candidate') self.device.running_config = FortiConfig('running') self._load_config(filename, config) for block in self.device.candidate_config.get_block_names(): try: self.device.load_config(path=block, empty_candidate=True) except CommandExecutionException as e: raise MergeConfigException(e.message)
def load_merge_candidate(self, filename=None, config=None): self.replace = False self.loaded = True if not filename and not config: raise MergeConfigException( 'filename or config param must be provided.') self.merge_candidate += '\n' # insert one extra line if filename is not None: with open(filename, "r") as f: self.merge_candidate += f.read() else: self.merge_candidate += config
def load_merge_candidate(self, filename=None, config=None): if filename: file_config = True content = self._get_file_content(filename) config = content.splitlines() self._send_merge_commands(config, file_config) elif config: file_config = False self._send_merge_commands(config, file_config) else: raise MergeConfigException('You must provide either a file ' 'or a set-format string')
def commit_config(self): if self.loaded: self.backup_file = 'config_' + str(datetime.now()).replace(' ', '_') install_config.save_config(self.device, self.backup_file) if self.replace: if install_config.rollback(self.device, self.fc.dst) is False: raise ReplaceConfigException else: try: self._commit_merge() except Exception as e: raise MergeConfigException(str(e)) self.changed = True self.loaded = False else: raise ReplaceConfigException('No config loaded.')
def commit_config(self): if self.loaded: self.backup_file = 'config_' + str(datetime.now()).replace( ' ', '_') self._save_config(self.backup_file) if self.replace: if self._load_config() is False: raise ReplaceConfigException else: try: self._commit_merge() self.merge_candidate = '' # clear the merge buffer except Exception as e: raise MergeConfigException(str(e)) self.changed = True self.loaded = False else: raise ReplaceConfigException('No config loaded.')
def commit_config(self): """ Netmiko is being used to commit the configuration because it takes a better care of results compared to pan-python. """ if self.loaded: if self.ssh_connection is False: self._open_ssh() try: self.ssh_device.commit() time.sleep(3) self.loaded = False self.changed = True except: # noqa if self.merge_config: raise MergeConfigException('Error while commiting config') else: raise ReplaceConfigException('Error while commiting config') else: raise ReplaceConfigException('No config loaded.')
def _send_merge_commands(self, config, file_config): """ Netmiko is being used to push set commands. """ if self.loaded is False: if self._save_backup() is False: raise MergeConfigException('Error while storing backup ' 'config.') if self.ssh_connection is False: self._open_ssh() if file_config: if isinstance(config, str): config = config.splitlines() else: if isinstance(config, str): config = str(config).split() self.ssh_device.send_config_set(config) self.loaded = True self.merge_config = True
def _load_config(self, filename=None, config=None, replace=True): if self.config_session is not None: raise SessionLockedException('Session is already in use by napalm') else: self.config_session = 'napalm_{}'.format( datetime.now().microsecond) commands = list() commands.append('configure session {}'.format(self.config_session)) if replace: commands.append('rollback clean-config') if filename is not None: with open(filename, 'r') as f: lines = f.readlines() else: if isinstance(config, list): lines = config else: lines = config.splitlines() for line in lines: line = line.strip() if line == '': continue if line.startswith('!'): continue commands.append(line) try: self.device.run_commands(commands) except pyeapi.eapilib.CommandError as e: self.discard_config() if replace: raise ReplaceConfigException(e.message) else: raise MergeConfigException(e.message)
def _load_candidate(self, filename, config, overwrite): if filename is None: configuration = config else: with open(filename) as f: configuration = f.read() if not self.config_lock: # if not locked during connection time # will try to lock it if not already aquired self.lock() # and the device will be locked till first commit/rollback try: self.device.cu.load(configuration, format='text', overwrite=overwrite) except ConfigLoadError as e: if self.config_replace: raise ReplaceConfigException(e.message) else: raise MergeConfigException(e.message)