Hacked By AnonymousFox

Current Path : /opt/cloudlinux/venv/lib64/python3.11/site-packages/clselector/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/clselector/__pycache__/selectorlib.cpython-311.pyc

�

!�g�-��r�ddlmZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlmZddlmZddlZddlZddlZddlZddlZddlZddlZddlmZddlm Z dd	l!m"Z"m#Z#dd
l$m%Z%m&Z&m'Z'ddl(m)Z)ddlm*Z*dd
lm+Z+ddlm,Z,m-Z-m.Z.ddl/m0Z0m1Z1m2Z2m3Z3ddlm4Z4ddl5m6Z6m7Z7ddlm8Z9ddl:m;Z;ddl<m=Z=ddl>m?Z?m@Z@ddlAmBZBddlCmDZEddlFmDZGddlHmIZImJZJmKZKmLZLmMZMmNZNddlOmPZPddlQmRZRmSZSGd�de��Z8Gd�de8��ZTdd iZUGd!�d"eV��ZWdS)#�)�print_function)�division)�absolute_importN)�Set)�	iteritems)�FormattedException)�resolve_username_and_doc_root)�CP_NAME�docroot)�NoDomain�NotSupported�
IncorrectData)�exec_utility)�clselectctl)�clpassenger)�ClUserSelect�ClSelect�ClExtSelect)�APP_STARTED_CONST�ENABLED_STATUS�DISABLED_STATUS�BaseSelectorError)�validate_env_vars)�format_summary�API_1��ClSelectExcept)�NodeJSConfigError)�NodeManager)�PythonAppFormatVersion�get_venv_rel_path)�
PythonManager)�Environment)�mkdir_p�	file_read�
file_write�get_using_realpath_keys�get_abs_rel�delete_using_realpath_keys)�get_perm)�_set_ui_config�UIConfigExceptionc��eZdZdS)rN)�__name__�
__module__�__qualname__���M/opt/cloudlinux/venv/lib64/python3.11/site-packages/clselector/selectorlib.pyrrGs�������Dr2rc��eZdZdZdS)�ClSelectDomainNotFoundzK
    Custom exception in case if user doesn't have the specific domain
    N)r.r/r0�__doc__r1r2r3r5r5Ks��������	�Dr2r5�status�okc�6�eZdZd�Zd�Zd�ZedMd���Zed���Zed���Z	edNd���Z
edMd	���Zed
���ZedNd���Z
d�Zed
���Zd�Zd�Zed���Zed���Zd�ZdNd�Zed���Zd�Zed���Zd�ZdNd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d �Z"d!�Z#d"�Z$d#�Z%d$�Z&d%�Z'd&�Z(d'�Z)		dOd(�Z*ed)���Z+dNd*�Z,dNd+�Z-ed,���Z.dPd.�Z/dQd0�Z0d1�Z1d2�Z2d3�Z3d4�Z4ed5���Z5d6�Z6ed7���Z7d8�Z8d9�Z9ed:���Z:dNd;�Z;dMd<�Z<d=�Z=ed>���Z>dRd?�Z?d@�Z@dA�ZAdB�ZBdC�ZCdNdD�ZDdE�ZEdF�ZFdG�ZGedH���ZHedI���ZIeJdJ���ZKdK�ZLdL�ZMdS)S�CloudlinuxSelectorLibc�l�||_d|_d|_d|_d|_d|_d|_d|_d|_d|_	d|_
d|_|j|jkrLdd	lm
}|��|_	t��|_tj|_
tj|_dS|j|jkrLdd	lm
}|��|_	t'��|_tj|_
tj|_dS|j|jkrdd
lm}|��|_dSdS)Nz/usr/bin/selectorctlz+/usr/share/l.v.e-manager/utils/dynamicui.py�/usr/sbin/cloudlinux-selector�nodejs�python�ruby�phpr)�ApplicationsManager)�
PhpManager)�interpreter�_SELECTORCTL_UTILITY�DYNAMIC_UI_CTL_CMD�CLOUDLINUX_SELECTOR_UTILITY�NODEJS_INTERPRETER�PYTHON_INTERPRETER�RUBY_INTERPRETER�PHP_INTERPRETER�selector_manager�apps_manager�selector_user_lib�selector_old_lib�$clselect.clselectnodejs.apps_managerrAr�clselect�clselectnodejsuser�clselectctlnodejsuser�$clselect.clselectpython.apps_managerr"�clselectpythonuser�clselectctlpython� clselect.clselectphp.php_managerrB)�selfrCrArBs    r3�__init__zCloudlinuxSelectorLib.__init__Vs[��&���$:��!�"O���+J��(�"*���"*��� &���$���!%��� ���!%��� $�����t�6�6�6�P�P�P�P�P�P� 3� 3� 5� 5�D��$/�M�M�D�!�%-�%@�D�"�$,�$B�D�!�!�!�
�
��!8�
8�
8�P�P�P�P�P�P� 3� 3� 5� 5�D��$1�O�O�D�!�%-�%@�D�"�$,�$>�D�!�!�!�
�
��!5�
5�
5�C�C�C�C�C�C�$.�J�L�L�D�!�!�!�6�
5r2c�f�|j|jkrdS|jj���dS)za
        Checks that selector is able to work on current os environment
        :return:
        N)rCrJrK�
cl_select_lib�check_requirements�rWs r3�check_selector_is_availablez1CloudlinuxSelectorLib.check_selector_is_availablexs:��
��t�3�3�3��F�	
��+�>�>�@�@�@�@�@r2c�n�|j�||��}|�||��\}}|S)z7Get doc_root from application config or raise exception)rL�get_app_domain�$safely_resolve_username_and_doc_root)rW�username�app_root�domain�_�doc_roots      r3�safely_resolve_doc_root_for_appz5CloudlinuxSelectorLib.safely_resolve_doc_root_for_app�s8���"�1�1�(�H�E�E���?�?��&�Q�Q���8��r2Nc��	t||���\}}ng#t$rt|�dnd||d�d����t$rt	ddt
id����t$rt	d	||d�d����wxYw||fS)
a8
        Safely resolve username and doc_root by domain,
        or resolve document root by username,
        or resolve document root and username by effective uid
        :param user: str -> name of unix user
        :param domain: str -> domain of panel user
        :return: tuple -> user, doc_root
        ��userrcNzNo such domain: %(domain)szNo such user: %(user)s)rcri��message�contextz+Nodejs selector not supported for %(panel)s�panelz3Domain %(domain)s is not owned by the user %(user)s)r	rr5r
