AlkantarClanX12

Your IP : 3.135.247.17


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

�

".g2��	��ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
mcmZddlmZddlmZddlmZddlmZmZmZmZmZmZddlZddlZddlZddl Z ddl!m"Z"ddl#m$Z$m%Z%m&Z&m'Z'ddl(m)Z)dd	l*m+Z+dd
l,m-Z-ddl.m/Z/m0Z0ddl1m2Z2dd
l3m4Z4ddl5m6Z6m7Z7m8Z8ddl9m:Z:m;Z;ddl m<Z<m=Z=m>Z>m?Z?m@Z@ddlAmBZBmCZCdZDdZEdZFdZGdZHdZIdZJdZKdZLeMejN�Odd����ZPdZQe&e"jR��sdZSdZTne?eP���ZSe=eS���ZTGd�d e��ZUd!eUfd"�ZVd#�ZWd$�ZXd%�ZYd&e)fd'�ZZd�d(�Z[d)�Z\d*d+d*d,dd-dd.d/�Z]d-Z^d0d+ddddddd/�Z_gd1�Z`d2aadabdacd3add3aeiafd3agd3ahd3aid3ajd3akd3ald3amd4and3aod3apd3aqd5�Zrd6�Zsd7�Ztd�d8�Zud9�Zvd:�Zwev��p	ew��Zxd�d;�Zyd<�Zzd=�Z{d>�Z|d�d?�Z}d@�Z~ejdA���Z�dBeUfdC�Z�dD�Z�d�dE�Z�dF�Z�dG�Z�d�dH�Z�dI�Z�dJ�Z�dK�Z�dLeMdMe�fdN�Z�dO�Z�dP�Z�dQee�eMffdR�Z�dS�Z�d�dU�Z�dV�Z�d�dW�Z�d�dX�Z�dY�Z�dZ�Z�d[�Z�d�d\�Z�d�d]�Z�d^�Z�d_�Z�d`�Z�d�da�Z�db�Z�ia�dcZ�ddZ�d�de�Z�df�Z�d�dg�Z�dh�Z�di�Z�dj�Z�dk�Z�d�dme�dne�doeMd!e�fdp�Z�dq�Z�dr�Z�d�du�Z�e�fdv�Z�d�dw�Z�dx�Z�dy�Z�dz�Z�d{�Z�d�d|�Z�d}�Z�d~�Z�d!e�e�e�ffd�Z�d�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d�d��Z�d��Z�d�d��Z�d�d��Z�d�d��Z�d�e�d�e�d!eeMfd��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d�d��Z�d��Z�d��Z�d��Z�d�d��Z�d��Z�d��Z�d��Z�da�da�da�da�da�d��Z�d�d��Z�d��Z�d�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�dS)��N)�range)�partial)�PAGESIZE)�Dict�List�Optional�Text�Tuple�	TypedDict)�Feature)�admins�get_main_username_by_uid�is_panel_feature_supported�reseller_users)�
EncodingError)�acquire_lock)�detect_panelclass)�reseller_limits_disabled_post�reseller_limits_enabled_post)�exec_utility)�get_subprocess_logger)�BadVeConfigException�get_xml_config�save_xml)�LockFailedException�setup_global_lock)�LVP_XML_TAG_NAME�Lve�NameMap�PyLve�
PyLveError)�create_dir_secure�write_file_via_tempfilez%/usr/bin/getcontrolpaneluserspackagesz
/proc/cpuinfoi'�
VE_DEFAULTFz/bin/umountz"/etc/container/exclude_mounts.conf�multi�single�PYLVE_DEBUG�plesk_id)�debug)�pyc�\�eZdZUeezed<eed<eed<eed<eed<eed<eed<dS)	�LiblveSettings�ls_cpu�ls_cpus�ls_io�	ls_enters�
ls_memory_phy�ls_nproc�ls_iopsN)�__name__�
__module__�__qualname__�int�str�__annotations__���py/lvectllib.pyr,r,IsY��������#�I����
�L�L�L��J�J�J��N�N�N������M�M�M�
�L�L�L�L�Lr;r,�returnc�D�dddddddd�}|�|��|S)Nr)r-r.r/r0r1r2r3)�update)�kwargs�defaultss  r<�create_liblve_settingsrBSs<��������� � �H�
�O�O�F�����Or;c�t�t���rtj���SgS)z-Helper function for easy mocking in unittests)�lve�reseller_limit_supported�proc�lvp_id_listr:r;r<�lvp_listrHas/��
�#�#�%�%�&��x�#�#�%�%�%�
�Ir;c�n�t��}|���|���S)zf
    Get list of resellers with activated reseller limits

    :return: list of pairs (name, uid)
    )r�
link_xml_node�load_from_node)�name_maps r<�get_active_resellersrMhs1���y�y�H��������"�"�$�$�$r;c�6�|d�t��D��vS)z]
    Check whether giver reseller has activated reseller limits or not
    :return: bool
    c3� K�|]	\}}|V��
dS�Nr:)�.0�name�uids   r<�	<genexpr>z,is_active_reseller_limits.<locals>.<genexpr>xs&����J�J�i�d�C�T�J�J�J�J�J�Jr;)rM��
reseller_names r<�is_active_reseller_limitsrWss%��
�J�J�3G�3I�3I�J�J�J�J�Jr;�ec���tj��s|�tr tddt	|��g��nt|��t
jd��dS)a"
    Since cPanel user can corrupt config file for some user with wrong encodings,
    we want to notify him that he should fix encoding problems with the link to documentation.
    Print error message and exit with code 1 or raise given exception if it isn't cPanel.
    :return: None
    r%�ERROR�N)�cldetectlib�	is_cpanel�JSON�json_formatr8�print�sys�exit�rXs r<�raise_cpanel_encoding_errorrd|sg��� �"�"�������G��c�!�f�f�%�	'�	'�	'�	'�	�a�����H�Q�K�K�K�K�Kr;c�h�	t|��dS#t$rtdd��YdSwxYw)az
    ~~~~~~~~~~~~~~~~~~
    !!! DEPRECATED !!!
    ~~~~~~~~~~~~~~~~~~
    Please, use setup_global_lock instead if possible

    Wrapper over setup_global_lock. If lock cannot be set,
    it will write message and close app

    The only reason why it is here is legacy function
    check_result_and_exit that we use in TWO places
    :type write: bool
    :return: Nothing
    r[zcan`t get lockN)rr�check_result_and_exit)�writes r<�get_global_lockrh�sP��3��%� � � � � ���3�3�3��a�!1�2�2�2�2�2�2�3���s��1�1c��|dtjfvrJtrttdd|��g��ntd|����t
j|��dSdS)NrrZzlvectl: zlvectl: Error: )�errno�ENOSYSr^r_�MULTI_FORMATr`rarb)�result�messages  r<rfrf�st��
�a�%�,��'�'�'��	/���w�0D�7�0D�0D�&E�F�F�F�F��-�G�-�-�.�.�.���������(�'r;�r[�i�)�cpu�ncpu�io�ep�mem�pmem�nproc�iops�d)rsrrrtrvrwrxryru���falsec�
�|adSrP)r^)�	json_flags r<�set_jsonr��s���D�D�Dr;c�
�|adSrP)�
BYTES_FLAG)�
bytes_flags r<�	set_bytesr��s���J�J�Jr;c��trtdkrd}ntdkrd}ntdkrd}nd}gd�gd�gd�gd	�d
�|}tr1|�d��dz}|d|�d
gz||d�zS|S)N��noiops_8�8��6�4)�ID�SPEED�PMEM�VMEM�EP�NPROC�IO)r�r�r�r�r�r�r��IOPS)r�r�r�r�r�)r�r�r�r�r�r[�CPU)�NOIOPS�LVE_VERSIONr^�index)�version�fields�	speed_idxs   r<�
get_fieldsr��s���
��+��"�"����	��	�	����	��	�	������B�B�B�
B�
B�
B�
;�
;�
;�
,�
,�
,�	��
��F��C��L�L��)�)�A�-�	��z�	�z�"�e�W�,�v�i�j�j�/A�A�B��Mr;c��|�3tj��}|d�td���|dan|at���}|std���t���adS)Nrzget_lve_version failedzinit_lve() failed.)�clcommon�get_lve_version�RuntimeErrorr��pylve�
initialize�liblve_settings�lve_settings)�lve_ver�
lve_statuss  r<�initr�s}�����*�,�,���1�:���7�8�8�8��a�j������!�!�#�#�J��1��/�0�0�0��(�(�*�*�L�L�Lr;c��tj�d��}|�dS	tj|��S#t
tf$r<}tdt|����tj
d��Yd}~dSd}~wwxYw)z-Get cpu information from environment veriable�CPU_DATANz.Invalid environment variable 'CPU_DATA' formatr[)�os�environ�get�json�loads�	TypeError�
ValueErrorr`r8rarb)�packed_cpu_datarXs  r<�_get_cpu_data_from_envr�s����j�n�n�Z�0�0�O����t���z�/�*�*�*���z�"����
�@�#�a�&�&�I�I�I��������������������s�9�B�
1B�Bc��i}i}d}	ttdd���5}|D]�}|���s||d|��<|dz}i}�&t|�d����dkr\|�d��d���||�d��d���<��d	||�d��d���<��	d
d
d
��n#1swxYwYn<#t
$r/t
dt�d���tjd��YnwxYw||d
dgS)zF
    Parse /proc/cpuinfo

    return [NumProc, frequency in MHZ]

    r�r�utf-8��encodingrFr[�:�r|Nzlvectl: Error: Can`t open �.�proc0zcpu MHz)	�open�CPUINFO�strip�len�split�IOErrorr`rarb)�cpuinfo�procinfo�nprocs�f�lines     r<�get_cpu_datar�)s����G��H�
�F��
�'�3��
1�
1�
1�	B�Q��
B�
B���z�z�|�|�
B�/7�G�O�6�O�O�,�#�a�Z�F�!�H�H��4�:�:�c�?�?�+�+�q�0�0�?C�z�z�#���q�?Q�?W�?W�?Y�?Y�����C����!3�!9�!9�!;�!;�<�<�?A�����C����!3�!9�!9�!;�!;�<�<�
B�	B�	B�	B�	B�	B�	B�	B�	B�	B�	B�	B����	B�	B�	B�	B�������
�5�7�5�5�5�6�6�6�������������
�G�G�$�Y�/�0�0s5�D�CD�<D�D�D�D�D�6E
�E
c�d�t|��}|pd}t}t|d��}tjd|��}|�it|��}d|cxkrdkrMnnJtttdz|z|z����}|dkr|St|tz|��SdS)z�
    Try converting to kernel format from old CPU format (percentage of whole
    cpu) and optionally the NCPU format. Return whichever is less.

    :param data: string presumably in old CPU format
    :param lncpu: integer number of cores limit
    rz
\d{1,2}0?$Nrz)r8�CPUINFO_DATAr7�re�match�round�CORE_WEIGHT�min)�data�lncpu�cpu_datars�cpu_percent�from_cpu_limits      r<�convert_from_old_cpur�Ns����t�9�9�D��J�Q�E��H��x��{���D��(�=�$�/�/�K����4�y�y���t�?�?�?�?�s�?�?�?�?�?� ��{�c�'9�D�'@�4�'G�!H�!H�I�I�N���z�z�%�%��u�{�*�N�;�;�;��4r;c�J�t|��}t}t|d��}tjd|��}|�`t|�dd����}||dzkr|dz}|dkr'tttdz|z����SdSdS)zd
    Try converting cpu limit from SPEED in percentage of one CORE format to
    kernel format.
    rz\d+(?:\.\d+)?%$N�%r|rz)	r8r�r7r�r��float�replacer�r�)r�r�rs�percents    r<�convert_from_speed_percentr�ds���
�t�9�9�D��H��x��{���D��h�)�4�0�0�G�������S�"�-�-�.�.���T�C�Z����S�j�G��Q�;�;��u�[�C�/�'�9�:�:�;�;�;��t��4r;c��t|��}t}t|d��}t|d��}t	jdtj��}|�|��}|��|�d��}t|�d����}|�	��dkr|dz}|||zkr||z}|dkr'tt|tz|z����SdS)	zQ
    Try converting cpu limit from SPEED in mhz/gzh format to kernel format.
    rr[z,(?P<freq>\d+(?:\.\d+)?)(?P<suffix>mhz|ghz)+$N�suffix�freq�GHZi�)r8r�r7r�r��compile�
IGNORECASEr��group�upperr�r�)r�r�rs�cpu_freq�patternr�r�r�s        r<�convert_from_speed_hzr�vs����t�9�9�D��H��x��{���D��X�a�[�!�!�H��j�H�"�-�X�X�G��M�M�$���E������X�&�&���U�[�[��(�(�)�)���<�<�>�>�U�"�"��$�;�D��(�T�/�!�!��d�?�D��!�8�8��u�T�K�/�(�:�;�;�<�<�<��4r;c�>�t|��pt|��S)z}
    Try converting cpu limit value from either SPEED limit format
    (percentage of CORE or mhz/ghz) to kernel format.
    )r�r�)r�s r<�convert_from_speedr��s$��	#�4�(�(�	'� ��&�&�r;c�z�t||��}|�|St|��}|�|St|��}|�|SdS)z�
    Convert different variants of cpu limit to kmod ver 8 variant

    :param data: Value in old CPU format or SPEED with % or mhz/ghz.
    :param lncpu: Limit in old NCPU format.
    :return: CPU limit in kmod ver 8+ format or None for bad format
    N)r�r�r�)r�r��from_cpu�from_speed_percent�
from_speed_hzs     r<�convert_to_kernel_formatr��sV��$�D�%�0�0�H�����3�D�9�9���%�!�!�)�$�/�/�M�� ����4r;c�p�t}t|d��}t|��}d|vrOdtttt|�d����|z������zStttt|��|z������S)zm
    convert speed to old cpu format
    args: cpu limit in speed value
    return: old cpu limit format
    r�*)r�r7r8r��lstrip)�speedr�rxs   r<�speed_to_old_cpur��s����H�������E���J�J�E�
�e�|�|��S��U�3�u�|�|�C�'8�'8�#9�#9�U�#B�C�C�D�D�E�E�E�E��s�5��U���u�,�-�-�.�.�/�/�/r;c#�K�t���t���}	t�||��n2#t$r%tjt
jd|�d����wxYw	|V�t�|��dS#t�|��wxYw)zF
    Run subprocess in lve with pseudo-random id and given limits
    zUnable to setup lve with id z-, something is wrong, check dmesg for detailsN)r�r��get_available_lve_id�	lve_setupr!�syslog�	LOG_ALERT�lve_destroy)�settings�lve_ids  r<�
temporary_lver��s�����
������
�
'�
'�
)�
)�F��
�����)�)�)�)�������
�f�&�)V�v�)V�)V�)V�	W�	W�	W�
�����
"�����
���&�!�!�!�!�!�����&�!�!�!�!���s�A�/B�B%�%Cr�c�R�t���}t|d|d���|_|d|_|d|_|d|_|d|_ttj
d|d	ztz����|_|S)
a
    Just a nice user-friendly constructor of liblve_settings object
    You can pass the following ls_cpu and ls_cpus values:
    - in percents of one core (just ls_cpu='75%', ls_cpus will be ignored)
    - in old 'CPU' format (two arguments, ls_cpu and ls_cpus required, both int)
    r-r.�r�r/r0r2r3g�?r1)
r�r�r�r-r/r0r2r3r7�math�ceilrr1)r��ss  r<�make_liblve_settingsr��s���	�����A�'���(:�(�9�BU�V�V�V�A�H��w��A�G��;�'�A�K��*�%�A�J���#�A�I��$�)�B��/�)B�$B�X�$M�N�N�O�O�A�O��Hr;c���t|�d����}|s.|�d��}tj|��j}t|��S)N�id�user)r8�getAttribute�pwd�getpwnam�pw_uidr7)�ve_lve_element�user_uid�	user_names   r<�get_ve_lve_user_uidr
�sV���>�.�.�t�4�4�5�5�H��2�"�/�/��7�7�	��<�	�*�*�1���x�=�=�r;c���dt|d��i}|tkrt|d��|d<|�|�|��tt	j|����dS)a�
    Print output in json as:
    {"status": "ERROR/OK", "msg": "Some Message", "ext1": "foo", "ext2": "bar"}
    where "status" and "msg" field are mandatory

    :param str error_type: Either MULTI_ERROR or SINGLE_ERROR
    :param list data: List with a status string and a message string
    :param dict extensions: Some additional fields for the final json object
    :return: None
    �statusrr[�msgN)r8rlr?r`r��dumps)�
error_typer��
extensionsrms    r<r_r_�sl����D��G���
%�F��\�!�!��D��G����u�
����
�
�j�!�!�!�	�$�*�V�
�
�����r;c��	tt�|��d�d����||<dS#tt
tf$rp||||<|�|��}|�dt||����	|j
|��YdS#t$rYYdSwxYwwxYw)Nr�limit)r7�
ve_default�getElementsByTagNamerr��
IndexErrorr��
createElement�setAttributer8�appendChild�	Exception)�xml�ve_defaults�ve_cfg�val�default�nodes      r<�check_def_valuer �s���	��z�>�>�s�C�C�A�F�S�S�T[�\�\�]�]��C������
�I�.����"�3�<��C���#�#�C�(�(�����'�#�g�c�l�"3�"3�4�4�4�	��C�O�D�!�!�!�!�!�!���	�	�	��D�D�D�	����
���s+�AA�A C�(B;�;
C
�C�	C
�
Cc�*���fd�|jD��S)Nc�^��g|])}t|tj���|j�k�'|��*Sr:)�
isinstancer�Element�tagName)rQ�_�tags  �r<�
<listcomp>z"xml_filter_tag.<locals>.<listcomp>	s7���Z�Z�Z�!�*�Q���*D�*D�Z���VY�IY�IY�A�IY�IY�IYr;)�
childNodes)rr's `r<�xml_filter_tagr*s���Z�Z�Z�Z�t��Z�Z�Z�Zr;c��t||��D]9}|�|�|��s�|�|�|��|kr�6|cSdSrP)r*�hasAttributer)rr'�attr�attr_val�
child_nodes     r<�xml_filter_firstr0sn��$�T�3�/�/���
���J�$;�$;�D�$A�$A�����J�$;�$;�D�$A�$A�X�$M�$M�������r;c�p�t|||���}|�t���|�|��S)N)rr'r-)r0rr)rr'r-�filtered_child_nodes    r<�get_child_tag_atrrr3s;��*��#�D�I�I�I���"��l�l���+�+�D�1�1�1r;c�x�t||��d}|�|t|����dS)z�
    Find in children nodes node with tag and setup attribute
    insted el.getElementsByTagName not search recursiveli in tree
    rN)r*rr8)rr'r-r�first_child_nodes     r<�set_child_tag_atrrr6s;��
&�d�C�0�0��3���!�!�$��C���1�1�1�1�1r;c��	t��\aadS#t$rg}tr%ttdt|��g��ntt|����tj
d��Yd}~dSd}~wwxYw)zLoad config from ve.cfgrZr[N)rr�ve_lveconfigrr^r_rlr8r`rarbrcs r<�_load_config_wrapperr9%s���
�-�/�/������������	���w��A���&7�8�8�8�8��#�a�&�&�M�M�M��������������������s��
B�AB�B�lvp_id�lvp_defaultsc��da	|s"ttd��datS|r!t
�d��atStttdt|����}|r|�d��dant�t��atS#t$r@tr'tdddg��t!jd��nt%d	��YnwxYwtS)
z�Load default limits

    :param int lvp_id: lvp id
    :param bool lvp_defaults: load reseller's default limits instead of global
    :return: dict with default limits
    �truerArrr%�WARNINGzdefault section error in ve.cfgr[z(warning: default section error in ve.cfg)�ubcr*r8r�LVE_DEFAULTrr�LVP_DEFAULTr0rr8r�	cloneNoderr^r_rarbr`)r:r;�defaults_root_nodes   r<�_load_default_limitsrD4s���C�>��	�'��j�A�A�!�D�J����	��-�-�j�9�9�J���-�l�<L�d�TW�X^�T_�T_�`�`���	:�+�@�@��L�L�Q�O�J�J�#�-�-�j�9�9�J�����>�>�>��	>���)�-N�!O�P�P�P��H�Q�K�K�K�K��<�=�=�=���>�����s�#B8�"B8�A,B8�8AD�Dc	�|�td�ttttt
tfD����S)Nc3�"K�|]
}|dkV��dS)r|Nr:)rQ�xs  r<rTz._all_config_elements_loaded.<locals>.<genexpr>[s&����j�j�1�q�B�w�j�j�j�j�j�jr;)�all�ve_lve�ve_lvp�
ve_package�	ve_binary�ve_enter_by_name�ve_cfg_versionr:r;r<�_all_config_elements_loadedrOZs.���j�j����Y�HX�Zh� i�j�j�j�j�j�jr;c��t�d��at�d��at�t��atjj�	t��t�d��}t|��dkr	|dan9t�
d��at�t��t�d��at�d��}t|��dkrt!|djj��ndadS)	z$Load all config elements from ve.cfgrD�packagez
enter-by-namer�binaryr�r[N)r8rrIrKrrJrD�maprLrJr�rMrrrrLr7�
firstChild�	nodeValuerN)�enter_by_name_elems�cfg_version_elemss  r<�_load_config_elementsrX]s���
.�
.�u�
5�
5�F��2�2�9�=�=�J�
�
.�
.�/?�
@�
@�F��G��"�"�<�0�0�0�&�;�;�O�L�L��
����!�#�#�.�q�1���!�/�/��@�@��� � �!1�2�2�2� �5�5�h�?�?�I�%�9�9�)�D�D��GJ�K\�G]�G]�`a�Ga�Ga�S�*�1�-�8�B�C�C�C�gh�N�N�Nr;�default_limitsc�0�iattttt|���}|d���	t�d��d�d��}t|td���td<n�#tttf$r�t|dtd���td<t�d��}|�dt|d����	t�|��n#t$rYnwxYwYnwxYw	t!t�d��d�d	����td
<n�#tttf$r�|d
td
<t�d��}|�d	t|d
����	t�|��n#t$rYnwxYwYnwxYw|d���t"dkr
|d
���n|d
d
t$i���|d���|d���|d���t'��dS)z~Create ve_defaults dict with default values for all limits

    :param dict default_limits: default limits for lve or lvp
    )rrrrrs)rrrrrr��other�
maxentryprocsrurt�rv)rrrwrxryN)rrr rrrrr�r�rr�rrr8rrr7r��MEM_DEFAULT_CL5�_check_defaults_for_nones)rY�
check_def_valr�rrrus     r<�_load_ve_defaultsra�s����K����������M��M�f�����
��/�/��6�6�q�9�F�F�w�O�O��5�e�;�v�CV�W�W�W��E�����
�I�.����5�n�U�6K�S^�_e�Sf�g�g�g��E���"�"�5�)�)������#�n�U�&;�"<�"<�=�=�=�	��"�"�3�'�'�'�'���	�	�	��D�	������
����	��
� ?� ?�� H� H�� K� X� X�Yh� i� i�j�j��D�����
�I�.����*�4�0��D��
�
!�
!�'�
*�
*��
�����^�D�-A�)B�)B�C�C�C�	��"�"�2�&�&�&�&���	�	�	��D�	������
�����M�d������Q����
�%� � � � � ��
�%�%��)A�B�B�B�B��M�f������M�g������M�f����������so�AB�BE�D0�/E�0
D=�:E�<D=�=E�E�AF�A*H)�;H�H)�
H#� H)�"H#�#H)�(H)c��t���D]^\}}|��d|�d�}trtdd|g��n"tj�|�d���t	jd���_dS)z*Check that all default values are not NoneNzERROR: Incorrect z default valuer%rZ�
r[)r�itemsr^r_ra�stderrrgrb)�key�value�err_msgs   r<r_r_�s���"�'�'�)�)�	�	�
��U����9�c�9�9�9���	-���'�7�!3�4�4�4�4��J����^�^�^�,�,�,��������	�	r;Tc��t��t||��}|st��st��t	|��dS)z�
    :param bool lvp_defaults: load reseller's default limits instead of global
    :param int lvp_id: lvp id to load customise defaults
    N)r9rDrOrXra)r:r;�load_config_elementsrYs    r<�get_XML_cfgrk�sW������)�&�,�?�?�N�� �#>�#@�#@� ������n�%�%�%�%�%r;c��	tt||d�����}|||<|S#tttf$rt||��cYSwxYw)Nr�r'r-)r7r3r�rr�)r�elr�
setup_datargs     r<�check_valuerp�sm��%��&�r�s��A�A�A�B�B���
�3������
�I�.�%�%�%��;�s�#�$�$�$�$�$�%���s�%(�,A�Ac��tj��}|���|�|��}t	||���dS)a5
    This function is a pure workaround for our ugly globals-based API which
    should be fixed partially with LU-496, because there is no clean way
    to retrieve reseller's data from ve.cfg without touching globals
    :param reseller: reseller name
    :return: Nothing. It just updates some globals
    �rjN)�lveapirrJ�get_idrk)�reseller�xml_config_load_elementsrL�reseller_ids    r<�_load_resellers_xml_datarx�sN���~���H��������/�/�(�+�+�K���2J�K�K�K�K�K�Kr;c
�����tjt��a���Kt��}���|vr�|�vr��fd�}n�t	j��r�|��	t
j|��}d|j�d�}t|d���5}|�
��}ddd��n#1swxYwYn#t$rd}YnwxYwd|vr#tjtj
d	|�d
����fd�}tD�]n}	||	���r]td|	tt��}
	t!t#t%|	d
