HEX
Server: LiteSpeed
System: Linux srv146.niagahoster.com 4.18.0-553.30.1.lve.el8.x86_64 #1 SMP Tue Dec 3 01:21:19 UTC 2024 x86_64
User: kodi1989 (1633)
PHP: 8.1.34
Disabled: symlink,shell_exec,exec,popen,system,dl,passthru,escapeshellarg,escapeshellcmd,show_source,pcntl_exec
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/pyroute2/ndb/__pycache__/source.cpython-37.pyc
B

��f?B�@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
ddlmZddlmZddlmZddlmZd	d
lmZmZd	dlmZmZmZej�d�r�dd
lm Z ddl!m"Z"ddl#m$Z$ndZ$dZ"d	Z%dZ&Gdd�de'�Z(Gdd�de)�Z*dS)aa	

Local RTNL
----------

Local RTNL source is a simple `IPRoute` instance. By default NDB
starts with one local RTNL source names `localhost`::

    >>> ndb = NDB()
    >>> ndb.sources.summary().format("json")
    [
        {
            "name": "localhost",
            "spec": "{'target': 'localhost', 'nlm_generator': 1}",
            "state": "running"
        },
        {
            "name": "localhost/nsmanager",
            "spec": "{'target': 'localhost/nsmanager'}",
            "state": "running"
        }
    ]
    >>> ndb.sources['localhost']
    [running] <IPRoute {'target: 'localhost', 'nlm_generator': 1}>

The `localhost` RTNL source starts an additional async cache thread.
The `nlm_generator` option means that instead of collections the
`IPRoute` object returns generators, so `IPRoute` responses will not
consume memory regardless of the RTNL objects number::

    >>> ndb.sources['localhost'].nl.link('dump')
    <generator object RTNL_API.filter_messages at 0x7f61a99a34a0>

See also: :ref:`iproute`

Network namespaces
------------------

There are two ways to connect additional sources to an NDB instance.
One is to specify sources when creating an NDB object::

    ndb = NDB(sources=[{'target': 'localhost'}, {'netns': 'test01'}])

Another way is to call `ndb.sources.add()` method::

    ndb.sources.add(netns='test01')

This syntax: `{target': 'localhost'}` and `{'netns': 'test01'}` is the
short form. The full form would be::

    {'target': 'localhost', # the label for the DB
     'kind': 'local',       # use IPRoute class to start the source
     'nlm_generator': 1}    #

    {'target': 'test01',    # the label
     'kind': 'netns',       # use NetNS class
     'netns': 'test01'}     #

See also: :ref:`netns`

Remote systems
--------------

It is possible also to connect to remote systems using SSH. In order to
use this kind of sources it is required to install the
`mitogen <https://github.com/dw/mitogen>`_ module. The `remote` kind
of sources uses the `RemoteIPRoute` class. The short form::

    ndb.sources.add(hostname='worker1.example.com')


In some more extended form::

    ndb.sources.add(**{'target': 'worker1.example.com',
                       'kind': 'remote',
                       'hostname': 'worker1.example.com',
                       'username': 'jenkins',
                       'check_host_keys': False})

See also: :ref:`remote`
�N)�
basestring)�IPRoute)�NetlinkError)�NetlinkSocketBase)�ifinfmsg)�
RemoteIPRoute�)�ShutdownException�State)�
cmsg_event�cmsg_failed�cmsg_sstart�linux)�netns)�NetNSManager)�NetNS�c@seZdZdd�Zdd�ZdS)�SourceProxycCs||_t��|_||_dS)N)�ndb�queue�Queue�events�target)�selfrr�r�D/opt/alt/python37/lib/python3.7/site-packages/pyroute2/ndb/source.py�__init__ts
zSourceProxy.__init__c	Os�tt��j�}t��|jj|<}|��|jj	�
d|j||||d��|��|jj�
|�}d|krn|dSd|kr~|d�dS)N�api)�typer�call_id�name�argv�kwarg�return�	exception)�str�uuid�uuid4�hex�	threading�EventrZ_call_registry�clear�	messenger�emitr�wait�pop)rr r!r"r�event�responserrrrys 
zSourceProxy.apiN)�__name__�
__module__�__qualname__rrrrrrrssrc@seZdZdZdZdZdZdZdZe	e
eed�Z
dd�Zedd	��Zed
d��Zedd
��Zdd�Zedd��Zdd�Zedd��Zedd��Zedd��Zedd��Zedd��Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Z d(d)�Z!e"j#d*fd+d,�Z$d6d.d/�Z%d0d1�Z&d2d3�Z'd4d5�Z(dS)7�Sourcez�
    The RNTL source. The source that is used to init the object
    must comply to IPRoute API, must support the async_cache. If
    the source starts additional threads, they must be joined
    in the source.close()
    �srcN�sources)�localr�remote�	nsmanagercKsd|_d|_||_|jj|_|d|_|�dd�|_|�dt�|_	|�d�|_
