HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/primordial/service/__pycache__/registration.cpython-39.pyc
a

�,�hp4�@s�ddlmZddlmZddlmZddlmZddlZddlZddl	Z	ddl
Z	ddlZddlZddl
Z
ddlZddlmZmZmZmZmZmZmZmZddlmZddlmZmZdd	lmZdd
lm Z m!Z!ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ee,�Z-iZ.e�/e0�Z1dd
�Z2eee3ee3fZ4Gdd�d�Z5e3ej6d�dd�Z7e3e3ej6d�dd�Z8ee,edd�dd�Z9e,dd�dd�Z:dd�dd�Z;e,e<e,ee,e,fd�d d!�Z=e,e>d"�d#d$�Z?d(eee3e5d%�d&d'�Z@dS))�)�ArgumentParser)�datetime)�defaultdict)�partialN)�Any�Callable�Dict�Iterable�List�Optional�Tuple�Union)�uuid1)�KazooClient�
KazooState)�CANONICAL_TIMESTRING_FORMAT)�retry�
RetryError)	�ZK_HOSTS�GD_ZKRUN_VIRTUALENV_PATH�GD_ZKRUN_COMMAND�
GD_ZKRUN_NAME�GD_ZKRUN_HOSTS�GD_ZKRUN_SERVICE_TYPE�
GD_ZKRUN_PORT�GD_ZKRUN_SSL_PORT�GD_ZKRUN_LOCATIONScCstS)z*Gets the zkhosts from the default settings)r�rr�P/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/primordial/service/registration.py�get_zkhosts src
@sreZdZdZdZdeeeeeeeeeeee	dd�dd�Z
edd�dd	�Zdd
�dd�Zdd
�d
d�Z
dS)�ZkRegistrarz=Implementation of a basic Zookeeper ephemeral node registrar.N�
registrations)�listener�name�zkhosts�service_type�port�ssl_port�	locations�returncCsx|durt�}t|ttf�r&d�|�}|p.|j|_||_t|d�|_	t
t��|_||_
||_||_||_d|_d|_dS)a!Create a zookeeper client and hang on to the listener.

        :param listener:  The zookeeper listener action; is activated by the kazoo client when updates occur to watched
                          zookeeper parameters.
        :param name: The path base for this service.
        :param zkhosts: The zkhost or hosts (list) to use with the zk client; if none set, a local function is used
                        that uses hfs best practices to find the zk host setting for the machine this is running on.
        :param service_type: verticals for REST interfaces and registrations for Kafka listeners, pollers, etc.
        :param port: port the service is listening on if any
        :param ssl_port: ssl port if any
        :param locations: URLs that correspond to this server if any
        N�,)�hostsFT)r�
isinstance�list�tuple�join�default_zk_state_handlerr"r#r�zk�strr�zk_uuidr%r&r'r(�connection_was_lost�need_to_register)�selfr"r#r$r%r&r'r(rrr�__init__,s
zZkRegistrar.__init__)�stater)cCsJt�d||jj�|tjkr*d|_d|_n|tjkrF|jrFd|_d|_dS)z A default state handler functionzZookeeper state change: %s (%s)TFN)	�LOGGER�debugr1Zclient_staterZLOSTr4r5Z	CONNECTED)r6r8rrrr0Os
z$ZkRegistrar.default_zk_state_handler�r)cCs�|j�|j�|j��|jdur(g}n t|jttf�sB|jg}n|j}t�	�|j
|j|t�
��t�dd�}|jdur�|j|d<|jdur�|j|d<|j�d|j|jf�|jjd|j|j|j
ft�|��d�d	d
�d|_dS)zRegister the listenerNZDYNAMIC)�address�idr#r(ZregistrationTimeUTCZserviceTyper&ZsslPortz/service/%s/%s�/service/%s/%s/%szutf-8T)Z	ephemeralF)r1Zadd_listenerr"�startr(r,r-r.�platform�noder3r#r�utcnow�strftimerr&r'Zensure_pathr%�create�json�dumps�encoder5)r6r(Zzk_register_datarrr�register[s.


