HEX
Server: LiteSpeed
System: Linux s787.bom1.mysecurecloudhost.com 4.18.0-477.13.1.lve.el8.x86_64 #1 SMP Thu Jun 1 16:40:47 EDT 2023 x86_64
User: mobilech (5348)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/minion.cpython-310.pyc
o

;j>�@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Zddl
ZddlmZe�
e�ZddiZdd�Zdd	�Zd
d�Zdd
�Zdd�Z	ddd�Zdd�Zdd�Zddd�Zddd�ZdS) zW
Module to provide information about minions and handle killing and restarting
minions
�N)�CommandExecutionError�list_�listcCs,t�d�dk}tjj�t�}|r|rdSdS)z*
    Check if the system uses systemd
    �kernel�LinuxTF)�
__grains__�get�salt�utils�systemdZbooted�__context__)Zis_linuxZ	is_booted�r
�G/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/minion.py�_is_systemd_systems
rcCst�d�dkS)z(
    Check if the system is Windows
    r�Windows)rrr
r
r
r�_is_windows_system#srcCsTt�sdStjj�d�d|��dddg}td|dd�}|d	d
kr(t|d��|S)zD
    Schedule a retry for the minion restart on systemd systems
    Fzsystemd-runz--on-active=z/bin/shz-cz>systemctl is-active salt-minion || systemctl start salt-minion�cmd.run_all)Zpython_shell�retcoder�stderr)rr	r
�path�which�__salt__r)�retry_delay�cmd�outr
r
r�_schedule_retry_systemd*s�rcCslt�sdStj��tj|d�}tjj�d�}d}tddddd	||d
|�	d�|�	d�d
�	}|s4t
d��|S)zD
    Schedule a retry for the minion restart on Windows systems
    F)�secondszpowershell.exez�-ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -Command "if ((Get-Service -Name salt-minion).Status -ne 'Running') { Start-Service -Name salt-minion }"ztask.create_taskzretry-minion-restart�SystemTZExecuteZOncez%Y-%m-%dz%H:%M:%S)	�nameZ	user_name�forceZaction_typerZ	argumentsZtrigger_typeZ
start_dateZ
start_timezFailed to add retry task)r�datetime�now�	timedeltar	r
rrr�strftimer)rZwhenr�argsrr
r
r�_schedule_retry_windows@s&�r%c	CsNzt|�Wnttfytd�|���wt�rt|�St�r%t|�SdS)z2
    Schedule a retry for the minion restart

    z;Invalid retry_delay value: {}. Must be a number of seconds.F)	�int�
ValueError�	TypeErrorr�formatrrrr%)rr
r
r�_schedule_retryZs���r*Fc	
Cs6dii}d}zc|r7i|dd<d}t|�}d}t�r&|�dd�|ddd<||ddd<|�d	|�d
��|rFtd|dd�|dd
<ntd|�|dd
<|dd
s^td|�d���|�d�d|d<W|Sty�}z%|�|rwdnd�d|dd
<t|�|dd<tjj	j
|d<WYd}~|Sd}~ww)zM
    Helper function to restart a service with optional retry scheduling
    �service_restartF�schedule_retryTr�Zdetail�delayz&Scheduled retry for minion restart in z secondszservice.restart��no_block�resultzFailed to restart z servicezService restart successfulrrzAdding scheduled retry failedzService restart failedN)r*rr�appendrr�strr	�defaults�	exitcodes�EX_SOFTWARE)	Zservice_namer,r�commentr0�retZschedule_retry_failedZsched�er
r
r�_restart_serviceosL�
��

�����r:c	Cs�tddd�}|�dd�}t|�}i}|D]=}g|tj�|�<z*tjj�	t�
|��D]}|�d�sGtj�tj�
||��rG|tj�|��|�q*WqtyRYqw|S)z�
    Return a list of accepted, denied, unaccepted and rejected keys.
    This is the same output as `salt-key -L`

    CLI Example:

    .. code-block:: bash

        salt 'master' minion.list
    �
config.get�pki_dirr-ZminionZmaster�.)r�replace�_check_minions_directories�osr�basenamer	r
�dataZsorted_ignorecase�listdir�
startswith�isfile�joinr2�OSError)r<Zkey_dirsr8Zdir_Zfn_r
r
rr�s"
���cCs\tj�|tjjj�}tj�|tjjj�}tj�|tjjj�}tj�|tjjj	�}||||fS)zz
    Return the minion keys directory paths.

    This function is a copy of salt.key.Key._check_minions_directories.
    )
r@rrFr	�keyZKeyZACCZPENDZREJZDEN)r<Zminions_acceptedZminions_preZminions_rejectedZminions_deniedr
r
rr?�s
r?�cCs�ddd�}g}t�d�}|s|�d�tjjj|d<nLdtvr,|�d�tjjj|d<n;ttd|��|d<|drA|�d	�n&t	|�D]}t
�d�td|�}|sZ||d
<n
qE|�d�tjjj|d<|rm||d<|S)
a�
    Kill the salt minion.

    timeout
        int seconds to wait for the minion to die.

    If you have a monitor that restarts ``salt-minion`` when it dies then this is
    a great way to restart after a minion upgrade.

    CLI Example:

    .. code-block:: bash

        salt minion[12] minion.kill

        minion1:
            ----------
            killed:
                7874
            retcode:
                0
        minion2:
            ----------
            killed:
                29071
            retcode:
                0

    The result of the salt command shows the process ID of the minions and the
    results of a kill signal to the minion in as the ``retcode`` value: ``0``
    is success, anything else is a failure.
    N�)�killedr�pidzUnable to find "pid" in grainsrzps.kill_pidzMissing command: ps.kill_pidzps.kill_pid failedrKz$Timed out waiting for minion to exitr7)
