def _get_connection(self): """Context manager providing a netmiko SSH connection object. This function hides the complexities of gracefully handling retrying failed connection attempts. """ retry_exc_types = (paramiko.SSHException, EOFError) # Use tenacity to handle retrying. @tenacity.retry( # Log a message after each failed attempt. after=tenacity.after_log(LOG, logging.DEBUG), # Reraise exceptions if our final attempt fails. reraise=True, # Retry on SSH connection errors. retry=tenacity.retry_if_exception_type(retry_exc_types), # Stop after the configured timeout. stop=tenacity.stop_after_delay( int(self.ngs_config['ngs_ssh_connect_timeout'])), # Wait for the configured interval between attempts. wait=tenacity.wait_fixed( int(self.ngs_config['ngs_ssh_connect_interval'])), ) def _create_connection(): return netmiko.ConnectHandler(**self.config) # First, create a connection. try: net_connect = _create_connection() except tenacity.RetryError as e: LOG.error("Reached maximum SSH connection attempts, not retrying") raise exc.GenericSwitchNetmikoConnectError( config=device_utils.sanitise_config(self.config), error=e) except Exception as e: LOG.error("Unexpected exception during SSH connection") raise exc.GenericSwitchNetmikoConnectError( config=device_utils.sanitise_config(self.config), error=e) # Now yield the connection to the caller. with net_connect: yield net_connect
def send_commands_to_device(self, cmd_set): if not cmd_set: LOG.debug("Nothing to execute") return try: net_connect = netmiko.ConnectHandler(**self.config) net_connect.enable() output = net_connect.send_config_set(config_commands=cmd_set) except Exception as e: raise exc.GenericSwitchNetmikoConnectError(config=self.config, error=e) LOG.debug(output)
def show_port_list(self): try: with ngs_lock.PoolLock(self.locker, **self.lock_kwargs): with self._get_connection() as net_connect: net_connect.enable() # output = net_connect.send_config_set( # config_commands=cmd_set) # NOTE (vsaienko) always save configuration # when configuration is applied successfully. if self.SHOW_PORT_LIST: output = net_connect.send_command(self.SHOW_PORT_LIST) except Exception as e: raise exc.GenericSwitchNetmikoConnectError(config=self.config, error=e) return output
def send_commands_to_device(self, cmd_set): if not cmd_set: LOG.debug("Nothing to execute") return try: with ngs_lock.PoolLock(self.locker, **self.lock_kwargs): with self._get_connection() as net_connect: net_connect.enable() output = net_connect.send_config_set( config_commands=cmd_set) # NOTE (vsaienko) always save configuration # when configuration is applied successfully. if self.SAVE_CONFIGURATION: net_connect.send_command(self.SAVE_CONFIGURATION) except Exception as e: raise exc.GenericSwitchNetmikoConnectError(config=self.config, error=e) LOG.debug(output) return output
def send_commands_to_device(self, cmd_set): if not cmd_set: LOG.debug("Nothing to execute") return try: with ngs_lock.PoolLock(self.locker, **self.lock_kwargs): with self._get_connection() as net_connect: output = self.send_config_set(net_connect, cmd_set) # NOTE (vsaienko) always save configuration # when configuration is applied successfully. self.save_configuration(net_connect) except exc.GenericSwitchException: # Reraise without modification exceptions originating from this # module. raise except Exception as e: raise exc.GenericSwitchNetmikoConnectError( config=device_utils.sanitise_config(self.config), error=e) LOG.debug(output) return output