o
    Œ }iv1  ã                   @   sà   d Z ddlZddlZddlZddlZddlZddlZddl	Zddl
ZddlZddlmZ e e¡Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zd#dd„Zd$dd„Zdd„ Zd%dd„Zd#dd„Zd&d!d"„ZdS )'a)  
Support for Apache

.. note::
    The functions in here are generic functions designed to work with
    all implementations of Apache. Debian-specific functions have been moved into
    deb_apache.py, but will still load under the ``apache`` namespace when a
    Debian-based system is detected.
é    N)ÚSaltExceptionc                  C   s   t ƒ } tjj | ¡rdS dS )z5
    Only load the module if apache is installed
    Úapache)FzFThe apache execution module cannot be loaded: apache is not installed.)Ú
_detect_osÚsaltÚutilsÚpathÚwhich)Úcmd© r
   ú@/opt/saltstack/salt/extras-3.10/saltext/apache/modules/apache.pyÚ__virtual__   s   r   c                  C   s$   t d } | dkr
dS | dkrdS dS )zA
    Apache commands and paths differ depending on packaging
    Ú	os_familyZRedHatZ	apachectlZDebianZ
apache2ctl)Z
__grains__)r   r
   r
   r   r   '   s   r   c                  C   s2   t ƒ › d�} td | ƒ ¡ }|d  d¡}|d S )z
    Return server version (``apachectl -v``)

    CLI Example:

    .. code-block:: bash

        salt '*' apache.version
    z -vúcmd.runr   ú: é   )r   Ú__salt__Ú
splitlinesÚsplit)r	   ÚoutÚretr
   r
   r   Úversion4   s   
r   c                  C   s    t ƒ › d�} i }g |d< td | ƒ ¡ }t d¡}|D ]2}d|v r<| d¡}|s)q|d  ¡ ||d  ¡  ¡  dd	¡< qd
|v rM| 	d|¡}|d  
|¡ q|S )zƒ
    Return server version (``apachectl -V``)

    CLI Example:

    .. code-block:: bash

        salt '*' apache.fullversion
    z -VZcompiled_withr   z	^\s+-D\s+r   r   r   ú Ú_z -DÚ )r   r   r   ÚreÚcompiler   ÚstripÚlowerÚreplaceÚsubÚappend)r	   r   r   Z	define_reÚlineÚcompsZcwithr
   r
   r   ÚfullversionD   s    


&€r#   c                  C   s€   t ƒ › d�} i }g |d< g |d< td | ƒ ¡ }|D ]#}| ¡ }|s#qd|v r0|d  |d ¡ d|v r=|d  |d ¡ q|S )z’
    Return list of static and shared modules (``apachectl -M``)

    CLI Example:

    .. code-block:: bash

        salt '*' apache.modules
    z -MZstaticZsharedr   z(static)r   z(shared))r   r   r   r   r    )r	   r   r   r!   r"   r
   r
   r   Úmodulesa   s   
€r$   c                  C   sJ   t ƒ › d�} g }td | ƒ ¡ }|D ]}|sqd|v r"| | ¡ ¡ q|S )zœ
    Return list of modules compiled into the server (``apachectl -l``)

    CLI Example:

    .. code-block:: bash

        salt '*' apache.servermods
    z -lr   z.c)r   r   r   r    r   )r	   r   r   r!   r
   r
   r   Ú
servermods{   s   
€r%   c                  C   sh   t ƒ › d�} i }td | ƒ}| dd¡}| ¡ D ]}|sq| d¡}d |dd… ¡}|||d < q|S )	zÓ
    Return list of directives together with expected arguments
    and places where the directive is valid (``apachectl -L``)

    CLI Example:

    .. code-block:: bash

        salt '*' apache.directives
    z -Lr   z
	ú	Ú
r   Nr   )r   r   r   r   r   Újoin)r	   r   r   r!   r"   Údescr
   r
   r   Ú
