HEX
Server: Apache
System: Linux host.creative4all.com 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Tue Nov 5 04:50:16 EST 2024 x86_64
User: agrimasfadeltral (1173)
PHP: 8.3.30
Disabled: exec,passthru,shell_exec,system,proc_open,parse_ini_file,show_source
Upload Files
File: //opt/cpmigrate/__pycache__/transfer.cpython-313.pyc
�

-}�i����8�SrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKJr SSKJ
r
 SSKJr SSKJr SSKJr SSKrSSKrSSKJr SS	KJr SS
KJrJr SSKJrJr SSKJ r J!r!J"r" \
"S
S55r#"SS\5r$"SS5r%g)z&
Handles everything Transfer related.
�N)�datetime)�	dataclass)�Enum)�
PriorityQueue)�DEVNULL)�whmapi1)�ColorFormatter)�
APIFailure�FatalMigrationFailure)�Journal�
save_state)�User�Status�Domainc�T�\rSrSr%Sr\\S'\\S'\\S'Sr\\S'Sr	\\S'S	r
g
)�SSHKey�z$Holds generated SSH key information.�name�path�keyF�	validated�removed�N)�__name__�
__module__�__qualname__�__firstlineno__�__doc__�str�__annotations__r�boolr�__static_attributes__r��/opt/cpmigrate/transfer.pyrrs(��.�

�I�

�I�	�H��I�t���G�T�r#rc�0�\rSrSrSrSrSrSrSrSr	Sr
S	rg
)�
TransferStage�(zStages of the Transferr�����rN)rrrrr�NOT_STARTED�ENVIRONMENT�	USERS_PKG�USERS_RSYNC�FINAL_STEPS�	COMPLETEDr"rr#r$r&r&(s#�� ��K��K��I��K��K��Ir#r&c��\rSrSrSrSrSrS7SjrSrSr	S	r
S
rSrSr
S
r\S5r\S5r\S5r\S8Sj5r\S5r\S5rSrSrS9SjrS:SjrS:SjrS;SjrS;SjrSrSr\S:Sj5rS\ S\ 4S jr!S\ S4S!jr"\S"5r#S#r$S$r%S9S%jr&S&r'S'r(S(r)S)r*S*r+\S9S+j5r,\S,5r-\S-5r.\S.5r/S/r0\S05r1S1r2S2r3S3r4S4r5S5r6S6r7g)<�Transfer�3z7Primary class for handling all things Transfer related.c��[R"5RS5UlUR	5UlX lXlURS5Ul	[URS55UlURS5UlURS5Ul
URSS5Ul[R R#URS5UR5UlSUlSUlSUlSUlSUlSUlSUl/Ul/Ul:Ul[?U5Ul [BRD"URF5 g)	Nzmigration-%d%m%Y%H%M%S�host�port�skipenv�bwlimit�debugF�logpath)$r�now�strftime�migration_id�
get_ipaddr�	my_ipaddr�environments�args�get�
origin_serverr�origin_port�	skip_envsr:r;�osr�join�log_path�ssh_key�access_hash�
ea_version�cpanel_version�server_type�checked_env�approved_env�users�notesr&r-�stager�journal�atexit�register�clean_up)�selfrC�envss   r$�__init__�Transfer.__init__6s��$�L�L�N�3�3�4L�M������*��� ���	�!�X�X�f�-����t�x�x��/�0������)�,����x�x�	�*����X�X�g�u�-��
������T�X�X�i�%8�$�:K�:K�L��
����������"������ ���!�����
���
�"�.�.��
��t�}�������
�
�&r#c���[RRS5(a[R"S5 SUlg[R"S5 SUlg)zCheck if vps or dedz/proc/vz/veinfoz"Server has been detected as a VPS.�vpsz$Server has been detected as non-VPS.�dedN)rHr�exists�logging�inforO�rYs r$�check_server_type�Transfer.check_server_typeOs@��
�7�7�>�>�+�,�,��L�L�=�>�$�D���L�L�?�@�$�D�r#Fc�>�U(a[USS9 g[USSS9 g)z!Prints progress without newlines.T)�flush�)�endrgN)�print)rY�msg�dones   r$�print_progress�Transfer.print_progressXs����#�T�"��#�2�T�*r#c��[SUR35 [RR	UR5(d [R
"UR5 UR(a[RO[Rn[R"5nSUlURU5 UR5(aURR5 [R "["R$5nUR'[)55 URU5 UR+U5 [R,"S5n[R."[RR1URS55nUR'U5 URU5 UR+U5 g)zSets up logging.zLog directory path: Fz(%(asctime)s: [%(levelname)s] %(message)sz
master.logN)rjrJrHrr`�makedirsr;ra�DEBUG�INFO�	getLogger�	propagate�setLevel�hasHandlers�handlers�clear�
StreamHandler�sys�stdout�setFormatterr	�
addHandler�	Formatter�FileHandlerrI)rY�
logging_level�main_logger�console_handler�
log_format�log_handlers      r$�
setup_logging�Transfer.setup_logging_s?��
�$�T�]�]�O�4�5��w�w�~�~�d�m�m�,�,��K�K��
�
�&�)-����
�
����
��'�'�)�� %������]�+��"�"�$�$�� � �&�&�(�!�/�/��
�
�;���$�$�^�%5�6�� � ��/�����/��&�&�6�
�
��)�)��G�G�L�L�����5�
��	� � ��,����]�+����{�+r#c��UR5 UR5 UR5 UR(d[	S5eUR
(d[	S5eURR(d[	SURS35eUR5 UR5 UR5 UR5 g)z*Sets up the SSH key and confirms it works.zNo SSH key setup.zNo access_hash found/loaded.zLSSH key test failed. Exiting. Did you whitelist this server's IP address on �?N)�check_local_cpanel_license�generate_sshkey�test_sshkeyrKrrLrrE�check_origin_cpanel_license�check_cpanel_version�	check_ea4rdrcs r$�setup�Transfer.setup|s����'�'�)����������|�|�'�(;�<�<����'�(F�G�G��|�|�%�%�'�*�*.�*<�*<�)=�Q�@��
�
	
�(�(�*��!�!�#�������� r#c�
�[RnUS:Xa&[Rn[RUlO�US:Xa&[Rn[RUlOWUS:Xa&[Rn[RUlO+US:Xa%[Rn[RUlU[R:wa[R"SU5 URH	nX#lM g)z:Sets every user to the same stage, if we are overriding it�pkg_xfer�pkg_restore�
rsync_home�acct_verifyz"Setting status of all users to %s.N)rr-�FINISHED_PKGr&r/rT�FINISHED_PKG_XFER�FINISHED_PKG_RESTOREr0�FINISHED_RSYNCr1rarbrR�status)rYrTr��users    r$�	set_stage�Transfer.set_stage�s����#�#���J���(�(�F�&�0�0�D�J�
�m�
#��-�-�F�&�0�0�D�J�
�l�
"��0�0�F�&�2�2�D�J�
�m�
#��*�*�F�&�2�2�D�J��V�'�'�'��L�L�=�v�F��J�J�D� �K�r#c��[R"S5 URS[RSSS9upUS:XagUSRS5Sn[
U5UlURS:�a[R"S	U5 g
[R"S	U5 g
US
:Xa[S5e[S5e)z)Checks the origin server's cPanel versionzChecking origin cPanel version.z"/bin/cat /usr/local/cpanel/versionr(T��command_out�sleep�quietr�.�Fz"Origin server is using cPanel v%s.�z4Failed to obtain cPanel version. No /bin/cat binary.z Failed to obtain cPanel version.N)rar;�origin_command�
subprocess�PIPE�split�intrNrb�warningr)rY�ret_code�out�versions    r$r��Transfer.check_cpanel_version�s����
�
�7�8��+�+�0�"�����	,�
�
���q�=��!�f�l�l�3�'��*�G�"%�g�,�D���"�"�R�'����A�7�K���� D�g�N�
��_�'�F��
�(�(J�K�Kr#c���[R"S5 URSSSS9upUS:Xa[R"S5 SUlg[R
"S	5 S
Ulg)z*Checks if the server is using EasyApache 4z#Checking origin EasyApache version.z /usr/bin/test -d /etc/cpanel/ea4r(T�r�r�rz$Origin server is using EasyApache 4.r+z$Origin server is using EasyApache 3.r*N)rar;r�rbrMr��rYr��_s   r$r��Transfer.check_ea4�s^���
�
�;�<��)�)�.�a�t�*�
����q�=��L�L�?�@��D�O��O�O�B�C��D�Or#c�R�URHnURU:XdMUs $ g)z)Returns User() object for the name given.N)rRr)rYrr�s   r$�get_user�Transfer.get_user�s&���J�J�D��y�y�D� ����r#c�T�[R"[R"55$)zIReturns the hostname IP address. Should be the public IP on our
servers.
)�socket�
gethostbyname�gethostnamercs r$r@�Transfer.get_ipaddr�s���#�#�F�$6�$6�$8�9�9r#c�J�URHnUR5 M g)z*Performs validation checks for every user.N)rR�
test_sites�rYr�s  r$�validate_transfers�Transfer.validate_transfers�s���J�J�D��O�O��r#c�J�URHnUR5 M g)z$Performs rsync phase for every user.N)rR�
rsync_homedirr�s  r$�rsync_accounts�Transfer.rsync_accounts�s���J�J�D���� �r#c��URHSnUR5 UR5 UR5 UR	5 UR5 MU [R"S5 g)z-Performs package account phase for every userzPackaging phase has completed.N)rR�
start_pkgacct�
check_pkgacct�transfer_pkgacct�restore_pkgacct�check_restorepkgrarbr�s  r$�pkg_accounts�Transfer.pkg_accounts�s]���J�J�D���� ���� ��!�!�#�� � �"��!�!�#��	���5�6r#Nc��U(d/nURS5nURSS/5nURS5 U(dBUH;nXT;a&URR	[XP55 M.[
SUS35e O(U(a!UVs/sHn[XP5PM snUl[UR5S:Xa[
S5egs snf)	z-Loads users provided and confirms they exist.�
list_usersrR�rootz	The user z$ was not found on the origin server.rzNo users to transfer, exiting.N)�origin_whmapi_callrD�removerR�appendrr�len)rY�	all_users�	user_list�data�remote_usersr�s      r$�
load_users�Transfer.load_users�s�����I��&�&�|�4���x�x��&��2�����F�#��!���'��J�J�%�%�d�4�&6�7�/�#�D�6�)M�N���	"��;G�H�<�4�d�4�.�<�H��
��t�z�z�?�a��'�(H�I�I� ��Is�Cc��URS:�a�URS5nURS5nU(arUHknURS5nURS5nURU5nU(dM?SU;dMGURR[
U55 Mm ggURHnUR5 M g)z1Loads all domains for the users we are migrating.r��get_domain_info�domainsr��domain�*N)	rNr�rDr�r�r�rrR�legacy_load_domains)rY�respr�r��domain_user�domain_namer�s       r$�load_domains�Transfer.load_domains	s�������#��*�*�+<�=�D��h�h�y�)�G��%�F�"(�*�*�V�"4�K�"(�*�*�X�"6�K��=�=��5�D��t��;� 6����+�+�F�;�,?�@�
&���
�
���(�(�*�#r#c���URS5nURS5HEnURS5nURS5nURU5nU(dM?XElMG g)z,Loads the IP addresses for all of the users.�	listaccts�acctr��ipN)r�rDr��ipaddr)rY�acctsr�r�ip_addrr�s      r$�load_ipaddrs�Transfer.load_ipaddrssZ���'�'��4���I�I�f�%�D��8�8�F�#�D��h�h�t�n�G��=�=��&�D��t�%��&r#c�h�[R"S5 SnU(dtURS/SSS9up#US:Xa[R"S5 SnO7[R"S	UR5 [
R"S
5 U(dMsgg![an[S5UeSnAff=f)
zlChecks the local cPanel license to ensure it is valid. The script
will wait 60 seconds before trying again.
zChecking local cPanel license.F�/usr/local/cpanel/cpkeycltr(Tr�rz(Confirmed local cPanel license is valid.z{Local cPanel license is invalid. Please check to ensure a license has been added for %s. Waiting 60 seconds to check again.�<z1Local cPanel license is invalid. Cannot Continue.N)	rarb�
local_command�errorrA�timer��KeyboardInterruptr)rY�validr�r��excs     r$r��#Transfer.check_local_cpanel_license&s���	���5�6���	��"�0�0�1�2�!�4�1�����q�=��L�L�!K�L� �E��M�M�A����	��J�J�r�N��e��!�	�'�C���
��	�s�A8B�
B1� B,�,B1c��[R"S5 URSSSS9upUS:Xa[R"S5 g	[S5e)
zXChecks the origin cPanel license to ensure it is valid. Script
will exit if this fails.
zChecking origin cPanel license.r�r(Tr�rz)Confirmed origin cPanel license is valid.z2Origin cPanel license is invalid. Cannot Continue.N)rarbr�rr�s   r$r��$Transfer.check_origin_cpanel_licenseAsU��	���6�7��)�)�(���*�
����q�=��L�L�D�E�'�D��
r#c
�z�URS5nU(aOURS5nUS:wa'URS5n[SUSUSUSU3US	9eURS
5$URS5nU(aT[URS5RS55nU(aURS
5$[S
USUSU3US	9eURS5nU(aPURS5n	U	S:wa(URS5n[SUSUSUSU35eURS
5$g)z/Checks whmapi1 result for success, returns data�metadata�resultr(�reasonzWHMAPI1 call failed: z

Command: z
Args: z
Response: )�suppress_errorsr��cpanelresult�eventzWHMAPI1 call failed, command: r��errorszWHMAPI+UAPI call failed: z
Args:z
Full:N)rDr
r!)
rYr��commandrCr�r�r�r�rr�s
          r$�whmapi_success�Transfer.whmapi_successPse���8�8�J�'����\�\�(�+�F���{�!���h�/�� �+�F�8�;�w�i�H�!�F�,�x�j�:�$3���
�8�8�F�#�#��x�x��/����,�*�*�7�3�7�7��A�B�F��#�'�'��/�/��0��	�:���l�<�.�:� /��
����(�#����Z�Z��)�F���{����H�-�� �/��x�8 � '�y���v�W�T�F�D����:�:�f�%�%�r#c�Z�U(d0nXS'SUS'X$S'X4S'URSU5$)�6Makes a cpapi2 call on the origin server using whmapi.�cpanel_jsonapi_user�2�cpanel_jsonapi_apiversion�cpanel_jsonapi_module�cpanel_jsonapi_func�cpanel�r��rYr��module�funcrCs     r$�origin_cpapi2_call�Transfer.origin_cpapi2_callu�C����D�&*�
"�#�,/��
(�)�(.�
$�%�&*�
"�#��&�&�x��6�6r#c�Z�U(d0nXS'SUS'X$S'X4S'URSU5$)rr�3r
rrr
rrs     r$�origin_uapi_call�Transfer.origin_uapi_callrr#c���U(d0nSnSUS'[R"SURSU3USSUR30SS	S
9n[R
"SXUR5 URUR5XU5$![[4aUnSUR;a[S
5UeU(a[SUSUR3U5Ue[SU3U5UeSnAff=f)z�Performs a WHMAPI1 call on the origin server and returns its data
if successful. Requires accesshash which is obtained automatically.
N�1zapi.versionzhttps://z:2087/json-api/�
Authorizationz	WHM root:�xF)r��headers�timeout�verifyzCommand: %s Args: %s Output: %sz
Access deniedz6Failed to make remote WHMAPI call, invalid accesshash.z#Failed to make remote WHMAPI call: z | Output: )�requests�postrErLrar;�contentr�json�
ValueError�	TypeError�textrr
)rYrrCr�r��errs      r$r��Transfer.origin_whmapi_call�s'����D���	�"%�D����=�=��4�-�-�.�o�g�Y�G��(�I�d�6F�6F�5G�*H�I����D�
�M�M�1�7�$�,�,�
��&�&��	�	��W�O��
���I�&�	��$�)�)�+�+�L����� �9�#��k��|�|�n�&�#���	�
�5�c�U�;����
��	�s�A>B�C1�AC,�,C1c�N�U(d0n[X5nURXAX#5$)z:Performs a WHMAPI1 call and returns its data if successful)rr)rYrrCr�r�s     r$�whmapi_call�Transfer.whmapi_call�s'����D��w�%���"�"�4�$�H�Hr#c�^�[R[R-mSRU4Sj[	U555$)z5Generates a random alnum string with the given lengthrhc3�P># �UHn[R"T5v� M g7f�N)�random�choice)�.0�i�letnums  �r$�	<genexpr>�+Transfer.generate_string.<locals>.<genexpr>�s����D�m��v�}�}�V�,�,�m�s�#&)�string�
ascii_letters�digitsrI�range)rY�lengthr3s  @r$�generate_string�Transfer.generate_string�s0����%�%��
�
�5���w�w�D�e�F�m�D�D�Dr#c�z�SURS5-nURSUSSSS.5 SU3nSn[US	S
9nUR5nSSS5 [	XU5Ul[R"SU5 [R"[R"S
SSS95 [US3S	S
9nSRUR5R55nURn[SUSUSUSUS3	5 SSS5 [S5 g!,(df   N�=f!,(df   N+=f)z}Generates an SSH key to use for the migration. Provides a oneliner
to install the SSH key and also whitelist the IP address.
zautomigration-��generatesshkeypairrh�0�	cpmigrate)r�
passphrase�abort_on_existing_key�commentz/root/.ssh/�utf-8��encodingNzCreated SSH key %s.zcPlease paste the following on the origin server to install the key and whitelist this server's IP:
�magentaT��fg�bold�.pub� zbmkdir -p /root/.ssh/; ( read key; echo $key >> /root/.ssh/authorized_keys; echo $key > /root/.ssh/z
.pub; ) <<< 'z
'; csf -a z	; apf -a �;z/
Hit enter when finished to continue migration.)r;r*�open�readrrKrarb�click�echo�stylerIr�rArj�input)rY�key_name�key_path�key_coder��pubkeyr�s       r$r��Transfer.generate_sshkey�s:��$�d�&:�&:�2�&>�>����� � � �),�&�	
�	
�!��
�+����
�(�W�
-���x�x�z�H�.��h�(�;������*�H�5�
�
�
��K�K�D���	
�	
��X�J�d�#�g�
6�#��X�X�c�h�h�j�.�.�0�1�F��^�^�F��*�*2��=���I� ���6�(�!�5�
�7�	�@�A�1.�
-��7�
6�s�D�5AD,�
D)�,
D:c
���[RRURUS35nSS[	UR
5SSSSUSS	URRS
UR3/
nURRS5(aURS5 URRS
5(d URRS5(aURS5 [U[5(aURU5 OURU5 URU5 [R"SU5 [R "SX55 Sn["R$"U[&[&S9nUR)S5 Uc;UR+5nUR)5 [,R."S5 UcM;UR)SS9 US;a[1SUS35eUS:Xa[R2"SU5 O�US;ag[R4"SU5 U(a UR6RSUS35 OUR6RSUS35 SSS5 gUS :Xa
SSS5 g[R4"S!UU5 SSS5 g$!,(df   g$=f![8an	[1S"US#35U	eSn	A	ff=f)%zcPerforms rsync with the provided origin and destination.
Returns whether or not it was successful.
z.logz/usr/bin/rsyncz	--bwlimitz
--compressz	--archivez--hard-linksz
--log-filez-ez@ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=no -i z -p �deletez--delete�resync_homedirs�resync_mailz--ignore-existingzRSYNC COMMAND: %sz"RSYNC %s has started. Log file: %sN�r{�stderr�Waiting.r,T�rl)���#zRSYNC z$ failed due to SSH connection issue.�z,RSYNC %s received interrupt (SIGINT) signal.)��z4RSYNC %s reported partial transfer. Check rsync log.z& reported partial transfer. Check log.rz+RSYNC %s returned error code %s. Check log.zRSYNC (z) received interrupt signal.F)rHrrIrJrr:rKrFrCrDr��
isinstance�list�extendrar;rbr��Popenrrm�pollr�r�rr�r�rSr�)
rY�origin�destinationrr��	rsync_logrr��rsync�keyboard_errs
          r$�do_rsync�Transfer.do_rsync�s����G�G�L�L����4�&��
�>�	����������������|�|�(�(�)��d�.>�.>�-?�A�
�� �9�9�=�=��"�"��N�N�:�&��9�9�=�=�*�+�+�t�y�y�}�}�]�/K�/K��N�N�.�/��f�d�#�#��N�N�6�"��N�N�6�"����{�#��
�
�)�7�3����9�4�K�1	 ��H��!�!�������#�#�J�/��&�$�z�z�|�H��'�'�)��J�J�q�M��&��#�#��#�.��}�,�/� ���&J�K����r�>��M�M�F�����)��O�O�N�����
�
�)�)�$�T�F�+)�)��
�
�
�)�)�$�T�F�+)�)�� �G��H��]��K��N�O�O�E�� ��O�`�a��`��	!�	 �'��$��;�<���
 ��	 �sV�+ K�AJ;�B&J;�K�
J;�K�J;�2K�;
K	�K�	K�
K+�K&�&K+r,c��SnSn[R"UUUUS9nU(dURS5 UcBUR5nU(dUR5 [R
"U5 UcMBU(dURSS9 U[R:Xa2[URR5S5R5nXg4sSSS5 $!,(df   g=f![a gf=f)z3Runs a local command, returns code and Popen objectN)�stdinr{r_r`TrarE����N)r�rkrmrlr�r�r�rr{rP�
splitlinesr�)	rYrr�r�r��
command_inr�r��cmd_runs	         r$r��Transfer.local_commandGs���	��H��C��!�!�� �"�"�	�
���'�'�
�3��&�&�|�|�~�H� ��+�+�-��J�J�u�%�	�&�
��'�'�T�'�2��*�/�/�1��g�n�n�1�1�3�W�=�H�H�J�C��}�)����*!�	��	�s6�C4�AC#�;AC#�	C4�#
C1�-C4�1C4�4
D�Dc
��SSSSSSSSURRSURURU/
nSnSn[R
"XSUS	9nU(dUR
S
5 UcBUR5nU(dUR
5 [R"U5 UcMBU(dUR
SS9 US
:Xa[SUS35eU[R:Xa2[URR5S5R5nXg4sSSS5 $!,(df   g=f![ a gf=f)zARuns a remote command via SSH, returns the return code and
outputz/usr/bin/sshz-ozStrictHostKeyChecking=nozPasswordAuthentication=nozConnectTimeout=30z-iz-pNr^r`Trarbzorigin_command z5 failed due to SSH failure or remote command failure.rErv)rKrrFrEr�rkrmrlr�r�rr�rr{rPrxr�)	rY�cmdr�r�r��ssh_commandr�r��sshs	         r$r��Transfer.origin_commandksC��
��&��'�����L�L�����������
��	��H��C��!�!�������'�'�
�3��&�"�x�x�z�H� ��+�+�-��J�J�u�%�	�&�
��'�'�T�'�2��s�?�/�)�#��/2�2����*�/�/�1��c�j�j�o�o�/��9�D�D�F�C��}�)����*!�	��	�s7�D>�AD-�0A3D-�#	D>�-
D;�7D>�;D>�>
E�
Ec�|�[RRURS5n[R
"SU5 [
USSS9nURSUS9up4SSS5 WS	:Xa[R
"S
5 gUS:Xa[S5e[R"S
5 g!,(df   NS=f)z5Runs mysqlcheck with auto-repair on the remote serverzmysql-repair.logz-Starting MySQL database check+repair. Log: %s�wrErFz1/usr/bin/mysqlcheck --all-databases --auto-repair)r�Nrz%Completed MySQL check+repair process.rwzeMySQL repair ssh connection received interrupt signal. Cannot check status on remote server. Exiting.z.Failed MySQL check+repair process. Continuing.)
rHrrIrJrarbrOr�rr�)rY�
repair_logr�r�r�s     r$�mysql_repair�Transfer.mysql_repair�s����W�W�\�\�$�-�-�1C�D�
����;�Z�	
��*�c�G�
4���-�-�C��.��K�H�5��q�=��L�L�@�A�
��^�'�A��
�

�M�M�J�K�5�
4�s�
B-�-
B;c���[R"SUR5 URS[R
SS9upUS:Xa#UR
U5 SURlgg)zNTests the SSH connection to ensure it works and obtains the
remote accesshash
zTesting SSH connection to %s.z6/usr/local/cpanel/bin/whmapi1 accesshash --output=jsonr))r�r�rTN)	rarbrEr�r�r��load_accesshashrKr)rYr�r�s   r$r��Transfer.test_sshkey�se��	���4�d�6H�6H�I��+�+�D�"����,�
�
���q�=�� � ��%�%)�D�L�L�"�r#c�,�[U5S:�aH[R"US5nURS5nU(aURS5Ulg[S5e![R
Ran[S5UeSnAff=f)z>Loads the accesshash from the provided output from test_sshkeyrr��
accesshashTz1Received JSONDecodeError from parsing accesshash.Nz/Failed to obtain accesshash from remote server.)r�r#�loadsrDrL�decoder�JSONDecodeErrorr)rYr{�parsedr��
json_errors     r$r��Transfer.load_accesshash�s���
	��6�{�Q�����F�1�I�.���z�z�&�)���'+�x�x��'=�D�$��$�=�
�	
���|�|�+�+�	�'�C���
��	�s�AA$�$B�B�Bc���U(a7URSUSS.5 [R"SXR5 gUR	SUSS.5 [R"SU5 g)z1Removes the SSH key from the provided destination�deletesshkeyr@)�file�leave_authorizedzRemoved key %s on %s.zRemoved key locally: %sN)r�rarbrEr*)rYr�remotes   r$�
remove_sshkey�Transfer.remove_sshkey�s]����#�#���#� F�
�
�L�L�0�#�7I�7I�J������#� F�
�
�L�L�2�C�8r#c�z�URSU3[RSSS9up#US:XaUH
nX;dM
 g g)z=Returns True if the given package is installed on the origin.z
/bin/rpm -qa r)Tr�rF)r�r�r��rY�packager�r��lines     r$�check_origin_package�Transfer.check_origin_package�sR���+�+��G�9�%�"�����	,�
�
���q�=����?���r#c�z�URSSU/[RSSS9up#US:XaUH
nX;dM
 g g)z=Returns True if the given package is installed on the target.z/bin/rpmz-qar)Tr�rF�r�r�r�r�s     r$�check_target_package�Transfer.check_target_package�sR���*�*�
���(�"�����	+�
�
���q�=����?���r#c�0�URSSU/SS9up#U$)z=Returns 0 if given service restarts, else returns error code.�/usr/bin/systemctl�restartr()r�)r��rY�servicer�r�s    r$�restart_service�Transfer.restart_service�s.���(�(�
!�9�g�6�a�)�
����r#c�P�URSSU/[RSSS9up#U$)z?Returns 0 if given service is running, else returns error code.r�r�r(Tr�r�r�s    r$�service_status�Transfer.service_statuss9���(�(�
!�8�W�5�"�����	)�
����r#c��UR5(a[UR5$SUR;dURUR;agg)z/Checks if the Environment should be ran or not.�allFT)�check_should_resyncr!�resync_relevantrGr�rY�envs  r$�should_run_environment�Transfer.should_run_environmentsC���#�#�%�%���+�+�,�,��D�N�N�"�c�h�h�$�.�.�&@��r#c�>�[5nURHQnURU5(a1URURS-UR5U45 MJSUlMS [RUl	UR5(d�UR5SnUR(d+U(aURU5 O3URU5 O![R "SUR"5 UR5(dM�gg)z8Checks origin for any config that need to be moved over.rwTr)z&Skipping Environment %s, already done.N)rrBr��put�priority�qsize�skippedr&r.rT�emptyrD�	completed�check�runrarbr)rYr��queuer��next_envs     r$�match_environment�Transfer.match_environments�������$�$�C��*�*�3�/�/��	�	�3�<�<�"�,�e�k�k�m�S�A�B�"���	%�#�.�.��
��+�+�-�-��y�y�{�1�~�H��%�%���N�N�4�(��L�L��&����<�h�m�m���+�+�-�-r#c
��SnUR(a�URURR5 URURRS35 URURRS3SS9 SURlO[R
"S5 U(a[R"S5 [R
"S	5 UR5(d9[R
"S
5 URHnUR5 M [UR5S:�a2SRUR5n[R"S
U5 [R "[R""SSSS95 g![a$n[R"SU5 SnSnAGN SnAff=f!U(a[R"S5 [R
"S	5 UR5(d9[R
"S
5 URHnUR5 M [UR5S:�a2SRUR5n[R"S
U5 [R "[R""SSSS95 f=f)zLPerforms final clean, removes extraneous SSH keys and performs
final report
FrLT)r�z,No SSH key was generated, nothing to remove.zFailed to remove keys: %sNzbDue to failure, please double check both servers to ensure there are no left-over authorized keys.zMigration clean up completed.zUser status report:rz

z%Important end of transfer notes:

%s
zEnd of migration.�greenrI)rKr�rrrarb�	Exceptionr�r�r�rR�reportr�rSrIrQrRrS)rY�failedr'r�rSs     r$rX�Transfer.clean_up7s��
��	P��|�|��"�"�4�<�<�#4�#4�5��"�"�d�l�l�&7�&7�%8��#=�>��"�"�d�l�l�&7�&7�%8��#=�d�"�K�'+����$����K�L�
����E��
�L�L�8�9��+�+�-�-����2�3� �J�J�D��K�K�M�'��4�:�:���"����D�J�J�/�����>���
�J�J�u�{�{�#6�7��N�O��-�	��M�M�5�s�;��F��	������E��
�L�L�8�9��+�+�-�-����2�3� �J�J�D��K�K�M�'��4�:�:���"����D�J�J�/�����>���
�J�J�u�{�{�#6�7��N�O�s=�BF)�G�F)�1G�)
G�3G�G�G�G�C8Kc���URH>nSUR;dMUR(aM(XlUR	5 M@ UR5 UR
5 g)zVFinal post-migration steps, any Environment related tasks
and also domain validation.
r�N)rBrGr��xfer�
post_transferr��
check_ipaddrsr�s  r$r��Transfer.post_transfer]sT��
�$�$�C��D�N�N�*��{�{�{�#�H��%�%�'�	%�	
���!����r#c�|�URS5nURS5nU(GaUGH
nURS5n[URS55n[URS55nU(aMRU(a�/nURH3nURU:XdMURSUR35 M5 [U5S:�aSUS3n	U	S	RU5-
n	OSUS
3n	URRU	5 M�URRSUS35 GM
 gg)
zOChecks all IP addresses on origin to find out what needs to be
moved/assigned.
�listipsr��mainaddr�usedz
- rzOrigin has dedicated IP z
 assigned to:rhz] assigned to a user not being migrated or is marked as used without being assigned to a user.z' which is not assigned to any accounts.N)
r�rDr!rRr�r�rr�rIrS)
rYr��ipaddrs�ipinfo�uip�mainr�rRr��notes
          r$r��Transfer.check_ipaddrsks$��
�&�&�y�1���(�(�4�.���!���j�j��&���F�J�J�z�2�3���F�J�J�v�.�/���t�� "��$(�J�J�D�#�{�{�c�1� %���t�D�I�I�;�-?� @�%/��u�:��>�%=�c�U�-�#P�D� �B�G�G�E�N�2�D�#;�3�%�@D�!D�!�
�
�
�)�)�$�/��
�
�)�)�6�s�e�<8�8��+"�r#c�n�URHnURU:XdMUs $ [U<S35e)z5Returns the Environment class based on name provided.z not found in environments)rBr�KeyError)rY�env_namer�s   r$�get_environment�Transfer.get_environment�s8���$�$�C��x�x�8�#��
�%��(��%?�@�A�Ar#c
�8�UR(aUR5 gUR(d%[R"S5 URSS9 SUR
;Ga1UR
5(Gd[R"[R"SSSS95 URGH!n[UR5S:�a�[R"[R"URS	SS95 [R"UR5 [R"S
5 URHnnURS5(a,[R"[R"USSS95 ME[R"[R"US
S95 Mp [R"5 SUlGM$ SUlUR"R%5 S[&R(;a*[R"[R"SSSS95 UR*R-S5(d.[R."S5(aSUlO[1S5eUR5 ggg)z6Responsible for checking and running the Environments.z,Running environment checks on origin server.T)r�r�zjNOTICE: Please read the actions each environment check will perform and confirm that you wish to continue.rHrIr�cyanz#Relevant information and action(s):z!!�redr�)rJF�STYz�Screen detected. Please ensure to read over all of the Environment actions above. You can use Ctrl+A followed by the '[' key to enter 'copy mode' which will allow you to scroll up using the arrow keys. Press escape to exit copy mode.�noenvconfirmz"Are you sure you want to continue?z#Did not accept environment changes.N)rQr�rPrarbrGr�rQrRrSrBr��actionsrr�
startswithr�rU�saverH�environrCrD�confirmr)rYr��actions   r$�run_environment�Transfer.run_environment�s�������"�"�$��#�#����K�L��&�&�T�&�2��D�N�N�*�4�3K�3K�3M�3M��
�
��K�K�O�$�!�	�� �,�,�C��3�;�;�'�!�+��
�
�5�;�;�s�x�x�F��#N�O��
�
�3�;�;�/��
�
�#H�I�&)�k�k�F�%�0�0��6�6� %�
�
�$)�K�K��5�t�$L�!"�!&�
�
�5�;�;�v�'�+J� K�
'2��
�
��$)�C�M�-�$(�� ����!�!�#��B�J�J�&��J�J����)� )�!%�	���y�y�}�}�^�4�4��}�}�%I�J�J�,0��)�3�A����&�&�(�[4N�*r#c��[R"SUR5 URR[
RR::aUR5 URRS5(dUR5 UR5(a<UR5 [R"S5 [
RUlgURR[
RR::aUR5 URR[
R R::aUR#5 URR[
R$R::aUR'5 [R"S5 [
RUlg)z+Primary function for running the migration.z Running migration, user list: %s�
skiprepairzRe-sync process is complete.NzMigration is complete.)rarbrRrT�valuer&r.r�rCrDr�r��run_resyncsr2r/r�r0r�r1r�rcs r$�
run_migration�Transfer.run_migration�s.�����7����D��:�:���}�8�8�>�>�>�� � �"��y�y�}�}�\�*�*������#�#�%�%������L�L�7�8�&�0�0�D�J���:�:���}�6�6�<�<�<������:�:���}�8�8�>�>�>����!��:�:���}�8�8�>�>�>���� ����-�.�"�,�,��
r#c���URRS5(agURRS5(agURRS5(agg)zCheck if a resync arg was set�resync_databasesTr]r\F)rCrDrcs r$r��Transfer.check_should_resync�sI���9�9�=�=�+�,�,���9�9�=�=��'�'���9�9�=�=�*�+�+��r#c���[R"[R"SSSS95 URR	S5(a!URS5nUR
5 URR	S5(aUR5 URR	S5(a$URHnUR5 M g	g	)
zRun re-syncsz<Running requested re-syncs. Everything else will be skipped.r�TrIr��mysqlr\r]N)
rQrRrSrCrDr��
run_resyncr�rR�
rsync_maildir)rY�	mysql_envr�s   r$r��Transfer.run_resyncs�s���
�
�
��K�K�N���
�	
��9�9�=�=�+�,�,��,�,�W�5�I�� � �"��9�9�=�=�*�+�+����!��9�9�=�=��'�'��
�
���"�"�$�#�(r#c��URVs/sHoR5PM snURVs/sHo"R5PM snURURUR
URRURURURURURS.$s snfs snf)z/Captures necessary information for journal.json)rRrBrJr?rSrTrCrMrNrQrP)
rR�
capture_staterBrJr?rSrTrrCrMrNrQrP)rYr�r�s   r$r��Transfer.capture_state	s���8<�z�z�B�z�t�(�(�*�z�B�<@�<M�<M�N�<M�S�.�.�0�<M�N��
�
� �-�-��Z�Z��Z�Z�_�_��I�I��/�/�"�1�1� �-�-��+�+�
�	
��B��Ns
�C�Cc�*�URS5UlURS5UlURS5Ul[URS5UlURS5Ul[URS55Ul[U5Ul
[URS55Ul[URS55Ul
URS	5HJn[URS
5U5nURU5 UR R#U5 ML URS5HNnUR$H;nUR&URS
5:XdM$URU5 XlM= MP UR+5 UR
[R,:Xa[/S5eUR15 [2R4"S
5 UR75 g)zILoads necessary information from journal.json and resumes the
migration.
rJr?rSrTrMrNrQrPrRrrBz-Cannot resume a transfer marked as completed.z)Resuming migration from provided journal.N)rDrJr?rSr&rTrMr�rNrrUr!rQrPr�
load_staterRr�rBrr�r�r2rr�rarbr�)rY�	loadstate�usrr��einr�s      r$r��Transfer.load_states���"�
�
�j�1��
�%�M�M�.�9����]�]�7�+��
�"�9�=�=��#9�:��
�#�-�-��5���!�)�-�-�0@�"A�B����t�}��� ����~�!>�?����	�
�
�m� <�=����=�=��)�C��������.�D��O�O�C� ��J�J���d�#�*�
�=�=��0�C��(�(���8�8�s�w�w�v��.��N�N�3�'�#�H�)�1�	
�����:�:��0�0�0�'�?��
�	
�
�
�����@�A����r#)rLrQrCr:rPrNr;rMrBrUrJr?rArSrFrErOrGrKrTrR)r�F)TN)Fr.)NF)8rrrrrr[rdrmr�r�r�r�r�r�r@r
r�r�r�r�r�r�r�r�rrrr�r*r;r�rrrr�r�r�r�r�r�r�r�r�r�r�r�rXr�r�r�r�r�r�r�r�r�r"rr#r$r4r43s���A�'�2%�+�,�:!�*!�,L�0 ��:�����
�!��!�
�	7��	7��J��J�,�+��+�"�&��&��6
�#�J7�7�$�LI�E�
*B�X��]��]�D����
"�H)*�w�e�+�Z�L��L�0
*�
�$9�� � �
������0�#P��#P�J�����!��!�FB��6)��6)�p-�8�%�(
� !r#r4)&rr#rarHr/r�r6r�rzr�rVr�dataclassesr�enumrr�rrr rQ�cpapisr�colorloggerr	�environments.baser
rrUrr
r�rrrrr&r4rr#r$�<module>rs������	�
�
�
��
��
��!�������&�?�'�%�%�������D��G�Gr#