�



�zZkRegistrar.registercCs8|j�d|j|j|jf�|j�|j�|j��dS)z Unregister the listener and stopr>N)r1�deleter%r#r3Zremove_listenerr"�stop)r6rrr�
unregistervszZkRegistrar.unregister)NNNr!NNN)�__name__�
__module__�__qualname__�__doc__r(rrr2�int�
LocationsTyper7r0rHrKrrrrr (s*��#r )�commandr)cCst�dd|g�S)aA hyper simplistic shell command runner.

    Linux only.  Uses bash as /bin/bash.  NB: this does NOT change the current
    working directory of the shell or the parent process; any command you run
    will have to be on the PATH or part of the entered virtualenv, etc.

    :param command:  This is a command that will be run in a subshell, verbatim.
        The caller is fully responsible for all related security concerns.
        This function will not perform any processing on the string, e.g.no
        string substitutions, no interpolations, etc.  It will simply create a
        shell, execute the command on a single input to the shell, and then
        manage the process.  Shell metacharacters and expansions ** will ** be
        processed as per standard shell semantics.  E.g.

        .. code-block:: python

            uwsgi_exe = ("source %(virtualenv)s/bin/activate && "
                        "cd %(virtualenv)s/.. && "
                        "uwsgi --master --lazy-apps --processes 5 "
                        "--die-on-term --module wsgi:application --http :80"
                        % {'virtualenv': vpath})

    :returns:  The structure returned by subprocess.Popen:
               https://docs.python.org/3/library/subprocess.html#subprocess.Popen

    z	/bin/bashz-c)�
subprocess�Popen)rRrrr�	shell_run}srU)�virtualenv_pathrRr)cCsFtj�|�}tj�tj�|dd��s0td|��d||d�}t|�S)a�Enter a virtual environment and run a command.

    Simplifies a common python use case of entering a virtualenv and running a
    local shell command.  NB: this does NOT change the current working directory
    of the shell or the parent process; any command you run will have to
    be on the PATH or part of the entered virtualenv.

    :param virtualenv_path: A string representing the absolute path to a virtualenv.
    :param command:  As per shell_run.
    :returns:  The structure returned by subprocess.Popen:
               https://docs.python.org/3/library/subprocess.html#subprocess.Popen
    :raises ValueError: if the virtualenv path appears to be in error.
    �bin�activatez;Specified path (%s) does not look like a python virtualenv!z6source %(virtualenv_path)s/bin/activate && %(command)s)rVrR)�os�path�abspath�existsr/�
ValueErrorrU)rVrRZvpathZvenv_commandrrr�enter_virtualenv_and_run�s�r^)�signals�signal_handler_funcr)cCs|D]}t�||�qdS)z�Bind one or more signals to a signal handler.

    :param signals: A list of signals to be bound
    :param signal_handler_func: The handler to bind to the signals
    :raises ValueError: on invalid signal number
    N)�signal)r_r`Zsignal_rrr�bind_signal_handlers�srb)�signumr)cCst|d7<dS)zYBy default, count calls to a signal.

    :param signum: The signal which was called
    �N)�SIGNALS)rc�_rrr�default_signal_handler�srgr;cCsttjtjgt�dS)zBind default signal handlers.N)rbra�SIGINT�SIGTERMrgrrrr�set_default_signals�srj)�pid�
sleep_secs�max_attemptsr)cCsHdd�}ztt||�||d�\}}Wnty>d\}}Yn0||fS)z�Wait for a process to finish running.

    :param pid: The process ID on which to wait
    :param sleep_secs: The number of seconds to sleep between checks
    :param max_attempts: The number of times to attempt checking on the pid
    cSs*t�|tj�\}}|dkr"td��||fS)NrzNot done yet)rY�waitpid�WNOHANG�	Exception)ZmypidZw_process_idZ
w_exit_statusrrr�wait_for_pid�sz&wait_for_process.<locals>.wait_for_pid)rlrm)NN)rrr)rkrlrmrq�
process_id�exit_statusrrr�wait_for_process�srt)r&r)cCsvt�tjtjtj�}|�d�d}zDz"|�d|f�|�tj�d}WntjyZYn0W|�	�n
|�	�0|S)zpCheck to see if a local port is accepting TCP connections.

    :param port: The port number to be checked.
    rdF�T)
�socket�AF_INET�SOCK_STREAM�IPPROTO_TCP�
settimeout�connect�shutdown�	SHUT_RDWR�timeout�close)r&�s�retrrr�check_tcp_port�s
r�)�argsr)cCs
tdd�}|jddd�|jdddd	�|jd
ddd	�|jdd
dd�|jdddd	�|jdtdd�|jdtdd�|jdd
dd�|j|d�}|jp�tp�d}|jp�tp�d}|jp�t	p�d}|j
p�tp�tp�d}|j
p�tp�d}|jp�tp�d}|jp�tp�d}	|j�p
t�p
d}
|du�r4t�dt||�t||�j}nt�dt|�t|�j}t�d|�dt|<d&dd�}ttjtjgt||d ��t |||||	|
d!�}
|
�!�t|du�r�|
j"du�r�|
�!�t#|d"d#�\}}|du�r�t�d$||��q�q�t�d%|�|
�$�|
S)'aStart a process wrapped in a watcher and register with zookeeper.

    Steps:

        1.  Get the ephemeral registration data from command-line arguments or environment variables
        2.  Get the command to run from command-line arguments or environment variables
        3.  Run the command.
        4.  If successful, register with zookeeper.
        5.  Wait for the command to complete.
        6.  De-register with zookeeper.

    Command-line arguments:

        * --virtualenv-path
        * --command
        * --name
        * --zkhosts
        * --service_type (verticals, registrations)
        * --port
        * --ssl-port
        * --locations

    Environment variables (from settings.py):

        * GD_ZKRUN_VIRTUALENV_PATH
        * GD_ZKRUN_COMMAND
        * GD_ZKRUN_NAME
        * ZK_HOSTS
        * GD_ZKRUN_HOSTS
        * GD_ZKRUN_SERVICE_TYPE
        * GD_ZKRUN_PORT
        * GD_ZKRUN_SSL_PORT
        * GD_ZKRUN_LOCATIONS

    Methodology:

        * Command-line arguments override environment variables.

    z-Start a process with a zookeeper registration)�descriptionz--virtualenv-pathzPath to a python venv)�helpz	--commandTz&Shell command to run, properly escaped)�requiredr�z--namezService name (zk base node)z	--zkhosts�appendzZookeeper hosts)�actionr�z--service_typez(Service type, registrations or verticalsz--portzPort of service if any)�typer�z
--ssl-portzSsl port of service if anyz--locationszUrls for MCP use if any)r�Nz!Running venv run %s { %s } { %s }zRunning shell run %s { %s }zStarted process %scSst�d||�dt|<dS)Nz!Signal handler called with %s, %sF)r9�info�ZKSTART_STATUS)Z_signumZ_frame�runpidrrr�zkstart_signal_handler8sz(zk_start.<locals>.zkstart_signal_handler)r�)r#r$r%r&r'r(��z Process %s exited with status %szClean shutdown of %s)N)%r�add_argumentrP�
parse_argsrVrrRrr#rr$rrr%rr&rr'rr(rr9r�r^rkrUr�rbrarhrirr rHr5rtrK)r��parser�resrVrRr#r$r%r&r'r(rkr�Z	registrarrrrsrrr�zk_start�sR(




�
r�)N)A�argparserr�collectionsr�	functoolsrrE�loggingrY�os.pathr@rarvrS�typingrrrr	r
rrr
�uuidrZkazoo.clientrrZprimordial.constantsrZprimordial.flowrrZprimordial.settingsrrrrrrrrrrPrer��	getLoggerrLr9rr2rQr rTrUr^rbrgrj�floatrt�boolr�r�rrrr�<module>s>(,
U