&07\ddlmZddlmZddlmZddlZddlZddlZddlmcm cm Z ddl Z ddl mZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlm Z m!Z!ddl"m#Z#m$Z$ddl%m&Z&dZ'e#Z(d&dZ)d'dZ*dZ+dZ,d'dZ- d(dZ.dZ/d'dZ0d'dZ1d'dZ2d'dZ3d'd Z4d)d!Z5d*d#Z6d$Z7d%Z8dS)+)print_function)absolute_import)divisionN)ConnectionError) iteritems) basestring) clpassenger) clselectctl)utils) userdomains) mod_makedirs)ClSelectExcept)ApplicationsManager) environments interpreters)APP_STARTED_CONSTAPP_STOPPED_CONST)clprintzapp.jsFc2t||}|s|}tj|||}|r|rV t jd|}n"#t $rt j|wxYw||||S)Nversion)key) destroy_first) _get_prefixr EnvironmentexistsrKeyErrorrNoSuchAlternativeVersioncreate)user directoryrenv_namerprefix environment interpreters /./commons/lib/clselect/clselectctlnodejsuser.py_create_environmentr'$s y ) )F *8T6BBK     E=E C&3 BBB7KKK C C C 9'BB B C;mDDD s AA=ct||}|Qtj|} tj|||}n%#t $rt jd|zwxYw|d}tj tj tj |}tj |||}|S)N8No such application (or application not configured) "%s"binary)rr summaryr get_using_realpath_keysrrNoSuchApplicationospathbasenamedirnamerr)r r! app_summaryr# user_summaryr*r"r$s r&_get_environmentr42s y ) )F"*400  7iVVKK    2J   "Fw0G0G H HIIH*8T6BBK s ?"A!ctj||\}}tjt j|SN)r get_abs_relr.r/joinrDEFAULT_PREFIX)r r!_rel_dirs r&rrBs1"433JAw 7<< 3W = ==cddlm}||st j||ddS)z Check whether particular interpreter version is enabled and raises exception if not :param user: user to include in exception r ) node_managerzversion is not enabledN)clselectnodejsr> NodeManageris_version_enabledrUnableToSetAlternative)rr r>s r&_ensure_version_enabledrCGs_ -,,,,,  # # % % 8 8 A A53 '355 555r<cD|t}tj||\}}||}|D]M}tj||\}}|t jz|t jzr|cSNdS)z Return None if new_app_directory is not nested inside an existing user application, and the name of conflicting application otherwise N)rr r7get_user_config_datakeysr.sep startswith)new_app_directoryr apps_manager abs_app_pathr: full_config existing_appexisting_app_abs_paths r&_get_existing_nesting_app_forrOSs *,, '.?@@OL!33D99K#((**  #(#4T<#H#H q 26 ! - -.Cbf.L M M     4r<c :| t} |tjdt|||t}|i}|d}t ||| } | tj| tj|}t|||} | j } tj |||| j| ||| tj|||s2tj|}t#j|||}|d}|||t&||g|d}| r| |d<| ||| | ||||dS#t,$r0}t/jdd t3|d Yd}~dSd}~wwxYw) a Create application :param user: unix user name :param directory: application path in user's home (app-root) :param alias: alias (app-uri) :param version: version of interpreter :param doc_root: doc_root :param app_mode: application mode for nodejs :param env_vars: dict with enviroment variables :param startup_file: main application file :param domain_name: domain name :param apps_manager: Application Manager. Class that responsible for gathering and writing information about applications :param passenger_log_file: Passenger log filename to write to app's .htaccess :return: None NzNot passed version as argument production)doc_root startup_filepassenger_log_filedomain)nodejs_versionrUapp_uri app_statusrSapp_mode config_filesenv_varsrTtextERRORstatusmessage)rr WrongDatarCDEFAULT_STARTUP_FILErOBusyApplicationRootr get_aliasr'r%r*r configure INTERPRETERrestartr+r r,DEFAULT_APP_STATEadd_app_to_configadd_env_vars_for_htaccess Exceptionr print_diagstr)r r!aliasrrRrYr[rS domain_namerJrTconflicting_appr$r* summary_datar2app_dataerrs r&rrfs&*,, &'GHHHGT***+ 3It\RRO"0AAA  !% ( (E%dIw??K  $ $ & & -F$ 5,2JF#+,-?AAAAi((( ,"*400 3D)\RR !(+ "(%  H=*<&'""4H===M..tY(SSSSS MMM6g#c((#K#KLLLLLLLLLMsE F*%FFcd}tjtj|j|d}tj|r= tj|nZ#t$rtj dd|izwxYwtj |rtj |tj|d}tj |st|d tj||dS#t$rtjd|zwxYw) z Creates symlink to app's node_modules in app_root :param username: user name :param environment_path: Path to app's virtual environment :param app_root: Application root :return: None zCloudlinux NodeJS Selector demands to store node modules for application in separate folder (virtual environment) pointed by symlink called "node_modules". That`s why application should not contain folder/file with such name in application root node_modulesz"Can`t remove symlink %(link_name)s link_namezlib/node_modulesizError creating symlink. N)r.r/r8pwdgetpwnampw_dirislinkremoveOSErrorrRemoveSymlinkErrorr SymlinkErrorrsymlinkCreateSymlinkError)usernameenvironment_pathapp_rootr`rvlink_tos r&_create_symlink_to_node_modulesrsW3G S\(33:HnUUI w~~i  3  Ii     34i()    " "3)'222gll+-?@@G 7>>' " "%We$$$V 7I&&&&& VVV/0JW0TUUUVsA00$BD11"Ec|t}|||} ||||d|n<#t$r/}t jddt |dYd}~nd}~wwxYw|dtkrtj |j }d |d|g} tj|n#t$rYnwxYw|+||||}t#j|||| t#j||n#t*j$rYnwxYwdSt/||} t1j|| \} } tj| n#t$rYnwxYwt#j|} |||} t1j||| n*#t8$r| rYdSt+jd|zwxYwt#j|| t#j||dS#t*j$rYdSwxYw) aa Destroy web app with specified directory for specified user :param user: username :param app_directory: application directory :param doc_root: Document root for selected domain :param apps_manager: Application Manager. Class that responsible for gathering and writing information about applications :return: None Nr\r]r^rX/nodevenvr))rget_app_configrjrkrrlrmgetrrwrxryr8shutilrmtreer|get_htaccess_by_appdirr $remove_passenger_lines_from_htaccessremove_app_from_configrgrMissingApprootDirectoryrr r7r+r,rra unconfigure)r app_directoryrRrJ app_configrs user_home nodevenv_pathhtaccess_filenamer#abs_dirr:r3 app_in_configs r&destroyrs*,, ,,T=AAJM    2 24h W W W MMM6g#c((#K#KLLLLLLLLM~~l##'888L&&- )Z!GHH   M- ( ( ( (    D    , C CD-Ya b b   <=N O O O++D-@@@   m 4 4 4 45    D  } - -F"400JGQ  g      &t,,L 77mLLM< %dM<HHHH <<<  <44 *+g-:,;<< < <D-000 D-00000  1     sqA A< %A77A<C$$ C10C18EE E F## F0/F0G55 HH5I IIcT|t}|||}|tjd|z|dt krdS|||||||t tj ||dS)a` Starts web app with specified directory for specified user :param user: username :param app_directory: application directory :param doc_root: Document root for selected domain :param apps_manager: Application Manager. Class that responsible for gathering and writing information about applications :return: None Nr)rX) rrrrarrupdate_htaccess_fileset_app_statusr rgr rrRrJrs r&startrs*,, ,,T=AAJ&'c)6(788 8~~l##'888%%dM8DDDm5FGGGm,,,,,r<c|t}|||}|tjd|z|dt krt |||dStj||dS)ab Restarts web app with specified directory for specified user :param user: username :param app_directory: application directory :param doc_root: Document root for selected domain :param apps_manager: Application Manager. Class that responsible for gathering and writing information about applications :return: None Nr)rX) rrrrarrrr rgrs r&rgrg:s*,, ,,T=AAJ&'c)6(788 8~~l##'888 dM8,,,,,D-00000r<c~|t}|||}|tjd|z|dt krdS|||||}tj|| ||t tj ||dS)a_ Stops web app with specified directory for specified user :param user: username :param app_directory: application directory :param doc_root: Document root for selected domain :param apps_manager: Application Manager. Class that responsible for gathering and writing information about applications :return: None Nr)rX) rrrrarrrr rrrg)r rrRrJrrs r&stoprSs*,, ,,T=AAJ&'c)6(788 8~~l##'888$;;D-QY[eff45FGGGm5FGGGm,,,,,r<cd}d}d}tjjtjt |stjdd ||}ddi} tj |d | }nd#t$rW| d d } tj |d | }n%#t$rtj ||zwxYwYnwxYw|j d } |j d} | tj |d | }n%#t$rtj ||zwxYw|j d } |j d} | | ks&| | kr| | dddks| (| dddkrtj || | | fzdSdS)a@ Check response from user's webapp before and after calling action. Also compare both responses :param domain: domain associated with webapp :param alias: URI associated with webapp :param action: called action, that make something with webapp: install modules, transit it, etc :return: None zTWeb application is inaccessible by its address "%s". The operation wasn't performed.zThe operation was performed, but check availability of application has failed. Web application is inaccessible by its address "%s" after the operation.zThe operation was performed, but check availability of application has failed. Web application responds, but its return code "%s" or content type before operation "%s" doesn't equal to contet type after operation "%s".z+Wrong action for calling in checking webappzhttps://{domain}/{alias})rUrnz User-AgentzjMozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101209 CentOS/3.6-2.el5.centos Firefox/3.6.13F)verifyheaderszhttps://zhttp://z Content-TypeStatusN500)requestspackagesurllib3disable_warningsurllib_exceptionsInsecureRequestWarningcallablerraformatrrreplace WebAppErrorrlower) rUrnactionapp_is_inaccessible_beforeapp_is_inaccessible_after app_is_broken webapp_urlrrequestbefore_mime_type before_statusafter_mime_type after_statuss r&check_response_from_webapprqs"y!kmM ../@/WXXX F  V&'TUUU+223J NGV,z%III VVV'' I>>  Vl:eWMMMGG V V V ,-G*-TUU U V G V**>::O''11M FHHHQ,z%III QQQ()BZ)OPPPQo)).99O?&&x00L?#8#8#:#::: , & &=+DWYXYWYIZ^cIcIc(\"1"-=-F-F(,HXZi9j)jkkk)(-F-Fs64B C--CC-"C''C-,C-/E"E)ctjdtjdd}t}fd|D}t|dkrtjd|d}||fS)z Get info (alias and domain) about user's web application :param app_summary: dict -> summary info about user's web application :param user: str -> name of unix user :return: tuple -> (alias, domain) Nz3Was passed incorrect summary info about applicationz!Was passed incorrect name of userrncRg|]#\}}tj|d!|$S)docroot)r realpaths_are_equal).0rUrRr2r s r& z*_get_info_about_webapp..sNRRR'vx(xY9OPPR RRRr<rz-Can not found suitable domain for application)rrar len)r2r rn user_domains found_domains app_domains`` r&_get_info_about_webapprs&'\]]] |&'JKKK  Et$$LRRRRR+7RRRM =Q&'VWWWq!J * r<-cN |t}|} tj|}n5#t$r(t jdwxYw|dtkrd}n|d}|d}|d} tj \ } t|  fd} |sC t||| dS#t j $r} t j d | zd} ~ wwxYw| dS) a Install nodejs extension to user's webapp :param user: name os unix user :param directory: directory with webapp (app-root) :param extension: name and version of extension :param skip_web_check: skip check web application after change it's properties :param apps_manager: Application Manager. Class that responsible for gathering and writing information about applications :return: None Nz%Record about application {} is absentrXTrWrUrVc`tjdS)N) extensioncwd)extension_installr rg)rr!r$rr sr&rzinstall..actions5%% s%CCCD),,,,,r<)rUrnrz3An error occured during installation of modules. %s) rrEr r,rrrarrr7r'rr)r r!rskip_web_checkrJ user_configrrrnrrVr:rrsrr$s``` @@r&installrs*,, 33D99Kb0y+NN bbb&'N'U'UV_'`'`aaab !222#h' ./N  tY / /FC%dI~FFK---------   j &!       ) j j j ,-beh-hii i j s#A2A5C--D<DDcli}ttj|D]\}}|dtjkrt |||}|d|d|d|dd||<d|vr*t|ddkr|d||d<|S)Nr%rUrnname)rUrnr$r%domainsr )rr r+rrfr4 as_deepdictr)r summr!datar$s r&r+r+s D$[%8%>%>?? 9 9 4  "5"A A A &tY==IIKK 8n']&v.&}5   Y   T)_!5!5!9!9)-iDOI & Kr<ct|turt|D]$}t||tst%|Sr6)typedict TypeError isinstancer)r[items r&validate_env_varsrsL H~~T!!(4.*55 O  Or<)NFr6)NNNNNNNN)NN)rFN)9 __future__rrrr.rwr$requests.packages.urllib3.exceptionsrr exceptionsrrrequests.exceptionsr future.utilsr past.builtinsrr r r clcommon.cpapir clcommon.utilsrclselectexceptrclselectnodejs.apps_managerrclselectnodejsuserrr baseclselectrr clselectprintrrbrhr'r4rrCrOrrrrrgrrrrr+rr<r&rs&%%%%%&&&&&& @@@@@@@@@@@@ //////""""""$$$$$$&&&&&&''''''******<<<<<<::::::::>>>>>>>>""""""%         >>> 5 5 5&JNeiCMCMCMCMLVVVDL L L L ^----:11112----<6l6l6l6lr0,,,,^(r<