rr
r)rirc�result_user�result_doc_roots    r3r`z:CloudlinuxSelectorLib.safely_resolve_username_and_doc_root�s��"	�+H���,�,�,�(�K�����
	�
	�
	�(��)� <�;�/G�"(� $� � ���	�	�	
��	�	�	� �L��� �����
��		�		�		� �T�"(� $� � �����
�		�����O�+�+s
��A$A;c���d}t��\}}|�d}nHt|t��sd}n0|d�|d�d}n|dkr	|dkrd}n|ddkrd}|S)	z�
        Check whether selector should be run through "su - user"
        :param opts: dict of parsed cli params
        :return: True if should be run through su or False if not
        TNF�--user�--domainr�
--interpreterr@)r*�
isinstance�dict)�opts�result�euid�egids    r3�should_be_runned_as_userz.CloudlinuxSelectorLib.should_be_runned_as_user�s������Z�Z�
��d��<��F�F��D�$�'�'�	��F�F�
�(�^�
#��Z�(8�(@��F�F�
�Q�Y�Y�4�1�9�9��F�F�
�/�
"�e�
+�
+��F��
r2c�\�|ddko |dduotj��dkS)Nrsr@rqr)�os�geteuid)rvs r3�should_run_user_without_cagefsz4CloudlinuxSelectorLib.should_run_user_without_cagefs�s8���O�$��-�!��H�~�T�)�!��z�|�|�q� �	!r2c�.�tj|��}|S)z�
        Resolves user eigher with passed username or with getting current
        user ID
        :param user: str or None -> username to be resolved
        :return: obj -> pwd user object
        )�pwd�getpwnam)ri�userdatas  r3�_get_user_pwd_dataz(CloudlinuxSelectorLib._get_user_pwd_data�s���<��%�%���r2c�6�|�|�tdid����dSdS)z�
        Check if user and domain are None
        :param user: name of unix user
        :param domain: domain of panel user
        :return: None
        N�BUser or domain parameter must be specified if current user is rootrjrrhs  r3�user_and_domain_checkerz-CloudlinuxSelectorLib.user_and_domain_checker�s:���<�F�N� �c�!�����
��<�N�Nr2c�d�dd|zi}t|��dkr|�|��|S)Nr7z	ERROR: %sr)�len�update)rw�kwargs�errs   r3�
_return_errorz#CloudlinuxSelectorLib._return_error�s7����v�-�.���v�;�;��?�?��J�J�v�����
r2c�B�d|d�}|r|�d|i��|S)z�
        Construct error dict in one place
        :param result: error string
        :return: dict with 'status':'error' and error message
        �error)r7rw�details)r�)rwr�r�s   r3�_return_with_status_errorz/CloudlinuxSelectorLib._return_with_status_error�s<����
�
���	-��J�J�	�7�+�,�,�,��
r2c�F�|ttfvr(|�d�|����S	|tk|j_n,#t$r}|�|��cYd}~Sd}~wwxYw|j|jkr6dd|tkii}	t|��tS#t$rYnwxYw|j|jkr6dd|tkii}	t|��tS#t$rYnwxYw|�
��tS)z�
        Set CL selector status in it's config
        :param status: set status of selector
        :return: error or ok message
        zKUnknown selector status provided: "{}". Can be only 'enabled' or 'disabled'N�
uiSettings�
hidePythonApp�
hideNodeJsApp)rrr��formatrK�selector_enabledrrCrHr+�OK_RES_DICTr,rG�	update_ui)rWr7�e�config_dicts    r3�_change_selector_statusz-CloudlinuxSelectorLib._change_selector_status	so���.�/�;�;�;��%�%�6�6<�f�V�n�n�F�F�
F�	)�5;�~�5M�D�!�2�2�� �	)�	)�	)��%�%�a�(�(�(�(�(�(�(�(�����	)������t�6�6�6�'�/�6�^�;S�)T�U�K�
��{�+�+�+�"�"��$�
�
�
���
������t�6�6�6�'�/�6�^�;S�)T�U�K�
��{�+�+�+�"�"��$�
�
�
���
�����������sA�A�
A9�A4�.A9�4A9�B2�2
B?�>B?�"C8�8
D�Dc�>�ddi}t��jrd|d<|S�Nr�FT)rr���ress r3�get_nodejs_selector_statusz0CloudlinuxSelectorLib.get_nodejs_selector_status-s+��!�5�)���=�=�)�	+�&*�C�"�#��
r2c�:�d|jjrtntiS)N�PHPSelector)rKr�rrr\s r3�get_php_selector_statusz-CloudlinuxSelectorLib.get_php_selector_status4s����1F�1W�l�~�~�]l�m�mr2c���	|j���}|�|�����|S#t$r}|�|��cYd}~Sd}~wwxYw�N)rK�get_summaryr��get_selector_statusrr�)rWr�r�s   r3r�z!CloudlinuxSelectorLib.get_summary7s���	)��'�3�3�5�5�C��J�J�t�/�/�1�1�2�2�2��J�� �	)�	)�	)��%�%�a�(�(�(�(�(�(�(�(�����	)���s�AA�
A-�A(�"A-�(A-c�>�ddi}t��jrd|d<|Sr�)r"r�r�s r3�get_python_selector_statusz0CloudlinuxSelectorLib.get_python_selector_status?s+��!�5�)���?�?�+�	+�&*�C�"�#��
r2c�t�	tj|��jS#t$rt	ddi���wxYw)NrkzNo such user: `%(user)s`)r�r��pw_dir�KeyErrorr)ris r3�
get_user_homez#CloudlinuxSelectorLib.get_user_homeFsR��	��<��%�%�,�,���	�	�	� ��5�"���
�	���s��7c��t�����}|�|�����|Sr�)rr�r�r�)rWr�s  r3�get_nodejs_summaryz(CloudlinuxSelectorLib.get_nodejs_summaryOs:���m�m�'�'�)�)���
�
�4�+�+�-�-�.�.�.��
r2c�$�|�|��}|j|jkr|j�|||d��}n<|j|jkr|j�|||d��}nt
���|g}|j|jkr|�d��|�|��|�:|j|jkr|�d��|�|��|�	|||��}|S)aD
        Runs script to execute other script inside user app environment
        :param user: str -> owner of application
        :param app_root: str -> application directory
        :param script_name: str -> name of script
        :param script_args: list of str -> arguments for the script
        :return: dict
        �npmr>z
run-scriptNz--)
r�rCrGrL�get_binary_pathrH�NotImplementedError�append�extend�_run_interpreter)	rWrirb�script_name�script_args�	user_home�interpreter_path�cmdrws	         r3�
run_scriptz CloudlinuxSelectorLib.run_scriptTs%���&�&�t�,�,�	���t�6�6�6�#�0�@�@��h�	�5� 2� 2���
�
��!8�
8�
8�#�0�@�@��h�	�8� 5� 5���&�'�'�'�� ����t�6�6�6��J�J�|�$�$�$��
�
�;�����"���4�#:�:�:��
�
�4� � � ��J�J�{�#�#�#��&�&�s�I�x�@�@���
r2c�:�tj|tj�||��tjtjd���}	|���\}}n'#t$r}td||d�d����d}~wwxYwd�	|j
|���|�����}td�||fD����rKtj
��}d	|�d
�}	tj|	�����}
|d|	�d|
�d
�z
}dt#j|��������i}|�ddi��|j
dkr
d|j
��|d<|S)z�
        Run interpreter in users environment
        :param cmd: list -> command to execute
        :param user_home: -> user home directory
        :param app_root: -> app path
        :return: dict
        T)�args�cwd�stdout�stderr�textz6run-script call: `%(args)s` failed with error: %(err)s)r�r�rjNz%returncode: {}
stdout:
{}
stderr:
{}
c3�BK�|]}d|���vV��dS)z
out of memoryN)�lower)�.0�outputs  r3�	<genexpr>z9CloudlinuxSelectorLib._run_interpreter.<locals>.<genexpr>�s/����P�P�V��&�,�,�.�.�0�P�P�P�P�P�Pr2z/proc/z/limitsaD
Out of memory error may be caused by hitting LVE limits
or "Max data size", "Max address space" or "Max resident set" process limits
Please check LVE limits and process limits. Readjust them if necessary
More info: https://docs.cloudlinux.com/shared/cloudlinux_os_components/#known-restrictions-and-issues

process limits "z":
�
�datar7�successrzScript exit code: �warning)�
subprocess�Popenr|�path�join�PIPE�communicate�OSErrorrr��
returncode�strip�any�getpid�pathlib�Path�	read_text�base64�	b64encode�encode�decoder�)r�r�rb�pr�r�r��
output_string�pid�proc_limits_path�process_limitsrws            r3r�z&CloudlinuxSelectorLib._run_interpreterqs���
���"�'�,�,�y�(�;�;��?�:�?��
G�
G�
G��	��]�]�_�_�N�F�F�F���	�	�	� �2�$'��2�2�"�"���
�����	����
��&�
�L��L�L�N�N��L�L�N�N���		��P�P���?O�P�P�P�P�P�
	��)�+�+�C�4��4�4�4��$�\�*:�;�;�E�E�G�G�N��P�(8�	P�P�>L�	P�P�P�
�M��&�*�=�+?�+?�+A�+A�B�B�I�I�K�K�L���
�
�x��+�,�,�,��<�1��� C�Q�\� C� C�F�9���
s�
A%�%
B	�/B�B	c�b�|j|j|jfvr|j���SdSr�)rCrGrJrKr�r\s r3�get_fullzCloudlinuxSelectorLib.get_full�s9����� 7��9M�N�N�N��(�4�4�6�6�6�O�Nr2c���t�����}tj��}t	|��D]\}}||xxdz
cc<�|D]}||d|d<�|S)a_
        Add selector usage statistics (amount of
        domains that use some version, etc)
        Fist parameter is an array with such format:
        [{'version': '5.6'}, {'version': '7.6'}]
        Output is an array with such format:
        [{'version': '5.6', 'users': 10}, ...]
        :type versions_list: list
        :rtype: list
        ��version�total_users)r�get_user_version_map�collections�Counterr)�
versions_list�user_version_map�version_user_maprir��items      r3�_add_statistics_fieldz+CloudlinuxSelectorLib._add_statistics_field�s���(�>�>�>�>�@�@��&�.�0�0��&�'7�8�8�	+�	+�M�D�'��W�%�%�%��*�%�%�%�%�!�	D�	D�D�"2�4�	�?�"C�D�����r2c�^�|j|jkrSt|j���d��}t	|dt
���}t
j|��}n#|j�|j���}ni}|�	d��dkrd|di}n|}|S)zG
        Retrieves supported versions list and default version
        F�json)r��api_versionNr7�ERRORrk)
rCrJrr�rrr��loadsrK�get)rWr��	json_data�selectorctl_resultrws     r3�get_supported_versionsz,CloudlinuxSelectorLib.get_supported_versions�s�����t�3�3�3��D�,�-�-�9�9�%�@�@�D�&�t�F��N�N�N�I�!%��I�!6�!6���
�
"�
.�!%�!6�!B�!B�!D�!D���!#���!�!�(�+�+�w�6�6�� 2�9� =�>�F�F�'�F��
r2c��|r|gnd}|j|jkr|j�|��S|�d��S)z3
        Retrives current version for user
        NzSupported only by php selector)rCrJrK�get_current_versionr�)rWris  r3r�z)CloudlinuxSelectorLib.get_current_version�sQ���'��v�v�4����t�3�3�3��(�<�<�T�B�B�B��%�%�&F�G�G�Gr2c��|���}	|d}n6#t$r)d|vr|�|d��cYSd|d�cYSwxYwd|iS)N�default_versionrkr�)r7r�)r�r�r�)rW�	json_dictr�s   r3�get_default_versionz)CloudlinuxSelectorLib.get_default_version�s����/�/�1�1�	�	>�'�(9�:�O�O���	>�	>�	>��I�%�%��)�)�)�I�*>�?�?�?�?�?�")�9�=�=�=�=�=�		>����
"�?�3�3s��)A�
A�Ac���|j|jkr|���S|j|jkr|���S|j|jkr|���SdSr�)rCrGr�rHr�rJr�r\s r3r�z)CloudlinuxSelectorLib.get_selector_status�ss����t�6�6�6��2�2�4�4�4�
�
��!8�
8�
8��2�2�4�4�4�
�
��!5�
5�
5��/�/�1�1�1�6�
5r2c��|jjSr��rKr�r\s r3�php_selector_is_disabledz.CloudlinuxSelectorLib.php_selector_is_disabled�s���(�9�9�9r2c��|jjSr�rr\s r3�php_selector_is_enabledz-CloudlinuxSelectorLib.php_selector_is_enabled�s���$�5�5r2c��|j|jkrdS	ddlm}m}n#t
$rYdSwxYw|��rQ|���s=|��}|r1	|d}|�d��sdSn#t$rYnwxYwdS)z�
        Returns True when MultiPHP system default PHP version is alt-php and PHP Selector is NOT disabled
        For details please see LVEMAN-1170
        Fr)�is_ea4_enabled�read_cpanel_ea4_php_conf�defaultzea-phpT)	rCrJ�clcagefslib.selector.configurerr�ImportErrorr�
startswithr�)rWrr�conf�default_phps     r3�check_multiphp_system_defaultz3CloudlinuxSelectorLib.check_multiphp_system_default�s���
��t�3�3�3��5�	�_�_�_�_�_�_�_�_�_���	�	�	��5�5�	�����>���		�D�$A�$A�$C�$C�		�+�+�-�-�D��
��"&�y�/�K�&�1�1�(�;�;�$�#�t�$�������D������us��
+�+�A9�9
B�Bc	��|j|j|jfvrqt|��D]Z\}}	|�||��}n.#t
$r!}|�|��cYd}~cSd}~wwxYw|tkr|cS�[tS|���}	|d}n5#t$r(|�d|j
�d|j����cYSwxYwd�|D��}t|�����|z
r^|�dd�
t|�����|z
���d|j
�d|j�d	���Sd
}	g}
t|��D]u\}}|r5t|j���t!|����n4t|j���t!|����|	dz
}	�v|	t%|��krtS|	d
kr&dd
�|	t%|��|
��iS|�d�|
����S)N�available_versionszcorrupted answer from z  --json --summary --interpreter c��h|]
}|d��S�r�r1)r��xs  r3�	<setcomp>z?CloudlinuxSelectorLib.set_supported_versions.<locals>.<setcomp>s�� I� I� I�!��9�� I� I� Ir2zinvalid alternative versions (z, z), see z --summary --interpreter z for valid versionsrr�r7z>WARNING: only {} of {} commands was successful. Errors was: {}z&All commands were failedErrors was: {})rCrGrHr�set_version_statusrr�r�r�r�rD�set�keysr�r�enable_version�str�disable_versionr�r�)rW�versionsr��enable_disablerwr�r��alternatives_list�alternatives_versionsr��errors_list�	to_enables            r3�set_supported_versionsz,CloudlinuxSelectorLib.set_supported_versions	s������ 7��9P�R�R�R�+4�X�+>�+>�
"�
"�'���1�!�4�4�^�W�M�M�F�F��(�1�1�1��-�-�a�0�0�0�0�0�0�0�0�0�0�����1�����[�(�(�!�M�M�M�)����/�/�1�1�	�	>� )�*>� ?�����	>�	>�	>��%�%�%��)�)�)�4�+;�+;�'=�>�>�
>�
>�
>�	>����!J� I�7H� I� I� I���x�}�}�����"7�7�	'��%�%�%��I�I�c�(�-�-�/�/�2�2�5J�J�K�K�K�K�T�Mf�Mf�Mf��$�$�$�&�'�'�
'�
����"+�H�"5�"5�	�	��G�Y��
I���)�*�*�9�9�#�g�,�,�G�G�G�G���)�*�*�:�:�3�w�<�<�H�H�H��q�L�G�G��c�(�m�m�#�#���
�q�[�[��+�+1�6�'�3�x�=�=�+�+V�+V��
�
�%�%�'7�7=�v�k�7J�7J�L�L�
Ls/�A�
A-�A(� A-�(A-�B%�%/C�Cc�B�	|j�|j�|��nv#tjjj$rt	dd|id����tjjtf$r,}|�t|����cYd}~Sd}~wwxYwtS�NzNo such php version: %(ver)s�verrj)rK�switch_default_versionrP�clselectexceptr�NoSuchAlternativeVersion�BaseClSelectExceptionrr�rr��rWr�r�s   r3�set_default_versionz)CloudlinuxSelectorLib.set_default_version5s���	:��$�0��%�<�<�W�E�E�E����&�5�N�	@�	@�	@� �-K�.3�W�-=�"?�"?�@�@�
@��'�=�?P�Q�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:�����s�!$�AB�+!B�B�Bc��|r!|j�|t��n |j�|t��tS)z�
        Disable or enable version of selector
        :param target_version_status: disable or enable version of interpreter
        :param version: version of interpreter
        :return: OK_RES_DICT
        )rKrrrr�)rW�target_version_statusr�s   r3rz(CloudlinuxSelectorLib.set_version_statusAsH��!�	O��!�4�4�W�n�M�M�M�M��!�4�4�W�o�N�N�N��r2c�F�	|j|jkr|j�|��no#tjjj$rt
dd|id����tjj$r,}|�	t|����cYd}~Sd}~wwxYwtSr$)rCrJrK�switch_current_versionrPr'rr(r)r�rr�r*s   r3�set_current_versionz)CloudlinuxSelectorLib.set_current_versionPs���	:���4�#7�7�7��%�<�<�W�E�E�E����&�5�N�	@�	@�	@� �-K�.3�W�-=�"?�"?�@�@�
@��&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:�����s�*-�AB�-!B�B�Bc�@�i}	|j|jkr|j�|��}no#tjjj$rt
dd|id����tjj$r,}|�	t|����cYd}~Sd}~wwxYw|Sr$)rCrJrK�reset_extensionsrPr'rr(r)r�r)rWr��
extensionsr�s    r3r2z&CloudlinuxSelectorLib.reset_extensions\s����
�	:���4�#7�7�7�!�2�C�C�G�L�L�
����&�5�N�	@�	@�	@� �-K�.3�W�-=�"?�"?�@�@�
@��&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:�����s�*/�AB�/!B�B�Bc�f�|j|j|j|jfvr|�|��SdSr�)rCrGrHrJr�)rWr7s  r3�set_selector_statusz)CloudlinuxSelectorLib.set_selector_statusis>����� 7��9P�RV�Rf�g�g�g��/�/��7�7�7�h�gr2c��tj��s&tj��stj��rKt	|jdgd���\}}}|dkr)|�d�||����SdS)Nz--sync-conf=allT)r�rz"Can not sync UI with reason: {} {})�detect�	is_cpanel�is_plesk�is_darrEr�r�)rW�retcode�outr�s    r3r�zCloudlinuxSelectorLib.update_uims�������	K���!2�!2�	K�f�l�n�n�	K� ,��'�*;�)<�T�!K�!K�!K��G�S�#��!�|�|��)�)�8�?�?��S�I�I�K�K�K��tr2c�J�t���}|j|jkry	|j�||��}|r|�|��nC#tjj	$r,}|�
t|����cYd}~Sd}~wwxYw|SdSr�)r��copyrCrJrK�set_extensionsr�rPr'r)r�r)rWr3r�rwr�r�s      r3r?z$CloudlinuxSelectorLib.set_extensionsvs����!�!�#�#����t�3�3�3�
>��,�;�;�G�Z�P�P���(��M�M�$�'�'�'����*�@�
>�
>�
>��5�5�c�!�f�f�=�=�=�=�=�=�=�=�����
>�����M�4�3s�2A�B�2!B�B�Bc���|j|jkr`	|j�||��nC#tjj$r,}|�t|����cYd}~Sd}~wwxYwtSr�)
rCrJrK�set_optionsrPr'r)r�rr�)rW�optionsr�r�s    r3rAz!CloudlinuxSelectorLib.set_options�s�����t�3�3�3�
>��%�1�1�'�7�C�C�C�C���*�@�
>�
>�
>��5�5�c�!�f�f�=�=�=�=�=�=�=�=�����
>�����s�.�A.�!A)�#A.�)A.c�>�|��|����dd��}|�|dkr'td�|j�����|j|jkr@t
jd|��}|stdd|id����|�d	��S|St|ttf��r;|j|jkrtt|����St|��S|d
krtdd|id����|S)a3
        Attempts to get or verify version to be passed to external program
        Currently supported version is one digit (6 or 8).
        If version is None, return a default version
        :param version: str or None -> version to be verified or found
        :return: str -> digit as string
        Nr��z{} default version not definedz(?P<version>\d+)z#Incorrect selector version: %(ver)sr%rjr��nativezUnsupported version: %(ver)s)