d���|
�����}|td
<n#t&t(t*f$rYnwxYwtd|	tt��t,dkrtd|	tt��n
dtd<	t!t%|	dd�����}|td<n#t&t(t*f$rYnwxYwtd|	tt��td|	tt��td|	tt����ndSdS)a
    Put limit values that will be applied later in a global variable `setup_ve`.

    :param plan_id: package
    :param reseller:
        If reseller is None we only inherit from admin packages.
        In that case we ignore all tags in ve.cfg with a "reseller" attribute.
    Nc�h��|�d���ko|�d���kS�Nrru�r)rn�plan_idrus ��r<�is_needed_planz*prepare_setup_data.<locals>.is_needed_plans1������t�,�,��7�c�B�O�O�J�<W�<W�[c�<c�cr;z"/usr/local/directadmin/data/users/z
/user.confr�r�r|zpackage=customzPackage for user with id z� is incorrect, please recover it using Note from https://docs.cloudlinux.com/cloudlinux_os_components/#installation-enabling-and-disablingc�b��|�d���ko|�d��Sr{r|�rnr}s �r<r~z*prepare_setup_data.<locals>.is_needed_plans/������t�,�,��7�[����PZ�@[�@[�<[�[r;rsrrrrmr�rtr=rvrr[r\rurxrwry)�copyrro�get_reseller_packages_mapr\�is_dar�getpwuid�pw_namer��readrr�r�rKrpr7r�r3r�rr�r?)
r}rur��res_pkg_dictr~�user_pwd�filenamer��textrnr�rrrus
``           r<�prepare_setup_datar��s.������;�'�'�J���0�2�2����H��$<�$<��L�Ya�Lb�Ab�Ab�
d�
d�
d�
d�
d�
d�
d�� �"�"�
�v�'9��"�|�F�3�3�H�`�H�DT�`�`�`�H��h��9�9�9�(�Q� �v�v�x�x��(�(�(�(�(�(�(�(�(�(�(����(�(�(�(��� �����D�D�D�����
$�4�/�/��M��(�t�F�t�t�t����
\�
\�
\�
\�
\��	A�	A�B��~�b�!�!�
A�#�F�B��Z�H�H����6�7I�"�RW�^e�7f�7f�7f�ns�t�t�t�u�u�C�(+�J�u�%�%��"�J�	�:�����D������D�"�k�:�>�>�>��6�M�M���r�;�
�C�C�C�C�()�J�u�%���/���o�V�V�V�W�W�B�')�J�t�$�$��"�J�	�:�����D������G�R��j�A�A�A��F�B��Z�@�@�@��F�B��Z�@�@�@��q��H	A�	AsZ�0B7�
B+�B7�+B/�/B7�2B/�3B7�7C�C�(8E!�!E;�:E;�
)G7�7H�Hc	�6�	tjtd|gtjtj���5}|���|jdkcddd��S#1swxYwYdS#t$r tddt�d|�d���YdSwxYw)Nz-l)�stdoutrer���zfailed to run "z -l �")�
subprocess�Popen�UMOUNT�PIPE�communicate�
returncode�OSErrorrf)�pathrFs  r<�
umount_dirr�8s��
I�
�
�
�T�4� ��?��?�
�
�
�	(���������?�a�'�
	(�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(����	(�	(�	(�	(�	(�	(���I�I�I��b�"G�F�"G�"G��"G�"G�"G�H�H�H�H�H�H�I���s4�3A.�A!�A.�!A%�%A.�(A%�)A.�.&B�Bc�R�tj�t��sdSg}	t	tdd���5}|D]?}|���}|r'|�tj|�����@	ddd��n#1swxYwYn(#t$rtddt����YnwxYw|sdStjtj��	t	ddd���5}d�|�
��D��}ddd��n#1swxYwYn'#ttf$rtdd	��YnwxYwd
}t|��D]=}d}|D]1}	|D],}
|
�|	��}|rt#|	��p|}n�-�2|sdS�>dS)zy
    Unmount all paths from /proc/mounts that match regular expressions from /etc/container/exclude_mounts.conf file
    Nr�r�r�r�zfailed to read z/proc/mountsc�B�g|]}|���d��S)r[)r�)rQ�ms  r<r(z"prepare_mounts.<locals>.<listcomp>Zs$��:�:�:�q�a�g�g�i�i��l�:�:�:r;zfailed to parse /proc/mounts�
F)r�r��isfile�EXCLUDE_MOUNTS_CONFr�r��appendr�r�r�rf�unshare�CLONE_NEWNS�	readlinesrr�searchr�)�reg_exp_list�confr�r�r��mounts�ATTEMPTSr&�error�mount�reg_expr�s            r<�prepare_mountsr�Fs����7�>�>�-�.�.�����L�K�
�%�s�W�
=�
=�
=�	=���
=�
=���'�'�)�)���=� �'�'��
�7�(;�(;�<�<�<��
=�	=�	=�	=�	=�	=�	=�	=�	=�	=�	=�	=����	=�	=�	=�	=���
�K�K�K��b�"I�4G�"I�"I�J�J�J�J�J�K���������O�G�'�(�(�(�B�
�.�#��
8�
8�
8�	;�A�:�:�A�K�K�M�M�:�:�:�F�	;�	;�	;�	;�	;�	;�	;�	;�	;�	;�	;����	;�	;�	;�	;����� �B�B�B��b�"@�A�A�A�A�A�B�����H�
�8�_�_�	�	�����	�	�E�'�
�
���N�N�5�)�)����&�u�-�-�6��E��E����	��E�E�	�	�	sl�B�AB�B�B�B�B�B�"C�C�(D1�:D%�D1�%D)�)D1�,D)�-D1�1!E�Ec���d}	tj|dd���n #t$rtd|��YnwxYwt	��t
�d���dS)	zI
    Start LVE engine and initialize default mount namespace for LVE
    z,/bin/mount --make-rprivate / >/dev/null 2>&1Tz	/bin/bash)�shell�
executablezError: failed to executezCan`t init lve default settings�rhN)r��callr�r`r�r��	lve_start)�	MOUNT_CMDs r<r�r�js���?�I�5���	��+�F�F�F�F�F���5�5�5�
�(�)�4�4�4�4�4�5��������	�O�O�=�O�>�>�>�>�>s��9�9c�H�t�|d|�d�|���dS)zs
    Create LVE container for given ID
    :type lve_id: int
    :type ignore_error: bool
    :return: Nothing
    z!lvectl: Can`t create lve with id �; error code {code}�rh�ignore_errorN)r��
lve_create)r�r�s  r<r�r�ws>��
���V�^��^�^�^�".��0�0�0�0�0r;c�H�t�|d|�d�|���dS)zs
    Create LVP container for given ID
    :type lvp_id: int
    :type ignore_error: bool
    :return: Nothing
    z!lvectl: Can`t create lvp with id r�r�N)r��lve_lvp_create)r:r�s  r<�
lvp_creater��s>��
����!b�V�!b�!b�!b�&2��4�4�4�4�4r;c���td��}g}t��D]M}|�d|��t�|d|�d����|�|���N|S)N�	lvectllibz*destroy_lvp_all: destroying LVP with id %s�"lvectl: Can`t destroy lvp with id r�r�)rrHr)r��lve_lvp_destroyr�)�logger�destroyed_listr:s   r<�destroy_lvp_allr��s���
"�;�
/�
/�F��N��*�*�&�&�����A�6�J�J�J�
���f�&h�6�&h�&h�&h�	�	j�	j�	j����f�%�%�%�%��r;c��td��}|dkrt��dS|�d|��t�|d|�d����dS)Nr�rHz&lvp_destroy: destroying LVP with id %sr�r�r�)rr�r)r�r�)r:r�s  r<�lvp_destroyr��sz��
"�;�
/�
/�F�
����������
�L�L�9�6�B�B�B�	���&�"d�v�"d�"d�"d��f�f�f�f�fr;c���td��}d|�d�}d|�d�}d}|dkr�t���rtt	����}tt
tj�������dkrVtj���D]6}|�	d	|��t�
||�
��d}�7n�d}n�tj�|��s=tj���rTtj�
|���5|�	d|��t�
||�
��d}n!tj�|��sd}|r!trtt d
g��dSdStrtt"d|g��dSt%d|����dS)Nr�zCan't remove lve z from kernel - error code -3zCan`t destroy lve with id r�FrHrz*lve_destroy all: destroying LVE with id %sr�Tz&lve_destroy: destroying LVE with id %s�OK�WARNz	warning: )rrDrE�boolr�r��listrF�lve_id_listr)r��check_inside_list�resellers_supported�detect_inside_lvpr^r_�
SINGLE_FORMATrlr`)r�r��cant_remove_msg�cant_destroy_msg�	destroyed�id_s      r<r�r��s���
"�;�
/�
/�F�O�6�O�O�O�O�Q�F�Q�Q�Q���I�
�����'�'�)�)�	0��_�.�.�/�/�I��t�C�H�(�(�*�*�+�+�,�,�q�0�0��x�+�+�-�-�
!�
!�����I�3�O�O�O�����-=��>�>�>� �	�	�
!�
�I�I��8�%�%�f�-�-�	���-�-�/�/�	�47�H�4N�4N�v�4V�4V�4b��L�L�A�6�J�J�J��O�O�F�,<�O�=�=�=��I�I���+�+�F�3�3�	��I��1��	/��
��v�.�.�.�.�.�	/�	/��	1���v��&?�@�@�@�@�@��/�o�/�/�0�0�0�0�0r;c	�0�|r.tj�|��st|��t	j��5t	jd��	ttd��t_
ttd��t_ttd��t_ttd��t_
ttd��t_tdkrHttd��t_ttd	��t_td
kr$ttd��t_|rS|dkr&t&�|td
|�����nut&�|td|�d����nN|dkr"t&�td���n&t&�|td|�d����nJ#t.$r=}t1ddt3|��zdzt3|��z��Yd}~nd}~wwxYwddd��dS#1swxYwYdS)Nr�rtrrrsrvrur]rwrxr�ryrz%Can`t setup default settings for LVP r�zCan`t setup lvp with id r�zCan`t setup default settingszCan`t setup lve with id r[zCan`t setup lve z. RuntimeWarning excepted: )rDrF�	exist_lvpr��warnings�catch_warnings�filterwarningsr7ror�r/r-r.�	ls_memoryr0r�r1r2r3r��lve_set_default�
lve_lvp_setupr��RuntimeWarningrfr8)r�r:�rws   r<r�r��s���
��c�h�(�(��0�0���6����	�	 �	"�	"�p�p����(�(�(�	p�!$�Z��%5�!6�!6�L��"%�j��&7�"8�"8�L��#&�z�&�'9�#:�#:�L� �%(��E�):�%;�%;�L�"�%(��D�)9�%:�%:�L�"��Q���-0��F�1C�-D�-D��*�(+�J�w�,?�(@�(@��%��Q���'*�:�f�+=�'>�'>��$��
l��Q�;�;��)�)���6f�^d�6f�6f�*�h�h�h�h��'�'���6n�QW�6n�6n�6n�(�p�p�p�p��1����%�%�l�<Z�%�[�[�[�[�����L�2j�V�2j�2j�2j� �l�l�l����	p�	p�	p�!�!�$6��V���$D�Gd�$d�gj�km�gn�gn�$n�o�o�o�o�o�o�o�o�����	p����7p�p�p�p�p�p�p�p�p�p�p�p����p�p�p�p�p�ps=�J�GH4�3J�4
I;�>3I6�1J�6I;�;J�J�Jc��d}d}t}td��t}|at|t��rL	||d}|sd}n#t$rd}YnwxYw	||d}|sd}n#t$rd}YnwxYw||fS)z�
    Get pair of package, reseller for lve_id
    :param lve_id: lve_id, UID with package, reseller
    :return: tuple of (package, reseller); Both can be None
    N�
list-usersrurQ)�packages_users�GetControlPanelUsersr#�dict�KeyError)r�rQru�old_packages_users�temp_packages     r<�"get_package_and_reseller_by_lve_idr��s����G��H�'����&�&�&�"�L�'�N��,��%�%��	�#�F�+�J�7�H��
 ������	�	�	��H�H�H�	����	�"�6�*�9�5�G��
������	�	�	��G�G�G�	�����H��s#�A
�
A�A� A3�3B�Bz+/var/run/cloudlinux/effective-normal-limitsz;/opt/cloudlinux/flags/enabled-flags.d/burstable-limits.flagc�t�tj�t��sdS	tj�t
��}tj�|��s2tj�|��}t|ddd|��n(#t$r}td|�d|�����d}~wwxYwtt
�d���}|5|ri}n�	tj�t
��rDtt
dd�	��5}tj|��}ddd��n#1swxYwYni}nZ#tj$r }td
t
�d|�����d}~wt$r }tdt
�d|�����d}~wwxYw|�t"��	t%tj|��t
d��n1#t$r$}tdt
�d|����Yd}~nd}~wwxYwddd��dS#1swxYwYdS)
ai
    Save calculated effective normal limits for an LVE to a cache file.

    The cache file is stored in /var/run (tmpfs) for faster operations.
    It disappears on reboot, but lvectl apply all runs in the lvectl systemd service anyway,
    so the file will always reappear if the service functions normally.

    If additional speed is desired, consider replacing the standard JSON lib
    with a faster one like rapidjson or orjson.

    :param reset: If True, recreate the cache file from scratch with data available in the dict,
    instead of updating it, defaults to False
    :type reset: bool, optional
    Ni�rz#Error: failed to create the folder z: z.lockr��utf8r�z=Error: failed to parse the effective normal limit cache file z<Error: failed to read the effective normal limit cache file z<Error: failed to save the effective normal limit cache file )r�r��exists�BURSTABLE_LIMITS_FLAG_FILE�dirname�EFFECTIVE_CACHE_FILE�isdirr"r�r`rr�r�r��load�JSONDecodeErrorr?�CACHED_EFFECTIVE_LIMITSr#r)�reset�
effective_dir�var_run_dirrX�effective_cache_lock�effective_limits�readfiles       r<�write_effective_cacher�s��0�7�>�>�4�5�5���������(<�=�=�
��w�}�}�]�+�+�	G��'�/�/�-�8�8�K��m�U�A�q�+�F�F�F�������
�H�M�H�H�Q�H�H�I�I�I�
���������(�+?�(F�(F�(F�G�G��	�n�n��	�!���
��7�>�>�"6�7�7�*��2�C�&�I�I�I�?�X�+/�9�X�+>�+>�(�?�?�?�?�?�?�?�?�?�?�?����?�?�?�?��(*�$����'�
�
�
��q�Vj�q�q�no�q�q�r�r�r�������
�
�
��p�Ui�p�p�mn�p�p�q�q�q������
����	��� 7�8�8�8�	n�#�D�J�/?�$@�$@�BV�X]�^�^�^�^���	n�	n�	n��l�Qe�l�l�ij�l�l�m�m�m�m�m�m�m�m�����	n����)n�n�n�n�n�n�n�n�n�n�n�n����n�n�n�n�n�ns��A5B�
C�(B>�>C�H-�&;E�!E�6E�E	�E�	E	�
E�H-�F(� E;�;
F(�F#�#F(�(H-�(G/�.H-�/
H�9H�H-�H�H-�-H1�4H1c�>�ttt|��<dS)a�
    Cache the calculated effective normal limits for an LVE to a dictionary.

    setup_data is a global dict that contains the LVE configuration last applied.
    Holds only one entry - during operations like lvectl apply all, we enter this
    function repeatedly with setup_data containing info for one processed LVE each time.

    These limits can be used by the Burstable Limits components
    to set/reset burst or normal limits without having to go
    through effective limit calculation or invoking lvectl as a middleman.
    Comparing the cached limits to current active limits (in kernel)
    also shows whether or not the LVE has burst limits active at the moment.

    This function gets called in lve_apply, which means it also runs within:
    * lvectl apply all
    * lvectl apply-many
    * lvectl set
    * and others, that also call lve_apply

    :param lve_id: LVE ID for which the limits are being saved.
    :type lve_id: int
    N)ror�r8�r�s r<�cache_effective_limitsr�^s��0,6��C��K�K�(�(�(r;c�H��t}�dkrtd���t|c}atdkrt|���d}|�|}n1|rtnt
}	t
t�fd�|	��d��}|dkrR	|�d}n#ttf$rd}YnwxYw	|�d}n#ttf$rd}YnwxYw|���t||�	��td
|tt��}
	tt|dd�
��|
���td<n#t t"t$f$rYnwxYwtd�tdtd<td|tt��t&dkrtd|tt��n
dtd<	t)t|dd�
����td<n#t t"t$f$rYnwxYwtd|tt��td|tt��td|tt��n't||����t&dkr
dtd<|s"t+���t-�|���dSdS)a

    Aplly limits to LVE lve_id
    :param lve_id: lve id
    :type lve_id: int
    :param plan_id: package for user with lve_id. deprecated
    :type plan_id: string
    :param result: if True = don't apply limits. only create setup_data with actual limits
    :type result: boolean
    :param reseller: if True = plan_id is resellers plan. deprecated
    :type reseller: boolean
    :param out_node: node with limits for lve_id
    :type out_node: xml_node
    :param lvp_id: reseller container id; host container if 0
    r�useridr|�r:Nc�,��t|����kS)N�r�r
)rr�s �r<�<lambda>zlve_apply.<locals>.<lambda>�s��� 3�4� H� H� H�F� R�r;rQru�rursrrrrmr�rtr=rvr[r\rurxrwry)rur�r})r�r�rrkrJrI�next�filter�	NameErrorr�r�rprror�r3r�rr�r?r7r�r�)r�r}rmru�out_noder:r��new_packages_usersrn�	node_listr�s`          r<�	lve_applyr{s����$(��
��{�{��X�v�.�.�.�-;�=O�*��N�
��|�|��6�"�"�"�"�	
�B���
���$�0�F�F�&�	�
��R�R�R�R�T]�^�^��
�
����{�{�	�(��0��;�G�G���8�$�	�	�	��G�G�G�	����	�)�&�1�*�=�H�H���8�$�	�	�	��H�H�H�	����
�~��7�X�6�6�6�6��F�B��Z�@�@��	� 8�9K�B�TY�`g�9h�9h�9h�pu� v� v� v�J�u�����J�	�2�	�	�	��D�	�����e��$� +�E� 2�J�u���D�"�k�:�6�6�6��&�=�=���r�;�
�;�;�;�;� !�J�u��	�"�#5�b�g�O�#\�#\�#\�]�]�J�t�����J�	�2�	�	�	��D�	����	�G�R��j�9�9�9��F�B��Z�8�8�8��F�B��Z�8�8�8�8�	�7�X�f�E�E�E�E��'�>�>� !�J�u���)��v�&�&�&��&��(�(�(�(�(�(�)�)sH�B�B(�'B(�,B;�;C�C�)D/�/E	�E	�='G%�%G?�>G?c�h�t|t|��dz
g���}t|��dS)zt
    Print data with the last column 30 symbols wide.
    Useful for printing data that contains package names.
    r[��wide_indicesN��_format_fieldsr�r`�r��formatted_strings  r<�_pprintr�s9��
&�f�C��K�K�!�O�;L�M�M�M��	�
�����r;c��t|t|��dz
t|��dz
g���}t|��dS)z�
    Print data with the two last columns 30 symbols wide.
    Useful for printing full data of every user with package name
    and reseller name.
    r�r[r	Nrr
s  r<�	_pprint_fr�sO��&���&�k�k�A�o�s�6�{�{�Q��7�����
�
�����r;c�H�t|dg���}t|��dS)zb
    Print data with the first column 30 symbols wide.
    Useful for printing packages data.
    rr	N)rr`r
s  r<�	_pprint_pr�s.��
&�f�A�3�?�?�?��	�
�����r;c�j�t|dt|��dz
g���}t|��dS)z�
    Print data with the first and last columns 30 symbols wide.
    Useful for printing data with user names and package names.
    rr[r	Nrr
s  r<�	_pprint_rr�s;��
&�f�A�s�6�{�{�Q��;O�P�P�P��	�
�����r;�r�r
�widthc���g}t|��D]J\}}t|��}||vr|�|d|������3|�|d����Kd�|��S)aK
    Helper function to format fields
    based on specified indices for wide columns.

    Args:
        fields: The fields to format.
        wide_indices: List of indices in the fields that should be wide.
        width: The width of the wide columns.

    Returns:
        A formatted string with specified fields widened.
    �>z>8r|)�	enumerater8r��join)r�r
r�formatted_fieldsr��field�	field_strs       r<rr�s�����!�&�)�)�7�7���u���J�J�	��L� � ��#�#�y�$;�5�$;�$;�$;�<�<�<�<��#�#�y�$5�$5�6�6�6�6�
�7�7�#�$�$�$r;c���t|��}t|t��o|�d��}|�dd��}|rt	|��}|dz}nd}|rd|��n|}|S)z�
    Convert pmem or vmem limits to bytes value

    :param value: pmem or vmem limits in kbytes value
    :return: bytes value of limit

    r�r|ir)r8r#�
startswithr�r7)rg�was_changeds  r<�_pmem_vmem_to_bytes_valuer"s���
��J�J�E��U�C�(�(�B�U�-=�-=�c�-B�-B�K��M�M�#�r�"�"�E����E�
�
��
��
�����&�1�K��K�K�K�E�E��Lr;c��d}d}t|t��r!|�d��r|dd�}d}	t|��}n#t$rYdSwxYw|rd}|dzdz}|d	kr|�|�d
�}n
|�|dz�d�}|S)z�
    Convert amount of RAM to M format

    :param string value: amount of memory in KB
    :rtype: string
    :return: amount of memory in MB like "1234M"
    r|Fr�r[NTr{rqr�M�K)r#r8r r7r�)rgrmr!�vs    r<�_mb_memr'-s����F��K��%�����%�"2�"2�3�"7�"7���a�b�b�	�������J�J���������r�r���������
��E�T�M�E��q�y�y��$�E�$�$�$����$�A��E�$�$�$���Ms�A�
A�A�0rc�.�
��t����
�
ddz�
d<�
fd�}|d��dkrt|d����nd}d�}|t|d����t|��t|d����t||d������t||d������t|d	����t|d
����t|d����t|d����|d
��g}t	��}|�||z
}t
r)d��fd�|D����}	d|	�d�g}n||�|�fd�|D���|S)z^
    Generate header and default package data either as print to stdout or as json string
    rrrzc�0����|d��S)Nr|�r�)rfrAs �r<�get_dataz_formatter.<locals>.get_dataPs����|�|�C��$�$�$r;r|c�L�trt|��nt|��SrP�r�r"r'�rgs r<�convert_mem_limitsz&_formatter.<locals>.convert_mem_limitsUs!��3=�Q�(��/�/�/�7�5�>�>�Qr;rsrwrvrurxrtry)r�r�r��NCPUr�r�r�r�r�r��PACKAGEN�,c3�R�K�|]!}d|�d��|d���d�V��"dS)r��":"r|Nr+�rQr��
fields_maps  �r<rTz_formatter.<locals>.<genexpr>dsE�����K�K�q�:�A�:�:�*�.�.��B�"7�"7�:�:�:�K�K�K�K�K�Kr;�{�}c�<��g|]}��|d����S�r|r+r6s  �r<r(z_formatter.<locals>.<listcomp>hs'���8�8�8�A�*�.�.��B�'�'�8�8�8r;)rr�r�r8r�r^r)�printer�
default_id�default_package�more_fieldsr,�_cpur0�resr�r�rAr7s          @@r<�
_formatterrBHs��������!�!�H��u�o��,�H�U�O�%�%�%�%�%�19�����2�0E�0E��H�H�U�O�O�,�,�,�2�D�R�R�R��3�x�x����#7�#7��4�y�y�#�h�h�v�&6�&6�"7�"7��&�&�x�x��'7�'7�8�8�9�9�3�GY�GY�Zb�Zb�ch�Zi�Zi�Gj�Gj�Ck�Ck��(�(�4�.�.�!�!�C����0A�0A�,B�,B�#�h�h�W[�n�n�J]�J]��H�H�V�$�$�%�%�/���J�
�C�
�\�\�F����+����:��x�x�K�K�K�K�F�K�K�K�K�K���D�}�}�}�o����������8�8�8�8��8�8�8�9�9��Jr;c������fd�}|S)aA
    Generate inner function with closured fields names and printer function

    :param list fields: List of strings that represent names of fields in final output
    :param callable printer: Function to format and print data for every entry
    :rtype: callable
    :return: function to format data for every user
    c����d�t|d}t|d}|dkrd}tdkrt|d���n5|�t|d���nt	d�	��t||���tjt���t|||d
����fd�}d
�}t|���d<t|d�����d<t|d�����d<t||d�������d<t||d�������d<t|d�����d<t|d�����d<t|d�����d<t|d�����d<trt|���d<n|�d<|�trdnd�d<n|�d<d�dvr?dtt�d�d����dz��z�d<nt�d��dz�d<�d�d<tt�d�����d<g}tr)d��fd��D����}d|�d�g}n�	�fd ��D���|S)!z�
        :param string user: Find and format data for this User ID
        :rtype: list
        :return: List of given user's statistics data line or empty list
        r|rQruNr[rF)rvrrT)r}rurmc����t�|��tt|��krdtt|��znt�|��S)Nr�)r8ro)rfr�s �r<�
check_changedz7_user_formatter.<locals>.wrapper.<locals>.check_changed�sP���14�T�#�Y���3�z�RU��CW�CW�1W�1W�3��Z��_�-�-�-�-�]`�ae�fi�aj�]k�]k�kr;c�L�trt|��nt|��SrPr.r/s r<r0z<_user_formatter.<locals>.wrapper.<locals>.convert_mem_limits��!��7A�U�,�U�3�3�3�w�u�~�~�Ur;rrrrsrwrv�vmemrurtrxry�N/Ar�rzr�r3c3�Z�K�|]%}d|�d�|����d�V��&dS)r�r5N��lower�rQr�r�s  �r<rTz3_user_formatter.<locals>.wrapper.<locals>.<genexpr>�sC�����I�I�Q�8��8�8�d�1�7�7�9�9�o�8�8�8�I�I�I�I�I�Ir;r8r9c�D��g|]}�|�����Sr:rLrNs  �r<r(z4_user_formatter.<locals>.wrapper.<locals>.<listcomp>�s%���6�6�6�!�d�1�7�7�9�9�o�6�6�6r;)r�rNr�rxrkr�rorr8r^�_normalize_strr7r�r�r)
rrQrurFr0rAr�r�r�r<s
       @��r<�wrapperz _user_formatter.<locals>.wrapperus5������ ��&�y�1��!�$�'�
�3���r�>�>��H��Q���
�w��6�6�6�6�6��#�)��E�R�R�R�R�R���7�7�7�7��w��:�:�:�:��y��$�$���$��(�4�H�H�H�H�	l�	l�	l�	l�	l�	V�	V�	V���Y�Y��T�
��-�-��.�.�/�/��U���=�=��0�0�1�1��V���-�-�m�m�F�.C�.C�D�D�E�E��V���-�-�m�m�E�.B�.B�C�C�D�D��V�����t�,�,�-�-��T�
����t�,�,�-�-��T�
��M�M�'�2�2�3�3��W�
��=�=��0�0�1�1��V���	&�,�W�5�5�D��O�O�%�D��O���(,�4�u�u�"�D����'�D����$�u�+����#�c�$�u�+�*<�*<�S�*A�*A�&B�&B�c�&I�"J�"J�J�D��K�K��d�5�k�*�*�c�1�D��K��U���W�
��*�4��=�9�9�:�:��U�����	8��8�8�I�I�I�I�&�I�I�I�I�I�D� ��=�=�=�/�C�C��G�6�6�6�6�v�6�6�6�7�7��
r;r:)r�r<rQs`` r<�_user_formatterrRls0����D�D�D�D�D�D�J�Nr;c���t��	td��|�|rtd|��n#|�td|���ntd��n#t$rYnwxYwdg}tt|���}t��|z}t
|��}tD]}|||��z
}�tr*tdd�
|��zd	z��dSdS)
Nr�r��list-reseller-usersrr2�r?�	{"data":[r3�]})rkr�rrBrr�rRr�r^r`r)r�rur?rmr��	formatterrs       r<�paneluserslimitsrY�s���M�M�M�
��\�*�*�*���&�� ��6�2�2�2�2�
�
!� �!6��J�J�J�J�J� ��.�.�.����
�
�
���
�����+�K�
��[�
9�
9�
9�F�
�\�\�K�
'�F���'�'�I��"�"���)�)�D�/�/�!����5�
�k�C�H�H�V�,�,�,�t�3�4�4�4�4�4�5�5s�AA�
A%�$A%c���td��g}t���D]6\}}|�t	|��|d|df���7|S)z@Get list of tuples[lve_id, reseller, package] from control panelr�rurQ)r�r�rdr�r7)rm�str_uid�payloads   r<�paneluserslistr]�sj����&�&�&�
�F�*�0�0�2�2�O�O�����
�
�s�7�|�|�W�Z�%8�'�)�:L�M�N�N�N�N��Mr;c���ddlm}i}td��tt�����||<td��|�t��|S)zBGet dict of pairs[provider, list[package_name]] from control panelr)�DEFAULT_PROVIDER�
list-packages�list-resellers-packages)�
clveconfigr_r�r�r��keysr?)r_�packagess  r<�panelpackagesdictre�sp��,�+�+�+�+�+��H���)�)�)�!%�n�&9�&9�&;�&;�!<�!<�H�
���2�3�3�3�
�O�O�N�#�#�#��Or;c�T�t��td��ttddg���}t	��ddgz}t|t���}tD]}|||��z
}�tr*tdd�	|��zdz��d	Sd	S)
z]
    Implements lvectl all-user-list command

    :return: None, prints result to stdout
    r�r2�RESELLERrU)r<rVr3rWN)
rkr�rBrr�rRr�r^r`r)rmr�rXrs    r<�all_users_limitsrh�s����M�M�M���&�&�&�
�	�	�:�/F�
G�
G�
G�F�
�\�\�Y�
�3�
3�F���	�:�:�:�I��"�"���)�)�D�/�/�!����5�
�k�C�H�H�V�,�,�,�t�3�4�4�4�4�4�5�5r;c���tj��dkr8d�t��D����fd�|���D��}|S)z�
    Filtering DirectAdmin's admins for `lvectl apply all` command

    :param ve_dict: dict with LVE
    :return: filtering dict

    �DirectAdminc�@�g|]}tj|��j��Sr:)rrr)rQrs  r<r(z(_filtering_da_admins.<locals>.<listcomp>s%��I�I�I��#�,�t�,�,�3�I�I�Ir;c�$��i|]\}}|�v�	||��
Sr:r:)rQrfrg�uids_da_adminss   �r<�
<dictcomp>z(_filtering_da_admins.<locals>.<dictcomp>s)���]�]�]�*�#�u�3�n�C\�C\�3��C\�C\�C\r;)r\�	getCPNamer
rd)�ve_dictrms @r<�_filtering_da_adminsrq
sV�������-�/�/�I�I����I�I�I��]�]�]�]��
�
���]�]�]���Nr;c���
�	t��tt��}|rHtjj����
�
fd�t���D��}n#t$ri}YnwxYw|durt}t��}n't}tj�
|���}i}|D]}|dd�|t|���<�|D]}||vrddd�||<�t|��dkrjt!|��}|D]X}||vrd}n||d}||}t#|��}	t|	��dkr||	dd�||<�P|dd�||<�Y|S)Nc�$��i|]\}}|�v�	||��
Sr:r:)rQ�kr&�cfg_lvp_id_lists   �r<rnz&prepare_apply_data.<locals>.<dictcomp>"s)���_�_�_���1�!��J^�J^�q�!�J^�J^�J^r;Tr�)rrur�rr)r�r�r�rDrSrL�id_listrdrrJrHrIrFr�r
r�rq�guess_reseller_by_package)r:�packages_users_rrvrprr�rS�pkg�	resellersrus          @r<�prepare_apply_datar{s����������~�.�.���	`�!�g�.�6�6�8�8�O�_�_�_�_��0D�0D�0F�0F�_�_�_�O�����������������~�~��	��*�*����	��(�&�&�f�&�5�5���G��_�_��FJ�Y]�<^�<^��#�4�8�8�8�9�9��?�?���� � �'+��>�>�G�F�O���O����!�!�'�w�/�/��"�	B�	B�C��'�!�!�����s�|�F�+��!�#�&�C�1�#�6�6�I��9�~�~��!�!�)-�I�a�L�I�I�����)-�D�A�A������Ns�A,A0�0A?�>A?c�0�t��\}}ttj�����}|���D]�}|�|d��dkr|�|���2||vr/t�|��|�|��tj	�
d���5t|||d||d���ddd��n#1swxYwY��td���t����r�td��}ttj�����}|���D�]E}||vr/t�|��|�|��t#||��tj�|��}tj���}t(j�|��D]�}	t/j|	��j}||vr/t�|��|�|��|�|d��|kr)tj	�||d|�d	|�d
����t||�������G|D]}t�|���|D]}t�|���dS)
NrT�r�rru�rrur��Can`t move lve_id=� to lvp_id=r�r��r�ru)�_get_lve_ve_dict_and_lvp_map�setrDrFr�rcr��discardr�r*�context_ignore_errorrrEr{rGr�r��&_create_if_necessary_and_configure_lvprS�get_reseller_namer��cpapirrrr�lve_lvp_move)
�lve_ve_dict�lve_lvp_map�remaning_alive_lvesr��lvp_ve_dict�remaining_alive_lvpsr:rV�kernel_mappingrs
          r<�lve_destroy_and_recreate_allr�LsX��;�=�=��K���c�h�2�2�4�4�5�5���"�"�$�$�����?�?�6�1�%�%��*�*��'�'��/�/�/���(�(�(��O�O�F�#�#�#��'�'��/�/�/�
�V�
(�
(�d�
(�
;�
;�	�	���$�V�,�V�4�$�V�,�Z�8�
�
�
�
�	�	�	�	�	�	�	�	�	�	�	����	�	�	�	���Q�����
�#�#�%�%�*�(��.�.��"�3�8�#7�#7�#9�#9�:�:��"�&�&�(�(�	A�	A�F��-�-�-��%�%�f�-�-�-�$�,�,�V�4�4�4�2�;��G�G�G� �G�5�5�f�=�=�M� �X�\�\�^�^�N� ��5�5�m�D�D�
A�
A����d�+�+�2���0�0�0��O�O�F�+�+�+�'�/�/��7�7�7�!�%�%�f�a�0�0�F�:�:��F�'�'��� e�V� e� e�� e� e� e�(����
��-�@�@�@�@�@�
A�+�	*�	*�F��!�!�&�)�)�)�)�&� � ���������� � s�+D
�
D	�D	c��t��\}}tj�d���5|���D]F}|�|d��dkr*t
|||d||d����G	ddd��n#1swxYwYt
d���t���sdStd��}tj	�
��}|���D]�}t||��tj
�|��}tj�|��D]o}t!j|��j}|�|d��|kr)tj�||d|�d	|�d
����t
||����p��dS)
NTr}rrrur~r�rr�r�r�r�)r�rDr*r�rcr�rrEr{rFrSr�r�r�r�rrrrr�)rpr�r�r��lvp_id_rVr�lve_id_s        r<�
lve_apply_allr��s)��7�9�9��G�[�	��	$�	$�$�	$�	7�	7�j�j��l�l�n�n�	j�	j�F����v�q�)�)�Q�.�.��&�7�6�?�6�+B�W�U[�_�]g�Mh�i�i�i�i��	j�j�j�j�j�j�j�j�j�j�j�j����j�j�j�j�
�Q������'�'�)�)���� ��&�&�G��X�\�\�^�^�N��<�<�>�>�>�>��.�w��@�@�@���1�1�'�:�:�
��N�1�1�-�@�@�		>�		>�D��l�4�(�(�/�G��!�!�'�1�-�-��8�8���#�#���c��c�c�W�c�c�c�$����

�W�}�=�=�=�=�=�		>�>�>s�AB�B�"Bc���t��td��t��}t���r&tt�����ni}||fS)Nr�)rkr�r{rDrEr��lve_id_lvp_id_pairs)r�r�s  r<r�r��s_���M�M�M���&�&�&�$�&�&�K�58�5Q�5Q�5S�5S�[�$�s�.�.�0�0�1�1�1�Y[�K���#�#r;c���t|dd���t|||d||d|���t|d���td|�	��dS)
NTF)r:r;rjrru)r�rrur:)r:rjr)r�r:)rkr)rpr:s  r<r�r��s{���v�D�u�M�M�M�M�
�V��v��v�.��v��z�2������
�v�E�:�:�:�:�
�Q�v�&�&�&�&�&�&r;c���td��t|���tD�]F}t|���|k�r.tj�|��}|dd�D]�}t�d|��	tj
|���3#t$rQtj�
|��rt�|��|�|��Y��wxYwt!|��|j�|��t't(��td���|D]}t+|���t|���dS��HdS)z,Remove reseller from ve.cfg and from procfs.Tr�r�NrF)rhrkrJr
rDrF�map_lve_id_listr�r�rr�r�r*�
lve_existsr��remover��
parentNode�removeChildrrr)r:rn�usersr�s    r<�_remove_resellerr��s����D�����v����������b�1�1�1�V�;�;��H�,�,�V�4�4�E�����(�
)�
)���"�"�1�f�-�-�-�)��L��(�(�(�(���)�)�)��v�(�(��0�0�0�����/�/�/��L�L��(�(�(�(�(�)����
������M�%�%�b�)�)�)��V�����q�!�!�!�!��
"�
"���&�!�!�!�!��v�&�&�&�&��4�4�)<�*�5s�B�AC6�5C6c���t|��rtj|���dStr+t	ddd|��g��tjd��dStd|����dS)z&Disable reseller limits and call hooksrr%r>zno configuration found for LVP r�z(warning: no configuration found for LVP N)r�r�throw_eventr^r_rarbr`)rVr:s  r<�disable_reseller_limitsr��s�������G�%�1�=�I�I�I�I�I�I��	G���)�-W�v�-W�-W�!X�Y�Y�Y��H�R�L�L�L�L�L��E�V�E�E�F�F�F�F�Fr;c���td��t��d}tD]�}t|���|krkd}t	|��t|��|j�|��tt��t��t|����|sFtr+tddd|��g��tjd��dStd|����dSdS)	NTFr�r%r>zno configuration found for VE r�z'warning: no configuration found for VE )rhrkrIr
r�r�r�r�rrrr^r_rarbr`)r��Deletedrns   r<�
lve_deleter��s
���D�����M�M�M��G������b�1�1�1�V�;�;��G�������v�����M�%�%�b�)�)�)��V�����M�M�M��f������F��	F���)�-V�f�-V�-V�!W�X�X�X��H�R�L�L�L�L�L��D�F�D�D�E�E�E�E�E�F�Fr;c���tj�d��s?trt	dddg��ntd��t
jd��dSdS)N�/proc/lve/enterr%r>zenter by name not supportedz$warning: enter by name not supportedr�)r�r�r�r^r_r`rarbr:r;r<�lve_enter_checkr�sd��
�7�>�>�+�,�,���	:���)�-J�!K�L�L�L�L��8�9�9�9����������r;c���t��	||���z}tddd���5}|�|��ddd��dS#1swxYwYdS#t$rYdSwxYw)Nr��wr�r�)r�r�r�rgr)�signrRr
r�s    r<�enter_applyr�s�������
��V�\�\�^�^�#��
�#�S�7�
;�
;�
;�	�q�
�G�G�C�L�L�L�	�	�	�	�	�	�	�	�	�	�	�	����	�	�	�	�	�	���
�
�
����
���s4�)A)�A�A)�A � A)�#A �$A)�)
A7�6A7c�N�t��trTd}d}tD]2}|�d��}|r|d|zdzz
}d}�'|d|zdzz
}�3|dz
}t	|��dSt	d��tD]$}t	|�d�����%dS)	NrVTr�r�Fz,"rW�Binaries)rkr^rLrr`)rm�firstrnr�s    r<�
list_binariesr�s����M�M�M��+������	,�	,�B��?�?�6�*�*�D��
,��#��*�s�*�*������$��+��+�+����$���
�f�
�
�
�
�
�
�j�����	+�	+�B��"�/�/�&�)�)�*�*�*�*�	+�	+r;c�|�t��tD]%}td|�d�����&dS)N�+r�)rkrLr�r)rns r<�
load_binariesr�)sC���M�M�M��2�2���C�����0�0�1�1�1�1�2�2r;c��t��tddd���5}|D]}td|���	ddd��n#1swxYwYt��dS)Nr�r�r�r��-)r�r�r�r�)r�r�s  r<�reload_binariesr�/s�������	
���w�	7�	7�	7�#�1��	#�	#�D���T�"�"�"�"�	#�#�#�#�#�#�#�#�#�#�#�#����#�#�#�#��O�O�O�O�Os�A�A�Ac���td��t��t��d}tD]i}|�d��|krNd}td|��|j�|��tt��t���j|sEtrtddd|��g��ntd|����tjd	��dSdS)
NTFr�r�r%r>zno configuration found for z$warning: no configuration found for r�)rhr�rkrLrr�r�r�rrr^r_r`rarb)rR�deletedrns   r<�
del_binaryr�7s����D���������M�M�M��G�����
�?�?�6�"�"�f�,�,��G���V�$�$�$��M�%�%�b�)�)�)��V�����M�M�M�����	C���)�-S�6�-S�-S�!T�U�U�U�U��A��A�A�B�B�B����������r;c��td��t��tD]}|�d��|krdS�t	d|��t
�d��}|�d|��t�	|��tt
��t��dS)NTr�r�rR)rhrkrLrr�rrrrMrr)rRrn�bin_xmls   r<�
set_binaryr�Ls����D�����M�M�M�����
�?�?�6�"�"�f�,�,��F�F�-���V�����"�"�8�,�,�G������(�(�(�� � ��)�)�)��V�����M�M�M�M�Mr;c�R���	|rt}n|rt}nt}��fd�|D��d}n#t$rYdSwxYw|rR�dD]G}|dkrt	|dd��}nt	||d��}|r|j�|���HdStt���dz
}|D]�}	|	dkr\t|�
d����dkr6|	�vr1|�
d��d�d���|	<�dt|�
|	����dkr5|	�vr1|�
|	��d�d���|	<��|rt�d	��dSt�d	��dS)
a�
    Set given lve or package to default values for given parameters

    :param dict set_data: Arguments of lvectl call
    :param bool package_flag: Should we delete package or lve with given id
    :param callable is_needed: Function that takes xml element and set_data dict and returns
                               whether current xml element contains info about needed ID from set_data
    c�,��g|]}�|����|��Sr:r:)rQrX�	is_needed�set_datas  ��r<r(z#lve_set_default.<locals>.<listcomp>ls*���
8�
8�
8�A���1�h�!7�!7�
8�a�
8�
8�
8r;rN�set-defaultrur[r\r�ve_id)rKrJrIrr0r�r�r��LIMITS_LIST_NAMEr�rr�plan_deleter�)
r��package_flagr�r:r�rn�tag_�n�to_keeprs
` `       r<r�r�\s�����	��	��D�D�
�	��D�D��D�
8�
8�
8�
8�
8��
8�
8�
8��
;���������������
�	��]�+�	,�	,�D��t�|�|�$�R��/�B�B���$�R��w�7�7���
,���(�(��+�+�+�����"�#�#�h�}�&=�=�G��Z�Z���D�=�=�S��!8�!8��!A�!A�B�B�Q�F�F��H�$�$�"$�"9�"9�'�"B�"B�1�"E�"R�"R�Sb�"c�"c�����
��(�(��/�/�
0�
0�1�
4�
4��H�$�$�"$�"9�"9�%�"@�"@��"C�"P�"P�QX�"Y�"Y������&��H�W�%�&�&�&�&�&��8�G�$�%�%�%�%�%s�05�
A�Ac��|dvrdSt|��}|dvrdS	t|��}n#t$rg}YnwxYw||vrdSdS)a
    Checks is uid owned by reseller
    :param uid: uid for check
    :param reseller_name: Reseller name, None treats as root
    :return: True - valid reseller/user pair, False - else
        Special case:
          if reseller_name is None (root) - always valid
    )N�rootT)r�rJF)rrr)rSrV�username�reseller_users_lists    r<�_check_reseller_user_pairr��s�����&�&��t�(��,�,�H��?�"�"��u�!�,�]�;�;�����!�!�!� ����!�����&�&�&��t��5s�-�<�<c
�$�|dkr0|�dd��}|d}t||��sdStd��|rN||dkrBtj�|��rt
|d���ntt
|���nc|dkrMt���r4t
tj�|d�����nt
|���	t��n#t$rYnwxYw	t|d}t|��}|r|dnd}t||�	��n#t$r
taYnwxYw|ddk�r�d}d
�}d|vrt!|d||���d
|vrt#|d
��}	n
td
}	d}
d|vr*t%|d|	���}|tdkrd}
|rt&}nt(}|D�]<}
||
|���r,t*D�]+}||v�r#	|dkrt-|
dd||��nt-|
|d||���A#t.t0t2f$r�|dkr|
}nt|||k}|s|dr�|dkrDt4�d��}|�dt;||����nCt4�|��}|�dt;||����|
�|��Y��'wxYw��-|�dd��st?t4��d}|rN||dkrBtj�|��rt
|d���ntt
|���nc|dkrMt���r4t
tj�|d�����nt
|���tA|d|�����<��>|�s�|d�r�|rtBnd}t4�|��}
|rZ|
�dt;|����|
�d|d��|
�tD��nr|�d��r4|
�dtGj$|d��j%��n)|
�dt;|d����t*D]�}||vr�|dkr|
}nt|||k}|s|dr�|dkrDt4�d��}|�dt;||����nCt4�|��}|�dt;||����|
�|����d}tLD]}|j'�(|
|��d}|st4j)�|
��|�dd��st?t4��|�rtj�|��}t
|d���tA|d|���tT�|tT�+d����t
|���tj,�-|��}tj,�.|���D]3}tj/�0||��tA||�	���4|rtcj2|�	���ngt���r4t
tj�|d�����nt
|���tA|d��n�t*D]�}||vr�|dkrHtD�3d��d�dt;||�����TtD�3|��d�dt;||������|�dd��st?t4��t
|���tA|d|���dS)NrrVr�FT)r:r;r�r|rc�6�t|���|dkS)Nr�r�r��rnr�s  r<�is_needed_userzlve_set.<locals>.is_needed_user�s��&�b�9�9�9�X�g�=N�N�Nr;r�)r�r�r:rsrrr�rur[r\r�savezskip-update-cfgrDrrz
save-username)4r�r�rhrDrFr�rkrEr�r�rr�rwr�rror�r7r�rJrIr�r6r�rr�rrrr8rrrrrrr�r�rKr��insertBefore�	lastChildr��lve_inforSr��lvp_lve_id_listr*r�rr�r)r�r:rVr�rQrzru�has_ver�r��cpu_is_different�
setted_cpu�el_listrnrf�is_differentr�el_name�added�el2�enables_reseller_limitsr�s                      r<�lve_setr��sV	����{�{� ���_�d�;�;�
��'�"��(���?�?�	��5��D����
�
#�&�H�W�-�-�-��8���f�%�%�	'��v�D�9�9�9�9�9�
�v�&�&�&�&�&�	�1����5�5�7�7���3�8�5�5�h�w�6G�H�H�I�I�I�I�I�	�6�"�"�"�"�
��������
�
�
���
����!�!��'�!2�3��-�g�6�6�	�#,�4�9�Q�<�<�"���7�X�6�6�6�6�6���!�!�!� �
�
�
�!�������A�����	O�	O�	O��H�$�$��H�5�N�[a�b�b�b�b��X������(�)�)�E�E���'�E� ���H���1�(�5�/�5�Q�Q�Q�J��Z��.�.�.�#(� ��	��G�G��G��-	�-	�B��~�b�(�+�+�,
�+�5�5�C��h���5�"�d�{�{� 2�2�w��QY�Z]�Q^� _� _� _� _� 2�2�s�G�X�c�]� S� S� S��� *�J�	�B�5�5�5�"�e�|�|�/?���0:�#��(�3�-�/O��+�5�x��/?�5�#&�$�;�;�+1�+?�+?��+H�+H�D�$(�$5�$5�o�c�(�SV�-�FX�FX�$Y�$Y�$Y�$Y�+1�+?�+?��+D�+D�D�$(�$5�$5�g�c�(�3�-�>P�>P�$Q�$Q�$Q� "���t� 4� 4� 4���5����
'�, �|�|�$5�u�=�=�%��V�$�$�$����
/�f���(9�9�9��x�)�)�&�1�1�3�#�6��E�E�E�E�E�$�6�2�2�2�2�2��q�[�[�S�%A�%A�%C�%C�[��s�x�'A�'A�(�7�BS�'T�'T�U�U�U�U�U� �v�.�.�.�.��(�7�+�F�;�;�;�;�;���?	-�(�7�+�?	-�*0�;�&�&�e�G��%�%�g�.�.�B��	
B�����c�&�k�k�2�2�2�������(8�9�9�9����z�*�*�*�*��<�<��0�0�B��O�O�F�C�L��'�9J�,K�,K�,S�T�T�T�T��O�O�D�#�h�w�.?�*@�*@�A�A�A�'�
-�
-���(�?�?��e�|�|�'7���(2�#��(�3�-�'G��#�-�x��'7�-��$�;�;�#)�#7�#7��#@�#@�D� �-�-�o�c�(�3�-�>P�>P�Q�Q�Q�Q�#)�#7�#7��#<�#<�D� �-�-�g�c�(�3�-�6H�6H�I�I�I����t�,�,�,���E�!�
�
����+�+�B�s�3�3�3�����
1�� �,�,�R�0�0�0��<�<� 1�5�9�9�
!��� � � ��
-�.1�h�.@�.@��.H�.H�*H�'��6��=�=�=�=��(�7�+�F�;�;�;�;��%�%�f�e�n�n�Q�.?�.?�@�@�@��6�*�*�*�*� #�� 9� 9�&� A� A�
�"�w�6�6�f�6�E�E�?�?�G��F�'�'���8�8�8��g�
�>�>�>�>�>�*�U�0�<�m�T�T�T�T���/�/�1�1�/��s�x�'A�'A�(�7�BS�'T�'T�U�U�U�U�U��v�.�.�.�.��(�7�+�,�,�,��#�	e�	e�C��h����$�;�;��3�3�G�<�<�Q�?�L�L�_�]`�ai�jm�an�]o�]o�p�p�p�p��3�3�C�8�8��;�H�H��QT�U]�^a�Ub�Qc�Qc�d�d�d���|�|�-�u�5�5�	��V�����6�"�"�"�"��(�7�#�F�3�3�3�3��4s7�:D	�	
D�D�?E�E.�-E.�$7I�C#M�Mc�8�td��t��t|d��}t|��dkrd}nt|��dkr|d}|�#tdkr||d<t|d���dSt|d���dS)	zX
    Set package with some heuristic algorithm to simulate old package set behavior
    Tr�rNr[rV��is_resellerF)rhrkrwr�rN�package_set_ext)r�r��
reseller_listrus    r<�package_setr�ms����D�����M�M�M�.�h�w�.?�@�@�M�
�=���Q������	�]�	�	�q�	 �	 � ��#������ 2� 2�$,���!���d�3�3�3�3�3�3���e�4�4�4�4�4�4r;c
�r�td��t��d}|rd�}nd�}d|vrt|d|���tD�]�}|||���r�tD�]U}||v�rM	|dkrC|�d��d	�d
t||����nB|�|��d	�dt||������#tttf$r�|dkrDt�d��}|�d
t||����nCt�|��}|�dt||����|�
|��Y��QwxYw��Wtj��rRt!|�dd
��|d��}|�t$t|����d}���|�s~d
}t�d��}|�d|d��|r$|�d|d��|d}tj��r>t!||d��}|�t$t|����tD]�}||vr�|dkrDt�d��}|�d
t||����nCt�|��}|�dt||����|�
|����tj�
|��t)t��t��t+|��d|vrt-|d��}	n
t.d}	d|vrt1|d|	���|d<|r|dnd}
t3|d|
���dS)NTFc�~�|�d��|dko|�d��|dkS)Nrr�rurVr|r�s  r<r~z'package_set_ext.<locals>.is_needed_plan�s=������%�%��'�):�:�w�r���z�?Z�?Z�^f�gv�^w�?w�
r;c�l�|�d��|dko|�d��S)Nrr�rur|r�s  r<r~z'package_set_ext.<locals>.is_needed_plan�s3���?�?�4�(�(�H�W�,=�=�a�b�o�o�V`�Fa�Fa�Ba�ar;r�)r�r�rur[rr\rrVr|r�rQrrursrrr�r)rhrkr�rKr�rrr8r�rr�rrrr\�is_plesk�_plesk_get_package_idr��XML_PLESK_IDr�r�copy_package_settings_to_cpanelr7rr��
plan_apply)r�r��has_packager~rnrfrr(�package_resellerr�rus           r<r�r��s=���D�����M�M�M��K��b�	�	�	�	�
	b�	b�	b��� � ���t�~�N�N�N�N������>�"�h�'�'�	�'�
-�
-���(�?�?�-��$�;�;��3�3�G�<�<�Q�?�L�L�_�]`�ai�jm�an�]o�]o�p�p�p�p��3�3�C�8�8��;�H�H��QT�U]�^a�Ub�Qc�Qc�d�d�d���&�
�I�>�-�-�-��$�;�;�#)�#7�#7��#@�#@�D� �-�-�o�c�(�3�-�>P�>P�Q�Q�Q�Q�#)�#7�#7��#<�#<�D� �-�-�g�c�(�3�-�6H�6H�I�I�I����t�,�,�,�,�,�-����
#��#�%�%�
=�0����o�r�1R�1R�T\�]d�Te�f�f������c�(�m�m�<�<�<��K���)���
�
!�
!�)�
,�
,��
����h�w�/�0�0�0��	9��O�O�J���(A�B�B�B�'��8����!�!�	9�,�-=�x��?P�Q�Q�H��O�O�L�#�h�-�-�8�8�8�#�	%�	%�C��h����$�;�;�!�/�/��8�8�D��%�%�o�c�(�3�-�6H�6H�I�I�I�I�!�/�/��4�4�D��%�%�g�c�(�3�-�.@�.@�A�A�A����t�$�$�$����$�$�R�(�(�(��V�����M�M�M�#�H�-�-�-�
�����H�V�$�%�%����F�#������2�8�E�?�%�P�P�P����,7�A�x��(�(�T�H��x�� �8�4�4�4�4�4�4s�&BC2�2B9F/�.F/rurQc����t��}|���}	tt��fd�|����}|dS#t$rYdSwxYw)z7
    Find the right package id from plesk DB query
    c�4��|d�dhvo|d�kS)Nrr�r[r:)rGrQrus ��r<r�z'_plesk_get_package_id.<locals>.<lambda>�s"���a��d�x��0�0�D�Q�q�T�W�_�r;r�N)r�list_domain_packages_with_idrr�
StopIteration)rurQ�panelrd�packs``   r<r�r��s�����
���E��1�1�3�3�H���F�D�D�D�D�D��
�
�����A�w��������t�t����s�(A�
A�Ac�h�t���}td��t}|a|S)z�
    Retrives resellers to packages map from panel using /usr/bin/getcontrolpaneluserspackages
    :return: Dictionary:
    { 'reseller1' -> ['pack1', 'pack2'], 'reseller2' -> ['pack'] }
    ra)r�r�r�)�packages_users_copy�reseller_packages_maps  r<r�r��s6��)�-�-�/�/���2�3�3�3�*��(�N� � r;c��|d}|d}t��}||vr|||vrt|d���dSdS)z�
    Set reseller package limits
    :param set_data: input data dictionary
    :return: True - limits was set succesfully
            False - supplied provider has no supplied package
    rVr�Tr�F)r�r�)r�rV�package_namer�s    r<�reseller_package_setr��s^���_�-�M��G�$�L�5�7�7���-�-�-�,�BW�Xe�Bf�2f�2f���d�3�3�3�3��t��5r;c��|d}tj��sdSd|��}tj�|��sdSt|dd���5}|���}ddd��n#1swxYwY|dd�}i}|D]�}|�d��r�|����	d��}|d	�
dd
�����}	|ddkr|d||	<|	tvr|�|��|�d
��rd|vrdS��dD]2}	|	|vr,tj||	��}
|
pt|	||	<�3t!||��rdSt#|t$��}tD](}	||	}d|	�d|�d�}
|�|
���)t)d
�|��|d��dS)zA
    Copy package limits from ve.cfg to cpanel packages data
    r�Nz/var/cpanel/packages/r�r�r��lve_�=rr|r[�DEFAULT�_PACKAGE_EXTENSIONSrD)rwrvrIrci�)r\r]r�r�r�r�r�r r�r�r�r�r�r��memory_to_pager�is_limits_equals�create_cpanel_limitsrKr�r#r)r�rQ�package_pathr��cpanel_package_data�new_cpanel_package_data�old_cpanel_datar��
line_parts�
limit_name�memory_page_value�cpanel_data�limit_value�
limit_lines              r<r�r�	sz���w��G�� �"�"����4�7�4�4�L�
�7�>�>�,�'�'����	
�l�C�'�	2�	2�	2�,�a��k�k�m�m��,�,�,�,�,�,�,�,�,�,�,����,�,�,�,�1�!�!�!�4���O�$�	�	���?�?�6�"�"�	5������+�+�C�0�0�J�#�A��.�.�v�r�:�:�@�@�B�B�J��!�}�	�)�)�.8��m��
�+��-�-�-�'�.�.�t�4�4�4��?�?�0�1�1�	�e�4�6G�6G��F�F��-�W�W�
���(�(� (� 7��
�8S� T� T��*;�*V�{�:�?V�O�J�'�����2�2����'�w�
�;�;�K�&�3�3�
�!�*�-��8�J�8�8��8�8�8�
��&�&�z�2�2�2�2��B�G�G�$;�<�<�l�E�R�R�R�R�Rs�A7�7A;�>A;c��|���D]/}|dvr�	||||krdS�#t$rYdSwxYwdS)zC
    check if new set of limits for package are equals to used
    )r�r�FT)rcr�)�
old_limits�
new_limitsrfs   r<rr2	sy����� � �����#�#�#��	��#��*�S�/�1�1��u�u�2���	�	�	��5�5�5�	�����4s�3�
A�Ac���i}|D�]h}|�d��|k�rKtD�]B}	|dkrQt|�d��d�d�������||<n�|dvrpttjt|�|��d�d�����������||<nPt|�|��d�d�������||<��!#tttf$r	d||<Y��@wxYw��j|S)	z�
    create limits for cpanel package file
    use data from ve.cfg:
    limit = limit if found in ve.cfg or DEFAULT
    return dict
    rrur[rr\)rvrIrwrr)rr�r8rr�r��page_to_memoryr7r�rr�)�
package_id�xml_packages�result_datarnrs     r<rrA	s����K��3�3��
�?�?�4� � �J�.�.�)�
3�
3��3���}�}�-0��3�3�G�<�<�Q�?�L�L�_�]�]�.�.��%�'�'�$�E�*�*��"9�9�9�-0�$�3� #�B�$;�$;�E�$B�$B�1�$E�$R�$R�SZ�$[�$[� \� \���.�.� �%�'�'�	$�E�*�*�.1��1H�1H��1O�1O�PQ�1R�1_�1_�`g�1h�1h�-i�-i�-o�-o�-q�-q��E�*���"�J�	�:�3�3�3�)2�K��&�&�&�3������s�DE
�
E*�)E*c����td��t��d}���fd�}n��fd�}tD]?}||��r2d}|j�|��tt��n�@|s���At���}t|��dkrd}n|d}|�t�|��dStr*tddd���g��tj
d	��ntd
�����t��dS)NTFc�b��|�d���ko|�d��Sr{r|r�s �r<�is_needed_packagez&plan_delete.<locals>.is_needed_packaged	s.����?�?�4�(�(�G�3�W�B�O�O�J�<W�<W�8W�Wr;c�h��|�d���ko|�d���kSr{r|)rnr}rVs ��r<rz&plan_delete.<locals>.is_needed_packageg	s1����?�?�4�(�(�G�3�d����
�8S�8S�Wd�8d�dr;rr%r>z no configuration found for plan r�z)warning: no configuration found for plan )rhrkrKr�r�rrrwr�r�r^r_rarbr`r�)r}rVr�rrn�resellers_listrus``     r<r�r�_	s������D�����M�M�M��G���	X�	X�	X�	X�	X�	X�	e�	e�	e�	e�	e�	e�������R� � �	��G��M�%�%�b�)�)�)��V�����E�		�
�I�� �6�w�?�?�N��>�"�"�a�'�'����*�!�,���#��G�X�.�.�.����	I����H�w�H�H�I�
�
�
�
�H�R�L�L�L�L��G�g�G�G�H�H�H��O�O�O�O�Or;c�(�t||���dS)NrU)r�)r}rVs  r<�reseller_plan_deleter�	s����}�5�5�5�5�5�5r;c���	t|�|��d�d����S#ttf$r)|dkr
t
|nt
|dz�d�cYSwxYw)Nrrrrrzr�)r8rrr�rr)rnrfs  r<�
get_xml_limitr�	s���S��2�*�*�3�/�/��2�?�?��H�H�I�I�I���
�#�S�S�S�#&�%�<�<�{�3����C�8H�C�8O�5R�5R�5R�R�R�R�S���s�:=�7A7�6A7c��d�}|�dStjd|i��}||dd��}|�d��}||dz|�S)z�
    Normalize string for JSON output.
    Example:
     - Input string: -_&[{}]'"`te\s/t
     - Output string: -_&[{}]'"`te\\s/t\a
    :param data_str: String for normalize
    :return: Normalied string
    c�^�d}t|��D]\}}||kr|dz
}||kr|cS�dS)z�
        Get the index of the specified occurrence of character in string
        :param input_string: String
        :param char_to_search: Character to search
        :param ordinal: Required occurence number
        :return: Char index
        rr[r�)r)�input_string�char_to_search�ordinal�count�idx�chs      r<�_get_char_indexz'_normalize_str.<locals>._get_char_index�	sR���� ��.�.�	�	�G�C���^�#�#���
���G�#�#��J�J�J���rr;Nr8r��r[)r�r�rfind)�data_strr(�json_str�trd_idx�last_idxs     r<rPrP�	sl�����$���t��z�5�(�+�,�,�H��o�h��Q�/�/�G��~�~�c�"�"�H��G�a�K��(�)�)r;c�D�������n�rtnt�d���fd�	}|S)a�
    Generate inner function with closured fields names, is_reseller flag and printer function

    :param list fields: List of strings that represent names of fields in final output
    :param boolean is_reseller: Format output with info about reseller or not
    :param callable printer: Function to format and print data for every entry
    :rtype: callable
    :return: function to format data for every user
    Nc
�<������
r4t���tjt�����d<��fd�}n�fd�}tjt���trt	���n��d<d�}|�d���d<t
dkr|�d	���d	<tD�]^}||���rOt|d
��}|dkr|nttd
���d
<ttt|d��t�d
���
��dz���d<	tt|�d��d�d�������d<n#ttf$rYnwxYw|t|d	�����d	<|t|d�����d<t|d���d<t|d���d<t|d���d<��`��d����ddz�d<tt!�d�����d<g}tr)d��fd��	D����}d|�d�g}n��fd��	D���|S)a
        :param string package_name: Find and format data for this package name
        :param string reseller_name: reseller name, owner of supplied package
        :rtype: list
        :return: List of giver package's statistics data line or empty list
        ruc�h���|�d��ko|�d���kSr{r|)rnr�rVs ��r<rz>_package_formatter.<locals>.wrapper.<locals>.is_needed_package�	s2���#�r���t�'<�'<�<�m����Q[�A\�A\�`m�Am�mr;c�b���|�d��ko|�d��Sr{r|)rnr�s �r<rz>_package_formatter.<locals>.wrapper.<locals>.is_needed_package�	s/���#�r���t�'<�'<�<�`�R�_�_�U_�E`�E`�A`�`r;rc�L�trt|��nt|��SrPr.r/s r<r0z?_package_formatter.<locals>.wrapper.<locals>.convert_mem_limits�	rHr;rvrIr{rwrsr|rrr�rzr�r[rr\rurtrxryNr3c3�v�K�|]3}d|�d��|���d���d�V��4dS)r�r5rJN�r�rMrNs  �r<rTz6_package_formatter.<locals>.wrapper.<locals>.<genexpr>
sM�����T�T��C��C�C�d�h�h�q�w�w�y�y�%�&@�&@�C�C�C�T�T�T�T�T�Tr;r8r9c�`��g|]*}��|���d����+Sr;r5rNs  �r<r(z7_package_formatter.<locals>.wrapper.<locals>.<listcomp>
s/���>�>�>�!�d�h�h�q�w�w�y�y�"�-�-�>�>�>r;)rxr�rr^rPr�rKrr8r�r7rrrr�r�r�r)r�rVrr0rnr�rAr�r�r�r�r<s``      @���r<rQz#_package_formatter.<locals>.wrapper�	s,�������
	*�$�]�3�3�3��9�[�)�)�D�,�D���
n�
n�
n�
n�
n�
n�
n�
a�
a�
a�
a�
a��9�[�)�)�D�59�K�^�L�1�1�1�|��T�
�	V�	V�	V�*�)�$�u�+�6�6��V����?�?�-�-�d�6�l�;�;�D��L��	9�	9�B� � ��$�$�
9�%�b�&�1�1��(-����u�u��[��=P�9Q�9Q��V�� #�$<�!�"�e�,�,�C��V��4E�4E�%G�%G�%G�JM�%N�!�!��W�
��!$�S��/�/��8�8��;�H�H��Y�Y�&�&�"�"�D��J�J��#�J�/�����D�����1�1�-��F�2K�2K�L�L��V��1�1�-��E�2J�2J�K�K��V��*�2�t�4�4��T�
� -�b�'� :� :��W�
�,�R��8�8��V����8�8�G���$� ��K�3�.�D��M��*�4��=�9�9�:�:��U�����	@��8�8�T�T�T�T�V�T�T�T�T�T�D� ��=�=�=�/�C�C��G�>�>�>�>�v�>�>�>�?�?��
s�;AF�F�FrP)rr)r�r�r<rQs``` r<�_package_formatterr7�	sO�����!�,�g�g�{�2Y�)�)�PY�G�<�<�<�<�<�<�<�<�z�Nr;c���t��td��t���}td��t���}t	t
t���}tt��dt
���}|D]}|||��z
}�tdkr#tt��dt
���}|�
��D]M\}}tj��r|dkr�|D]+}tdkr||||��z
}�|||��z
}�,�Ntr*td	d
�|��zdz��dSdS)Nr`ra�r=F�r�r<r[T�adminrVr3rW)rkr�r�r�rBr�DEFAULT_PACKAGEr7r�rNrdr\r�r^r`r)rd�reseller_packagesrmrXrQrV�
packages_list�reseller_packages        r<�get_packages_listr@	
s����M�M�M���)�)�)��"�"�$�$�H��2�3�3�3�&�+�+�-�-��
�	�o�
>�
>�
>�F�"�:�<�<�U�I�V�V�V�I��%�%���)�)�G�$�$�$�������&�z�|�|��y�Y�Y�Y�	�(9�(?�(?�(A�(A�6�6�$�
�}�����	�=�G�#;�#;�� -�	6�	6����!�!��)�)�$4�m�D�D�D����)�)�$4�5�5�5���		6��5�
�k�C�H�H�V�,�,�,�t�3�4�4�4�4�4�5�5r;c��t��td��dg}t��|z}ttt
���}t
|d���}t���D]\}}|D]}||||��z
}��tr*tdd�|��zdz��dSdS)	Nrargr9Tr�rVr3rW)rkr�r�rBrr<r7r�rdr^r`r)r?r�rmrXrVr>r?s       r<�get_resellers_packages_listrB&
s����M�M�M��2�3�3�3��,�K�
�\�\�K�
'�F�
�	�o�
>�
>�
>�F�"�6�t�<�<�<�I�(6�(<�(<�(>�(>�A�A�$�
�}� -�	A�	A���i�i� 0�-�@�@�@�F�F�	A��5�
�k�C�H�H�V�,�,�,�t�3�4�4�4�4�4�5�5r;c�V�t��td��t���}td��t���}dg}t	��|z}ttt|���}t|dt���}|D]}|||��z
}�t|d���}|�	��D]\}}|D]}	|||	|��z
}��tr*td	d
�|��zdz��dSdS)Nr`rarg)r=r?Fr:Tr�z
{"data": [r3rW)
rkr�r�r�r�rBrr<r7rdr^r`r)
rdr=r?r�rmrXrQrVr>r?s
          r<�get_all_packages_listrD9
sP���M�M�M���)�)�)��"�"�$�$�H��2�3�3�3�&�+�+�-�-���,�K�
�\�\�K�
'�F�
�	�o�;�
W�
W�
W�F�"�6�u�i�P�P�P�I��%�%���)�)�G�$�$�$���"�6�t�<�<�<�I�(9�(?�(?�(A�(A�A�A�$�
�}� -�	A�	A���i�i� 0�-�@�@�@�F�F�	A��6�
�l�S�X�X�f�-�-�-��4�5�5�5�5�5�6�6r;c��i}|D]}|||<�|S)z�
    Converts package list to internal format
    :param package_list: Package list. Example: ['BusinessPackage', 'Package2']
    :return: Package list as dictionary. Example: {'BusinessPackage': 'BusinessPackage', 'Package2': 'Package2'}
    r:)�package_list�packages_users_dictrQs   r<�_convert_packages_listrHZ
s-�����/�/��'.��G�$�$��r;�list-allc�f�|dvrdS|dvr|dkrdSddlm}m}m}m}m}m}m}		|dkrxt�D	|t|d	t|d
d�ia	�n #t$r|ddd�ia	Y�nwxYw	||��\}
}n#t$rdx}
}YnwxYw|||
d�ia	dS|d	kr||||��ia	dS|d
kr1t�!|��}t|��a	tanta	dS|dkr"t�|	��a	tanta	dS|dkrt�
|��ata	dS|dkr t�||��}
|
a|
a	nta	dS|dkrt �
|��at a	dSn1#t"$r}t%|��Yd}~nd}~wt&$rYnwxYwdS)a�
    Parse output from GET_CP_PACKAGE_SCRIPT and get package and lve relations

    :param option: option for GET_CP_PACKAGE_SCRIPT.
     Option is one from the following possible values:  'userid', 'package', 'list-packages', 'list-resellers-packages'
    :type option: string
    :param lve_package_id: lve_id or package_name
    :type lve_package_id: string or int
    :param reseller:
    :type reseller: string
    )rIr�rQr`rar�rTF)r�rQr|r)�admin_packages�get_reseller_users�get_uids_list_by_package�list_all�
list_users�reseller_package_by_uid�resellers_packagesr�NrQru)rQruTr`rar�rTrI)�clcommon.cpapirKrLrMrNrOrPrQ�cached_usersr�r�r��cached_list_packagesrH�cached_resellers_packages�cached_reseller_users�cached_defaultrrdr�)�option�lve_package_idrurKrLrMrNrOrPrQrVrQrF�reseller_users_dictrXs               r<r�r�g
s��&�V�V�V��u�
�&�&�&�>�R�+?�+?��u�������������������P
��X����'�W�&�'3�N�'C�I�'N�(4�^�(D�Z�(P�)�)�&�N�N�� �W�W�W�&4�"�RT�6U�6U�%V�N�N�N�W����1�-D�-D�^�-T�-T�*�M�7�7��!�1�1�1�.0�0�M�G�G�G�1����#1�g�S`�2a�2a�!b���t�
�y�
 �
 �,�.F�.F�~�W_�.`�.`�a�N��4�
��
&�
&�$�+�-�~�/�/��!7��!E�!E��'5�$�$�"6���4�
�0�
0�
0�)�0�!3�!3�!5�!5��,:�)�)�";���4�
�|�
#�
#�
�#�)�z�|�|��)�N��4�
�,�
,�
,�%�,�&8�&8��&B�&B�#�)<�%�!4���"7���4�
�z�
!�
!��%�!)�����+�N��4����'�'�'�#�A�&�&�&�&�&�&�&�&������
�
�
���
�����5s��
F�)A�F�A4�0F�3A4�4F�8B�F�B�F�B�
F�$F�:5F�1&F�F�9$F�F�
F.�
F�
F.�-F.c�F�t��tt��S)z2
    Retrieves panel users count
    :return:
    )r�r�r�r:r;r<�get_panel_users_countr\�
s��
�����~���r;c��td��td||���r/t|D]#}tt|��||����"dSdS)Nr�rQr)r�r�rr7)r}rurSs   r<r�r��
sm����&�&�&��I�w��B�B�B�<�!�'�*�	<�	<�C��c�#�h�h��(�;�;�;�;�;�<�<�	<�	<r;c��|D]�}|�dd��}|������}|D]E}t|��dkr0	t	|��}t|���5#t$rY�AwxYw�F��dS�Nrcr|r)r�r�r�r�r7r�r��
users_listr�r�rs    r<�destroy_manyrb�
s����	�	���|�|�D��$�$���
�
���"�"�$�$���	�	�D��D�	�	�Q�����t�9�9�D���%�%�%�%�� �����D�����	�	�	�	s�A8�8
B�Bc�t�t��	t��n#t$rYnwxYw|D]�}|�dd��}|������}|D]E}t
|��dkr0	t|��}t|���5#t$rY�AwxYw�F��dSr_)	rkr�rr�r�r�r�r7rr`s    r<�
apply_manyrds����M�M�M�
��������
�
�
���
�����	�	���|�|�D��$�$���
�
���"�"�$�$���	�	�D��D�	�	�Q�����t�9�9�D��d�O�O�O�O�� �����D�����	�	�	�	s��
,�,�B&�&
B3�2B3c
��t�t|��t|��|d|�d|�d����dS)NzCan`t put proccess with pid z in lve r�r�)r��lve_enter_pid_flagsr7)r��pid�flagss   r<�	limit_pidrisR��	����F���S��X�X�u�Y�s�Y�Y�F�Y�Y�Y������r;c�^�t�t|��d|�����dS)NzCan`t release process with pid r�)r��
lve_leave_pidr7)rgs r<�release_pidrls0��	����C���*Q�C�*Q�*Q��R�R�R�R�Rr;c	�h�tttttt
ttd�S)N�rr8rrIrrKr?rMrnr:r;r<�get_globalsro s(���l�$��[�$�S� 0�	2�2�2r;c�*�g}t���}td��t���}|���a|���D]&\}}|D]}||kr|�|g����'|S)Nra)r�r�r�rd�extend)rQru�
pkg_users_oldr=rVr>�package_name_in_keys       r<rwrw2s����H�"�'�'�)�)�M��2�3�3�3�&�+�+�-�-��"�'�'�)�)�N�):�(?�(?�(A�(A�1�1�$�
�}�#0�	1�	1���-�-�-������0�0�0��	1��Or;c�~�tjtjdd���}|r�tj�|��rxt
||��\}}|dkrad|��}trtdd|g��d
Sd|��}tj
�|�d	���tj|��d
Sd
Sd
Sd
S)z�
    Call Endurance's custom script

    :param args: list of arguments for pass to Endurance's custom script
    :return: None

    �ENDURANCE_CUSTOM_SCRIPTr�)�	separatorrz0Error while executing Endurance's custom script
r%rZzerror: rcN)
r\�get_param_from_file�CL_CONFIG_FILEr�r�r�rr^r_rarergrb)�args�endurance_custom_script�ret_code�std_outrn�err_messages      r<�call_endurance_custom_scriptr~Cs���*�=�k�>X�>W�HK�M�M�M���
#�2�7�>�>�2I�#J�#J�
#�(�)@�$�G�G���'��q�=�=�T�7�T�T�G��
#��G�g�w�%7�8�8�8�8�8�1��1�1���
� � �K�!3�!3�!3�4�4�4����"�"�"�"�"�
#�
#�
#�
#��=r;c��tr)tt|tjz����St|��S)a
    Convert page value to human-readable value or bytes, depending on BYTES_FLAG;
    E.g.
    >>> _page_to_memory_or_bytes(1233254)  # BYTES_FLAG=False
    '100M'
    >>> _page_to_memory_or_bytes(1233254)  # BYTES_FLAG=True
    654321

    :type value: int
    :rtype: str | int
    )r�r7r��mmaprr'r/s r<�_page_to_memory_or_bytesr�\s5���1��5����.�/�/�0�0�0��5�>�>�r;c��g}ttj�����}t	��t��D]m}	tj�|��}||vr|�|���<#tttf$r|�|��Y�jwxYw|D]}t|���dS)zV
    Remove from LVE all resellers, which are absent from panel
    :return: None
    N)r�rDrSrzrkrHr�r�r�r�r�r�)�reseller_id_list_for_delete�cpapi_resellers_list�lve_reseller_id�lve_reseller_name�reseller_id_for_deletes     r<�remove_absent_resellersr�ms���#%�� ��� 1� 1� 3� 3�4�4���M�M�M�#�:�:�@�@��	@� #�� 9� 9�/� J� J�� �(<�<�<�+�2�2�?�C�C�C����'�7�+�	@�	@�	@�'�.�.��?�?�?�?�?�	@����#>�1�1���/�0�0�0�0�1�1s�
8B�,B5�4B5c��tj���D]�}	tj|���#t
$rz	t�|��n[#t$rN	tj�	|��t�|��n#t$rYnwxYwYnwxYwY��wxYwdS)zM
    Remove from LVE all users, which absent in system
    :return: None
    N)
rDrFrSrr�r�r�r!r*r�r�s r<�remove_absent_usersr��s����(�,�,�.�.����	��L�� � � � ���		�		�		�
�����'�'�'�'���
�
�
���F�%�%�f�-�-�-��O�O�F�+�+�+�+��!�����D�������
������		�����sW�7�
B;�A�B;�
B5�(9B"�!B5�"
B/	�,B5�.B/	�/B5�2B;�4B5�5B;�:B;)FrP)r)NN)rFT)T)NNN)NFNNr)r)r(rN)r=N)FN)rIr|N)��
contextlibr�rjr�r�r�r�rr�r�rar�r��xml.dom.minidom�dom�minidomr�builtinsr�	functoolsrr�typingrrrr	r
rr�r�r\rs�clcommon.constrrRr
rrr�clcommon.cpapi.cpapiexceptionsr�
clcommon.lockr�clcontrollibr�cleventsrr�cllimits.libr�cllvectl.logr�clveconfig.ve_configrrr�clveconfig.ve_lockrrrrrr r!�secureior"r#�GET_CP_PACKAGE_SCRIPTr�r�r<r�r�r�rlr�r7r�r��IS_DEBUGr��LVEr�rDr,rBrHrMrWrdrhrfr@r^rAr�r�r^r�r�ror�rr8rrIrJrrKr?rMrLrNr�r�r�r�r�r�r�r�r�r�r�r�r��contextmanagerr�r�r
r_r r*r0r3r6r9r�rDrOrXr8rar_rkrprxr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr�tupler�rr"r'rBrRrYr]rerhrqr{r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr�rrrPr7r@rBrDrUrTrSrVrWrHr�r\r�rbrdrirlrorwr~r�r�r�r:r;r<�<module>r�sc��������������������������	�	�	�	�
�
�
�
�	�	�	�	�����
�
�
�
�
�
�
�
��������������������������������?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�������������
�
�
�
�"�"�"�"�"�"�g�g�g�g�g�g�g�g�g�g�g�g�8�8�8�8�8�8�&�&�&�&�&�&�*�*�*�*�*�*�P�P�P�P�P�P�P�P�%�%�%�%�%�%�.�.�.�.�.�.�O�O�O�O�O�O�O�O�O�O�E�E�E�E�E�E�E�E�D�D�D�D�D�D�D�D�D�D�D�D�D�D�?�?�?�?�?�?�?�?�?��
������	��	��:�����
��3�r�z�~�~�m�Q�/�/�0�0����!�!�'�+�.�.���E�
�C�C��E��!�!�!�E�

�#��-�-�-�C������Y�������������%�%�%�K�K�K��=�����$3�3�3�3�*����
�
�
���
��
�
�����
�
�
��
�
�
�	�	��O�N�N������
�
���
�
�
��
����
�
�	��	����
�
�
�����	������
���
���,+�+�+�+�,
�
�
�1�1�1�D&�%�'�'�9�<�<�>�>������,���$���*�������*0�0�0���"�"���"�(
�>�
�
�
�
�&�������&
�
�
�[�[�[�����2�2�2�2�2�2����#��#�D�#�#�#�#�Lk�k�k�"i�"i�"i�J3 �d�3��8�n�3 �3 �3 �3 �l
�
�
� 
&�
&�
&�
&� %�%�%�L�L�L�L�EA�EA�EA�EA�PI�I�I�!�!�!�H
?�
?�
?�	0�	0�	0�	0�	4�	4�	4�	4�	�	�	�	f�	f�	f�!1�!1�!1�J"p�"p�"p�"p�J!�!�!�N��D��Z��>n�>n�>n�>n�B6�6�6�:V)�V)�V)�V)�r���
�
�
�������%�%�5�%��%�S�%�#�%�%�%�%�6���,���6!�!�!�!�H%,�N�N�N�N�f5�5�5�5�6������$5�5�5�(���".�.�.�.�b< �< �< �@>�>�>�D$�e�D�$�J�&7�$�$�$�$�
'�
'�
'�
'����:	G�	G�	G�F�F�F�*���
�
�
�+�+�+�(2�2�2�������*
�
�
� +&�+&�+&�+&�\���F}�}�}�}�@5�5�5�5�2H5�H5�H5�H5�V
�C�
�#�
�(�3�-�
�
�
�
� !�!�!����*+S�+S�+S�\������<&�&�&�&�R6�6�6�S�S�S�$*�$*�$*�NJ�J�J�J�\5�5�5�:5�5�5�&6�6�6�4!�� �� �� �� ��	�	�	�t�t�t�t�n���<�<�<�<�
�
�
����(���S�S�S�2�2�2�$
�
�
�"#�#�#�2���"1�1�1�4����r;