Hacked By AnonymousFox
�
qu�f�5 � �� � d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl m
Z
ddlmZ ddl
mZ ddlmZmZ dd lmZ dd
lmZ ddlmZ ddlmZ dd
lmZ G d� d� � Zdd�ZdS )zA
This module contains classes implementing SSA Manager behaviour
� N)�contextmanager)�iglob)�
disable_quota)�Tuple� )�load_validated_parser�load_configuration)� flag_file)�SSAManagerError)�ssa_version)�
AutoTracer)�
DecisionMakerc �� � e Zd ZdZd� Zedefd�� � Zede fd�� � Z
edefd�� � Zedefd�� � Z
dedefd �Z d'dedej fd�Zd
edefd�Zdefd�Zdefd�Zdefd�Zdefd�Zdefd�Zdedefd�Zdeeeef ef fd�Zed� � � Zdedededdfd�Z d(d�Z!deeeef ef fd�Z"d(d�Z#d(d �Z$d(d!�Z%defd"�Z&d(d#�Z'd(d$�Z(defd%�Z)d(d&�Z*dS ))�Managerz
SSA Manager class.
c �� � t j d� � | _ d| _ d| _ d| _ t
dd� �� � f| _ t t t j f| _ d S )N�managerzclos_ssa.ini)�php44�php51�php52�php53zphp\d+-imunifyzphp-internal)z /opt/alt/php[0-9][0-9]/link/confz+/opt/cpanel/ea-php[0-9][0-9]/root/etc/php.dz$/opt/plesk/php/[0-9].[0-9]/etc/php.dz'/usr/local/php[0-9][0-9]/lib/php.conf.dzM/usr/share/cagefs/.cpanel.multiphp/opt/cpanel/ea-php[0-9][0-9]/root/etc/php.dzA/usr/share/cagefs-skeleton/usr/local/php[0-9][0-9]/lib/php.conf.dz./var/cagefs/*/*/etc/cl.php.d/alt-php[0-9][0-9]c �\ � t j | � d� � d � � S )N�/� )�pwd�getpwnam�split)�paths �B/opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/manager.py�<lambda>z"Manager.__init__.<locals>.<lambda>5 s � �3�<��
�
�3����0B�#C�#C� � )r �user)
�logging� getLogger�logger�
ini_file_name�substrings_to_exclude_dir_paths�wildcard_ini_locations�dict�wildcard_ini_user_locations�OSError�
ValueError�
subprocess�SubprocessError�subprocess_errors��selfs r �__init__zManager.__init__$ sv � ��'� �2�2���+���0
��,�'
��#�
�F�C�C�
E�
E�
E�,
��(�
�Z��!;�"
����r �returnc � � ddi}|� d� |� � � D � � � � t j |� � S )z@
Form a success json response with given kwargs
�result�successc � � i | ]\ }}||�� S � r7 )�.0�k�vs r �
<dictcomp>z$Manager.response.<locals>.<dictcomp>A s � �=�=�=�d�a��Q��=�=�=r )�update�items�json�dumps)�args�kwargs�raw_responses r �responsezManager.response; sI � �
!�)�,�����=�=�f�l�l�n�n�=�=�=�>�>�>��z�,�'�'�'r c �J � t j � t � � S )z
Is SSA enabled
)�osr �isfiler
r/ s r �_enabledzManager._enabledD s � �
�w�~�~�i�(�(�(r c �
� ddhS )zK
Configuration settings required Request Processor restart
�requests_duration�ignore_listr7 r/ s r �_restart_required_settingsz"Manager._restart_required_settingsK s � �
$�]�3�3r c �
� h d�S )N> �time�correlation�domains_number�request_number�correlation_coefficientr7 r/ s r �solo_filtered_settingszManager.solo_filtered_settingsR s � �*� *� *� *r �settingsc �6 � | j � |� � S )z�
SSA Agent requires restart in case of changing these configuration:
- requests_duration
- ignore_list
)rK �intersection)r0 rS s r �_restart_requiredzManager._restart_requiredW s � � �.�;�;�H�E�E�Er F�commandc � � t j dd|gdd|�� � }| j � d|� d�� � �n# t j $ r�}| j � dt
|j � � t
|j � � t
|j � � |j |j |j |j
d�� � � t d
|j � d|j � d|j p|j
� �� � �d
}~w| j $ rS}| j � dt
|� � dt
|� � i� � � t d|� d|� �� � �d
}~ww xY w|S )z�
Run /sbin/service utility to make given operation with SSA Agent service
:command: command to invoke
:check_retcode: whether to run with check or not
:return: subprocess info about completed process
z
/sbin/servicez ssa-agentT)�capture_output�text�checkz
ssa-agent z
succeededz$SSA Agent %s failed with code %s: %s)�cmd�retcode�stdout�stderr��extraz
SSA Agent z failed with code z: Nz&Failed to run %s command for SSA Agent�errzFailed to run z for SSA Agent: )
r, �runr$ �info�CalledProcessError�error�strr\ �
returncoder^ r_ r r. )r0 rW �
check_retcoder4 �es r �run_service_utilityzManager.run_service_utility_ s� � � ?��^�_�%0�%,�%.� 48�d�*7� 9� 9� 9�F�
�K���=�'�=�=�=�>�>�>�>���,� ^� ^� ^��K���6��A�E�
�
��A�L�!�!��A�H�
�
��e���!"��Q�X�?� ?�
�
@�
@�
@� "�\�Q�U�\�\�a�l�\�\�a�h�FZ�RS�RZ�\�\�^� ^�
^������%� ?� ?� ?��K���F��G���%*�C��F�F�O�
�
5�
5�
5�!�=��=�=�!�=�=�?� ?�
?����� ?����
�
s# �9= �E�BC(�(
E�5AE�Er@ c � � t � � }|� |� � |� � � nO# t $ rB}| j � ddt
|� � i�� � t d|� �� � �d}~ww xY w| � |� � r| � dd�� � | �
� � S ) z�
Change SSA config and restart it.
:args: dict to override current option values
:return: JSON encoded result of the action
z Failed to update SSA config filerb r` z"Failed to update SSA config file: N�restartT�ri )r �override�write_ssa_confr* r$ rf rg r rV rk rC )r0 r@ �configrj s r �
set_configzManager.set_config s� � � '�(�(��������� L��!�!�#�#�#�#��� L� L� L��K���@�%*�C��F�F�O�
�
5�
5�
5�!�"J�q�"J�"J�K�K�K����� L����
�!�!�$�'�'� D��$�$�Y�d�$�C�C�C��}�}���s �: �
B�=B�Bc �J � t � � }| � |�� � S )zV
Get current SSA config.
:return: JSON encoded current config
)rq )r rC )r0 �full_configs r �
get_configzManager.get_config� s# � �
)�*�*���}�}�K�}�0�0�0r c �D � | j rdnd}| � |�� � S )zY
Get current status of SSA.
:return: JSON encoded current status
�enabled�disabled)�
ssa_status)rG rC )r0 �statuss r �get_ssa_statuszManager.get_ssa_status� s( � �
#�m�;������}�}��}�/�/�/r c � � | j s<| � � � | � � � | � � � | � � � S )a�
Enable SSA:
- add clos_ssa extension for each PHP version on server
- add clos_ssa extension into cagefs for each user and each ver
- start SSA Agent (if it is not already started)
- restart Apache (etc.) and FPM, reset CRIU images
- create flag_file indicating that SSA is enabled successfully
:return: JSON encoded current status
)rG �
generate_inis�start_ssa_agent�create_flagr{ r/ s r �
enable_ssazManager.enable_ssa� sV � � �}� ���� � � �� � �"�"�"��������"�"�$�$�$r c � � | j r<| � � � | � � � | � � � | � � � S )a{
Disable SSA:
- remove clos_ssa extension for each PHP version on server
- remove clos_ssa extension from cagefs for each user and each ver
- stop SSA Agent
- restart Apache (etc.) and FPM, reset CRIU images
- remove flag_file indicating that SSA is enabled
:return: JSON encoded current status
)rG �remove_clos_inis�stop_ssa_agent�remove_flagr{ r/ s r �disable_ssazManager.disable_ssa� sV � � �=� ��!�!�#�#�#����!�!�!��������"�"�$�$�$r c � � d� t � � � � � D � � }| � |t � � | j rdnd| � � � t
� � � � � �� � S )z�
Get SSA statistics.
Includes:
- config values
- version
- SSA status (enabled|disabled)
- SSA Agent status (active|inactive)
:return: JSON encoded current statistics
c �X � i | ]'\ }}|t |� � � � � ��(S r7 )rg �lower)r8 �key�values r r; z%Manager.get_stats.<locals>.<dictcomp>� s<