HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/tomcat.cpython-310.pyc
o

�N�g�I�@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Zddl
ZddlZe�
e�ZddiZddgddgd	�Zd
d�Zdd
�Zdd�Zdd�Zdd�ZdCdd�ZdDdd�ZdDdd�ZdDdd�ZdDdd�ZdDd d!�ZdDd"d#�ZdDd$d�ZdDd%d&�ZdDd'd(�Z dDd)d*�Z!dDd+d,�Z"	-		.			/dEd0d1�Z#dFd4d5�Z$d6d7�Z%d8d9�Z&dGd:d;�Z'ed<kr�iZ(iZ)d=d>d?�Z*e�Z+d@d=d>d	�iZ*e�Z,e+e,kr�e�-dA�dSe�.dB�dSdS)Ha�
Support for Tomcat

This module uses the manager webapp to manage Apache tomcat webapps.
If the manager webapp is not configured some of the functions won't work.

:configuration:
    - Java bin path should be in default path
    - If ipv6 is enabled make sure you permit manager access to ipv6 interface
      "0:0:0:0:0:0:0:1"
    - If you are using tomcat.tar.gz it has to be installed or symlinked under
      ``/opt``, preferably using name tomcat
    - "tomcat.signal start/stop" works but it does not use the startup scripts

The following grains/pillar should be set:

.. code-block:: yaml

    tomcat-manager:
      user: <username>
      passwd: <password>

or the old format:

.. code-block:: yaml

    tomcat-manager.user: <username>
    tomcat-manager.passwd: <password>

Also configure a user in the conf/tomcat-users.xml file:

.. code-block:: xml

    <?xml version='1.0' encoding='utf-8'?>
    <tomcat-users>
        <role rolename="manager-script"/>
        <user username="tomcat" password="tomcat" roles="manager-script"/>
    </tomcat-users>

.. note::

   - More information about tomcat manager:
     http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html
   - if you use only this module for deployments you've might want to strict
     access to the manager only from localhost for more info:
     http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html#Configuring_Manager_Application_Access
   - Tested on:

     JVM Vendor:
         Sun Microsystems Inc.
     JVM Version:
         1.6.0_43-b01
     OS Architecture:
         amd64
     OS Name:
         Linux
     OS Version:
         2.6.32-358.el6.x86_64
     Tomcat Version:
         Apache Tomcat/7.0.37
�N�reload_�reload�tomcat-manager.userztomcat-manager:user�tomcat-manager.passwdztomcat-manager:passwd��user�passwdcCst�std�r	dSdS)zO
    Only load tomcat if it is installed or if grains/pillar config exists
    ZdummyZtomcat)Fz�Tomcat execution module not loaded: neither Tomcat installed locally nor tomcat-manager credentials set in grains/pillar/config.)�__catalina_home�_auth�rr�G/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/tomcat.py�__virtual__Wsr
cCsJddg}|D]}t�|�}|r"|D]}tj�|d�r!|SqqdS)z4
    Tomcat paths differ depending on packaging
    z/usr/share/tomcat*z/opt/tomcatz/binF)�glob�os�path�isdir)Z	locations�locationZfoldersZ
catalina_homerrrr	ds
��r	cCsdddd�}|D]"}tttfD]}t|D]}tjj�||d�}|r'|||<nqqq|d|dfS)zD
    Get the username and password from opts, grains, or pillar
    FrNrr)�__opts__�
__grains__�
__pillar__�__valid_configs�salt�utils�dataZtraverse_dict_and_list)�ret�item�structZ
config_key�valuerrr�_get_credentialsrs
���rcCsdt�\}}|dus
|durdStj��}|jd|||d�tj��}|jd|||d�tj�||�S)z�
    returns a authentication handler.
    Get user & password from grains, if are not set default to
    modules.config.option

    If user & pass are missing return False
    FzTomcat Manager Application)�realm�urirr)r�urllib�requestZHTTPBasicAuthHandlerZadd_passwordZHTTPDigestAuthHandlerZbuild_opener)r r�passwordZbasic�digestrrrr
�s
	
�
�r
cCsDtj�|�}tj�|�d}t�d|�}|r t|�dkr |dSdS)ac
    Extract the version from the war file name. There does not seem to be a
    standard for encoding the version into the `war file name`_

    .. _`war file name`: https://tomcat.apache.org/tomcat-6.0-doc/deployer-howto.html

    Examples:

    .. code-block:: bash

        /path/salt-2015.8.6.war -> 2015.8.6
        /path/V6R2013xD5.war -> None
    rz-([\d.-]+)$�N)rr�basename�splitext�re�findall�len)�warr&Zwar_package�versionrrr�extract_war_version�sr-�http://localhost:8080/manager�c	Cspdgd�}t|�}|durd|d<d|d<|S|ddkr!|d7}|}|d	|��7}||�7}|rG|d
