%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /opt/alt/python37/lib/python3.7/site-packages/clwpos/__pycache__/
Upload File :
Create Path :
Current File : //opt/alt/python37/lib/python3.7/site-packages/clwpos/__pycache__/utils.cpython-37.pyc

B

�4LbJ~�@s"ddlmZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlmZddlmZddlmZddlmZmZmZddlmZmZmZmZmZddlZddlmZdd	l m!Z!dd
l"m#Z#ddl$m%Z%m&Z&m'Z'ddl(m)Z)dd
l*m+Z+m,Z,m-Z-ddl.m/Z/m0Z0m1Z1ddl2m3Z3m4Z4ddl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;ddl<m=Z=ddl>m?Z?m@Z@ddlAmBZBddlCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMddlNmOZOmPZPddlQmRZRdaSdd�ZTd�eUeUd�dd�ZVd�eUeWeXd�dd �ZYd�eUeWd"�d#d$�ZZeUd%�d&d'�Z[eUd(�d)d*�Z\d�eUd%�d+d,�Z]d-d.�Z^d/d0�Z_d�e`e`d1�d2d3�Zae`d%�d4d5�Zbdd6�e`eXd1�d7d8�Zcdd6�e`d6�d9d:�ZdGd;d<�d<�ZeeWeeWd=�d>d?�Zfee`d%�d@dA�Zge`ddB�dCdD�Zhe`ddE�dFdG�Zie`ddE�dHdI�ZjdJdK�Zke`ee`e`fdL�dMdN�Zle`eeXdO�dPdQ�ZmGdRdS�dSen�Zoed�e`eeXdU�dVdW��ZpdXdY�ZqGdZd[�d[e`�ZrGd\d]�d]er�ZsGd^d_�d_er�Zterd`�dadb�Zue`dc�ddde�Zvee`d%�dfdg�ZweUeeXe`fdh�didj�Zxee`eUfd%�dkdl�Zydmdn�Zzdodp�Z{eerd%�dqdr�Z|Gdsdt�dt�Z}d�dudv�Z~Gdwdx�dx�Zee`dy�dzd{��Z�e`eUd|�d}d~�Z�eUd%�dd��Z�eUd%�d�d��Z�eUd%�d�d��Z�eeeedd��d�d��Z�e`eeeedd��d�d��Z�eXd%�d�d��Z�eUd%�d�d��Z�eeXe`e`fd%�d�d��Z�eUe`eUeeXe`e`fd��d�d��Z�eUeeXe`e`fd��d�d��Z�dS)��)�absolute_importN)�contextmanager)�wraps)�Path)�socket�AF_UNIX�SOCK_STREAM)�Dict�List�Tuple�Optional�Set)�write_file_via_tempfile)�NoDomain)�ClPwd)�is_cl_solo_edition�is_cl_shared_pro_edition�CLEditionDetectionError)�
check_license)�docroot�	getCPName�CPANEL_NAME)�exec_utility�run_command�demote)�gettext�	wp_config)�	WposError�WPOSLicenseMissing�WpCliUnsupportedException�WpNotExists�WpConfigWriteFailed�PhpBrokenException)�UIConfig)�	in_cagefs�_is_cagefs_enabled�)�
setup_logging)
�
USER_WPOS_DIR�WPOS_DAEMON_SOCKET_FILE�CLCONFIG_UTILITY�RedisRequiredConstants�CAGEFS_ENTER_USER_BIN�CAGEFS_ENTER_UTIL�CLWPOS_OPT_DIR�ALT_PHP_PREFIX�
EA_PHP_PREFIX�USER_CLWPOS_CONFIG)�pack_data_for_socket�'read_unpack_response_from_socket_client)�RollbackExceptioncs�fdd�}|S)z'
    Decorator for catching errors
    c
