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__/swarm.cpython-310.pyc
o

�N�g�5�@s�dZddlZdd�Zz
ddlZee�ZWneydZYnwdZdd�Zd	d
�Z	dd�Z
eee
fd
d�Zeeefdd�Ze
fdd�Zeeeeeeefdd�Zefdd�Zefdd�Zefdd�Zee
fdd�Zeeeeefdd�ZdS)a�
Docker Swarm Module using Docker's Python SDK
=============================================

:codeauthor: Tyler Jones <jonestyler806@gmail.com>

.. versionadded:: 2018.3.0

The Docker Swarm Module is used to manage and create Docker Swarms.

Dependencies
------------

- Docker installed on the host
- Docker python sdk >= 2.5.1

Docker Python SDK
-----------------

.. code-block:: bash

    pip install -U docker

More information: https://docker-py.readthedocs.io/en/stable/
�Ncsddg}t�fdd�|D��S)N�	APIClient�from_envc3s�|]}t�|�VqdS)N)�hasattr)�.0�attr��mod��F/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/swarm.py�	<genexpr> s�z$_is_docker_module.<locals>.<genexpr>)�all)rZrequired_attrsr	rr
�_is_docker_modulesr
F�swarmcCstrtSdS)zC
    Load this module if the docker python module is installed
    )FzGThe swarm module failed to load: Docker python module is not available.)�
HAS_DOCKER�__virtualname__r	r	r	r
�__virtual__-srcCs trt��td<tdtd<dS)N�client�id�server_name)r�dockerr�__context__Z
__grains__)�selfr	r	r
�__init__9srcCstjdd�}|��}|dS)z�
    Get the Docker Swarm Manager or Worker join tokens

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.swarm_tokens
    �unix://var/run/docker.sock�Zbase_urlZ
JoinTokens)rrZ
inspect_swarm)r�servicer	r	r
�swarm_tokens?s
rc
Cs�zi}tdj�|||�d�td�}|�|t�d��W|StjjyW}z*i}d|j	vr:|�|j	dd��n|�t
|j	�dd��WYd	}~|SWYd	}~|Sd	}~wtyii}|�d
di�Y|Sw)a�
    Initialize Docker on Minion as a Swarm Manager

    advertise_addr
        The ip of the manager

    listen_addr
        Listen address used for inter-manager communication,
        as well as determining the networking interface used
        for the VXLAN Tunnel Endpoint (VTEP).
        This can either be an address/port combination in
        the form 192.168.1.1:4567,
        or an interface followed by a port number,
        like eth0:4567

    force_new_cluster
        Force a new cluster if True is passed

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.swarm_init advertise_addr='192.168.50.10' listen_addr='0.0.0.0' force_new_cluster=False
    rzRDocker swarm has been initialized on {} and the worker/manager Join token is belowr)�CommentZTokensz%This node is already part of a swarm.F)r�result)�ErrorrNrz]Please make sure you are passing advertise_addr, listen_addr and force_new_cluster correctly.)rr�init�format�updaterr�errorsZAPIErrorZexplanation�str�	TypeError)Zadvertise_addr�listen_addrZforce_new_cluster�salt_return�output�errr	r	r
�
swarm_initNs8���
 
�
�����r*cCsfzi}tdjj|g||d�tdd}|�||d��W|Sty2i}|�ddi�Y|Sw)a�
    Join a Swarm Worker to the cluster

    remote_addr
        The manager node you want to connect to for the swarm

    listen_addr
        Listen address used for inter-manager communication if the node gets promoted to manager,
        as well as determining the networking interface used for the VXLAN Tunnel Endpoint (VTEP)

    token
        Either the manager join token or the worker join token.
        You can get the worker or manager token via ``salt '*' swarm.swarm_tokens``

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.joinswarm remote_addr=192.168.50.10 listen_addr='0.0.0.0'             token='SWMTKN-1-64tux2g0701r84ofq93zppcih0pe081akq45owe9ts61f30x4t-06trjugdu7x2z47j938s54il'
    r)Zremote_addrsr&Z
join_tokenrz has joined the Swarm)rZManager_AddrrzuPlease make sure this minion is not part of a swarm and you are passing remote_addr, listen_addr and token correctly.)rr�joinr"r%)Zremote_addrr&�tokenr'r(r	r	r
�	joinswarm�s"
�����r-cCs4i}tdjj|d�tdd}|�d|i�|S)z�
    Force the minion to leave the swarm

    force
        Will force the minion/worker/manager to leave the swarm

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.leave_swarm force=False
    r��forcerz has left the swarmr)rrZleaver")r/r'r(r	r	r
�leave_swarm�s

r0cCs�z<i}tjjd|d�}tjj||id�}	tdjj|||||	d�tdd|}
|�|
td|||||||d�	�W|StyOi}|�d	d
i�Y|Sw)a
    Create Docker Swarm Service Create

    image
        The docker image

    name
        Is the service name

    command
        The docker command to run in the container at launch

    hostname
        The hostname of the containers

    replicas
        How many replicas you want running in the swarm

    target_port
        The target port on the container

    published_port
        port that's published on the host/os

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.service_create image=httpd name=Test_Service             command=None hostname=salthttpd replicas=6 target_port=80 published_port=80
    Z