tj�|���7}|d
tj�|���7}tj�|�ztjj||d�����|d<Wn't	y�ztjj||d�����|d<Wn
t	y�d|d<YnwYnwt
|d�D]\}}ztjj
�|d
�|d|<Wq�ttfy�Yq�w|dd�d�s�d|d<|S)a�
    A private function used to issue the command to tomcat via the manager
    webapp

    cmd
        the command to execute

    url
        The URL of the server manager webapp (example:
        http://localhost:8080/manager)

    opts
        a dict of arguments

    timeout
        timeout for HTTP request

    Return value is a dict in the from of::

        {
            res: [True|False]
            msg: list of lines we got back from the manager
        }
    T)�res�msgFr0z5missing username and password settings (grain/pillar)r1����/ztext/�?��timeoutzFailed to create HTTP requestzutf-8r�OK)r
r!�parse�	urlencoder"Zinstall_opener�urlopen�read�
splitlines�	Exception�	enumeraterrZstringutilsZ
to_unicode�UnicodeDecodeError�AttributeError�
startswith)	�cmd�opts�urlr6rZauthZurl6�keyrrrr�_wget�sF

 
����
�rFcCsPz|t|�|dd�}d�t||||d�d�WSty'd|��YSw)zI
    Simple command wrapper to commands that need only a path option
    r,)rr,�
r5r1z"FAIL - No context exists for path )�ls�joinrFr=)rB�apprDr6rCrrr�_simple_cmd�s�rKcCstdddi||d�dS)z�
    Find memory leaks in tomcat

    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.leaks
    Z	findleaksZ
statusLine�truer5r1�rF�rDr6rrr�leakssrOcCstdi||d�dS)aE
    Used to test if the tomcat manager is up

    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.status
        salt '*' tomcat.status http://localhost:8080/manager
    �listr5r0rMrNrrr�statussrQcCs�i}tdd||d�}|dduriS|d�d�|dD]/}|�d�}|d	|d
|ddd�||d<|d�d
�}t|�d	krL|d	||dd<q|S)a2
    list all the deployed webapps

    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.ls
        salt '*' tomcat.ls http://localhost:8080/manager
    rP�r5r0Fr1r�:r%��)�mode�sessions�fullnamer,z##r,)rF�pop�splitr*)rDr6rr�line�tmpZslicedrrrrH/s"
��rHcC�td|||d�S)ab
    Stop the webapp

    app
        the webapp context path
    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.stop /jenkins
        salt '*' tomcat.stop /jenkins http://localhost:8080/manager
    �stopr5�rK�rJrDr6rrrr^T�r^cCr])a_
    Start the webapp

    app
        the webapp context path
    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.start /jenkins
        salt '*' tomcat.start /jenkins http://localhost:8080/manager
    �startr5r_r`rrrrbjrarbcCr])ah
    Reload the webapp

    app
        the webapp context path
    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.reload /jenkins
        salt '*' tomcat.reload /jenkins http://localhost:8080/manager
    rr5r_r`rrrr�racCr])a�
    return the status of the webapp sessions

    app
        the webapp context path
    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.sessions /jenkins
        salt '*' tomcat.sessions /jenkins http://localhost:8080/manager
    rWr5r_r`rrrrW�rarWcCs2t||d�}|D]}||kr||dSqdS)a�
    return the status of the webapp (stopped | running | missing)

    app
        the webapp context path
    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.status_webapp /jenkins
        salt '*' tomcat.status_webapp /jenkins http://localhost:8080/manager
    r5rV�missing)rH)rJrDr6Zwebapps�irrr�
status_webapp�s�recCsntdi||d�}|ddurd|diSi}|d�d�|dD]}|�d�}|d	��||d��<q!|S)
aD
    return details about the server

    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.serverinfo
        salt '*' tomcat.serverinfo http://localhost:8080/manager
    �
serverinfor5r0F�errorr1rrSr%)rFrYrZ�strip)rDr6rrr[r\rrrrf�s
rfcCr])al
    Undeploy a webapp

    app
        the webapp context path
    url : http://localhost:8080/manager
        the URL of the server manager webapp
    timeout : 180
        timeout for HTTP request

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.undeploy /jenkins
        salt '*' tomcat.undeploy /jenkins http://localhost:8080/manager
    �undeployr5r_r`rrrri�rari�no�baseTcCs&dtj�|���}|dur!tj�|�sd|�d�Stj�||�}n	tj�t��|�}d}	tj�|�sSd}	td|||�}