r�r�rr�rCrG�re�match�grouprt�int�floatr)rWr�r��ms    r3�resolve_versionz%CloudlinuxSelectorLib.resolve_version�sO���?�"�6�6�8�8�<�<�=N�PR�S�S�O��&�/�R�*?�*?�$�%E�%L�%L�T�M]�%^�%^�_�_�_���4�#:�:�:��H�0�/�B�B���P�(�5Z�6;�_�5M�*O�*O�P�P�P��w�w�y�)�)�)�&�&��g��U�|�,�,�	$���4�#:�:�:��3�w�<�<�(�(�(��7�|�|�#��h��� �-K�.3�W�-=�"?�"?�@�@�
@��r2c
��|�||��|�|��}|�!ttj|����}|�||��\}}	|j|jkr+|j�	|||||||||	|j
|
���no|j|jkr%tj
�	|||||���n:|j|jkr*|j�	||||||||||j
|
���nD#tjj$r-}|�t%|�����cYd}~Sd}~wwxYwt&S)a�
        Creates application for specified user, interpreter and version
        If user is None we hope that the external application resolves a user
        Currently NodeJS supported only
        :param domain: str -> domain of the application
        :param app_root: str -> app path relative to user home
        :param app_uri: str -> URI path of the application
        :param version: str or None -> version of the interpreter
        :param user: str or None -> username of user who owns the app
        :param app_mode: str or None -> application mode (development or production)
        :param startup_file: str or None -> main application file
        :param env_vars: json_string or None -> enviroment variables for application
        :param entry_point: Application entry point (used only for python interpreter).
        :param passenger_log_file: Passenger log filename
        :return: dict
        N)re�env_vars�startup_file�domain_name�entry_pointrL�passenger_log_file�re)r�re�app_moderNrOrPrLrR)rw)r�rLrr�r�r`rCrHrN�createrLrIrP�clselectctlrubyrGr'r)r�rr�)
rWrb�app_urir�rircrTrOrNrQrRrer�s
             r3�
create_appz CloudlinuxSelectorLib.create_app�s���$	
�$�$�T�6�2�2�2��&�&�w�/�/����(���H�)=�)=�>�>�H��B�B�4��P�P���h�	A���4�#:�:�:��%�,�,�T�8�W�g�X`�6>�\�9?�[�:>�:K�`r�-�t�t�t�t��!�T�%:�:�:��(�/�/��h���[c�/�d�d�d�d��!�T�%<�<�<��%�,�,��(�G�W�x�%��|� &�T�5F�[m�-������
�&�<�	A�	A�	A��1�1��Q���1�@�@�@�@�@�@�@�@�����	A�����s�)B*D�E�("E�
E�Ec��tj�|j|��}tj�|��stdd|id����|S)z�
        Resolve app root to absolute path and checks if it exists
        :param app_root: str -> relative a user homedir app path
        :param userdata: obj -> pwd user object
        :return: str -> absolute path to app
        zNo such application: %(app)s�apprj)r|r�r�r��isdirr)rbr��app_paths   r3�_get_application_pathz+CloudlinuxSelectorLib._get_application_path�sc���7�<�<����:�:���w�}�}�X�&�&�	A� �-K�.3�X�->�"@�"@�A�A�
A��r2c���t���}|�|d��|�|��}|�||��}t
j�||��}t
j�|��stdd|id����	t|d��5}|���}	|�dtj|	�����i��ddd��n#1swxYwY|S#t $r.}
|�t%|
dd����cYd}
~
Sd}
~
wwxYw)	a 
        Reads file and returns its content as Base64 string
        :param app_root: str -> path to app relative to user home
        :param user: str -> username to resolve app path
        :param config_path: str -> file to be read (relative to app path)
        :return: dict
        Nz&Configuration file not found: %(path)sr�rj�rbr��strerrorrD)r�r>r�r�r]r|r�r��existsr�open�readr�r�r�r��	Exceptionr��getattr)rWrb�config_pathrirwr�r\�full_config_path�fr�r�s           r3�read_app_configz%CloudlinuxSelectorLib.read_app_config�s����!�!�#�#���$�$�T�4�0�0�0��*�*�4�0�0���-�-�h��A�A���7�<�<��+�>�>���w�~�~�.�/�/�	� �G� &�(8�9�����
�	N��&��-�-�
I���v�v�x�x���
�
�v�v�'7��'=�'=�'D�'D�'F�'F�G�H�H�H�
I�
I�
I�
I�
I�
I�
I�
I�
I�
I�
I����
I�
I�
I�
I��M���	N�	N�	N��1�1�'�!�Z��2L�2L�M�M�M�M�M�M�M�M�����	N���sC�/D(�?AD�D(�D�D(�"D�#D(�(
E �2#E�E �E c���|�|d��|�|��}|�||��}tj�||��}	t
|d��5}|�tj	|����ddd��n#1swxYwYtS#t$r.}	|�t|	dd����cYd}	~	Sd}	~	wwxYw)a�
        Saves data passed as Base64 string to specified file
        :param content: data for saving in app's config
        :param app_root: str -> path to app relative to user home
        :param user: str -> username to resolve app path
        :param config_path: str -> file to be read (relative to app path)
        :param content: str -> Base64-encoded string
        :return: dict
        N�wbr`rD)r�r�r]r|r�r�rb�writer��	b64decoder�rdr�re)
