3 گa!@sUdZddlZddlmZddlmZddlmZddlmZddlmZddlm Z dd l m Z dd l m Z dd l m Z dd lmZddlm ZejeZejdd ddZeeedddZeeddddZejdd ddZeZeeeddddZejdd ddZgZ ee eddddZ!ddd d!Z"ejeeedd"d#d$Z#ejeeedd"d%d&Z$eeeee%dd'd(d)Z&eeed*d+d,Z'eeed-d.d/Z(dS)0z;Facilities for implementing hooks that call shell commands.N)List)Optional)Set) configuration)errors)util) filesystem)misc)os)ops)configreturncCs4t|jdt|jdt|jdt|jddS)z#Check hook commands are executable.ZpreZpostZdeployrenewN) validate_hookpre_hook post_hook deploy_hook renew_hook)r r/usr/lib/python3.6/hooks.pyvalidate_hookss   r) shell_cmdr cCs.tj|s"tj|tj|s"dStjj|S)zExtract the program run by a shell command. :param str shell_cmd: command to be executed :returns: basename of command or None if the command isn't found :rtype: str or None N)rZ exe_exists plug_utilZ path_surgeryr pathbasename)rrrr_progs   r)r hook_namer cCs\|rX|jddd}t|sXtjd}tjj|r@dj||}ndj|||}tj|dS)zCheck that a command provided as a hook is plausibly executable. :raises .errors.HookCommandNotFound: if the command is not found NrPATHz3{1}-hook command {0} exists, but is not executable.z>Unable to find {2}-hook command {0} in the PATH. (PATH is {1})) splitrr environrexistsformatrZHookCommandNotFound)rrcmdrmsgrrrr-s   rcCsB|jdkr,|jr,xt|jD] }t|qW|j}|r>t|dS)aRun pre-hooks if they exist and haven't already been run. When Certbot is running with the renew subcommand, this function runs any hooks found in the config.renewal_pre_hooks_dir (if they have not already been run) followed by any pre-hook in the config. If hooks in config.renewal_pre_hooks_dir are run and the pre-hook in the config is a path to one of these scripts, it is not run twice. :param configuration.NamespaceConfig config: Certbot settings rN)verbdirectory_hooks list_hooksZrenewal_pre_hooks_dir_run_pre_hook_if_necessaryr)r hookr#rrrr?s  r)commandr cCs.|tkrtjd|ntd|tj|dS)zRun the specified pre-hook if we haven't already. If we've already run this exact command before, a message is logged saying the pre-hook was skipped. :param str command: pre-hook to be run z*Pre-hook command already run, skipping: %szpre-hookN)executed_pre_hooksloggerinfo _run_hookadd)r*rrrr(Ws  r(cCsR|j}|jdkr@|jr2xt|jD] }t|q"W|rNt|n|rNtd|dS)aRun post-hooks if defined. This function also registers any executables found in config.renewal_post_hooks_dir to be run when Certbot is used with the renew subcommand. If the verb is renew, we delay executing any post-hooks until :func:`run_saved_post_hooks` is called. In this case, this function registers all hooks found in config.renewal_post_hooks_dir to be called followed by any post-hook in the config. If the post-hook in the config is a path to an executable in the post-hook directory, it is not scheduled to be run twice. :param configuration.NamespaceConfig config: Certbot settings rz post-hookN)rr%r&r'Zrenewal_post_hooks_dir_run_eventuallyr.)r r#r)rrrrgs   rcCs|tkrtj|dS)zRegisters a post-hook to be run eventually. All commands given to this function will be run exactly once in the order they were given when :func:`run_saved_post_hooks` is called. :param str command: post-hook to register to be run N) post_hooksappend)r*rrrr0s r0)r cCsxtD]}td|qWdS)zGRun any post hooks that were saved up in the course of the 'renew' verbz post-hookN)r1r.)r#rrrrun_saved_post_hookss r3)r domains lineage_pathr cCs|jrt|j|||jdS)aRun post-issuance hook if defined. :param configuration.NamespaceConfig config: Certbot settings :param domains: domains in the obtained certificate :type domains: `list` of `str` :param str lineage_path: live directory path for the new cert N)r_run_deploy_hookdry_run)r r4r5rrrrs rcCspt}|jr:x,t|jD]}t||||j|j|qW|jrl|j|krZtj d|jnt|j|||jdS)a]Run post-renewal hooks. This function runs any hooks found in config.renewal_deploy_hooks_dir followed by any renew-hook in the config. If the renew-hook in the config is a path to a script in config.renewal_deploy_hooks_dir, it is not run twice. If Certbot is doing a dry run, no hooks are run and messages are logged saying that they were skipped. :param configuration.NamespaceConfig config: Certbot settings :param domains: domains in the obtained certificate :type domains: `list` of `str` :param str lineage_path: live directory path for the new cert z0Skipping deploy-hook '%s' as it was already run.N) setr&r'Zrenewal_deploy_hooks_dirr6r7r/rr,r-)r r4r5Zexecuted_dir_hooksr)rrrrs  r)r*r4r5r7r cCs<|rtjd|dSdj|tjd<|tjd<td|dS)aRun the specified deploy-hook (if not doing a dry run). If dry_run is True, command is not run and a message is logged saying that it was skipped. If dry_run is False, the hook is run after setting the appropriate environment variables. :param str command: command to run as a deploy-hook :param domains: domains in the obtained certificate :type domains: `list` of `str` :param str lineage_path: live directory path for the new cert :param bool dry_run: True iff Certbot is doing a dry run z)Dry run: skipping deploy hook command: %sN ZRENEWED_DOMAINSZRENEWED_LINEAGEz deploy-hook)r,r-joinr r r.)r*r4r5r7rrrr6s r6)cmd_namerr cCs6tj||tjd\}}}tjd|d||||S)zRun a hook command. :param str cmd_name: the user facing name of the hook being run :param shell_cmd: shell command to execute :type shell_cmd: `list` of `str` or `str` :returns: stderr if there was any)envzHook '')r Zexecute_command_statusrZenv_no_snap_for_external_calls display_opsZreport_executed_command)r;r returncodeerroutrrrr.sr.)dir_pathr cs.fddtjD}dd|D}t|S)zList paths to all hooks found in dir_path in sorted order. :param str dir_path: directory to search :returns: `list` of `str` :rtype: sorted list of absolute paths to executables in dir_path c3s|]}tjj|VqdS)N)r rr:).0f)rBrr szlist_hooks..cSs&g|]}tj|r|jd r|qS)~)rZ is_executableendswith)rCrrrr szlist_hooks..)r listdirsorted)rBZallpathsZhooksr)rBrr's r'))__doc__ZloggingZtypingrrrZcertbotrrrZcertbot.compatrr r Zcertbot.displayr r>Zcertbot.pluginsrZ getLogger__name__r,ZNamespaceConfigrstrrrrr8r+r(rr1r0r3rrboolr6r.r'rrrrs<