stdkrtt�ay�|f|�|�Stk
r\}zt|j|j|j|jd��Wdd}~XYn�t	k
r�}zRt
|t�r�t�|�n
t�
|�|j|j|jd�}|jr�|j|d<t|j|�Wdd}~XYnDtk
�r}z$t�
|�t|jit|�d��Wdd}~XYnXdS)N)�context�resultZissues)r5r6�warning�details)r5r6)�loggerr'�__name__r4�error_and_exitZ_is_jsonr5�message�errorsr�
isinstancerr7Z	exceptionr8�	Exception�str)�self�args�kwargs�eZresponse)�func��=/opt/alt/python37/lib/python3.7/site-packages/clwpos/utils.py�func_wrapperQs(



z!catch_error.<locals>.func_wrapperrF)rErHrF)rErG�catch_errorLsrIF)�is_json�	is_prettycCs>|r2|rttj|ddd��q:ttj|dd��nt|�dS)z�
    Print specified dictionary
    :param data_dict: data dictionary to print
    :param is_json: True - print in JSON, False - in text
    :param is_pretty: True - pretty json print, False - none (default)
    :return: None
    �T)�indent�	sort_keys)rNN)�print�json�dumps)Z	data_dictrJrKrFrFrG�_print_dictionaryms
rR)rJr<�
error_codec
Cs�d|��kr |�d�s |�d�|rF|�dt��i�t||dd�nXy tt|d�|�di��Wn6tk
r�}ztdt|�|f�Wdd}~XYnXt	�
|�dS)	z�
    Print error and exit
    :param is_json:
    :param message: Dictionary with keys "result" as string and optional "context" as dict
    :param error_code: Utility return code on error
    r7�	timestampT)rKr6r5zError: %s [%s]N)�keys�get�pop�update�timerRrOr@�KeyError�sys�exit)rJr<rSrDrFrFrGr;�s
 &r;�success)rJ�datacCs0t|t�r|�|t��d��t||dd�dS)zo
    Output data wrapper
    :param is_json:
    :param data: data for output to stdout
    :param result:
    )r6rTT)rKN)r>�dictrXrYrR)rJr^r6rFrFrG�
print_data�s
r`)�returncCst��dkS)zQ
    Detects is we running under root
    :return: True - user, False - root
    r)�os�geteuidrFrFrFrG�is_run_under_user�srd)�safelycCs*yt�Stk
r$|rdS�YnXdS)z�
    Detecting of shared_pro edition depends on jwt token
    There are some cases when we do not fail if there are
    cases with decoding (e.g summary collection)
    FN)rr)rerFrFrG�is_shared_pro_safely�srfcCs t�tk}tdd�st|�o|S)u�
    Сheck if system environment is supported by WPOS
    :return:
        True - CPanel on Solo/ CL Shared Pro
        False - else
    T)�skip_jwt_check)rrrrf)reZ	is_cpanelrFrFrG�is_wpos_supported�s
rhcCs0tj�t|�t�}tj�|�s,tj|dd�dS)z@
    Creates {homedir}/.clwpos directory if it's not exists
    i�)�modeN)rb�path�join�home_dirr(�isdir�mkdir)�usernameZ
clwpos_dirrFrFrG�create_clwpos_dir_if_not_exists�srpcCsFt|�d}|�|�s0td|�d|�d|����|t|�d��d�S)Nrzdocroot z for domain z should start with �/)r�
startswithr�len�lstrip)�domain�homedirZdrrFrFrG�get_relative_docroot�s
rw)roracCst|d�}|jS)N)ro)�get_pw�pw_dir)roZpwrFrFrGrl�s
rlcCst�jS)N)rxZpw_namerFrFrFrG�	user_name�srz)rocCst|d�jS)N)ro)rxZpw_uid)rorFrFrG�user_uid�sr{cCs |rt�|�St�t���SdS)N)�pwd�getpwnam�getpwuidrbrc)rorFrFrGrx�s
rxc@s4eZdZdZd
eedd�dd�Zdd�Zdd	�ZdS)�WposUserzQ
    Helper class to construct paths to user's WPOS dir and files inside it.
    N)rorvracCsz||_|dkrt|�n||_tj�|jt�|_tj�|jt�|_tj�|jd�|_	tj�|jd�|_
tj�|jd�|_dS)Nz
redis.confz
redis.sockz.php_info-{file_id})�namerlrbrjrkr(�wpos_dirr1Zwpos_config�
redis_conf�redis_socketZphp_info)rArorvrFrFrG�__init__�szWposUser.__init__cCs|j|jkS)N)r�)rA�otherrFrFrG�__eq__�szWposUser.__eq__cCs
t|j�S)N)�hashr�)rArFrFrG�__hash__�szWposUser.__hash__)N)r:�
__module__�__qualname__�__doc__r@r�r�r�rFrFrFrGr�s	r)�cmd_dictracCs�t|�}ttt���}yp|�t�|�|�t|�}|dksDt|t	�sZt
td�t|�id��|ddkr�t
td�|d|�
d�d��|Stk
r�t
td�d	ti��YnDtttttjtfk
r�}zt
td
�t|�d��Wdd}~XYnXWdQRXdS)z�
    Send command to CLWPOS daemon via socket
    :param cmd_dict: Command dictionary
    :return: Daemon response as dictionary, None - daemon data/socket error
    NzPUnexpected response from daemon. Report this issue to your system administrator.)r<r8r5r6r]z3Daemon was unable to execute the requested command.r5zZCloudLinux WPOS daemon socket (%(filename)s) not found. Contact your system administrator.�filenamez&Unexpected daemon communication error.)r8)r2rrrZconnectr)Zsendallr3r>r_rrr@rV�FileNotFoundError�ConnectionError�OSError�IOError�AttributeError�struct�errorrZ)r�Z
bytes_to_send�sZ
response_dictrDrFrFrG�daemon_communicate�s*

r�c
Cs�tj�t�td�}t��}tj}tj	}tj
}tj}dd|j�d|j
�d�dtjj�d|�d�d|j�d|j
�d�d|j�d|j
�|�d�d|j�d|j
�d�dgS)	ak
    Construct list of lines (configuration settings)
    that should be in Wordpress config file to enable redis.
    Please note that deleting of the plugin would flush all keys related to the plugin (site) from redis.
    REDIS_PREFIX and SELECTIVE_FLUSH in wp-config.php would guarantee that plugin will not flush keys unrelated
    to this plugin (site)
    z
redis.sockz)// Start of CloudLinux generated section
zdefine('z', 'z');
z', z);
z'// End of CloudLinux generated section
)rbrjrkrlr(�uuidZuuid4r+�WP_REDIS_PREFIXZWP_REDIS_SCHEMEZWP_REDIS_CLIENTZWP_REDIS_SELECTIVE_FLUSHr��valueZ
WP_REDIS_PATH)Zsocket_pathZprefix_uuidZredis_prefixZredis_schemaZredis_clientZredis_flushrFrFrG�redis_cache_config_sectionsr�)�wp_config_pathracCsDtj�|�}tj�|�s t|��tj�|�s@ttd�d|id��dS)z�
    Check that wp-config.php exists inside Wordpress directory.
    :param wp_config_path: absolute path to Wordpress config file
    :raises: WposError
    z)Wordpress config file %(file)s is missing�file)r<r5N)rbrj�dirname�existsr �isfilerr)r�Zwp_pathrFrFrG�check_wp_config_existance2sr�)�abs_wp_pathrac
s�tt�|��}t|�t��ttd��fdd�}y.t�|�}tt||��}t	d�
|�|d�Wn0ttfk
r�}zt
||��Wdd}~XYnXdS)z�
    Clear cloudlinux section with redis object cache config from docroot's wp-config.php
    :param abs_wp_path: Absolute path to WordPress
    :raises: WposError
    )�lineracs|�kod|kS)zn
        Filter function that should delete CL config options from the `redis_cache_config_section()`
        r�rF)r�)�lines_to_filterrFrG�__config_filterKsz1clear_redis_cache_config.<locals>.__config_filter�i�N)r@rrjr�r��bool�read�list�filterrrkr�r�r!)r�r�r��wp_config_linesZcleared_wp_configrDrF)r�rG�clear_redis_cache_configAs
r�c
stt�|��}t|�y�|�d�}tj�|�s:t�||�dd�tD�}t�	|�}g}xJ|D]B��fdd�|�
�D�}|s~dSt�fdd�tD��s\|���q\W|dft
�|d	d��}td
�|�|d�Wn2ttfk
�r}zt||��Wdd}~XYnXdS)a
    Create config for redis-cache.
    We use manual copy cause we want to preserve file metadata
    and permissions and also we could add some custom config editing in the future.
    :param abs_wp_path: absolute path to WordPress
    :raises: WposError
    z.backupcSsi|]}|j|j�qSrF)r�r�)�.0ZconstantrFrFrG�
<dictcomp>isz-create_redis_cache_config.<locals>.<dictcomp>cs&i|]\}}d|�d��kr||�qS)zdefine('�'rF)r��k�v)r�rFrGr�nsNc3s |]}d|j�d��kVqdS)zdefine('r�N)r�)r�Zredis_constant)r�rFrG�	<genexpr>tsz,create_redis_cache_config.<locals>.<genexpr>rr&r�i�)r@rrjr�rbr��shutil�copyr+r��items�any�appendr�rrkr�r�r!)r�r�Zbackup_wp_configZabsent_constantsr�Z
cleaned_linesZupdated_configrDrF)r�rG�create_redis_cache_configYs*


r�cst���fdd��}|S)z,Decorator to check for license validity
    cst�st���||�S)zLicense check wrapper)rr)rBrC)rErFrG�wrapper�sz(check_license_decorator.<locals>.wrapper)r)rEr�rF)rErG�check_license_decorator�sr�)ruracCsByt|�\}}||fStk
r<ttd�d|id��YnXdS)z�
    Validates domain, determines it's owner and docroot or exit with error
    :param domain: Domain name to check
    :return: Tuple (username, docroot)
    zNo such domain: %(domain)s.ru)r<r5N)rrrr)ruZ
document_root�ownerrFrFrG�check_domain�s
r�)rj�attemptsc	Cs�tj}|dk	r|tjO}yft|d�}xVt|p.d�D]:}yt�|��|�PWq2tk
rjt�	d�Yq2Xq2Wt
td���Wn tk
r�t
td���YnX|S)zE
    Try to take lock on file with specified number of attempts.
    Nza+r&g333333�?zwAnother utility instance is already running. Try again later or contact system administrator in case if issue persists.z%IO error happened while getting lock.)
�fcntlZLOCK_EXZLOCK_NB�open�range�flock�filenor�rYZsleep�LockFailedExceptionrr�)rjr�Z	lock_type�lock_fd�_rFrFrG�	lock_file�s

r�c@seZdZdZdS)r�z,
    Exception when failed to take lock
    N)r:r�r�r�rFrFrFrGr��sr��
)�
resource_pathr�ccs t|d|�}dVt|�dS)z�
    Lock a file, than do something.
    Make specified number of attempts to acquire the lock,
    if attempts is None, wait until the lock is released.
    Usage:
    with acquire_lock(path, attempts=1):
       ... do something with files ...
    z.lockN)r��release_lock)r�r�r�rFrFrG�acquire_lock�s
r�cCs8yt�|��tj�Wntk
r*YnX|��dS)z
    Releases lock file
    N)r�r�r�ZLOCK_UNr��close)Z
descriptorrFrFrGr��s
r�c@s�eZdZdZdd�Zeed�dd��Zed�dd�Ze	d�d	d
�Z
ed�dd�Ze	d�d
d�Zed�dd�Z
e	d�dd�Zedd��ZdS)�PHPzDClass helper which hides differences of PHP behind abstract methods.cGsZ|tkrtj|f|��Sx*ttfD]}|d�|���r"||�Sq"Wtd|d����dS)Nrz
Unknown PHP: )r�r@�__new__�_AltPHP�_EaPHPrr�prefixr?)�clsrB�tprFrFrGr��szPHP.__new__)racCst�dS)zReturn prefix of PHP.N)�NotImplementedErrorrFrFrFrGr��sz
PHP.prefixcCst�dS)z#Return relative path to dir of PHP.N)r�)rArFrFrG�_dir_relative_path�szPHP._dir_relative_pathcCstd|�����S)zReturn path to dir of PHP.z/opt/)rr�)rArFrFrG�dir�szPHP.dircCst�dS)z#Return relative path to bin of PHP.N)r�)rArFrFrG�_bin_relative_path�szPHP._bin_relative_pathcCs|���|���S)zReturn path to bin of PHP.)r��joinpathr�)rArFrFrG�bin�szPHP.bincCst�dS)z#Return relative path to ini of PHP.N)r�)rArFrFrG�_ini_relative_path�szPHP._ini_relative_pathcCs|���|���S)zReturn path to ini of PHP.)r�r�r�)rArFrFrG�iniszPHP.inicCs |�|��d��dd�}t|�S)Nr�Zphp)�replacer��int)rA�versionrFrFrG�digitssz
PHP.digitsN)r:r�r�r�r��staticmethodr@r�r�rr�r�r�r�r��propertyr�rFrFrFrGr��s
r�c@s4eZdZdZedd��Zdd�Zdd�Zdd	�Zd
S)r�zImplementation for alt-phpcCsdS)Nzalt-rFrFrFrFrGr�
sz_AltPHP.prefixcCsd|t|���d���S)Nzalt/)rsr�)rArFrFrGr�sz_AltPHP._dir_relative_pathcCsdS)Nzusr/bin/phprF)rArFrFrGr�sz_AltPHP._bin_relative_pathcCsdS)Nzlink/conf/default.inirF)rArFrFrGr�sz_AltPHP._ini_relative_pathN)	r:r�r�r�r�r�r�r�r�rFrFrFrGr�
s
r�c@s4eZdZdZedd��Zdd�Zdd�Zdd	�Zd
S)r�zImplementation for ea-phpcCsdS)Nzea-rFrFrFrFrGr�sz
_EaPHP.prefixcCs
d|��S)Nzcpanel/rF)rArFrFrGr�"sz_EaPHP._dir_relative_pathcCsdS)Nzroot/usr/bin/phprF)rArFrFrGr�%sz_EaPHP._bin_relative_pathcCsdS)Nzroot/etc/php.inirF)rArFrFrGr�(sz_EaPHP._ini_relative_pathN)	r:r�r�r�r�r�r�r�r�rFrFrFrGr�s
r�)�php_versioncCsPt|���}t|dgid�}|jr4|js4t||j��|j}||�d�krLdSdS)z!
    Checks <module> enabled
    z-m)�env�
TF)r@r��run_in_cagefs_if_needed�stderr�stdoutr"�split)r��modulerjr6�outrFrFrG�is_conflict_modules_installed,sr�)�php_pathcCs2d}d|kr.ttd�|�r.ttd�d|id��dS)zX
    Ensures wp-cli is compatible, e.g some
    php modules may prevent stable work
    Z
snuffleupaguszea-php74z�Seems like ea-php74 %(module)s module is enabled. It may cause instabilities while managing object_cache. Disable it and try againr�)r<r5N)r�r�rr)r�Zdangerous_modulerFrFrG�wp_cli_compatibility_check<sr�cCsdg}tdd�s|�d�|S)z@
    Return list of supported handlers according to edition
    zphp-fpmT)rgZlsapi)rr�)Z	supportedrFrFrG�supported_php_handlersIs

r�)�hideracCs4ddt�ddd|iii�dg}tt|�\}}||fS)zf
    Call cloudlinux-config utility
    to hide/show WPOS icon in user's control panel interface.
    �setz--dataZoptions�
uiSettings�hideWPOSAppz--json)rPrQrr*)r�Zparams�
returncoder�rFrFrG�set_wpos_icon_visibilitySsr�cCs i}t��dd�}||d<|S)zC
    Return default content of /opt/clwpos/public_config.json.
    r�r�Z	show_icon)r#Z	get_param)Zconfig_dictZis_icon_hiddenrFrFrG�get_default_public_optionsbs
r�cKs�t�st�r4tj|fddtt��t���d�|��St��dkrVtt	d|�d��d��t
|t�rntd|}n
tg|}tj|ftt��t���ddd�|��Sd	S)
z�
    Wrapper for subprocess to enter cagefs
    do not enter cagefs if:
     - CloudLinux Solo
     - if process already started as user in cagefs
    T)�text�capture_output�
preexec_fnrzInternal error: command zf must not be run as root. Please contact support if you have questions: https://cloudlinux.zendesk.com)r<� )r�r�r�N)
rr$�
subprocess�runrrbrc�getegidrrr>r@r-)ZcommandrCZwith_cagefs_enterrFrFrGr�ls 

r�cCs(yt��|�Stjk
r"dSXdS)z
    Returns uid for user
    N)rZget_uidZNoSuchUserException)r�rFrFrG�uid_by_name�sr�cs*d}t�d���fdd�t�|�D�}|S)z1
    Get list of installed alt-php versions.
    z/opt/altz
^php\d{2}$cs$g|]}��|�rtd|����qS)zalt-)�matchr�)r�r�)�patternrFrG�
<listcomp>�sz(get_alt_php_versions.<locals>.<listcomp>)�re�compilerb�listdir)Zalt_dirZalt_php_versionsrF)r�rG�get_alt_php_versions�s
rc@s�eZdZdZed�dd�Zeeed�dd�Zee	ee
d�d	d
�Zee	ee
d�dd�Zeeeed
�dd�Z
eeeed�dd�ZdS)�PhpIniConfigz>
    Helper class to update extensions in php .ini files.
    )r�cCs"||_t�d�|_t�d�|_dS)Nz-^;\s*extension\s*=\s*(?P<module_name>\w+)\.soz,^\s*extension\s*=\s*(?P<module_name>\w+)\.so)r�r�r�disabled_pattern�enabled_pattern)rAr�rFrFrGr��szPhpIniConfig.__init__)rjrac	s(t|d��}�fdd�|D�SQRXdS)zU
        Return enabled modules.
        :param path: full path to .ini file
        �rcs.h|]&}�j�|�dk	r�j�|��d��qS)N�module_name)rr��group)r�r�)rArFrG�	<setcomp>�sz0PhpIniConfig._enabled_modules.<locals>.<setcomp>N)r�)rArj�frF)rArG�_enabled_modules�szPhpIniConfig._enabled_modules)rj�modulesrac	s�tj��j��|�}tj�|�s$dSt|���|���r�t|��@}��fdd�|�	�D�}x t
��D]}|�d�|��qfWWdQRXt
d�|�|d�dS)	z�
        Enable specified modules in .ini php file.
        :param path: path to .ini file related to php directory
        :param modules: list of modules that should be enabled
        Fcsg|]}��|���qSrF)�_enable_module)r�r�)�modules_to_enablerArFrGr��sz/PhpIniConfig.enable_modules.<locals>.<listcomp>zextension={}.so
Nr�i�T)rbrjrkr�r�r�r�rr��	readlines�sortedr��formatr)rArjr�	full_pathr
�
new_ini_linesr�rF)rrArG�enable_modules�s
zPhpIniConfig.enable_modulesc	s|tj��j��|�}tj�|�s$dSt|���|�@��rxt|��}��fdd�|�	�D�}WdQRXt
d�|�|d�dS)z�
        Disable specified modules in .ini php file.
        :param path: path to .ini file related to php directory
        :param modules: list of modules that should be disabled
        Fcsg|]}��|���qSrF)�_disable_module)r�r�)�modules_to_disablerArFrGr��sz0PhpIniConfig.disable_modules.<locals>.<listcomp>Nr�i�T)rbrjrkr�r�r�r�rr�rr)rArjrrr
rrF)rrArG�disable_modules�s
zPhpIniConfig.disable_modules)r�rracCsB|j�|�}|dk	r>|�d�}||kr>|�|�|�d���S|S)zV
        Search for disabled module in line, uncomment line to enable module.
        Nr�;)rr�r�removert)rAr�rr�rrFrFrGr
�s

zPhpIniConfig._enable_module)r�rracCs4|j�|�}|dk	r0|�d�}||kr0d|��S|S)zT
        Search for enabled module in line, comment line to disable module.
        Nrr)rr�r)rAr�rr�rrFrFrGr�s

zPhpIniConfig._disable_moduleN)r:r�r�r�r�r�r@r
rr
r�rrr
rrFrFrFrGr�s	rcCs�t�r
dSt�sL|dkr&ttd�d��t|gtjdd�tjdd�}n$t�sltt	�d�rlt
gtj}ndStj|tj
tjid�}|��t�|j�dS)a%
    All user-related actions must run inside of cagefs for security reasons.
    If solo just return because cagefs is only for shared and shared pro
    If root executed, we enter into user cagefs if user is pointed
    If not in cagefs and cagefs is enabeled for user enter into cagefs
    Nz�Internal Error: root enters into CageFS without specifying usernamePlease contact support if you have questions: https://cloudlinux.zendesk.com)r<r&�)�user)r��stdinr�)rrdrrr,r[�argvr$r%rzr-r��Popenr�rZcommunicater\r�)r�cmd�prFrFrG�_run_clwpos_as_user_in_cagefs�s
&r!c@s\eZdZdZedd�dd�Zdd�dd�Zdd�d	d
�Zed�dd�Z	e
ed�d
d��ZdS)�RedisConfigurePidFilezm
    Helper class that provides methods to work with
    pid files of php redis configuration processes.
    N)�
php_prefixracCs|�d�|_tt|j�|_dS)Nz-cloudlinux.pid)Z_pid_file_namerr.rj)rAr#rFrFrGr�szRedisConfigurePidFile.__init__)rac	Cs.|j�d��}|�tt����WdQRXdS)N�w)rjr��writer@rb�getpid)rAr
rFrFrG�createszRedisConfigurePidFile.createcCs|j��r|j��dS)N)rj�is_file�unlink)rArFrFrGrs
zRedisConfigurePidFile.removecCs
|j��S)N)rjr()rArFrFrGr�!szRedisConfigurePidFile.existsc	CsL|��sdS|j���,}yt|�����Stk
r<YnXWdQRXdS)N���)r�rjr�r�r��strip�
ValueError)rAr
rFrFrG�pid$szRedisConfigurePidFile.pid)
r:r�r�r�r@r�r'rr�r�r�r�r-rFrFrFrGr"sr")r#ccs*t|�}z|��dVWd|��XdS)za
    Context manager for creating pid file of current process.
    Removes pid file on exit.
    N)r"r'r)r#�pid_filerFrFrG�create_pid_file0s

r/)r#rac	CsTt|�}t��dkr|��Syt�|j�}d|��kSttj	fk
rNdSXdS)z�
    Find out if PHP redis configuration process is running.
    Based on looking for presence of pid files.
    For root also checks process existence.
    rZenable_redisFN)
r"rbrcr��psutil�Processr-r�r,Z
NoSuchProcess)r#r.ZprocessrFrFrG�"is_php_redis_configuration_running>sr2cCstt�S)zE
    Find out if alt-PHP redis configuration process is running.
    )r2r/rFrFrFrG�&is_alt_php_redis_configuration_runningNsr3cCstt�S)zD
    Find out if ea-PHP redis configuration process is running.
    )r2r0rFrFrFrG�%is_ea_php_redis_configuration_runningUsr4cCst�p
t�S)za
    Find out if redis configuration process
    is running for any PHP (ea-php or alt-php).
    )r3r4rFrFrFrG�is_redis_configuration_running\sr5)�new_user�old_userrac	sJt�j��}|��}WdQRX��fdd�|D�}td�|��jd�dS)z;
    Replace user's wpos directory path in redis.conf.
    Ncsg|]}|��j�j��qSrF)r�r�)r�r�)r6r7rFrGr�msz%update_redis_conf.<locals>.<listcomp>r�i�)r�r�rrrk)r6r7r
Zredis_conf_lines�
updated_linesrF)r6r7rG�update_redis_confesr9)r�r6r7rac
s|yt�|�}Wn<tk
rJ}ztd|�d|��tjd�dSd}~XYnX��fdd�|D�}td�|�t�|�d�dS)	z<
    Replace user's redis socket path in wp-config.php.
    z=Error occurred during opening wp-config.php located in path "z": )r�Ncs*g|]"}�j|kr"|��j�j�n|�qSrF)r�r�)r�r�)r6r7rFrGr�~sz$update_wp_config.<locals>.<listcomp>r�i�)	rr�r�rOr[r�rrkrj)r�r6r7r�rDr8rF)r6r7rG�update_wp_configrs
r:cCst�t���}|��S)z!
    Get parent process PID.
    )r0r1rbr&Zppid)�procrFrFrG�get_parent_pid�sr<cCs(tdddgdd�\}}}|dkr$dSdS)z`
    Detect CL WPOS daemon presence in system
    :return: True - daemon works / False - No
    z
