jgQ2ddlmZddlmZddlmZddlZddlZddlZddlZddlm Z m Z ddl m Z ddl Z ddlmZmZddlmZdd lmZmZmZdd lmZdd lmZmZmZdd lmZdd lm Z Gdde!Z"Gdde!Z#GddeZ$ddZ%dS))print_function)division)absolute_importN)AnyStrOptional) iteritems) get_abs_relrun_process_in_cagefs)drop_privileges)BaseSelectorErrorMissingVirtualenvErrorMissingAppRootError) WSGI_PATH)ClSelectExcept clpassenger clselectctl)BaseApplicationsManager) PythonManagerceZdZdZdZdZdS)PythonAppFormatVersionz@ Class represents possible python application versions. N__name__ __module__ __qualname____doc__LEGACYSTABLE[/opt/cloudlinux/venv/lib64/python3.11/site-packages/clselect/clselectpython/apps_manager.pyrr#s$ F FFFr!rceZdZdZdZdZdS)PythonVenvFormatVersionzJ Class represents possible python application venv path versions. rrNrr r!r"r$r$.s& FFFFr!r$ceZdZdZdZdZdZdZdZdZ dZ fdZ dfd Z e d Zd Zd ZddZddZdZdZdZdZddZdZdfd ZdZddZddZxZS)ApplicationsManagerz!.cl.selector/python-selector.jsonz/var/lve/.py.migration.statusz!.cl.selector/.py.migration.statusz//var/log/cloudlinux/py-selector/app_imports.logzimported_pip_requirements.txtpython virtualenvcptt|tdSN)superr&__init__r)self __class__s r"r,zApplicationsManager.__init__Ds, !4((11-//BBBBBr!r cZtt||||S)N)patterns)r+r&_find_config_files)r- user_name app_directoryr0r.s r"r1z&ApplicationsManager._find_config_filesHs*($//BB9mfnBooor!c2d|g}|S)z Return lines for htaccess that are specific to the python interpreter :param binary_path: path to the environment's python binary :param app_config: application's config dictionary zPassengerPython "{}" )format) binary_path app_configspecific_liness r"(get_interpreter_specific_passenger_lineszZ__EVEV@W@WXXX  !*--9 *9 *HD" gll29nEEO7==11 #q(#"""VZVZVZ\k\k\k#lmmm OO/ 6 6 60 *%T** ! !H,B,D,D ! !$!%!D!DIry")"="=,$$$!#$)-(E(EdJ(W(W%D+!  ! ! ! ! ! ! ! ! ! ! ! ! !! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !-6dOO22(Hj6))5;H5En5U >2171A*1M :.224:NNN(* RY$Jh(i(i%w~~&788Z :*^B\\\">299$:XYYY'1F8$$ "%V[[]]!3!3c$))++6F6F!F .))HJJx(((($T**mmH,B,D,Dmm44T6BBB77U`k7lllmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm * * * X $$\4;;;$T**llH,B,D,Dll77T_j7kkkllllllllllllllllllllllllllllll##D)))))))) * ,,\::: ! # #   E F F F F F $ #s!6M&F,F&EF E FE F0 F,; M& F,F F,F F, M&,F0 0M&3F0 4D?M&3M/M6 MM M M M M&M M& M !M&& P0(PO(,O  O(O O(O O( P(O, ,P/O, 0PPc|htj|jsdSt |j5}t j|cdddS#1swxYwYdStj|}tj |j |j }tj |sdSt |5}t j|cdddS#1swxYwYdS)zz Read import log which contains information about failed users or apps depending on current user. N) rSrTexists_IMPORT_STATUS_ADMINrXjsonloadpwdgetpwnamrUr_IMPORT_STATUS_USERrV)r-rcrjuser_pwmarkers r"_get_legacy_import_statusz-ApplicationsManager._get_legacy_import_statussb <7>>$";<< td/00 $Ay|| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $l4((GW\\'.$2JKKF7>>&)) f $y|| $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s#AA"%A"C::C>C>ctjtj|jdsdS tj|j|j}t jtj ||d|ddS#ttf$rtj dYdSwxYw)z Save information that some applications were not imported automatically to show warning for user in the future. rN)rapps_lookup_failedz$Unable to save migration status file)rSrTrrUrrrwrite_file_via_tempfilerdumpsIOErrorOSErrorrur`)r-rrrrs r"rz2ApplicationsManager._set_user_legacy_import_status s w~~bgll7>>JJKK  F FW\\'.$2JKKF  ,TZ$4&199..     ! F F F  D E E E E E E FsAB%CCchtjd|i}tj||jddS)z Save information that some users were not imported automatically to show warning for admin in the future. rrN)rrrrr)r-rpackeds r"rz3ApplicationsManager._set_admin_legacy_import_statuss6 ^\:;;(1JERRRRRr!c|}||dsdSdd|d|j|j}|S)NraUnexpected issue(s) happened during importing python applications for the following users: "{users}". Everything will work as before, but listed users wo't be able to control applications from Python Selector UI. Please, contact CloudLinux support and send them log file located at `{log_file_path}` to investigate and resolve this issue. Also you can hide this warning by deleting `{import_warning_marker}` file,)users log_file_pathimport_warning_marker)rr5rUrzr)r-r warning_msgs r" _get_admin_legacy_import_warningz4ApplicationsManager._get_admin_legacy_import_warning$so//11 >!7>4 chhvn/E&F&F.2.C6:6O QQ r!c||}|dSd|j}|drd|zS|dr!dd|dd |SdS) N)rcaEverything will work as before, but you won't be able to control listed applications from Python Selector UI. Please, ask you hoster to contact CloudLinux support to investigate and resolve this issue. Also you can hide this warning by deleting `~/{import_warning_marker}` file.)rrzEUnexpected issue(s) happened during importing python applications. %srzNUnexpected issue(s) happened during importing following python applications: "rz". )rr5rrU)r-usernamerwhat_to_do_msgs r"_get_user_legacy_import_warningz3ApplicationsManager._get_user_legacy_import_warning6s//X/>> >4  vD,DvEE  $ % S() )( ) S S88F+?$@AAAA>>S Str!cX||S||Sr*)rr)r-rcs r""_get_legacy_import_warning_or_nonez6ApplicationsManager._get_legacy_import_warning_or_noneNs- <88:: :77== =r!cddlm}|||}|tjd|z|dt jkrtjd|z||||} |d n-#t$r }tjd |d |d d}~wwxYwt j|d<| |||dS) zo Update environment of specific application to support features of new python selector r_get_environmentNzApplication %s does not existrCzCApplication %s is already new version and does not need any updates apps_managerT) auto_restorezUnable to migrate application z . Error is 'z', everything restored to work as before migration. Try again later or ask your hoster to contact CloudLinux support if the issue persists.) rOrget_app_configr WrongDatarrconfigure_environmentr_add_app_to_config)r-rcrrrF environmentrs r"migrate_applicationz'ApplicationsManager.migrate_applicationTs/ @?????))$99   *+JX+UVV V } %)?)F F F *02:;<< <'&tXDIII  $  - -4 - @ @ @ @ $ $ $ ** hh #$$ $ $&<%B M" tX{;;;;;s2B B3B..B3ctt||}||}|||d<|S)NrW)r+r&get_applications_users_infor)r-rcresultrWr.s r"rz/ApplicationsManager.get_applications_users_infoqsL*D11 ' ' - - 99$??   'F9  r!ct||\}}tj||j}tj|rdSddlm}t|5||||}| } dddn #1swxYwYtj |j } d| i} d} t|| ddg| } | d dkrE|d t| d zt!j| d | d s| d } t|5t%j5t)|d}|| |dddn #1swxYwYddddS#1swxYwYdS)z Generates requirements file from python apps :param user: username :param app_root: app root :param app_logger: app logger NrrrHOMEr@freezez-l)rM returncodez/Error during generation pip requirements file. outputfailedw)r rSrTrUrrrOrr piprrrr rWstrrExternalProgramFailedrrrXwriteclose)r-rcrrapp_path_req_pathrrpip_path user_homerMmodulesrrjs r"rz-ApplicationsManager.generate_pip_requirementsys"$11 !7<<$*HII 7>>( # #  F?????? T " " ) )**hT333K#((H  ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) L&&- I&&tX$7GRZ[[[ , 1 $ $   PSVW]^fWgShShh i i i 6vh7GHH H! 'X&GT " "  H$:$<$<  Xs##A GGG    GGIII                                 sH/#BB"%B" F?!:F' F?'F+ +F?.F+ /F??GGTc|j|j}t||\}}tj||}|r.tj|st|tj||}|r.tj|st|||fS)a Calculate, check exists and return application folders This method does not check that application exists in config. :raises: NoSuchUserException, MissingVirtualenvError, MissingAppRootError :return: tuple(app_root, app_venv) with absolute paths ) _pwdget_pw_by_namerget_venv_rel_pathrSrTrUrr r) r-rrchk_env chk_app_rootrrrel_pathapp_venvs r"get_app_foldersz#ApplicationsManager.get_app_folderssI,,X66= '(;; 87<< 844  327>>(33 3(22 27<< 844  0x 8 8 0%h// /!!r!c|||}||j}t||\}}tj|||d|S)a Return a path to the environment's interpreter binary Get interpreter path for application :param user: owner of the application :param app_root: app path relative to user home (app-root) :param user_dir: User's home directory :param binary_name: name of binary in virtual environemnt (python, npm, node) :return: path to interpreter binary in virtual environment Nbin)get_interpreter_version_for_app BINARY_NAMErrSrTrU)r-rcruser_dir binary_nameversionrrs r"get_binary_pathz#ApplicationsManager.get_binary_pathsV66tXFF  *K'h77 8w||Hh LLLr!)r r*)TT)rrr _USER_CONFIGrrrzrrRVENV_DIRrr,r1 staticmethodr9rrrrrrrrrrrrrrr __classcell__)r.s@r"r&r&:s6L:=H ?KHKCCCCCpppppp\D$D$D$L   JGJGJGJGX$$$$(FFF"SSS$0>>>> <<<:$$$L""""&MMMMMMMMr!r&c Xt|tjtj|\}}t|tjtjt j|\}}|Stj|r&|t j|krtj |fStj |fS|tj krtj |fS|tj krtj |fStd|z)a6 Get path to users VENV relative to home dir. Old python selector transforms app_root using get_prefix() method before creating venv directory. We should handle both cases: - when app is from old selector - when app is from new selector and return right env directory. If both old and new vevns exist we use old one. Return tuple with two values: - detected version of venv path (LEGACY for path with replacements "/" -> "_" & "_" => "__" and STABLE in other case) - path to venv relative to user's home dir Nzunknown version `%s`) r rSrTrUr&rr get_prefixrr$rr ValueError)rcr=rr new_rel_dir old_abs_dir old_rel_dirs r"rrs!rw||4G4PR['\'\]]NA{* bgll.79OPY9Z9Z[[ ] ]K 7>>+ & & ?9 8Ny8Y8Y+Y+Y*1;> >&-{:: +2 2 2&-{:: +2 2 2&-{::/'9:::r!r*)& __future__rrrrurSrrtypingrr future.utilsrrclselect.utilsr r clcommon.clpwdr clselect.baseclselectr r rclselect.clpassengerrclselectrrr"clselect.baseclselect.apps_managerr&clselect.clselectpython.python_managerrobjectrr$r&rr r!r"rs&%%%%%&&&&&& ########""""""========****** +*****==========FFFFFF@@@@@@V     f   FMFMFMFMFM1FMFMFMT ;;;;;;r!