Your IP : 216.73.216.224


Current Path : /lib/python3/dist-packages/certbot/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/certbot/__pycache__/hooks.cpython-38.pyc

U

�][#�@s�dZddlmZddlZddlmZmZddlmZm	Z	ddl
mZddl
mZddl
mZdd	l
mZddlmZe�e�Zd
d�Zdd
�Zdd�Zdd�Ze�Zdd�Zdd�ZgZdd�Zdd�Zdd�Z dd�Z!dd�Z"d d!�Z#d"d#�Z$d$d%�Z%dS)&z;Facilities for implementing hooks that call shell commands.�)�print_functionN)�Popen�PIPE)�Set�List)�errors)�util)�
filesystem)�oscCs4t|jd�t|jd�t|jd�t|jd�dS)z#Check hook commands are executable.ZpreZpostZdeploy�renewN)�
validate_hook�pre_hook�	post_hook�deploy_hook�
renew_hook)�config�r�//usr/lib/python3/dist-packages/certbot/hooks.py�validate_hookssrcCs.t�|�s"t�|�t�|�s"dStj�|�S)z�Extract 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
�path�basename)�	shell_cmdrrr�_progs
	


rcCs\|rX|�dd�d}t|�sXtjd}tj�|�r@d�||�}nd�|||�}t�|��dS)z�Check that a command provided as a hook is plausibly executable.

    :raises .errors.HookCommandNotFound: if the command is not found
    N�r�PATHz3{1}-hook command {0} exists, but is not executable.z>Unable to find {2}-hook command {0} in the PATH.
(PATH is {1}))	�splitrr
�environr�exists�formatrZHookCommandNotFound)rZ	hook_name�cmdr�msgrrrr*s
�rcCs>|jdkr(|jr(t|j�D]}t|�q|j}|r:t|�dS)a�Run 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)�verb�directory_hooks�
list_hooksZrenewal_pre_hooks_dir�_run_pre_hook_if_necessaryr
)r�hookr rrrr
<s
r
cCs.|tkrt�d|�ntd|�t�|�dS)z�Run 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_hooks�logger�info�	_run_hook�add��commandrrrr%Ts	
r%cCsN|j}|jdkr<|jr.t|j�D]}t|�q |rJt|�n|rJtd|�dS)a�Run 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

    r�	post-hookN)rr"r#r$Zrenewal_post_hooks_dir�_run_eventuallyr*)rr r&rrrrds


rcCs|tkrt�|�dS)z�Registers 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_hooks�appendr,rrrr/�s	r/cCstD]}td|�qdS)zGRun any post hooks that were saved up in the course of the 'renew' verbr.N)r0r*)r rrr�run_saved_post_hooks�sr2cCs|jrt|j|||j�dS)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_hook�dry_run)r�domains�lineage_pathrrrr�s
	�rcCslt�}|jr6t|j�D]}t||||j�|�|�q|jrh|j|krVt�	d|j�nt|j|||j�dS)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_dirr3r4r+rr(r))rr5r6Zexecuted_dir_hooksr&rrrr�s
��rcCs<|rt�d|�dSd�|�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_LINEAGEzdeploy-hook)r(Zwarning�joinr
rr*)r-r5r6r4rrrr3�s�
r3cCst||�\}}|S)z�Run 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)�execute)�cmd_namer�err�_rrrr*�sr*cCs�t�d||�t|dttdd�}|��\}}tj�|�dd�d�}|rXt�d|||�|j	dkrtt�
d|||j	�|r�t�
d	|||�||fS)
z�Run a 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: `tuple` (`str` stderr, `str` stdout)zRunning %s command: %sT)�shell�stdout�stderrZuniversal_newlinesNrrzOutput from %s command %s:
%sz&%s command "%s" returned error code %dz#Error output from %s command %s:
%s)r(r)rrZcommunicater
rrr�
returncode�error)r;rr �outr<Zbase_cmdrrrr:�s$�
�r:cs.�fdd�t���D�}dd�|D�}t|�S)z�List 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|]}tj��|�VqdS)N)r
rr9)�.0�f��dir_pathrr�	<genexpr>szlist_hooks.<locals>.<genexpr>cSs$g|]}t�|�r|�d�s|�qS)�~)r	Z
is_executable�endswith)rDrrrr�
<listcomp>s

zlist_hooks.<locals>.<listcomp>)r
�listdir�sorted)rGZallpathsZhooksrrFrr$s	r$)&�__doc__Z
__future__rZlogging�
subprocessrrZacme.magic_typingrrZcertbotrrZcertbot.compatr	r
Zcertbot.pluginsrZ	getLogger�__name__r(rrrr
r7r'r%rr0r/r2rrr3r*r:r$rrrr�<module>s4