Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/manager/__pycache__/base.cpython-311.pyc
�

��me;}���dZddlZddlZddlZddlZddlZddlZddlZddlmZm	Z	ddl
m
Z
ddlmZm
Z
mZddlZddlmZddlmZmZddlmZmZdd	lmZdd
lmZddlmZdd
lmZddl m!Z!ddl"m#Z#ddl$m%Z%m&Z&ddl'm(Z(ddl)m*Z*ddl+m,Z,ddl-m.Z.m/Z/m0Z0ddl1m2Z2ddl3m4Z4ddl5m6Z6m7Z7m8Z8ddl9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?ddl@mAZAmBZBmCZCmDZDmEZEGd�d��ZFdS)zX
This module contains classes implementing X-Ray Manager behaviour
and helper functions
�N)�datetime�	timedelta)�glob)�Iterator�Optional�Tuple)�Feature)�is_panel_feature_supported�get_installed_php_versions)�php_get_vhost_versions�is_wpos_supported)�is_litespeed_running)�drop_privileges)�
disable_quota�)�ClWposGetter)�
get_client)�ContinuousManager)�XRayManagerError�XRayAPIError)�FPMReloadController)�open_local_storage)�NginxUserCache)�
DomainInfo�Task�	url_split)�ManageUserAgent)�AdvancedMetrics)�get_xray_exec_user�user_mode_restricted�nginx_user_cache)�no_active_tasks�switch_schedstats�ssa_disabled�_cagefsctl_remount�set_privileges�is_file_recently_modified)�create_ini_files�remove_ini_files�is_global_ini_mode�remove_global_ini_mode_marker�create_global_ini_mode_markerc��eZdZdZdZddddddd	d
ddd
dd�Zdefd�Zdee	fd�Z
defd�Zdede
fd�Zdedefd�Zdedefd�Zdedefd�Zdede
fd�Zdeddfd�Zd�Zdedefd �Zed!edeejfd"���Zd#eddfd$�Zd%edefd&�Zd'edefd(�Zdede
fd)�Z d*edefd+�Z!d*eddfd,�Z"edede#ee	ffd-���Z$d.eddfd/�Z%edefd0���Z&d]d2�Z'e(d3d3d4�d%ed5ed6ed7e	d8e
d9e
dd1fd:���Z)d%edd1fd;�Z*d<d=�d%ed7e	dd1fd>�Z+d?edd1fd@�Z,dA�Z-e(d?edd1fdB���Z.d?edd1fdC�Z/edDede
fdE���Z0d]dF�Z1edGe2dHdIfdJ���Z3d?edd1fdK�Z4d%edLedd1fdM�Z5d%edd1fdN�Z6d%edd1fdO�Z7d%edd1fdP�Z8d]dQ�Z9d]dR�Z:dSedd1fdT�Z;dSedUe	dd1fdV�Z<d]dW�Z=d]dX�Z>d]dY�Z?d]dZ�Z@d]d[�ZAd]d\�ZBdS)^�BaseManagerz�
    Basic manager abstract class.
    Implements methods common for all managers.
    Requires managers to implement their custom methods.
    z/var/run/xray-agent.pidz/opt/alt/php54/link/confz/opt/alt/php55/link/confz/opt/alt/php56/link/confz/opt/alt/php70/link/confz/opt/alt/php71/link/confz/opt/alt/php72/link/confz/opt/alt/php73/link/confz/opt/alt/php74/link/confz/opt/alt/php80/link/confz/opt/alt/php81/link/confz/opt/alt/php82/link/confz/opt/alt/php83/link/conf)z	alt-php54z	alt-php55z	alt-php56z	alt-php70z	alt-php71z	alt-php72z	alt-php73z	alt-php74z	alt-php80z	alt-php81z	alt-php82z	alt-php83�	system_idc�:�t��|_||_tjd��|_t
��|_t��|_	tttj
f|_td��|j���|_dS)N�manager�lists�r/)r�api_client_object�sys_id�logging�	getLogger�loggerr�continuous_monitoringr�manage_user_agent�OSError�
ValueError�
subprocess�SubprocessError�subprocess_errors�
ui_api_client)�selfr/s  �H/opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/manager/base.py�__init__zBaseManager.__init__Xs~��!+���������'�	�2�2���%6�%8�%8��"�!0�!2�!2����Z��!;�"
���1�Z��0�0�4�;�G�G�G������returnc��	t|j��5}t|�����cddd��S#1swxYwYdS#tt
f$rt
jd��YdSwxYw)Nz'Unable to read daemon pid from pidfile.)�open�DAEMON_PIDFILE�int�readr;�IOErrorr6�warning)rA�fs  rB�_get_daemon_pidzBaseManager._get_daemon_pidcs���	G��d�)�*�*�
%�a��1�6�6�8�8�}�}�
%�
%�
%�
%�
%�
%�
%�
%�
%�
%�
%�
%����
%�
%�
%�
%�
%�
%����!�	G�	G�	G��O�E�F�F�F�F�F�F�	G���s3�A�!A�A�A�A�A�A�%A:�9A:c��|jS)z\
        Get supported PHP versions
        :return: a dict with supported versions
        )�VERSIONS�rAs rB�supported_versionszBaseManager.supported_versionsjs��
�}�rD�php_versionc�.�||���vS)z�
        Check if given PHP version is supported
        :param php_version: PHP version, e.g. 'alt-phpXY'
        :return: True if version is supported, False otherwise
        )rR)rArSs  rB�is_version_supportedz BaseManager.is_version_supportedqs���d�5�5�7�7�7�7rD�domain_infoc�Z�|����|j��S)zh
        Get a path to directory for additional ini file for based on
        panel-set version
        )rR�get�panel_php_version�rArVs  rB�	_ini_pathzBaseManager._ini_pathys'��
�&�&�(�(�,�,�[�-J�K�K�KrDc���|�|��}	|�|��r|j}n	|jp|}n#t$rYnwxYw|j�d|��|S)z�
        Resolve a path to directory for additional ini file
        It depends on version set for domain and on selector
        :param domain_info: a DomainInfo object
        :return: path to directory for ini files
        zIni path resolved as %s)r[�is_selector_enabled�selector_ini_path�phpd_location_ini_pathr<r8�info)rArV�ini_paths   rB�get_ini_pathzBaseManager.get_ini_path�s����>�>�+�.�.��	��'�'��4�4�
J�&�8���&�=�I������	�	�	��D�	����	
����2�H�=�=�=��s�&>�
A�
Ac���|�|��r(|j�d|j��|j}n|j}|j�d|��|S)a
        Get PHP version which serves given domain
        :param domain_info: a DomainInfo object, including user of domain
                            and PHP version set in control panel environment
        :return: real php version of domain (selector or panel one)
        zSelector is enabled for user %szPHP version detected as %s)r]r8r`�user�selector_php_versionrY)rArV�current_versions   rB�get_php_versionzBaseManager.get_php_version�sn���#�#�K�0�0�	<��K���>�(�-�
/�
/�
/�)�>�O�O�)�;�O�����5��G�G�G��rDc�t�ttj��sdS|j�dS|�|��S)a

        Is selector enabled for given domain
        :param domain_info: a DomainInfo object, including user of domain
                            and PHP version set in control panel
        :return: True if selector is enabled for domain, False otherwise
        F)r
r	�PHP_SELECTORre�panel_specific_selector_enabledrZs  rBr]zBaseManager.is_selector_enabled�s>��*�'�*>�?�?�	��5��+�3��5��3�3�K�@�@�@rDNc��|�|j��|jr|�|��dSt	��r|�|j��dS|�|j��dS)z�
        Reload FPM service or kill all *php* processes of user
        :param domain_info: a ready-to-use DomainInfo object
        N)�reset_criu_imgs�name�	panel_fpm�restart_fpm_servicer�gracefully_restart_litespeedrd�
kill_user_phprZs  rB�php_procs_reloadzBaseManager.php_procs_reload�s���
	
���[�-�.�.�.�� �	1��$�$�[�1�1�1�1�1�
!�
#�
#�	1��-�-�k�.>�?�?�?�?�?����{�/�0�0�0�0�0rDc��tj|��}t|��5t��5	t	t
j�|jd��d���	��n3#ttf$r|j�
dd���YnwxYwddd��n#1swxYwYddd��dS#1swxYwYdS)z�
        Litespeed's graceful restart.
        https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:php:detached_mode#for_a_user
        z.lsphp_restart.txt�wz$Unable to restart lsws after changesT)�exc_infoN)�pwd�getpwnamrrrG�os�path�join�pw_dir�close�FileNotFoundError�PermissionErrorr8rL)rA�username�pws   rBrpz(BaseManager.gracefully_restart_litespeed�s���
�\�(�
#�
#��
�X�
&�
&�	[�	[�
���	[�	[�
[��R�W�\�\�"�)�-A�B�B�C�H�H�N�N�P�P�P�P��%��7�
[�
[�
[���#�#�$J�UY�#�Z�Z�Z�Z�Z�
[����		[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[����	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[����	[�	[�	[�	[�	[�	[sX�C�B:�AA;�:B:�;-B+�(B:�*B+�+B:�.C�:B>	�>C�B>	�C�C�Crc���t��}|�|��D]P}d|jdvr?|�tj��|�|jd���Q|S)z�
        Kill all PHP processes, which belong to given username
        :param username: name of user
        :return: list of pids of successfully killed processes
        �phprm�pid)�list�
user_procsr`�send_signal�signal�SIGHUP�append)rAr�killed_procs�procs    rBrqzBaseManager.kill_user_php�sr���v�v���O�O�H�-�-�	6�	6�D���	�&�)�)�)�� � ���/�/�/��#�#�D�I�e�$4�5�5�5���rD�	user_namec#��K�tj|��j}gd�}tj|��D],}||jdj|jdjfvr|V��-dS)z�
        Generator yields processes, which belong to given user_name.
        Processes are checked using UID, not user_name
        :param user_name: user name
        :return: a generator object
        )rmr��uidsr�N)rvrw�pw_uid�psutil�process_iterr`�real�	effective)r��uid_by_name�attrs�ps    rBr�zBaseManager.user_procs�su�����l�9�-�-�4��'�'�'���$�U�+�+�	�	�A��q�v�f�~�2�A�F�6�N�4L�M�M�M������	�	rD�domainc��td|�d���}|r=tj|d��|j�d|d��dSdS)zt
        Reset criu images if any found for given domain in order to
        enable/disable X Ray correctly
        z/var/run/mod_lsapi/*�
_criu_imgsrzcriu images in %s droppedN)r�shutil�rmtreer8r`)rAr��
criu_imgs_dirs   rBrlzBaseManager.reset_criu_imgs�sk��
�F�F�F�F�F�G�G�
��	L��M�-��*�+�+�+��K���8�-��:J�K�K�K�K�K�	L�	LrD�urlc�P�t|��\}}|�|��S)z4
        DomainInfo retrieving based on url
        )r�get_domain_info�rAr��domain_name�_s    rB�_domain_info_by_urlzBaseManager._domain_info_by_url�s'��#�3�����Q��#�#�K�0�0�0rDr�c� �td���)z�
        Retrieve information about given domain from control panel environment
        Required to be implemented by child classes
        :param domain_name: name of domain
        :return: a DomainInfo object
        z0Manager should implement retrieving domain info!��NotImplementedError)rAr�s  rBr�zBaseManager.get_domain_info�s��"�>�@�@�	@rDc� �td���)z�
        Check if selector is enabled specifically for panel
        Required to be implemented by child classes
        :param domain_info: a DomainInfo object
        :return: True if yes, False otherwise
        zCManager should implement specific panel check for selector enabled!r�rZs  rBrjz+BaseManager.panel_specific_selector_enableds��"�Q�S�S�	SrD�dom_infoc� �td���)z�
        Get FPM service name for particular panel
        :param dom_info: DomainInfo object
        :return: name of FPM service
        z5Manager should implement FPM service name retrieving!r�)rAr�s  rB�fpm_service_namezBaseManager.fpm_service_name