|
s@dSz	td	|
d
�Wnt	yRYnw|}d|��|d�}|rr|durgt
|�n|}t|t�rr||d
<|dkrzd|d<t
d|||d�}d�|d�}
|	r�td|�|
S)aM
    Deploy a WAR file

    war
        absolute path to WAR file (should be accessible by the user running
        tomcat) or a path supported by the salt.modules.cp.get_file function
    context
        the context path to deploy
    force : False
        set True to deploy the webapp even one is deployed in the context
    url : http://localhost:8080/manager
        the URL of the server manager webapp
    saltenv : base
        the environment for WAR file in used by salt.modules.cp.get_url
        function
    timeout : 180
        timeout for HTTP request
    temp_war_location : None
        use another location to temporarily copy to war file
        by default the system's temp directory is used
    version : ''
        Specify the war version.  If this argument is provided, it overrides
        the version encoded in the war file name, if one is present.

        Examples:

        .. code-block:: bash

            salt '*' tomcat.deploy_war salt://salt-2015.8.6.war version=2015.08.r6

        .. versionadded:: 2015.8.6

    CLI Examples:

    cp module

    .. code-block:: bash

        salt '*' tomcat.deploy_war salt://application.war /api
        salt '*' tomcat.deploy_war salt://application.war /api no
        salt '*' tomcat.deploy_war salt://application.war /api yes http://localhost:8080/manager

    minion local file system

    .. code-block:: bash

        salt '*' tomcat.deploy_war /tmp/application.war /api
        salt '*' tomcat.deploy_war /tmp/application.war /api no
        salt '*' tomcat.deploy_war /tmp/application.war /api yes http://localhost:8080/manager
    zsalt.Nz	Error - "z" is not a directoryFTz
cp.get_urlz#FAIL - could not cache the WAR filez
file.set_modeZ0644zfile:)r+rr,ZyesrL�updateZdeployr5rGr1zfile.remove)rrr&rrI�tempfileZ
gettempdir�isfile�__salt__�KeyErrorr-�
isinstance�strrF)r+�context�forcerDZsaltenvr6Ztemp_war_locationr,Ztfile�cache�cachedrCZdeployedr0rrr�
deploy_war�s@=��
rwrR�sha1cCsNtt|d�}|dur|r|�|�d|�d|���n|�|�|r%|��p&dS)ak
    This function replaces the $CATALINA_HOME/bin/digest.sh script
    convert a clear-text password to the $CATALINA_BASE/conf/tomcat-users.xml
    format

    CLI Examples:

    .. code-block:: bash

        salt '*' tomcat.passwd secret
        salt '*' tomcat.passwd secret tomcat sha1
        salt '*' tomcat.passwd secret tomcat sha1 'Protected Realm'
    NrSF)�getattr�hashlibrl�	hexdigest)rrZalgrr$rrrrks
rcCsLt�d}td|���}|D]}|sqd|vr#|�d�}|dSqdS)z�
    Return server version from catalina.sh version

    CLI Example:

    .. code-block:: bash

        salt '*' tomcat.version
    �/bin/catalina.sh version�cmd.runzServer version�: r%N)r	ror<rZ)rB�outr[�compsrrrr,�s


��r,cCsXt�d}i}td|���}|D]}|sqd|vr)|�d�}|d��||d<q|S)z�
    Return all server information from catalina.sh version

    CLI Example:

    .. code-block:: bash

        salt '*' tomcat.fullversion
    r|r}r~r%r)r	ror<rZ�lstrip)rBrrr[r�rrr�fullversion�s


�r�cCs>ddddd�}||vr
dSt��d||��}td|�dS)	z�
    Signals catalina to start, stop, securestart, forcestop.

    CLI Example:

    .. code-block:: bash

        salt '*' tomcat.signal start
    zstop -forcezstart -securityrbr^)Z	forcestopZsecurestartrbr^Nz/bin/catalina.sh r})r	ro)�signal�
valid_signalsrBrrrr��s�r��__main__Zfoobarzbarfoo1!)rrztomcat-managerzConfig backwards compatiblezConfig not backwards compatible)Nr.r/)r.r/)rjr.rkr/NT)rRrxN)N)/�__doc__rrz�loggingrr(rm�urllib.parser!�urllib.requestZsalt.utils.datarZsalt.utils.stringutils�	getLogger�__name__�logZ__func_alias__rr
r	rr
r-rFrKrOrQrHr^rbrrWrerfrirwrr,r�r�rrrZold_format_credsZnew_format_creds�infoZifnorrrr�<module>sr>
�


I



%






�
p
��