rWrbrf�contentrir�r\rgrhr�s
          r3�save_app_configz%CloudlinuxSelectorLib.save_app_configs6��	
�$�$�T�4�0�0�0��*�*�4�0�0���-�-�h��A�A���7�<�<��+�>�>��	N��&��-�-�
3������(��1�1�2�2�2�
3�
3�
3�
3�
3�
3�
3�
3�
3�
3�
3����
3�
3�
3�
3�����	N�	N�	N��1�1�'�!�Z��2L�2L�M�M�M�M�M�M�M�M�����	N���sB�#B9�3(B'�B9�'B+�+B9�.B+�/	B9�9
C1�#C,�&C1�,C1c��t|��}|�|�d|g��n)|�|�d|g��ntd���|S)Nrrrqr�)�listr�r)rircr��result_argss    r3�_add_user_or_domainz)CloudlinuxSelectorLib._add_user_or_domainsf���4�j�j�������
�F�3�4�4�4�4�
�
�����$�/�0�0�0�0� �!e�f�f�f��r2Fc�Z�|�||��|j|jkrtdd|jid����	|D]}|j�|||���	tS#tjj	$r,}|�
t|����cYd}~Sd}~wwxYw)an
        Uninstall described modules for user's webapp
        :param app_root: directory with webapp
        :param modules: comma-separated list of modules to uninstall
        :param user: name of unix user
        :param domain: domain of user
        :param skip_web_check: skip check web application after changing its properties
        :return: None
        zJUninstall command is available only for python interpreter, not %(interp)s�interprjN)r�rCrHrrN�	uninstallr�rPr'r)r�r)rWrb�modulesrirc�skip_web_check�moduler�s        r3�uninstall_modulesz'CloudlinuxSelectorLib.uninstall_modules s���	
�$�$�T�6�2�2�2���t�6�6�6� �g��d�.��"�"���
�	�!�
H�
H���%�/�/��h��G�G�G�G�
H�
����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:���s�!A*�*B*�>!B%�B*�%B*r1c	���|�||��|j|jkrn	|j�||||j���tS#tjj	$r,}|�
t|����cYd}~Sd}~wwxYw|j|jkr�	|r+|D]'}|j�|||d||j����(n?|r&|j�||d|||j���nd}	|�
|	��StS#tjj	$r,}|�
t|����cYd}~Sd}~wwxYwtdd|jid����)a�
        Install described modules for user's webapp
        :type domain: domain of user
        :param user: name of unix user
        :param app_root: directory with webapp
        :param skip_web_check: skip check web application after change it's properties
        :param spec_file: file containing modules and their versions to install
        :param modules: list of installed modules
        :return: None
        )rxrLNz9Please, specify modules or requirements file with modules�Unknown interpreter: %(interp)srurj)r�rCrGrN�installrLr�rPr'r)r�rrHr)
rWrbrircrx�	spec_filerwr�ryr�s
          r3�install_modulesz%CloudlinuxSelectorLib.install_modules:s��	
�$�$�T�6�2�2�2���t�6�6�6�
#��%�-�-�d�H�^�;?�;L�.�N�N�N�
#�"���*�@�
>�
>�
>��5�5�c�!�f�f�=�=�=�=�=�=�=�=�����
>�����
��!8�
8�
8�
#��	?�")�V�V���-�5�5�d�H�f�d�cq�CG�CT�6�V�V�V�V�V��?��)�1�1�$��$�	�AO�^b�^o�2�q�q�q�q�V�C��9�9�#�>�>�>�#�"���*�@�
>�
>�
>��5�5�c�!�f�f�=�=�=�=�=�=�=�=�����
>����
!�<��d�.��"�"���
s<�#A�B�&!B
�B�
B�&A+D�E�-!E�E�Ec�j�|�|d��	|j|jkr!tj�||��n�	|�||��}n3#t$rtj	dd|i����t$rd}YnwxYw|j|j|jfvr$|j
�||||j���ntjdd|ji����nC#tjj$r,}|�t'|����cYd}~Sd}~wwxYwt(S)z�
        Destroy specified application root directory and user name
        :param app_root: Application directory
        :param user: name of unix user
        :return: dict
        Nz\No such application or it's broken. Unable to find app-root folder by this path %(app_root)srbrj)rerLr|ru)r�rCrIrPrV�destroyrf�	TypeError�ClSelectExceptions�	WrongDatar5rGrHrNrL�InterpreterErrorr'r)r�rr��rWrbrirer�s     r3�destroy_appz!CloudlinuxSelectorLib.destroy_appfs���	