/sbin/service�clwpos_monitoringZstatusT)�return_full_outputrF)r)r�r�rFrFrG�_is_monitoring_daemon_exists�sr?cCsHt�d|�tdddgdd�\}}}|s>tddgdd�\}}}|||fS)z5
    Update systemd unit file and reload systemd
    z//usr/share/cloudlinux/clwpos_monitoring.servicez/usr/bin/systemctlZenablezclwpos_monitoring.serviceT)r>z
daemon-reload)r�r�r)�systemd_unit_file�retcoder�r�rFrFrG�$_update_clwpos_daemon_config_systemd�s
rB)�is_solor@�is_module_allowed_on_serverracCsld\}}}dt��kr0tdddgdd�\}}}n|sF|rFt|�\}}}|sbtddd	gdd�\}}}|||fS)
z4
    Install WPOS daemon to system and start it
    )rNNZel6z/sbin/chkconfigz--addr=T)r>z
/sbin/service�start)�platform�releaserrB)rCr@rDrAr�r�rFrFrG�_install_daemon_internal�s


rH)rDracCs8d}t�}|stj�|�r"t|�t�r,dSt|||�S)a�
    Install WPOS daemon to server if need:
        - if daemon already present - do nothing;
        - on CL Solo install daemon always;
        - on CL Shared Pro install daemon if module allowed
    On solo and if /etc/systemd/system/clwpos_monitoring.service present it will be updated always
    We do not need restart installed daemon here, it's done in rpm_posttrans.sh
    :param is_module_allowed_on_server: True/False
    z-/etc/systemd/system/clwpos_monitoring.service)rr�r�)rrbrjr�rBr?rH)rDr@rCrFrFrG�install_monitoring_daemon�s
rI)FF)r&)r])F)N)r�)N)�Z
__future__rrbr�r�r�r[rYrPr|r�r�r�r0�
contextlibr�	functoolsrZpathlibrrrr�typingr	r
rrr
rFZsecureiorZclcommon.cpapi.cpapiexceptionsrZclcommon.clpwdrZclcommon.lib.cleditionrrrZcllicenselibrZclcommon.cpapirrrZclcommon.utilsrrrZclwposrrZclwpos.cl_wpos_exceptionsrrrr r!r"Zclcommon.ui_configr#Zclcommon.clcagefsr$r%Zlogsetupr'Zclwpos.constantsr(r)r*r+r,r-r.r/r0r1Zsocket_utilsr2r3Zuser.website_check.errorsr4r9rIr�rRr_r�r;r`rdrfrhrprwr@rlrzr{rxrr�r�r�r�r�r�r�r�r?r�r�r�r�r�r�r�r�r�r�r�r�r�rrr!r"r/r2r3r4r5r9r:r<r?rBrHrIrFrFrFrG�<module>
s� 0
!	)
0




Q
!
	


Zerion Mini Shell 1.0