s��"�C�E�E�	ErDc�f�|�|��}	tjd|dgdd���|j�d|��t|�����d	S#|j$r7}|j�dt|��|d����Yd	}~d	Sd	}~wwxYw)
zg
        Restart FPM service for particular version
        :param dom_info: DomainInfo object
        z
/sbin/service�reloadT)�capture_output�textzService %s reloadedzFailed to reload FPM service)�errr`��extraN)
r�r=�runr8r`r�save_latest_reloadr?�error�str)rAr��fpm_serv�es    rBrozBaseManager.restart_fpm_services��
�(�(��2�2��	?��N�O�$�$�&�+/�T�
;�
;�
;�
;�
�K���2�%�
'�
'�
'�
 ��)�)�<�<�>�>�>�>�>���%�	8�	8�	8��K���<�,/��F�F�-5�%7�%7�
�
8�
8�
8�
8�
8�
8�
8�
8�
8�����	8���s�5A/�/
B0�9,B+�+B0c�J�tj|��}d|j�d�}tj�tj�|����s@tjtj�|����t|��||j	fS)z5Resolve path for user and prepare directory if neededz/var/clwpos/uids/z
/info.json)
rvrwr�rxry�isdir�dirname�makedirsr%�pw_gid)r�_upwd�_paths   rB�prepare_wpos_info_pathz"BaseManager.prepare_wpos_info_path*s�����X�&�&��<�E�L�<�<�<���w�}�}�R�W�_�_�U�3�3�4�4�	)��K������.�.�/�/�/��x�(�(�(��e�l�"�"rDrdc���t��sdS|�|��\}}t|��r|j�d|��dS|dz}tt
|��t�����}|j�d||��td|d���5	t|d	��5}tj||��ddd��n#1swxYwYtj||��|j�d
��n?#t$r2}|j�dt#|����Yd}~nd}~wwxYwddd��dS#1swxYwYdS)z9Prepare and write info.json file for WPOS (AWP) utilitiesNz*File %s modified recently, skip generatingz.tmp)�vhost_versions�installed_versionszGoing to write file %s with %sr�_)�
target_uid�
target_gid�maskrtzWPOS data written successfullyz'Unable to write info for WPOS due to %s)r
r�r'r8r`�dictrr�debugr&rG�json�dumpr��mover;r�r�)rArd�dest�user_gid�working_dest�to_write�	info_jsonr�s        rB�write_wpos_infozBaseManager.write_wpos_info4s2�� �"�"�	��F��4�4�T�:�:���h�$�T�*�*�	��K���I�!�
#�
#�
#��F��f�}���1�$�7�7�9�;�;�
�
�
��	
����:���	*�	*�	*�
�q�X�E�
J�
J�
J�	*�	*�
*��,��,�,�3�	��I�h�	�2�2�2�3�3�3�3�3�3�3�3�3�3�3����3�3�3�3���L�$�/�/�/��� � �!A�B�B�B�B���
*�
*�
*���!�!�"K�"%�a�&�&�*�*�*�*�*�*�*�*�����
*����
	*�	*�	*�	*�	*�	*�	*�	*�	*�	*�	*�	*����	*�	*�	*�	*�	*�	*s`�2E-�4D!�C&�D!�&C*	�*D!�-C*	�.2D!� E-�!
E�+(E�E-�E�E-�-E1�4E1c��ddi}|r|�|��tjtt	|���������S)z�
        Create JSON response message with result field == success
        and given keyword arguments in other fields
        :return: json packed string
        �result�success)�updater��dumpsr��sorted�items)�kwargs�initials  rB�responsezBaseManager.responsePsP���Y�'���	#��N�N�6�"�"�"��z�$�v�g�m�m�o�o�6�6�7�7�8�8�8rD�json strc�b�|�d��r|d�d�|d<|jdi|��S)z�
        Create JSON response message for continuous actions.
        Extends action definition with 'continuous' addition
        :return: json packed string
        �actionz continuous�)rXr�)rAr�s  rB�response_continuouszBaseManager.response_continuous\sI���:�:�h���	@�"(��"2�?�?�?�F�8���t�}�&�&�v�&�&�&rDF)�	auto_task�autotracing�	client_ip�
tracing_by�
tracing_countr�r�c
�j�|�|��}|�|j��|�|��}|�|��r|�|��}	n5|j�dd|i���t|�d|�d�d����|�	|j
���}
|rd	n
t��}t|||||	|||j�
��}|
�
|��|_|�|���|�|j
���|
�|j��|�|��t)d�
��	t+���|j|j��n1#t0$r$}
t3jd|
j��Yd}
~
nd}
~
wwxYwt7|j�����t;��rt=��|�d|j���S)a

        Start monitoring of given URL.
        Arguments are only allowed by keyword
        :param url: URL to monitor
        :param client_ip: a client IP address
        :param tracing_by: time or qty
        :param tracing_count: a number of minutes|requests to monitor
        :param auto_task: if a task is an auto created one (x-ray 2.0)
        :param autotracing: task is an auto created with
                            user marked as *autotracing*
        :return: JSON encoded result of start action
        zPHP version is unsupported�versionr�z is served by z, which is unsupported by X-RayF��
needs_loggingr3z
*autotracing*)r�r�r�r��ini_locationr�rd�domain_owner�rST��enabledz0Failed to send metadata to Smart Advice with: %sN�start�r��tracing_task_id) r�r�rdrgrUrbr8rLrr4r5rr�create�task_id�add�set_cronjobr��	starttimerrr#r�
post_metadatarmrr6�reasonr�disabler*r(r�)rAr�r�r�r�r�r�rV�real_php_version�ini_files_location�clientrd�tracing_taskr�s              rBr�zBaseManager.startfs���$�.�.�s�3�3�����[�-�.�.�.��/�/��<�<���$�$�%5�6�6�	n�!%�!2�!2�;�!?�!?����K��� <�'0�2B�&C�
 �
E�
E�
E�"��W�W�&6�W�W�W�gl�n�n�n�
n��'�'�$�+�'�>�>��"-�G���3E�3G�3G����y�'1�*7�);�&/�d�)4�)9�;�;�;�� &�}�}�\�:�:������%5��6�6�6�� � �4�;� �7�7�7�	�
�
�l�,�-�-�-����k�*�*�*��$�'�'�'�'�	&��N�N�(�(��)9�;�;K�L�L�L�L���	&�	&�	&��O�N��H�
&�
&�
&�
&�
&�
&�
&�
&�����	&����	�{�'�(�(�0�0�2�2�2����	������}�}�G�-9�-A��C�C�	Cs�9,F&�&
G�0G�Gc�6�|�|dddd���S)z�
        Start continuous monitoring of given URL (auto task).
        Arguments are only allowed by keyword
        :param url: URL to monitor
        :return: JSON encoded result of start action
        �*�timei�T)r�r�r�r�r��r�)rAr�s  rB�
start_autozBaseManager.start_auto�s*���z�z�c�S�V�(,���>�>�	>rD�)r�c�8�|�|dd|dd���S)a6
        Start monitoring of given URL (autotracing task).
        Note, user is overridden as *autotracing*
        Arguments are only allowed by keyword
        :param url: URL to monitor
        :param tracing_count: count of requests to capture
        :return: JSON encoded result of start action
        r��request_qtyT)r�r�r�r�r�r�r)rAr�r�s   rB�start_autotracingzBaseManager.start_autotracing�s-���z�z�c�S�]�(5��&*��,�,�	,rDr�c��|�|j|���}|���}|���t	|j��5}|�|j���ddd��n#1swxYwYgd�}|j|vrV|j	�
d|j|j|���d����td|j�d	�d
����	|�|j��}|�|j��n@#t$r3}|jdkr|j	�d
��d}n�Yd}~nd}~wwxYw|���|���|���}|dkr|�|��n|�||��|�;t3|j�����|�|��t9��rt;��rt=d
���|�d|j���S)z�
        Stop monitoring of given task ID
        :param tracing_task_id: an ID of task to stop
        :return: JSON encoded result of stop action
        �r/r�)�next_request_idN)�running�stopped�	completed�holdzCannot stop task with status %s�r��taskr�zCannot stop task with status '�'Fr�iVz+Stopping task for an already deleted domainrr��stopr�) r4r5�get_task�_request_daemon_storage_flushr�fake_id�update_with_local_datar	�statusr8rLr��as_dictrr�r��set_domain_ownerrd�errnor`�remove�drop_cronjob�recalculate_countsr�	_completer�restorerrr"r$r#r�)	rAr�r�r��storage�try_to_complete_statusesrVr��remaining_counts	         rBrzBaseManager.stop�s����'�'�$�+�8G�(�I�I�����(�(���*�*�,�,�,�
�� 4�
5�
5�	Y���/�/��@W�/�X�X�X�	Y�	Y�	Y�	Y�	Y�	Y�	Y�	Y�	Y�	Y�	Y����	Y�	Y�	Y�	Y�$O�#N�#N� ���&>�>�>��K��� A� ,� 3�2>�2F�/;�/C�/C�/E�/E�'G�'G�
 �
H�
H�
H�#�G��1D�G�G�G�W\�^�^�^�
^�	��2�2�<�3C�D�D�K��)�)�+�*:�;�;�;�;���	�	�	��w�$����� � �!N�O�O�O�"�������������	����	�������!�!�#�#�#�'�9�9�;�;���Q����K�K��(�(�(�(��N�N�<��0�0�0��"��;�+�,�,�4�4�6�6�6��!�!�+�.�.�.����	-����	-��e�,�,�,�,��}�}�F�-9�-A��C�C�	Cs*�B�B�B�04D%�%
E"�/)E�E"c��tjtjtjh��|���}|rK	tj|tj��n*#t$r|j�	d��YnwxYw|j�
d��tjtjhd��}|�tj	d��tjtj
tjh��dS)z�
        Request daemon to flush it's in-memory storage on disk using SIGUSR2
        and wait for getting SIGUSR2 back that daemon sends when it successfully
        flushed data on disk and we are ready to proceed.
        zVUnable to send daemon signal. Some stats will not be flushed. Maybe daemon is stopped?z2Waiting for daemon to signal back about flush end.g.@NzUDaemon did not signal back in given timeout. Some stats may not be flushed. Continue.)r��pthread_sigmask�	SIG_BLOCK�SIGUSR2rNrx�killr;r8rLr`�sigtimedwaitr6�SIG_DFL)rA�
daemon_pid�siginfos   rBrz)BaseManager._request_daemon_storage_flush�s��	��v�/�&�.�1A�B�B�B��)�)�+�+�
��	@�
@���
�F�N�3�3�3�3���
@�
@�
@���#�#�%?�@�@�@�@�@�
@����	
����M�N�N�N��%�v�~�&6��=�=���?��O�G�
H�
H�
H�	��v�~���/?�@�@�@�@�@s�A"�"$B	�B	c�H�|�|j|���}|���}|jdvrV|j�d|j|j|���d����td|j�d�d�	���|�	|j
��}|�|j��d
|�
��vr|�|��}nd}|�|���|�|j�
��|�|j��t'|j�����|�|��t-d���|�d|j���S)z�
        Continue monitoring of given task ID
        :param tracing_task_id: an ID of task to continue
        :return: JSON encoded result of continue action
        r�r
rz#Cannot continue task with status %srr�z"Cannot continue task with status 'rFr��54Nr�r3Tr��continuer�)r4r5rrr8rLr�rrr�r�rrdrRrgr�r�r�r�rr�rrr#r�)rAr�r�r�rVr�s      rB�	continue_zBaseManager.continue_s����'�'�$�+�8G�(�I�I�����(�(����":�:�:��K��� E� ,� 3�2>�2F�/;�/C�/C�/E�/E�'G�'G�
 �
H�
H�
H�#�K�\�5H�K�K�K�[`�b�b�b�
b��.�.�|�/?�@�@���%�%�k�&6�7�7�7��4�*�*�,�,�,�,�#�3�3�K�@�@��� $�����%5��6�6�6�� � �4�;� �7�7�7�	�
�
�l�,�-�-�-��{�'�(�(�0�0�2�2�2����k�*�*�*��$�'�'�'�'��}�}�J�-9�-A��C�C�	CrDc��|�|j|���}|���}|jdvrV|j�d|j|j|���d����td|j�d�d�	���|�	||��|�
d
|j���S)z�
        Complete given tak ID
        :param tracing_task_id: an ID of task to complete
        :return: JSON encoded result of complete action
        rr,z#Cannot complete task with status %srr�z"Cannot complete task with status 'rFr��completer�)r4r5rrr8rLr�rrrr��rAr�r�r�s    rBr1zBaseManager.complete8s����'�'�$�+�8G�(�I�I�����(�(����":�:�:��K��� E� ,� 3�2>�2F�/;�/C�/C�/E�/E�'G�'G�
 �
H�
H�
H�#�K�\�5H�K�K�K�[`�b�b�b�
b����|�V�,�,�,��}�}�J�-9�-A��C�C�	CrDrc��tj��}|d}|dp|d}tj|��}|dkr|td���z|krdS|dkr|t|d	�
��z|krdSdS)zT
        Returns True if task should be completed.
        False otherwise.
        r�r��
createtimerr)�daysTrr�)�minutesF)r�now�
fromtimestampr)rr7r��	inception�task_start_times     rB�_is_to_completezBaseManager._is_to_completeLs����l�n�n���,�'�
���%�;��l�);�	�"�0��;�;����&�&�?�Y�A�=N�=N�=N�+N�QT�+T�+T��4�����O�i��_�H]�6^�6^�6^�$^�ad�$d�$d��4��urDc��|j����dg��}|j�dt|����|D][}|�|��s�tjdt|d����|�|d���\|�	d���S)z�
        Gets all server tasks and complete those which match following criteria:
         - traced_by requests_qty: if task started 2 days ago - complete
         - traced_by time: if task runs longer than start time + tracing time - complete
        r�z#Check tasks for autocompleting...%sz"Going to complete task with id: %sr�zautocomplete-tasks�r�)
r@�
get_task_listrXr8r`r�r;r6rr�)rA�all_server_tasks�	task_items   rB�autocomplete_taskszBaseManager.autocomplete_tasks\s��� �-�;�;�=�=�A�A�(�B�O�O������>��DT�@U�@U�V�V�V�)�	4�	4�I��'�'�	�2�2�
���L�=�s�9�M^�C_�?`�?`�a�a�a��I�I�i� 1�2�3�3�3�3��}�}�$8�}�9�9�9rD�t_taskr��	APIClientc��|���|���|���|jr|���dSdS)z�
        Common complete actions:
        - delete cron job,
        - erase request id file
        - send 'complete' status to mongo
        :param t_task: a tracing task object
        :param client: an APIClient object
        N)r�erase_request_id_storager1r��share)rBr�s  rBrzBaseManager._completeks^��	�������'�'�)�)�)���������	��L�L�N�N�N�N�N�	�	rDc��|�|j|���}|���}|jdkrV|j�d|j|j|���d����td|j�d�d�	���|�	��|�
d
|j���S)z�
        Delete given task ID
        :param tracing_task_id: an ID of task to delete
        :return: JSON encoded result of delete action
        rr
z!Cannot delete task with status %srr�z Cannot delete task with status 'rFr��deleter�)r4r5rrr8rLr�rrrHr�r2s    rBrHzBaseManager.delete{s����'�'�$�+�8G�(�I�I�����(�(����)�+�+��K��� C� ,� 3�2>�2F�/;�/C�/C�/E�/E�'G�'G�
 �
H�
H�
H�#�I�<�3F�I�I�I�Y^�`�`�`�
`��
�
�����}�}�H�-9�-A��C�C�	CrD�emailc��|�|��}|j�|j||��|�d|���S)z�
        Enable continuous monitoring for given URL
        :param url: URL to monitor
        :param email: email to send reports to
        :return: JSON encoded result of enable action
        �enable�r�r�)r�r9rKrmr�)rAr�rI�d_infos    rB�enable_continuouszBaseManager.enable_continuous�sM���)�)�#�.�.���"�)�)�&�+�s�E�B�B�B��'�'�x�S�'�A�A�ArDc��t|��\}}|j�|��|�d|���S)z�
        Disable continuous monitoring for given URL
        :param url: URL to monitor
        :return: JSON encoded result of disable action
        r�rL)rr9r�r�r�s    rB�disable_continuouszBaseManager.disable_continuous�sC��#�3�����Q��"�*�*�;�7�7�7��'�'�y�c�'�B�B�BrDc��t|��\}}|j�|��|�d|���S)z�
        Start continuous monitoring for given URL
        :param url: URL to monitor
        :return: JSON encoded result of start action
        r�rL)rr9r�r�r�s    rB�start_continuouszBaseManager.start_continuous�sC��#�3�����Q��"�(�(��5�5�5��'�'�w�C�'�@�@�@rDc��t|��\}}|j�|��|�d|���S)z�
        Stop continuous monitoring for given URL
        :param url: URL to monitor
        :return: JSON encoded result of stop action
        rrL)rr9rr�r�s    rB�stop_continuouszBaseManager.stop_continuous�sC��#�3�����Q��"�'�'��4�4�4��'�'�v�3�'�?�?�?rDc�b�|j���}|�d|���S)zq
        Get list of continuous monitoring tasks
        :return: JSON encoded result of get list action
        zget continuous list�r��data)r9�get_tracing_listr�)rA�tracing_lists  rB�continuous_tracing_listz#BaseManager.continuous_tracing_list�s9��
�1�B�B�D�D���}�}�$9�".��0�0�	0rDc�^�|�d|j������S)z#
        Get list of tasks
        z
tasks-listrV)r�r@r>rQs rB�
tasks_listzBaseManager.tasks_list�s6���}�}�L�"&�"4�"B�"B�"D�"D��F�F�	FrDr�c��|�|j|���}|���}|�d|j�|�����S)z@
        Get list of requests for given tracing task id
        rz
requests-listrV)r4r5rr�r@�get_request_list)rAr�r�r�s    rB�
requests_listzBaseManager.requests_list�sj��
�'�'�$�+�8?�(�A�A���O�O�����}�}�O�"&�"4�"E�"E�g�"N�"N��P�P�	PrD�
request_idc���|�|j|���}|���}|�d|j�||�����S)zO
        Get collected statistics for request ID of given tracing task
        rzrequest-datarV)r4r5rr�r@�get_request_data)rAr�r`r�r�s     rB�request_datazBaseManager.request_data�sn��
�'�'�$�+�8?�(�A�A���O�O�����}�}�N�"&�"4�"E�"E�!(�*�#6�#6��7�7�	7rDc�`�|j���|�d���S)a
        Enable X-Ray User Agent:
            start or restart service if it is accidentally already running
        For systemd systems -- start socket unit only
        For SysV -- start the entire service
        :return: JSON encoded result of enable action
        zenable-user-agentr=)r:rKr�rQs rB�enable_user_agentzBaseManager.enable_user_agent�s.��	
��%�%�'�'�'��}�}�$7�}�8�8�8rDc�`�|j���|�d���S)a7
        Disable X-Ray User Agent:
             stop the entire service
             or do nothing if it is accidentally not running
        For systemd systems -- also check if socket unit is running
                               and stop it too
        :return: JSON encoded result of disable action
        zdisable-user-agentr=)r:r�r�rQs rB�disable_user_agentzBaseManager.disable_user_agent�s.��	
��&�&�(�(�(��}�}�$8�}�9�9�9rDc�|�|j���}|�d|t�����S)zn
        Get status of X-Ray User Agent service
        :return: JSON encoded result of status action
        zuser-agent-status)r�r�user_nginx_cache)r:rr�r!)rA�agent_statuss  rB�user_agent_statuszBaseManager.user_agent_status�sD��
�-�4�4�6�6���}�}�$7��.>�.@�.@��B�B�	BrDc�*�t��}|ddur|���nS|ddur|���n4|ddur*|���}|�|���S|���S)�]
        Advanced metrics tool
        :return: JSON encoded result of status action
        rKTr�r)r)rrKr�rr�)rA�args�amrs    rB�advanced_metricszBaseManager.advanced_metricss���
�
�
����>�T�!�!��I�I�K�K�K�K�
�)�_��
$�
$��J�J�L�L�L�L�
�(�^�t�
#�
#��Y�Y�[�[�F��=�=��=�/�/�/��}�}���rDc�b�t��t��|���S�rm)r,r(r��rArns  rB�enable_serverwide_modez"BaseManager.enable_serverwide_modes*��
	&�'�'�'������}�}���rDc�b�t��t��|���Srr)r)r+r�rss  rB�disable_serverwide_modez#BaseManager.disable_serverwide_modes*��
	����%�'�'�'��}�}���rD)rEr�)C�__name__�
__module__�__qualname__�__doc__rHrPr�rCrrIrNr�rR�boolrUrr[rbrgr]rrrpr�rq�staticmethodrr��Processr�rlr�r�rjr�rorr�r�r�r�r r�rrrrr/r1r;rArrrHrNrPrRrTrZr\r_rcrergrkrprtrvr�rDrBr.r.As��������
/�N�0�/�/�/�/�/�/�/�/�/�/�/�
�
�H�	H�#�	H�	H�	H�	H�G��#��G�G�G�G��D�����8��8��8�8�8�8�L�Z�L�C�L�L�L�L��
��s�����&�:��#����� A�z�A�d�A�A�A�A�1�J�1�4�1�1�1�1�[�[�[��c��d�������c��h�v�~�&>�����\��L�c�L�d�L�L�L�L�1�s�1�z�1�1�1�1�@�3�@�:�@�@�@�@�S�:�S�$�S�S�S�S�E��E��E�E�E�E�?�J�?�4�?�?�?�?�(�#��#��s�C�x��#�#�#��\�#�*�C�*�D�*�*�*�*�8�	9�c�	9�	9�	9��\�	9�'�'�'�'��!&�"'�<C�<C�<C�C�<C�C�<C��<C�.1�<C��<C� �<C�-7�<C�<C�<C���<C�|>��>��>�>�>�>�02�,�,�,��,�),�,�6@�,�,�,�,�6C�C�6C�J�6C�6C�6C�6C�pA�A�A�2�'C��'C��'C�'C�'C���'C�RC��C�
�C�C�C�C�(�
�d�
�t�
�
�
��\�
�
:�
:�
:�
:��
�$�
��
�
�
��\�
�C�c�C�j�C�C�C�C�(B�S�B��B��B�B�B�B�C�c�C�j�C�C�C�C�A�C�A�J�A�A�A�A�@�3�@�:�@�@�@�@�0�0�0�0�F�F�F�F�	P�S�	P�Z�	P�	P�	P�	P�
7�C�
7�S�
7�Z�
7�
7�
7�
7�	9�	9�	9�	9�
:�
:�
:�
:�B�B�B�B����� ���������rDr.)Grzr�r6rxrvr�r�r=rrr�typingrrrr��clcommon.constr	�clcommon.cpapir
r�clwpos.papirr
�clcommon.utilsr�clcommon.clpwdr�secureior�adviser.clwpos_getr�	apiclientr�continuous.managerr�internal.exceptionsrr�internal.fpm_utilsr�internal.local_countersr�internal.nginx_utilsr�internal.typesrrr�internal.user_managerr�internal.advanced_metricsr�internal.user_plugin_utilsrr r!�internal.utilsr"r#r$r%r&r'�reconfiguration.global_inir(r)r*r+r,r.r�rDrB�<module>r�s�������������	�	�	�	�
�
�
�
�
�
�
�
�
�
�
�
�����(�(�(�(�(�(�(�(�������,�,�,�,�,�,�,�,�,�,�
�
�
�
�"�"�"�"�"�"���������B�A�A�A�A�A�A�A�/�/�/�/�/�/�*�*�*�*�*�*�"�"�"�"�"�"�-�-�-�-�-�-�"�"�"�"�"�"�2�2�2�2�2�2�@�@�@�@�@�@�@�@�4�4�4�4�4�4�8�8�8�8�8�8�1�1�1�1�1�1�8�8�8�8�8�8�8�8�8�8�3�3�3�3�3�3�7�7�7�7�7�7�����������
������������������������������b�b�b�b�b�b�b�b�b�brD