|�|j�|_||_
|jjdk	r�|jjj�|j�d|_t��|_t��|_t��|_t��|_|j��|j�d|j�|_t|jdgd	�|_|j�d
�|jj� |j|j|�|�!�dS)Nr�kindr8�
max_errorsr0rz
sources.%s�running)�logZ	wait_list�init)"�th�nlrZ_event_queue�evqrr/r;�SOURCE_MAX_ERRORSr<r0�	get_prime�nl_prime�nl_kwargr,�targets�add�errors_counterr)r*�shutdown�started�RLock�lock�
shutdown_lockr+r>Zchannelr
�state�set�task_managerZdb_add_nl_source�load_sql)rr�specrrrr�s.






zSource.__init__cCs|jj�d|j�S)NzSELECT count(*) FROM %s)rrQ�db_fetchone�table)�cls�viewrrr�_count�sz
Source._countcCs|jdks|j|jkrdSdS)NrTF)r<rI)rrrr�must_restart�szSource.must_restartc	Cs$ttdd�ddd|j�d�ff��S)NcSs|ddk	S)Nrr)�xrrr�<lambda>��z'Source.bind_arguments.<locals>.<lambda>)Zasync_cacheT)Zclone_socketT�groups)�dict�filterrF�get)rrrr�bind_arguments�szSource.bind_argumentscCsnyH|jdk	r,|jjt|j|j�f|jd�n|jjt|j�f|jd�Wn tk
rh|j�d�dSXdS)N)�source�stopFT)	r0rB�putrrr
r	rOrP)rrrr�	set_ready�s
zSource.set_readycCs�t|�}i}d|kr0d|d<d|d<|d|d<d|kr^d|d<|d|d<t�|d�|d<x |D]}||krd||||<qdW|S)N�hostnamer9r;�ssh�protocolrr)r^rZ_get_netnspath)rVrS�ret�defaults�keyrrrrj�s
zSource.defaultscCsBt|jt�r|jjj}nt|jt�r,|jj}d|j��||jfS)Nz[%s] <%s %s>)	�
isinstancerEr�	__class__r2rrOr`rF)rr rrr�__repr__�s
zSource.__repr__cCs|S)Nr)rVr rrr�nla2nameszSource.nla2namecCs|S)Nr)rVr rrr�name2nlaszSource.name2nlaccs>dVx2|��D]&}||j��|d||jffVqWdS)N)rOr rSz%s)�keysrOr`rF)rVrWrkrrr�summary
szSource.summarycCs
|�|�S)N)rr)rVrWrrr�dumpszSource.dumpcCst|t�r||dkSdS)Nr )rlr)r�left�rightrrr�compare_records
zSource.compare_recordcCs"|j�|jd�p tt�d�|j�S)N�pyroute2)�vmapr`r;�getattr�	importlib�
import_module)rr rrrrDszSource.get_primecOs�x�td�D]�}|j��y0|j�d|�d|�d|���t|j|�||�Stttt	t
tjt
jfk
rn�YnNtk
r�}z0|jd7_|j�d|�d��t�d�Wdd}~XYnXWdQRXq
Wtd��dS)N�dzsource api run � rzsource api error: <�>zapi call failed)�rangerMr>�debugryrAr�AttributeError�
ValueError�KeyError�	TypeError�socket�error�struct�	ExceptionrI�time�sleep�RuntimeError)rr r!r"�_�errrrs&*z
Source.apicCs�d}t�}d|d<d|d<d|d<d|d	d<d
|d	d<|j|d	d<d
|d<d|fdg|d<|��|jj|g|jd�dS)Nz.https://github.com/svinota/pyroute2/issues/737r�indexZuprOr�flags��header�rrZRTM_NEWLINKr0ZIFLA_IFNAME)ZIFLA_ADDRESSz00:00:00:00:00:00�attrs)rb)rr�encoderBrd)r�urlZzero_ifrrr�fake_zero_if6s
zSource.fake_zero_ifcCs2�x�|j��dk�r�|j��r P|j��(|jdk	rzy|jjdd�Wn2tk
rx}z|j�	d|�Wdd}~XYnXy�|j�
d�t|jt
�r�i}|�|j�|jdkr�|jj|d<|jf|�|_ntd��|j�
d	�|jjf|j�z@|jj�|j�|jd
k�r|��|jj|j��|jd�WdXd|_W�ntk
�rJ}z�|jd7_|j�
�|j�
d
|j���|j�dt
|��d|���y|jjt |j�f|jd�Wn$t!k
�r�|j�
d�PYnX|j"�r0|j�#d�|j�
d�|j�$t%�|j���r8|j�#d�|j�
d�Pn|�&�SwWdd}~XYnXWdQRX|j�B|j��d	k�r�|�&��szP|j�
�|j�'�|j�
d�WdQRX�x*|j��dk�r�yt(|j���}Wnrtk
�r<}zR|jd7_|j�dt
|�|f�d}|j"�r|j�
d�n|j�
d�PWdd}~XYnXd}|�rl|ddd�rl|dddj)}|dk�s�|t*j+k�r�|j�
d�Py|jj||jd�Wn$t!k
�r�|j�
d�PYnX�q�WqWy(|�,�|j�#d�|jj�|j�Wn"t!k
�r |j�#d�YnX|j�
d�dS)Nrcr)�codezsource restart: %sZ
connecting)r:�libczsource channel not supportedZloading)r8rr9)rbrzfailed, counter zsource error: r}zsleeping before restart�restartzsource shutdownr=)rcr�zsource error: %s %sr�r�zflush DB for the targetz#shutdown handled by the main thread�stopped)-rOr`rJ�is_setrMrA�closer�r>�warningrPrlrEr�updaterFr;rr�r��bindrarQZdb_flushrr�rBrdrsrIrKr�rr	rYr�r.�SOURCE_FAIL_PAUSErer+�tupler��errno�
ECONNRESET�sync)rr�rS�msgr�rrr�receiverGs�


"




zSource.receivercCs<|j�d�t��}|jjt|j|�f|jd�|��dS)Nr�)rb)	r>r�r)r*rBrdrrr.)rr�rrrr��szSource.syncc	Cs`|j�P|j�d�|jdk	r0|j��r0td��tj|jd|j	d�|_|j�
�|SQRXdS)Nzstarting the sourcezsource is runningzNDB event source: %s)rr )rMr>r�r@�is_aliver�r)�Threadr�r�start)rrrrr��s
zSource.startTcCs�|j��|j��r"|j�d�dS|j�d�|j��|jdk	r�y|jj|d�Wn2tk
r�}z|j�	d|�Wdd}~XYnXWdQRX|r�|j
dk	r�|j
��d|_
n|j�d�dS)Nzalready stoppedzsource shutdown)r�zsource close: %szreceiver thread missing)rNrJr�r>r�rPrAr�r�r�r@�join)rr�r�r�rrrr��s 


,

zSource.close�unknownc
Cs~|j�d|j�R|j�d|�|j��z.|��|jrD|j��|j	��|�
�WdXWdQRXWdQRX|j��dS)Nz"restarting the source, reason <%s>)rMrNr>r�rKr+r�r@r�rJr�r.)r�reasonrrrr��s


zSource.restartcCs|S)Nr)rrrr�	__enter__�szSource.__enter__cCs|��dS)N)r�)r�exc_type�	exc_value�	tracebackrrr�__exit__�szSource.__exit__cCs~|jj�d|jjj|jf�}|\|d<|d<xJ|jj�d|jjj|jf�D](}|\}}}}|dkrnt|�n|||<qNWdS)Nz�
                                        SELECT * FROM sources
                                        WHERE f_target = %s
                                        rr;z�
                                          SELECT * FROM sources_options
                                          WHERE f_target = %s
                                          �int)rrQrTZschemaZplchrZdb_fetchr�)rrSZf_target�f_nameZf_typeZf_valuerrrrR�s



zSource.load_sql)r�))r2r3r4�__doc__Ztable_aliasZdump_headerZsummary_headerrWrUrrrrrxr�classmethodrX�propertyrYrarerjrnrorprrrsrvrDrr�r�r�r�r�r�r�r�r�r�rRrrrrr5�sB

p
r5)+r�r�rzrr�r��sysr)r�r&Zpyroute2.commonrZpyroute2.iprouterZpyroute2.netlink.exceptionsrZpyroute2.netlink.nlsocketrZpyroute2.netlink.rtnl.ifinfmsgrZpyroute2.remoterrr	r
�messagesrrr
�platform�
startswithrwrZpyroute2.netns.managerrZpyroute2.nslink.nslinkrr�rC�objectrr^r5rrrr�<module>Qs6