directives�   s   
r*   c                  C   s  t ƒ › d�} i }d}td | ƒ}| ¡ D ]r}|sq| ¡ }d|v r*|d }i ||< q|d dkrOi || d< |d || d d< t d	d|d
 ¡|| d d< |d dkr†i || |d
 < |d
 || |d
  d< t d	d|d ¡|| |d
  d< |d || |d
  d< q|S )aj  
    Show the settings as parsed from the config file (currently
    only shows the virtualhost settings) (``apachectl -S``).
    Because each additional virtual host adds to the execution
    time, this command may require a long timeout be specified
    by using ``-t 10``.

    CLI Example:

    .. code-block:: bash

        salt -t 10 '*' apache.vhosts
    z -Sr   r   zis a NameVirtualHostr   Údefaulté   Zvhostz\(|\)é   ÚconfÚporté   r   )r   r   r   r   r   r   )r	   r   Z	namevhostr   r!   r"   r
   r
   r   Úvhosts¨   s,   
"€r1   c                 C   s°   d}d}| |vr| |vrdS | |v rd| › �}nd| › �}t ƒ | }td |ƒ}|d r8|d r8|d  ¡ }|S |d rD|d  ¡ }|S |d	 rP|d	  ¡ }|S d
|› d�}|S )z‡
    Signals httpd to start, restart, or stop.

    CLI Example:

    .. code-block:: bash

        salt '*' apache.signal restart
    )Z
configtestÚstatusZ
fullstatus)ÚstartÚstopZrestartZgracefulzgraceful-stopNz -k r   zcmd.run_allÚretcodeÚstderrÚstdoutz
Command: "z" completed successfully!)r   r   r   )ÚsignalZno_extra_argsÚvalid_signalsÚ	argumentsr	   r   r   r
   r
   r   r8   Ï   s(   


	ùûÿr8   r   c                 C   s   t d | |||ƒS )a{  
    Add HTTP user using the ``htpasswd`` command. If the ``htpasswd`` file does not
    exist, it will be created. Valid options that can be passed are:

    .. code-block:: text

        n  Don't update file; display results on stdout.
        m  Force MD5 hashing of the password (default).
        d  Force CRYPT(3) hashing of the password.
        p  Do not hash the password (plaintext).
        s  Force SHA1 hashing of the password.

    CLI Examples:

    .. code-block:: bash

        salt '*' apache.useradd /etc/httpd/htpasswd larry badpassword
        salt '*' apache.useradd /etc/httpd/htpasswd larry badpass opts=ns
    zwebutil.useradd©r   )ÚpwfileÚuserÚpasswordÚoptsr
   r
   r   Úuseraddô   s   r@   c                 C   s   t d | |ƒS )z§
    Delete HTTP user from the specified ``htpasswd`` file.

    CLI Example:

    .. code-block:: bash

        salt '*' apache.userdel /etc/httpd/htpasswd larry
    zwebutil.userdelr;   )r<   r=   r
   r
   r   Úuserdel  s   
rA   r+   c                 C   sæ  dddddddddddddœi}t d d| › d�dƒ}t d d| › d�d	ƒ}t d d| › d
�d	ƒ}t d d| › d�d	ƒ}t d d| › d�dƒ}|rr|rrtj ¡ }|j||||d� tj ¡ }|j||||d� tj tj ||¡¡ |d7 }z tjj||d��}	|	 	¡  
¡ }
W d  ƒ n1 s�w   Y  W n tjjy¢   Y dS w |
D ]K}| dd¡}|d  ¡ |d< |d  ¡ |d< |d dkrÕ|d D ]}|d |  d7  < qÇq¥|d  ¡ ræt|d ƒ||d < q¥t|d ƒ||d < q¥|S )ar  
    Get Information from the Apache server-status handler

    .. note::

        The server-status handler is disabled by default.
        In order for this function to work it needs to be enabled.
        See http://httpd.apache.org/docs/2.2/mod/mod_status.html

    The following configuration needs to exists in pillar/grains.
    Each entry nested in ``apache.server-status`` is a profile of a vhost/server.
    This would give support for multiple apache servers/vhosts.

    .. code-block:: yaml

        apache.server-status:
          default:
            url: http://localhost/server-status
            user: someuser
            pass: password
            realm: 'authentication realm for digest passwords'
            timeout: 5

    CLI Examples:

    .. code-block:: bash

        salt '*' apache.server_status
        salt '*' apache.server_status other-profile
    Z