rrr2r	r4r5r6rr&�range�time�sleep�EX_TEMPFAIL)�timeoutr8r7rL�_�signaledr
r
r�kill�s4#�



�
rTT�c
Cs�d}d}g}diidd�}tdd�}|r%|�d�|�dd	�|D��n=|r4t�r4|�d
�d}d}n.|rCt�rC|�d�d}d}nd
tjvr[tj}|�d�|�dd	�|D��nd}|�d�|r�|�t��d|vr�|dr�t	|dt
�r�|�|d�n|�|d�|dr�|�d�d}|r�td|tjd�|d<d|dvr�|dd=|d�
dd�r�|�d�|dd|d<d|dvr�|dd=|s�|r�t�r�td|||dd�}	|�|	�n|r�t�r�td|||�}	|�|	�|r�||d<|S)a!
    Restart the salt minion.

    The method to restart the minion will be chosen as follows:

        If ``minion_restart_command`` is set in the minion configuration then
        the command specified will be used to restart the minion.

        If the minion is running as a systemd service then the minion will be
        restarted using the systemd_service module, unless ``systemd`` is
        set to ``False``

        If the minion is running as a Windows service then the minion will be
        restarted using the win_service module, unless ``win_service`` is
        set to ``False``

        If the salt-minion process is running in daemon mode (the ``-d``
        argument is present in ``argv``) then the minion will be killed and
        restarted using the same command line arguments, if possible.

        If the salt-minion process is running in the foreground (the ``-d``
        argument is not present in ``argv``) then the minion will be killed but
        not restarted. This behavior is intended for minion processes that are
        managed by a process supervisor.

    systemd
        If set to ``False`` then systemd will not be used to restart the minion.
        Defaults to ``True``.

    win_service
        If set to ``False`` then the Windows service manager will not be used to
        restart the minion. Defaults to ``True``.

    schedule_retry
        If set to ``True`` then a scheduled job will be added to start the
        minion if it has failed to restart after the retry_delay

    retry_delay
        The amount of time to wait before attempting to start the minion if it
        has failed to restart. Defaults to 180 seconds.

    CLI Examples:

    .. code-block:: bash

        salt minion[12] minion.restart

        minion1:
        ----------
            comment:
                - Using systemctl to restart salt-minion
                - Service restart successful
            killed:
                None
            restart:
                ----------
            retcode:
                0
            service_restart:
                ----------
                result:
                    True
        minion2:
            ----------
            comment:
                - Using windows service manager to restart salt-minion
                - Service restart successful
            killed:
                None
            restart:
                ----------
            retcode:
                0
            service_restart:
                ----------
                result:
                    True

    The result shows that ``minion1`` was restarted using systemd and
    ``minion2`` was restarted using the Windows service manager. The
    ``service_restart`` field indicates the result of the service restart
    operation. The ``killed`` field is ``None`` because the minion was restarted
    using the service manager and not by killing the process. The ``restart``
    field is empty because the minion was restarted using the service manager
    and not by running the command line arguments of the minion process.

    .. code-block:: bash

        salt minion[12] minion.restart

        minion1:
            ----------
            comment:
                - Restart using process argv:
                -     /home/omniture/install/bin/salt-minion
                -     -d
                -     -c
                -     /home/omniture/install/etc/salt
            killed:
                10070
            restart:
                ----------
                stderr:
                stdout:
            retcode:
                0
        minion2:
            ----------
            comment:
                - Using configuration minion_restart_command:
                -     /home/omniture/install/bin/salt-minion
                -     --not-an-option
                -     -d
                -     -c
                -     /home/omniture/install/etc/salt
                - Restart failed
            killed:
                10896
            restart:
                ----------
                stderr:
                    Usage: salt-minion

                    salt-minion: error: no such option: --not-an-option
                stdout:
            retcode:
                64

    The result of the command shows the process ID of ``minion1`` that is
    shutdown (killed) and the results of the restart.  If there is a failure
    in the restart it will be reflected in a non-zero ``retcode`` and possibly
    output in the ``stderr`` and/or ``stdout`` values along with addition
    information in the ``comment`` field as is demonstrated with ``minion2``.
    TNr)rK�restartr+rr;Zminion_restart_commandz+Using configuration minion_restart_command:cS�g|]}d|���qS�z    r
��.0�argr
r
r�
<listcomp>��zrestart.<locals>.<listcomp>z&Using systemctl to restart salt-minionFz4Using windows service manager to restart salt-minionz-dzRestart using process argv:cSrWrXr
rYr
r
rr\�r]zCNot running in daemon mode - will not restart process after killingr7rzKill failed - not restartingr)�envrVrLzRestart failedzsalt-minionr/)rr2�extendrr�sys�argv�updaterT�
isinstancer3r@�environrr:)
rZwin_servicer,rZshould_killZshould_restartr7r8Zrestart_cmdZservice_resultr
r
rrVsz	�






�





�
�
rV)F)rI)TTFrU)�__doc__r �loggingr@r`rNZsalt.keyr	Zsalt.utils.dataZsalt.utils.pathZsalt.utils.systemdZsalt.exceptionsr�	getLogger�__name__�logZ__func_alias__rrrr%r*r:rr?rTrVr
r
r
r�<module>s0

�4#
I