def _commit_replace(self): cmd = self._bnc_cmd(self.filename_candidate, "replace", "continue-on-error") self._send_rpc(cmd) if self.error.getvalue(): self.rollback() raise ReplaceConfigException(self.error.getvalue())
def load_replace_candidate(self, filename=None, config=None): self.pending_changes = True self.replace = True try: self.device.load_candidate_config(filename=filename, config=config) except InvalidInputError as e: self.pending_changes = False self.replace = False raise ReplaceConfigException(e.message)
def commit_config(self): try: self.device.execute_command('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 _load_candidate(self, filename, config, overwrite): if filename is None: configuration = config else: with open(filename) as f: configuration = f.read() 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)
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 load_replace_candidate(self, filename=None, config=None): self.replace = True self.loaded = True if filename is None: temp_file = tempfile.NamedTemporaryFile() temp_file.write(config) temp_file.flush() cfg_file_path = temp_file.name else: cfg_file_path = filename self.fc = FileCopy(self.device, cfg_file_path) if not self.fc.file_already_exists(): try: self.fc.transfer_file() except FileTransferError as fte: raise ReplaceConfigException(fte.message)
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 _commit_replace(self): try: self.device.replace_config() except ConfigReplaceError as e: raise ReplaceConfigException(e.message)