replicated)�replicas)�portsr)�name�image�command�modeZ
endpoint_specrz* has a Docker Swarm Service running named )	ZInfoZMinion�Name�ImageZCommand�Hostname�ReplicasZTarget_PortZPublished_PortrzPlease make sure you are passing arguments correctly [image, name, command, hostname, replicas, target_port and published_port])	r�typesZServiceMode�EndpointSpecrZservices�creater"r%)r4r3r5�hostnamer1�target_port�published_portr'Zreplica_moder2Zechobackr	r	r
�service_create�sN(
����������rAcCsLz�i}tjdd�}|j|d�}tjj�|�}tjj�|�}|dd}|dd}|ddd	}|ddd
}	|d}
|d}|d
}|dd}
|dd	dd}|dd}|dddd}|	D](}|d}|d}|d}|d}|�|||
|||||||||t	d|d��qgW|St
y�i}|�ddi�Y|Sw)z�
    Swarm Service Information

    service_name
        The name of the service that you want information on about the service

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.swarm_service_info service_name=Test_Service
    rr)r�Version�Index�Specr7r<ZModeZPorts�IDZ	CreatedAtZ	UpdatedAtZLabelsZ
Replicatedr:ZEndpointZ
VirtualIPsZTaskTemplateZ
ContainerSpecr8Z
PublishedPortZ
TargetPortZPublishMode�Protocolr)zService Namer:z
Service IDZNetworkzNetwork Modez
Creation DatezUpdate DatezPublished PortzTarget PortzPublished ModerFzDocker Imagez	Minion IdrBrzservice_name arg is missing?)rrZinspect_service�salt�utils�json�dumps�loadsr"rr%)Zservice_namer'rr�getdata�dump�versionr3Znetwork_moder2Zswarm_idZcreate_dateZupdate_date�labelsr1�networkr4�itemsr@r?Zpublished_mode�protocolr	r	r
�swarm_service_infos\
�����rScCs\zi}tjdd�}|�|�}|�|tdd��W|Sty-i}|�ddi�Y|Sw)z�
    Remove Swarm Service

    service
        The name of the service

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.remove_service service=Test_Service
    rrr)zService Deletedz	Minion IDrzservice arg is missing?)rr�remove_servicer"rr%)rr'rr	r	r
rTOs

���rTcCs�zbi}tjdd�}|jd|id�}tjj�|�}tjj�|�}|D]>}|ddd}|dd}|dd	}	|d
}
|dd}|dd
}|d}
|dd}|�|||	|
|||
|d��q!W|St	yui}|�ddi�Y|Sw)z�
    Displays Information about Swarm Nodes with passing in the server

    server
        The minion/server name

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.node_ls server=minion1
    rrr3)�filtersZDescriptionZEngineZ
EngineVersion�Platformr9rErD�Role�Availability�StatusrBrC)zDocker VersionrVr9rEZRolesrXrYrBrz>The server arg is missing or you not targeting a Manager node?)
rrZnodesrGrHrIrJrKr"r%)Zserverr'rrrLrMrQZdocker_version�platformZ	hostnamesZids�role�availability�statusrNr	r	r
�node_lsisF
������r^cCsftjdd�}z|dkr|j|dd�}|WS|j|dd�}|WSty2i}|�ddi�|YSw)	aU
    Remove a node from a swarm and the target needs to be a swarm manager

    node_id
        The node id from the return of swarm.node_ls

    force
        Forcefully remove the node/minion from the service

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.remove_node node_id=z4gjbe9rwmqahc2a91snvolm5 force=false
    rr�TrueTr.Frz/Is the node_id and/or force=True/False missing?)rr�remove_noder%r")�node_idr/rrr'r	r	r
r`�s�r`cCshtjdd�}zi}|||d�}|j|||d�|�d|i�W|Sty3i}|�ddi�Y|Sw)a�
    Updates docker swarm nodes/needs to target a manager node/minion

    availability
        Drain or Active

    node_name
        minion/node

    role
        role of manager or worker

    node_id
        The Id and that can be obtained via swarm.node_ls

    version
        Is obtained by swarm.node_ls

    CLI Example:

    .. code-block:: bash

        salt '*' swarm.update_node availability=drain node_name=minion2             role=worker node_id=3k9x7t8m4pel9c0nqr3iajnzp version=19
    rr)rXr7rW)rarN�	node_speczNode InformationrzOMake sure all args are passed [availability, node_name, role, node_id, version])rr�update_noder"r%)r\Z	node_namer[rarNrr'rbr	r	r
rc�s ����rc)�__doc__Zsalt.utils.jsonrGr
rr�ImportErrorrrrrr$�int�boolr*r-r0rArSrTr^r`rcr	r	r	r
�<module>s:�6*
�R;0