�$�$�T�4�0�0�0�	:���4�#8�8�8��(�0�0��x�@�@�@�@�$�#�C�C�D�(�S�S�H�H�� ����,�6�![�'��!�����.�$�$�$�#�H�H�H�$�����#��(?��AX�'Y�Y�Y��)�1�1�$��8�?C�?P�2�R�R�R�R�-�=� A�!)�4�+;� <�>�>�>�>����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:�����sB�1C+�
A!� C+�!-B�C+�B�AC+�+D+�?!D&� D+�&D+c��|�|d��	|�||��}|j|j|jfvr#|j�||||j��ntj	dd|ji����tS#tjj
$r,}|�t|����cYd}~Sd}~wwxYw)z�
        Start specified application root directory and user name
        :param app_root: Application directory
        :param username: name of unix user
        :return: dict
        Nr|rurj)r�rfrCrGrHrN�startrLr�r�r�rPr'r)r�r�rWrbrarer�s     r3�	start_appzCloudlinuxSelectorLib.start_app�s���	
�$�$�X�t�4�4�4�
	:��;�;�H�h�O�O�H���D�$;�T�=T�#U�U�U��%�+�+�H�h��$�J[�\�\�\�\�(�9�=�%�t�'7�8�:�:�:�:�����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:���s�A1B
�
C
�!C�?C
�C
c��|�|d��	|�||��}|j|jkr#|j�||||j��nP|j|jkr#|j�||||j��ntj	dd|ji����tS#tjj
$r,}|�t|����cYd}~Sd}~wwxYw)z�
        Destroy specified application root directory and user name
        :param app_root: Application directory
        :param username: name of unix user
        :return: dict
        Nr|rurj)r�rfrCrGrN�restartrLrHr�r�r�rPr'r)r�rr�s     r3�restart_appz!CloudlinuxSelectorLib.restart_app�s��	
�$�$�X�t�4�4�4�	:��;�;�H�h�O�O�H���4�#:�:�:��%�-�-�h��(�D�L]�^�^�^�^��!�T�%<�<�<��%�-�-�h��(�D�L]�^�^�^�^�(�9�=�%�t�'7�8�:�:�:�:�����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:�����BB8�8C8�!C3�-C8�3C8c��|�|d��	|�||��}|j|jkr#|j�||||j��nP|j|jkr#|j�||||j��ntj	dd|ji����tS#tjj
$r,}|�t|����cYd}~Sd}~wwxYw)z�
        Start specified application root directory and user name
        :param app_root: Application directory
        :param user: name of unix user
        :return: dict
        Nr|rurj)r�rfrCrGrN�stoprLrHr�r�r�rPr'r)r�rr�s     r3�stop_appzCloudlinuxSelectorLib.stop_app�s��	
�$�$�T�4�0�0�0�	:��;�;�D�(�K�K�H���4�#:�:�:��%�*�*�4��8�T�EV�W�W�W�W��!�T�%<�<�<��%�*�*�4��8�T�EV�W�W�W�W�(�9�=�%�t�'7�8�:�:�:�:�����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:���r�c	��d|zdz���}d|zdz���}tjtj�|ddd����D�]}tj�|��s�	t
|d���}|���|vs||vra|�|���|�����}|�||��}	t||	d����#t$rYtj��\}
}
}tj�
ttj|������Y��wxYw��dS)	z�
        Replace old prompt and env_var in binaries in new environment
        Working with bytes here, because of python binary
        �(�:�*�binr_)�moderkN)r��globr|r�r�r[r%�replacer&�IOError�sys�exc_infor�rlr�	traceback�print_tb)�new_env�old_env�new_rel�old_rel�
old_prompt�
new_prompt�
venv_bin_file�old_activate�
_new_activate�new_activaterd�
traceback_s            r3�6_replace_old_env_and_prompt_in_binaries_in_environmentzLCloudlinuxSelectorLib._replace_old_env_and_prompt_in_binaries_in_environment�sx���G�m�c�)�1�1�3�3�
��G�m�c�)�1�1�3�3�
�!�Y�r�w�|�|�G�S�%��'M�'M�N�N�
	J�
	J�M��7�=�=��/�/�	
J�J�#,�]��#F�#F�#F�L��~�~�'�'�<�7�7�:��;U�;U�(4�(<�(<�W�^�^�=M�=M�w�~�~�O_�O_�(`�(`�
�'4�'<�'<�Z��'T�'T��"�=�,��E�E�E����J�J�J�'*�|�~�~�$�A�q�*��J�$�$�S��);�J�)G�)G�%H�%H�I�I�I�I�I�J����	
J�
	J�
	Js�BD�AE<�;E<c��	t|||��}tj|��j}t	|d��d}||jj|j�|||��|d|d|d�||ddg��d�||g��|dgd�i}|S#t$ricYSwxYw)a�
        Retrieve application info from user's applications config. Analog of function clpassenger.summary
        :param username: User name
        :param app_config_dict_full: Full user's application config.
        :param app_root: Application root
        :return: Dictionary with application info
         Example:
         { 'binary': '/home/cltest1/virtualenv/new_app_root/2.7/bin/python',   # +
           'domain': 'cltest1.com',                                            # +
           'alias': 'app1',                                                    # +
           'htaccess': '/home/cltest1/public_html/app1/.htaccess',             # +
           'interpreter': 'python',                                            # +
           'directory': '/home/cltest1/new_app_root',                          # +
           'docroot': '/home/cltest1/public_html',                             # +
           'domains': ['cltest1.com']
         }
        rcrrW�/�	.htaccess)rC�binary�aliasrcr�htaccess�	directory�domains)
r'r�r�r�rrL�INTERPRETERr�r�r�)rWra�app_config_dict_fullrb�app_config_dictr�re�
app_info_dicts        r3�get_app_summaryz%CloudlinuxSelectorLib.get_app_summary�s���$	�5�h��J^�_�_�O���X�.�.�5�I���x�8�9�9�!�<�H��#'�#4�#@�"�/�?�?��(�T]�^�^�,�Y�7�-�h�7�'� #���(�O�I�4N�P[�)\� ]� ]�!$���9�h�*?�!@�!@� /�� 9�:�	�	��M�!� ���	�	�	��I�I�I�	���s�B;B>�>C
�C
c�L�tj�|��s&t|��tj||��dStj|��D]5}t
jtj�||��|���6tj	|��dS)z�
        Move all items from old directory of application to new directory
        :param old_directory: full real path to old directory of applicaton
        :param new_directory: full real path to new directory of applicaton
        N)
r|r�rar$�rename�listdir�shutil�mover��rmdir)�
old_directory�
new_directoryr�s   r3�_move_app_from_old_dir_to_newz3CloudlinuxSelectorLib._move_app_from_old_dir_to_news����w�~�~�m�,�,�	$��M�"�"�"��I�m�]�3�3�3�3�3��
�=�1�1�
N�
N����B�G�L�L���=�=�}�M�M�M�M��H�]�#�#�#�#�#r2c
�	�|j�|��}	t|||��}n%#t$rt	jd|z���wxYwt
||��\}}t
||��\}}		tj|	��n3#t$r&}
t	j
t|
�����d}
~
wwxYw|�|||	��}	t||	|��t	j
|���#t$rYnwxYw|�|||��}	t|||��}
n%#t$rt	j
d|z���wxYw|
d}|
d}|j�||||���j}|j|jkrPt'||��\}}t
||��\}}t
|t'|||���d��\}}n�|j|jkr�t
|t*j�|jjj|����\}}t
|t*j�|jjj|	����\}}nt7���t*j�|d��}t*j�|d��}t*j�|��}t*j�|��st+j|��t?j ||��|�!||��|j|jkr|�"|||	|��|j|jkrt'||��\}}n:|j|jkr|j�#||��}nt7���|jj�$|||��}|���j%}t|||��d	}|tLkrmtOj(||��||�)d
d��}tOj*||||j|||d|���tOj+||��tY|||��|||<|j�-||��dS)
a
        Move user's application from directory to new_directory
        :param user:  application owner. unix like user name
        :param old_directory: current directory with application
        :param new_directory: new directory for application
        :return: None
        �8No such application (or application not configured) "%s"Nrr���app_summaryrr�rD�
app_statusrRrO�rerOrR).rL�get_user_config_datar'r�r��NoSuchApplicationr(r�check_directory�
ValueErrorr�rr��AppRootBusyrN�_get_environment�namerCrHr!rGr|r�r�rM�environments�DEFAULT_PREFIXr��dirnamera�makedirsr�r�r�r��_get_prefixr#r�rr�unconfigurer��	configurer�r)�write_full_user_config_data)rWrir�r��full_config�
app_config�old_absr��new_absr�r��new_user_summary�old_user_summary�old_user_app_summaryrer��env_namer%�rel_venv�_old_envrd�_new_envr�r��new_env_dir�prefix�environmentr�r��passenger_log_file_to_sets                              r3�	_relocatezCloudlinuxSelectorLib._relocates<���'�<�<�T�B�B��	�0��}�k�R�R�J�J���	�	�	�$�6�J�����
�	����'�t�]�;�;����&�t�]�;�;����	7�
�'��0�0�0�0���	7�	7�	7�$�.�s�1�v�v�6�6�6�����	7����" �/�/��k�7�K�K��	:�#�D�'�3C�D�D�D�%�0��9�9�9���	�	�	��D�	����
 �/�/��k�7�K�K��	@�#:�4��JZ�#[�#[� � ���	@�	@�	@�$�.�/k�1>�0?�@�@�
@�	@����(�	�2��$�W�-���(�9�9��-�-=�m�-L�:�N�N�NR�	���t�6�6�6�-�d�M�B�B�M�C��%��h� � �K�H�a�&��'��m�S�I�I�I�!�L�N�N�K�H�a�a�
�
��!8�
8�
8�%�d�B�G�L�L��&�3�B�G�-M�-M�N�N�K�H�a�&�d�B�G�L�L��&�3�B�G�-M�-M�N�N�K�H�a�a�&�'�'�'��'�,�,�x��,�,���'�,�,�x��,�,���g�o�o�h�/�/���w�~�~�k�*�*�	%��K��$�$�$���G�W�%�%�%��*�*�7�G�<�<�<���t�6�6�6��G�G��QX�Za�cj�k�k�k���t�6�6�6�)�$�
�>�>�I�A�v�v�
�
��!8�
8�
8��*�6�6�t�]�K�K�F�F�%�'�'�'��,�9�E�E�h�PT�V\�]�]���(�(�*�*�1��,�T�=�+�N�N�|�\�
��*�*�*��#�D�-�8�8�8�(3�M�(B�(F�(F�G[�]a�(b�(b�%��!�$�
�u�d�>N�PV�ai�/9�.�/I�5N�
P�
P�
P�
P�
���m�4�4�4�	#�4���D�D�D�%/��M�"���5�5�d�K�H�H�H�H�Hs?�.�"A�:B�
B?�!B:�:B?�C?�?
D�D�'D9�9"Ec�X�	|j|j|jfvr|�|||��n2|j|jkr"t
j�|||d��nC#t
jj	$r,}|�
t|����cYd}~Sd}~wwxYwtS)a=
        Call selectorctl to relocate application from old_app_root to new_app_root
        :param user: application owner
        :param old_app_root: current application directory (current application name)
        :param new_app_root: new application directory (new application name)
        :return: json
        N)
rCrHrGr�rIrPrV�relocater'r)r�rr�)rWri�old_app_root�new_app_rootr�s     r3r�zCloudlinuxSelectorLib.relocate�s���	:���D�$;�T�=T�#U�U�U����t�\�<�@�@�@�@��!�T�%:�:�:��(�1�1�$��l�TX�Y�Y�Y����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:�����s�AA"�"B"�6!B�B"�B"c���d}tj�|||��}tj�|||��}tj�|��tj�|��krdStj�|��}tj�|��stj|��tj||��dS)a
        :param old_doc_root: path to old doc root of application
        :param old_alias: old alias (uri) of application
        :param new_doc_root: path to new doc root of application
        :param new_alias: new alias (uri) of application
        :return: None
        r�N)	r|r�r��realpathr�r[r�r�r>)�old_doc_root�	old_alias�new_doc_root�	new_aliasr��old_htaccess_file�new_htaccess_file�new_htaccess_paths        r3�_transit_htaccess_filez,CloudlinuxSelectorLib._transit_htaccess_file�s������G�L�L��y�(�K�K���G�L�L��y�(�K�K��
�7���-�.�.��7�#�#�$5�6�6�7�7��F��G�O�O�,=�>�>���w�}�}�.�/�/�	+��K�)�*�*�*���%�'8�9�9�9�9�9r2c���|j�|��}	t|||��}n5#t$r(t	jd�|�����wxYw|�|||��}	t|||��}	n5#t$r(t	jd�|�����wxYw|	d}
|	d}|�|
ntj
|��}|j�||||���}
|
�
��j}||dtkru||�dd��}t#j||||j
|dd	||d
|��
�
t#j|||
|||���t#j||��n|�|}|�||
||��||d
<|�||d<|j�||��dS)a�
        Change application URI
        :param user: application owner. unix like user name
        :param directory: directory with application. (app-root)
        :param alias: new alias (app-uri) for application or None if change only the domain
        :param new_doc_root: NEW doc_root to transit application to
        :param new_domain: NEW domain to transit application to
        :return: None
        z8No such application (or application not configured) "{}"r�rNr�r�rRT�transitrOr�)r�r�rWrc)rLr�r'r�r�r�r�r�r�r�	get_aliasrNr�rCr�rr�rr�r�r�r�r�)rWrir�r��
new_domainr�r�r��apps_summary�old_app_summaryr�r�r�r�r�r�s                r3�_transitzCloudlinuxSelectorLib._transit�sf���'�<�<�T�B�B��	 �0��y�+�N�N�J�J���	 �	 �	 �$�6�J�Q�Q���� � �
 �	 ����
�+�+�D�+�y�I�I��	J�5�d�I�|�T�T�O�O���	J�	J�	J�$�.�7�7=�v�i�7H�7H�J�J�
J�	J����$�G�,�	�&�y�1��!&��I�I�K�4I�%�4P�4P�	��+�<�<��)��i�)@�=�B�B���(�(�*�*�1���y�!�,�/�3D�D�D�(3�I�(>�(B�(B�CW�Y]�(^�(^�%��!�$�	�9�d�>N�PV�X\�^g�%�J�~�4N�#<�
>�
>�
>�
>�
��T�9�i��Q]�lx�y�y�y�y����i�0�0�0�0��#�+���'�'��i��y�Y�Y�Y� )�
�9���!�#-�J�x� ���5�5�d�K�H�H�H�H�Hs�.�2A �;B
�
2B?c��	|�d}n|�||��\}}|j|j|jfvr|�|||||��nB|j|jkr$tj�||||���nt���nC#tj
j$r,}|�t|����cYd}~Sd}~wwxYwtS)aV
        Call selectorctl to transit application to new_app_uri
        :param user: application owner
        :param app_root: application directory (application name)
        :param new_app_uri: new uri or None if change only the domain
        :param new_domain: new domain or None if change only the app_uri
        :return: json
        NrS)r`rCrHrGr�rIrPrVr�r�r'r)r�rr�)rWrirb�new_app_urir�r�rdr�s        r3r�zCloudlinuxSelectorLib.transit�s���	:��!�#���"&�"K�"K�D�R\�"]�"]���<���D�$;�T�=T�#U�U�U��
�
�d�H�l�J��T�T�T�T��!�T�%:�:�:��(�0�0��x��Wc�0�d�d�d�d�)�+�+�+����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:�����s�BB�C�&!C
�C�
Cc���d}|r,	|�|��}n#tj$rYnwxYw|j�||j|��|dkrtjd���dS)z�
        Install nodejs extensions to new nodejs environment and change
        symlink <user_homedir>/<app-root>/node_modules to new environment.
        Raise exception `WebAppError` if npm will return non-zero code
        rz<Module installation has been failed. Please, check npm logs.N)�extension_install_single_callr��FileProcessErrorrN�_create_symlink_to_node_modulesr��WebAppError)rWrirbr��old_extensions�npm_ret_codes      r3�_relocate_nodejs_extensionsz1CloudlinuxSelectorLib._relocate_nodejs_extensionss������	�
�&�D�D�^�T�T����%�6�
�
�
���
����	
��=�=�d�G�L�RZ�[�[�[��1���$�0�1o�p�p�p��s��.�.c��t|�����}||z
D]-}	|�|���#tj$rY�*wxYw||z
D]-}	|�|���#tj$rY�*wxYwdS)z�
        Install python extensions to new python environment.
        They are equivalent to extensions from old environment.
        Remove python extensions which not existing
        in old environment from new environment
        N)rr3�extension_uninstallr��ExternalProgramFailed�extension_install)r�r�new_extensions�	extensions    r3�_relocate_python_extensionsz1CloudlinuxSelectorLib._relocate_python_extensionss����W�/�/�1�1�2�2��'�.�8�	�	�I�
��+�+�I�6�6�6�6��%�;�
�
�
���
����
(�.�8�	�	�I�
��)�)�)�4�4�4�4��%�;�
�
�
���
����	�	s#�A�A�A�A3�3B�Bc	�*�����
������j�����	t������n%#t$rt	jd�z���wxYw�j�����}�s-|j|�	���
��iS�j������}������t|�����}�j	�jkr��||��n(�j	�jkr����||��t'j���}t��|��}	|	d�|�	��j��j�|	���\�
}
�
��������f	d�}|sN	�j�|
�
|���dS#tj$r}t	jd|z���d}~wwxYw|��dS)a�
        Set current interpreter version for the application

        :param user: application owner. unix like user name
        :param directory: app_root - main directory with user application
        :param version: new version of python interpreter or None if we get current
        :param skip_web_check: skip check web application after change it's properties
        :return: None
        r�rc
�>�	�tj�������dd��}tj����j���d|���tj�����	��d�jz<�j�����dS)NrRrOr��
%s_version)rr�r�r�rCr�rLr�)
r�r�r�r�r�rer�rWrir�s
 ���������r3�actionz5CloudlinuxSelectorLib._change_version.<locals>.action`s�����#�D�)�4�4�4�(3�I�(>�(B�(B�CW�Y]�(^�(^�%��!�$�	�5�$�:J�F�]e�/9�.�/I�5N�
P�
P�
P�
P�
���i�0�0�0�FM�K�	�"�<�$�2B�#B�C���9�9�$��L�L�L�L�Lr2)rcr�rz,An error occured during changing version. %sN)rLr�r'r�r�r�rNr�r�rC�as_dict�_create_environment�_ensure_version_enabledrr3rHrrGrr�summaryr��_get_info_about_webapp�check_response_from_webappr)rWrir�r�rx�old_environment�new_environment�installed_extensions�user_summary_datar��
app_domainrr�r�r�r�rer�s````         @@@@@r3�_change_versionz%CloudlinuxSelectorLib._change_version6s�������������'�<�<�T�B�B��	�0��y�+�N�N�J�J���	�	�	�$�6�J�����
�	����
�/�@�@��y�Q�Q���	S�#�(�/�*E�*E�*G�*G�*O�*O�*Q�*Q�R�R��/�C�C�D�)�U\�]�]���$�$�W�d�3�3�3�"�?�#=�#=�#?�#?�@�@����t�6�6�6��,�,�_�>R�S�S�S�S�
�
��!8�
8�
8��,�,�T�9�o�Oc�d�d�d�(�/��5�5��-�d�I�?P�Q�Q���y�)�� �,�,�.�.�5�� �1�H�H��VZ�[�[���z�	M�	M�	M�	M�	M�	M�	M�	M�	M�	M�	M�	M�	M��
	�
k��%�@�@�%��!�A�������
&�1�
k�
k�
k�(�4�5c�fi�5i�j�j�j�����
k����
�F�H�H�H�H�Hs!�7�"A�<G�H�*H�Hc�4�	|j|j|jfvrt�|j|jkrM|j�||���dtj��}|tjkrddiS|j�	||��tkr|�||||���n|�|||��tS#tjj$r,}|�t%|����cYd}~Sd}~wwxYw)ax
        Call selectorctl to change current interpreter version to new_version for application
        :param user: application owner
        :param app_root: application directory (application name)
        :param new_version: new nodejs interpreter version
        :param skip_web_check: skip check web application after change it's properties
        :return: json
        �app_versionr7z�This application was created by too old version of python selector and we cannot change version without migration to the new application format. To do that you can use `cloudlinux-selector migrate` command or just click button in web UI.)rxN)rCrGrHr�rL�get_app_configr�r �LEGACY�get_app_statusrr�_change_version_for_stopped_appr�rPr'r)r�r)rWrirb�new_versionrxrr�s       r3�change_versionz$CloudlinuxSelectorLib.change_versionys>��	:����(?��AX�'Y�Y�Y�)�)���4�#:�:�:�"�/�>�>�t�X�N�N�R�R�!�#9�#@�B�B���"8�"?�?�?� �#L���� �/�/��h�?�?�CT�T�T��$�$�T�8�[�Q_�$�`�`�`�`��4�4�T�8�[�Q�Q�Q�����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:���s%�A8C�;AC�D�+!D�D�Dc�$�|j|jks|j|jkro|�||��}	|j�||||��dS#t$r,}tjd�	|�����d}~wwxYwdS)NzRUnable to set environment variables in htaccess file for the application.Error: {})
rCrHrGrfrL�add_env_vars_for_htaccessrdr�rr�)rWrirbrNrer�s      r3�set_variables_for_litespeedz1CloudlinuxSelectorLib.set_variables_for_litespeed�s�����t�6�6�6�$�:J�d�Ne�:e�:e��;�;�D�(�K�K�H�
��!�;�;�D�(�H�V^�_�_�_�_�_���
�
�
�(�4� � &��s����������
����	;f�:es�A�
B
�!'B�B
c	��|j�|��}		t|||	��}
n%#t$rt	jd|z���wxYw|j|jkr8|�6|
d|kr*|
dtj	krt	j
d���|�||
d<|�||
d<|�||
d<|�||
d<|�||
�d	��ks|��j|�g||
�d	��krN||
d	<|j|jkr9|j�
|||��}|j�||||��|�
|d
krdn||
d<|j�||��}t!j|��}
t|||
��}|d}|���j}|d
}|d}t!j|���|�6|
�d	���!t!j||||j|||���n4t!j||||j|||
�d	��|���t!j||��|j�||	��|�|||��dS)al
        Set application mode, environment variables and startup_file for application
        :param config_files: names of config files (such as requirements.txt or etc)
        :param entry_point: the specified entrypoint for application
        :param user: application owner. unix like user name
        :param directory: directory with application
        :param app_mode: expected application mode
        :param env_vars: dict with environment variables for application
        :param startup_file: main file for application
        :param passenger_log_file: Passenger log filename
        :return: None
        r�NrNrz�Unable to set environment variables. Application was created too long time ago. Please, migrate your application to newer version before changing interpreter versionrTrQ�config_filesrOrDrRr�rr�)r�)rerRr�)rLr�r'r�r�r�rCrHr r rr�rN�_get_full_path_to_startup_file�
setup_wsgir�rrr��_unconfigurer�r�r�r')rWrir�rTrNrOrQr)rRr��app_config_data�startup_file_full_path�env�user_summary�user_app_summaryr�r�re�
htaccess_paths                   r3�_set_variablesz$CloudlinuxSelectorLib._set_variables�s.���'�<�<�T�B�B��	�5�d�I�{�S�S�O�O���	�	�	�$�6�J�����
�	����
��t�6�6�6��(�#�J�/�8�;�;�#�M�2�6L�6S�S�S�$�0�6���
���*2�O�J�'���+3�O�J�'��"�-8�O�M�*��#�.:�O�N�+��#���8K�8K�N�8[�8[�([�([�"�.��'�L�O�<O�<O�P^�<_�<_�,_�,_�2>���/��#�t�'>�>�>�-1�-B�-a�-a��!�$�.�.�*��)�4�4�T�9�F\�^i�j�j�j�!�-�AS�VX�@X�@X���^p�� 4�5��'�8�8��y�I�I�C�&�.�t�4�4�L�6�t�Y��U�U��$�W�-�E��_�_�&�&�-�F�'�	�2�H�,�Z�8�M��$�m�<�<�<�<��#��(;�(;�N�(K�(K�(S��%�d�I�u�d�>N�PV�ai�9K�M�M�M�M�M��%�d�I�u�d�>N�PV�ai�3B�3F�3F�~�3V�3V�9K�M�M�M�M�	��D�)�,�,�,���5�5�d�K�H�H�H��(�(��y�(�C�C�C�C�Cs	�.�"Ac	
�"�|�O	ttj|����}	n.#ttf$r|�d��cYSwxYwd}		|j|j|jfvrA|j	�
||��tkr|�||||	||||��n|�
||||	||||��tS#tjj$r,}
|�t%|
����cYd}
~
Sd}
~
wwxYw)ak
        Call selectorctl to set variables for application
        :param config_files: names of config files (such as requirements.txt or etc) (only for python)
        :param entry_point: the specified entrypoint for application (only for python)
        :param user: application owner
        :param app_root: application directory (application name)
        :param app_mode: application mode
        :param env_vars: json_string with environment variables for application
        :param startup_file: main file for application
        :param passenger_log_file: Passenger log filename
        :return: json
        Nz/wrong json format for environment variable list)rr�r�r�r�r�rCrGrHrLr!rr3�_set_variables_for_stopped_appr�rPr'r)r)rWrirbrTrNrOrQr)rR�env_dictr�s           r3�
set_variablesz#CloudlinuxSelectorLib.set_variables�s`����
i�,�T�Z��-A�-A�B�B�����z�*�
i�
i�
i��5�5�6g�h�h�h�h�h�
i�����H�	:���D�$;�T�=T�#U�U�U��%�4�4�T�8�D�D�HY�Y�Y��#�#�D�(�H�h��Va�co�$6�8�8�8�8��3�3�D�(�H�h�Xd�4?��Oa�c�c�c�����&�<�	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:���s.�!&�&A�A�A8C�D�"!D	�D�	Dc��	|j�|��}|S#t$r}|j|jd�cYd}~Sd}~wwxYw)z�
        Retrieves info about all installed interpreters and user(s) applictions
        :param user: User name for read applictions. If None all users will be processed
        :return: Dict with info
        )rwrlN)rL�get_applications_users_inforrkrl)rWri�result_dictr�s    r3�get_apps_users_infoz)CloudlinuxSelectorLib.get_apps_users_info!sc��	?��+�G�G��M�M�K���� �	?�	?�	?��i�A�I�>�>�>�>�>�>�>�>�����	?���s��
A�<�A�Ac�|�|j�|��s!tj�||d���dS)z�
        Check whether particular interpreter version is enabled and raises
        exception if not
        :param username: user to include in exception
        :param new_version: new interpreter version
        zversion is not enabledN)rK�is_version_enabledrPr�UnableToSetAlternative)rWr#ras   r3rz-CloudlinuxSelectorLib._ensure_version_enabled-sF���$�7�7��D�D�	r��)�@�@��;�Xp�q�q�q�	r�	rr2c�"�|�||��|j�||��}|j�|||d��}t|�����}|j�|||d��}|D]7}	|�|���#tj	j
j$rY�4wxYw|j�|��}	||	|d|j
z<|j�||	��dS)z�
        Changes version for stopped application
        :param username: application owner
        :param app_root: application directory (application name)
        :param new_version: new nodejs interpreter version
        :return: None
        Nr)rrL�get_interpreter_version_for_apprNrrr3r	rPr'rrr�rCr�)
rWrarbr#�old_versionrrrrr�s
          r3r"z5CloudlinuxSelectorLib._change_version_for_stopped_app7s*��	
�$�$�[�(�;�;�;��'�G�G��RZ�[�[���/�C�C�H�h�Xc�ei�j�j���_�7�7�9�9�:�:���/�C�C�H�h�Xc�ei�j�j��'�	�	�I�
��1�1�)�<�<�<�<���*�9�O�
�
�
���
�����&�;�;�H�E�E�
�@K�
�8��\�D�,<�<�=���5�5�h�
�K�K�K�K�Ks�B(�(C�Cc	��|j�|��}	|r||	|d<|r||	|d<|r*||	|�d��kr||	|d<|r||	|d<|r||	|d<|�|dkr||	|d<nd|	|d<|j�||	��|�|||��dS)	a�
        Sets new app_mode, environment variables and startup file for stopped NodeJS application
        :param config_files: names of config files (such as requirements.txt or etc) (only for python)
        :param entry_point: the specified entrypoint for application (only for python)
        :param str username: application owner
        :param str app_root: application directory (application name)
        :param str app_mode: New application mode, can be None
        :param dict env_vars_dict: New environment variables, can be None
        :param startup_file: New startup file, can be None
        :param passenger_log_file: Passenger log filename
        :return: None
        rTrNrOrQr)NrDrR)rLr�r�r�r')
rWrarbrT�
env_vars_dictrOrQr)rRr�s
          r3r5z4CloudlinuxSelectorLib._set_variables_for_stopped_appRs���&�;�;�H�E�E�
��	8�/7�J�x� ��,��	=�/<�J�x� ��,��	@�L�J�x�,@�,D�,D�^�,T�,T�T�T�3?�J�x� ��0��	>�2=�J�x� ��/��	@�3?�J�x� ��0��)�.@�B�.F�.F�9K�J�x� �!5�6�6�9=�J�x� �!5�6���5�5�h�
�K�K�K��(�(��8�]�K�K�K�K�Kr2c�l�tt|�d��d����S)z�
        Retrieves major version from full. If already short, return it with no difference
        :param version: Full/short
        :return: Short version as string
        �.r)rrI�splitrs r3�get_major_version_from_shortz2CloudlinuxSelectorLib.get_major_version_from_shortss+���3�w�}�}�S�)�)�!�,�-�-�.�.�.r2c���ttd����������}t	��}|D]}|�|���dS)z�
        Replace mysqli extension to nd_mysqli for defaults.
        Warning: only for PHP. See LVEMAN-1399 for details
        :return:
        r@N)rqr�get_all_alternatives_datarr�list_extensions)�alt_php_versions_list�
cl_ext_select�alt_php_vers   r3�replace_mysqliz$CloudlinuxSelectorLib.replace_mysqli|si��!%�X�e�_�_�%N�%N�%P�%P�%U�%U�%W�%W� X� X��#�
�
�
�0�	7�	7�K��)�)�+�6�6�6�6�	7�	7r2c���tjddg��tjddg��tjgd���tjdg��|���dS)zf
        Setup php selector for work
        (suggested to use after native php is installed)
        �	cagefsctlz--force-updatez
--remount-all)r<zmake-defaults-configz--jsonrsr@z//usr/share/l.v.e-manager/utils/cache_phpdata.pyN)r��check_outputrN)�clss r3�setup_selectorz$CloudlinuxSelectorLib.setup_selector�s���	���.>� ?�@�@�@����o� >�?�?�?���!C�!C�!C�	D�	D�	D���!R� S�T�T�T��������r2c��|j|jkrt�tj��rt
S	|j���t
S#t$r,}|�	t|����cYd}~Sd}~wwxYw)zn
        Scan users home dirs for .htaccess files and import
        applications to new config file.
        N)rCrHr�r7r:r�rL�$import_legacy_applications_to_configrdr�r)rWr�s  r3�run_import_applicationsz-CloudlinuxSelectorLib.run_import_applications�s���
��t�6�6�6�%�%�
�\�^�^�	���	:���B�B�D�D�D�����	:�	:�	:��1�1�#�a�&�&�9�9�9�9�9�9�9�9�����	:���s�A�
B	�!B�>B	�B	c��|j|jkrtd���|j�||��t
S)z_
        Convert applications created in older selector
        versions to new format
        z/Migration is only available for python selector)rCrHr�rL�migrate_applicationr�)rWrirbs   r3�run_migrate_applicationz-CloudlinuxSelectorLib.run_migrate_application�sM��
��t�6�6�6�%�'<�=�=�
=�	
��-�-�d�H�=�=�=��r2)NNr�)NNNNNNN)NNF)NNFNr1)NF)Nr.r/r0rXr]rf�staticmethodr`rzr~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr"r+rr0r2r5r�r?rArLrXr]rirorsrzrr�r�r�r�r�r�r�r�r�r�r�r�rrrr$r'r3r7r;rr"r5rGrN�classmethodrSrVrYr1r2r3r:r:Us������� 1� 1� 1�D	A�	A�	A�����.,�.,�.,��\�.,�`����\��.�!�!��\�!�
�����\���
�
�
��\�
�����\�������\��"�"�"�H����\��n�n�n�)�)�)�����\������\�����
����:�*�*��\�*�X7�7�7�����\��*���*H�H�H�H�	4�	4�	4�2�2�2�:�:�:�6�6�6����0*L�*L�*L�X
�
�
�
�
�
�
�
�
����8�8�8����	�	�	�������BHL�im�*�*�*�*�X����\��N�N�N�N�8N�N�N�N�*����\������4*�*�*�*�X"�"�"�H:�:�:�(:�:�:�,:�:�:�,�J�J��\�J�,%�%�%�N�$�$��\�$�"pI�pI�pI�d���(�:�:��\�:�23I�3I�3I�3I�j����0q�q�q�&����\��6A�A�A�A�F":�":�":�H	�	�	�UD�UD�UD�n :� :� :�D
?�
?�
?�
?�r�r�r�L�L�L�6L�L�L�B�/�/��\�/��7�7��\�7��
�
��[�
�:�:�:� 
�
�
�
�
r2r:)X�
__future__rrrr�r�r�r�r|r�r�rFr�r�r�r��typingr�future.utilsr�cldetectlibr7�clselect.clselectctlnodejsuserrP�clselect.clselectexcept�clselect.clselectctlpython�clselect.clselectctlruby�clselect.clselectpythonuser�clselect.clselectnodejsuser�clcommon.clexceptionr�clcommon.clpwdr	�clcommon.cpapir
r�clcommon.cpapi.cpapiexceptionsrr
r�cllimits.librrrrrr�clselect.baseclselectrrrrr�clselect.clselectctlphprrrr��clselect.clselectnodejsr�$clselect.clselectnodejs.node_managerrrSr r!�&clselect.clselectpython.python_managerr"�(clselect.clselectpythonuser.environmentsr#�PythonEnvironment�(clselect.clselectnodejsuser.environments�NodeJsEnvironment�clselect.utilsr$r%r&r'r(r)�secureior*�clconfig.ui_config_libr+r,r5r��objectr:r1r2r3�<module>rxs���&�%�%�%�%�%�������&�&�&�&�&�&�
�
�
�
�������������	�	�	�	�����
�
�
�
�	�	�	�	�
�
�
�
�����
�
�
�
�����������"�"�"�"�"�"�����%�%�%�%�����!�!�!�!�����"�"�"�"�"�"�"�"�3�3�3�3�3�3�8�8�8�8�8�8�+�+�+�+�+�+�+�+�P�P�P�P�P�P�P�P�P�P�%�%�%�%�%�%� � � � � � � � � � � � �8�8�8�8�8�8�8�8�8�8�g�g�g�g�g�g�g�g�g�g�g�g�<�<�<�<�<�<�9�9�9�9�9�9�9�9�H�H�H�H�H�H�5�5�5�5�5�5�<�<�<�<�<�<���������A�@�@�@�@�@�U�U�U�U�U�U�U�U�U�U�U�U�����������������������D�D�D�D�D�D�D�D�	�	�	�	�	�'�	�	�	�	�	�	�	�	�^�	�	�	�����]�]�]�]�]�F�]�]�]�]�]r2

Hacked By AnonymousFox1.0, Coded By AnonymousFox