
    Vh)                         d dl mZmZmZ eZdZdZdZd dl	Z		 d dl
mZmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ  G d de      Z G d de      Zd Zedk(  r e        yy# e$ r Y Hw xY w)    )absolute_importdivisionprint_functiona  
module: docker_node
short_description: Manage Docker Swarm node
description:
  - Manages the Docker nodes through a Swarm Manager.
  - This module allows to change the node's role, its availability, and to modify, add or remove node labels.
extends_documentation_fragment:
  - community.docker.docker
  - community.docker.docker.docker_py_1_documentation
  - community.docker.attributes
  - community.docker.attributes.actiongroup_docker

attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
  idempotent:
    support: full

options:
  hostname:
    description:
      - The hostname or ID of node as registered in Swarm.
      - If more than one node is registered using the same hostname the ID must be used, otherwise module will fail.
    type: str
    required: true
  labels:
    description:
      - User-defined key/value metadata that will be assigned as node attribute.
      - Label operations in this module apply to the docker swarm node specified by O(hostname). Use M(community.docker.docker_swarm)
        module to add/modify/remove swarm cluster labels.
      - The actual state of labels assigned to the node when module completes its work depends on O(labels_state) and O(labels_to_remove)
        parameters values. See description below.
    type: dict
  labels_state:
    description:
      - It defines the operation on the labels assigned to node and labels specified in O(labels) option.
      - Set to V(merge) to combine labels provided in O(labels) with those already assigned to the node. If no labels are
        assigned then it will add listed labels. For labels that are already assigned to the node, it will update their values.
        The labels not specified in O(labels) will remain unchanged. If O(labels) is empty then no changes will be made.
      - Set to V(replace) to replace all assigned labels with provided ones. If O(labels) is empty then all labels assigned
        to the node will be removed.
    type: str
    default: 'merge'
    choices:
      - merge
      - replace
  labels_to_remove:
    description:
      - List of labels that will be removed from the node configuration. The list has to contain only label names, not their
        values.
      - If the label provided on the list is not assigned to the node, the entry is ignored.
      - If the label is both on the O(labels_to_remove) and O(labels), then value provided in O(labels) remains assigned to
        the node.
      - If O(labels_state=replace) and O(labels) is not provided or empty then all labels assigned to node are removed and
        O(labels_to_remove) is ignored.
    type: list
    elements: str
  availability:
    description: Node availability to assign. If not provided then node availability remains unchanged.
    choices:
      - active
      - pause
      - drain
    type: str
  role:
    description: Node role to assign. If not provided then node role remains unchanged.
    choices:
      - manager
      - worker
    type: str

requirements:
  - "L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 2.4.0"
  - Docker API >= 1.25
author:
  - Piotr Wojciechowski (@WojciechowskiPiotr)
  - Thierry Bouvet (@tbouvet)
a  
---
- name: Set node role
  community.docker.docker_node:
    hostname: mynode
    role: manager

- name: Set node availability
  community.docker.docker_node:
    hostname: mynode
    availability: drain

- name: Replace node labels with new labels
  community.docker.docker_node:
    hostname: mynode
    labels:
      key: value
    labels_state: replace

- name: Merge node labels and new labels
  community.docker.docker_node:
    hostname: mynode
    labels:
      key: value

- name: Remove all labels assigned to node
  community.docker.docker_node:
    hostname: mynode
    labels_state: replace

- name: Remove selected labels from the node
  community.docker.docker_node:
    hostname: mynode
    labels_to_remove:
      - key1
      - key2
zh
node:
  description: Information about node after 'update' operation.
  returned: success
  type: dict
N)DockerExceptionAPIError)DockerBaseClassRequestException)	to_native)AnsibleDockerSwarmClient)sanitize_labelsc                        e Zd Z fdZ xZS )TaskParametersc                    t         t        |           d | _        d | _        d | _        d | _        d | _        d | _        |j                  j                  j                         D ]  \  }}t        | ||        t        | j                  d|       y )Nlabels)superr   __init__namer   labels_statelabels_to_removeavailabilityrolemoduleparamsitemssetattrr   )selfclientkeyvalue	__class__s       p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/docker/plugins/modules/docker_node.pyr   zTaskParameters.__init__   s    nd,. 	  $ !	 --..446 	&JCD#u%	& 	Xv6    )__name__
__module____qualname__r   __classcell__r    s   @r!   r   r      s    7 7r"   r   c                   $     e Zd Z fdZd Z xZS )SwarmNodeManagerc                     t         t        |           || _        || _        | j                  j
                  | _        | j                  j                          t        |      | _        | j                          y )N)