Scoreboardr   )r   ÚSÚRÚWÚKÚDÚCÚLÚGÚIÚ.z
config.getzapache.server-status:z:urlzhttp://localhost/server-statusz:userr   z:passz:realmz:timeouté   )ÚrealmÚurir=   Úpasswdz?auto)ÚtimeoutNÚerrorú:r   )r   ÚurllibÚrequestÚHTTPBasicAuthHandlerÚadd_passwordÚHTTPDigestAuthHandlerÚinstall_openerÚbuild_openerÚurlopenÚreadr   rQ   ÚURLErrorr   r   ÚisdigitÚintÚfloat)Zprofiler   Úurlr=   rO   rM   rP   ÚbasicÚdigestÚhttp_responseÚresponser!   ZspltÚcr
   r
   r   Úserver_status  s`    õÿÿ

ÿ€ÿÿrf   c           
   
   C   sú  t  ¡ }t| tƒr!|rt|› d| › �|dd� nÜt| › |dd� nÓt| tƒrƒd}d}| D ]}d|v r:d}t|d ƒ}q,|rrtd|› d|› d	�|d
� | D ]}| ¡ D ]\}}|dkrett|t|ƒƒ|d
� qRqLtd|› d	�|d
� n‚| D ]}tt|t|ƒƒ|d
� qtnqt| tƒrôztd|› d| d › d	�|d
� W n t	y® }	 z	t
d|› d�ƒ|	‚d}	~	ww |  ¡ D ]6\}}|dkrét|tƒrÌt|› d|› �|d
� q³t|tƒrÛtt||ƒ|d
� q³t|tƒrétt||ƒ|d
� q³td|› d	�|d
� | d¡ | ¡ S )z¹
    Recursively goes through config structure and builds final Apache configuration

    :param conf: defined config structure
    :param slot: name of section container if needed
    r   r   )ÚfileÚendFNÚthisTú<ú>)rg   z</zApache section container "<z2>" expects attribute. Specify it using key "this".r   )ÚioÚStringIOÚ
isinstanceÚstrÚprintÚlistÚitemsÚ_parse_configÚdictÚKeyErrorr   Úseekr[   )
r.   Úslotr   Z
is_sectionZ	slot_thisÚitemÚkeyÚvalÚvalueÚerrr
   r
   r   rs   s  s`   

€€þÿ
"
ÿý€ÿ


€
rs   Tc                 C   sž   g }|D ]}t t| ¡ ƒƒ}| t|| |ƒ¡ qd tjj 	|¡¡}|rMtjj
 | d¡�}| d¡ | tjj |¡¡ W d  ƒ |S 1 sHw   Y  |S )a°  
    Create VirtualHost configuration files

    name
        File for the virtual host
    config
        VirtualHost configurations

    .. note::

        This function is not meant to be used from the command line.
        Config is meant to be an ordered dict of all of the apache configs.

    CLI Example:

    .. code-block:: bash

        salt '*' apache.config /etc/httpd/conf.d/ports.conf config="[{'Listen': '22'}]"
    r'   Úwz # This file is managed by Salt.
N)ÚnextÚiterÚkeysr    rs   r(   r   r   ÚdataÚdecodeÚfilesZfopenÚwriteZstringutilsZto_str)ÚnameÚconfigZeditZconfigsÚentryry   ZconfigstextZ
configfiler
   r
   r   r†   §  s   

þýr†   )N)r   )r+   )T)Ú__doc__rl   Úloggingr   Úurllib.errorrS   Úurllib.requestZsalt.utils.datar   Zsalt.utils.filesZsalt.utils.pathZsalt.utils.stringutilsZsalt.exceptionsr   Ú	getLoggerÚ__name__Úlogr   r   r   r#   r$   r%   r*   r1   r8   r@   rA   rf   rs   r†   r
   r
   r
   r   Ú<module>   s4    


'
%

[4