r   r)   r   r   results
check_modefail_task_if_not_swarm_managerr   
parametersnode_update)r   r   r+   r    s      r!   r   zSwarmNodeManager.__init__   sX    .0++00224(0r"   c                 	   | j                   j                  | j                  j                        s| j                   j	                  d       y | j                   j                         r| j                   j	                  d       	 | j                   j                  | j                  j                        }d}t        | j                  j                  | j                  j                  | j                  j                        }| j                  j                  d   d   |d<   n:d   d   | j                  j                  k(  s| j                  j                  |d<   d	}| j                  j                  |d   d
   |d
<   n=|d   d
   | j                  j                  k(  s| j                  j                  |d   d
<   d	}| j                  j                  dk(  ri| j                  j                  i |d<   |d   d   rd	}n|d   d   xs i | j                  j                  k7  r| j                  j                  |d<   d	}n| j                  j                  dk(  rqt        |d   d   xs i       |d<   | j                  j                  N| j                  j                  j                         D ]'  \  }}|d   j                  |      |k7  s||d   |<   d	}) | j                  j                   | j                  j                   D ]  }| j                  j                  | j                  j                  j                  |      s,|d   j                  |      sS|d   j#                  |       d	}j| j                   j$                  j'                  dt        |      z         |d   j                  |      s|d   j#                  |       d	} |d	u rp| j(                  s(	 | j                   j+                  |d   |d   d   |       | j                   j-                  |d         | j.                  d<   || j.                  d<   y || j.                  d<   || j.                  d<   y # t        $ r2}| j                   j	                  dt        |      z         Y d }~d }~ww xY w# t        $ r1}| j                   j	                  dt        |      z         Y d }~d }~ww xY w)N)node_idz!This node is not part of a swarm.z*Can not update the node. The node is down.z%Failed to get node information for %sF)AvailabilityRoleLabelsSpecr3   Tr2   replacer4   mergez\Label '%s' listed both in 'labels' and 'labels_to_remove'. Keeping the assigned label value.IDVersionIndex)r1   version	node_speczFailed to update node : %snodechanged)r   check_if_swarm_noder.   hostnamefailcheck_if_swarm_node_is_downinspect_noder   r
   dictr   r   r   r   r   getr   popr   warnr,   update_nodeget_node_inspectr+   )r   	node_infoexcr>   r<   r   r   s          r!   r/   zSwarmNodeManager.node_update   sO   //8P8P/QKK@A;;224KKIJ	W009Q9Q0RI 55%%??))
	 ??' )& 1& 9IfV$V,0D0DD$(OO$8$8	&!??''/(1&(9.(IIn%V$^48T8TT48OO4P4P	&!.1??''94%%-&(	(#V$X."Gf%h/52$//:P:PP*.//*@*@Ih'"G__))W4"&y'8'B'Hb"IIh%%1"&//"8"8">">"@ 'JC *..s3u<38	(+C0"&'
 //;??;; +C--9#5599#>(266s; )( 3 7 7 <*. KK..33!D"+C.!12
 %X.2237%h/33C8&*G+  d???TKK++IdOYW`MabiMj6? , A $(;;#?#?	RV#?#XDLL &-DLL##,DLL &-DLL#K  	WKKDyQT~UVV	W~   TKK$$%AIcN%RSSTs0   0Q 'R 	R#'RR	S!'SS)r#   r$   r%   r   r/   r&   r'   s   @r!   r)   r)      s    O.r"   r)   c                  ~   t        t        dd      t        d      t        ddddg      t        d	d
      t        dg d      t        dddg            } t        | dd      }	 t        d      }t        ||        |j                  j                  di | y # t
        $ rG}|j                  dj                  t        |            t        j                                Y d }~y d }~wt        $ rG}|j                  dj                  t        |            t        j                                Y d }~y d }~ww xY w)NstrT)typerequiredrD   )rN   r7   r6   )rN   defaultchoiceslist)rN   elements)activepausedrain)rN   rQ   workermanager)r@   r   r   r   r   r   z2.4.0)argument_specsupports_check_modemin_docker_versionF)r>   z(An unexpected docker error occurred: {0})	exceptionzhAn unexpected requests error occurred when Docker SDK for Python tried to talk to the docker daemon: {0} )rD   r   r)   r   	exit_jsonr   rA   formatr
   	traceback
format_excr	   )rY   r   r+   es       r!   mainrc     s+   540 ug?ST6E:u.JKux&;<M &# "F.
 	)*'* w>EEiPQlS_h_s_s_uvv .v}}  H  IJ  K  L**, 	 	. 	..s$   )4B 	D<'=C))D<5=D77D<__main__)
__future__r   r   r   rN   __metaclass__DOCUMENTATIONEXAMPLESRETURNr`   docker.errorsr   r   ImportError@ansible_collections.community.docker.plugins.module_utils.commonr   r	   +ansible.module_utils.common.text.convertersr
   ?ansible_collections.community.docker.plugins.module_utils.swarmr   >ansible_collections.community.docker.plugins.module_utils.utilr   r   r)   rc   r#   r]   r"   r!   <module>rp      s    A @Ob$L
 	7

 B d Z7_ 7(_. _.D.> zF I  		s   A! !A)(A)