B
    `9)                 @   s   d dl mZmZmZ eZdZdZdZd dl	Z	yd dl
mZmZ W n ek
rT   Y nX d dlmZmZ d dlmZ d d	lmZ G d
d deZG dd deZdd Zedkre  dS )    )absolute_importdivisionprint_functiona  
---
module: docker_node
short_description: Manage Docker Swarm node
description:
    - Manages the Docker nodes via Swarm Manager.
    - This module allows to change the node's role, its availability, and to modify, add or remove node labels.
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: yes
    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 I(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
              I(labels_state) and I(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 I(labels) option.
            - Set to C(merge) to combine labels provided in I(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 I(labels) will remain unchanged.
              If I(labels) is empty then no changes will be made.
            - Set to C(replace) to replace all assigned labels with provided ones. If I(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 I(labels_to_remove) and I(labels), then value provided in I(labels) remains
              assigned to the node.
            - If I(labels_state) is C(replace) and I(labels) is not provided or empty then all labels assigned to
              node are removed and I(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
extends_documentation_fragment:
- community.docker.docker
- community.docker.docker.docker_py_1_documentation

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)AnsibleDockerSwarmClientc                   s   e Zd Z fddZ  ZS )TaskParametersc                s\   t t|   d | _d | _d | _d | _d | _d | _x$|j	j
 D ]\}}t| || q@W d S )N)superr   __init__namelabelslabels_statelabels_to_removeavailabilityrolemoduleparamsitemssetattr)selfclientkeyvalue)	__class__ o/home/dcms/DCMS/lib/python3.7/site-packages/ansible_collections/community/docker/plugins/modules/docker_node.pyr      s    zTaskParameters.__init__)__name__
__module____qualname__r   __classcell__r   r   )r   r   r      s   r   c                   s$   e Zd Z fddZdd Z  ZS )SwarmNodeManagerc                sD   t t|   || _|| _| jj| _| j  t|| _| 	  d S )N)
r   r#   r   r   results
check_modeZfail_task_if_not_swarm_managerr   
parametersnode_update)r   r   r$   )r   r   r   r      s    


zSwarmNodeManager.__init__c          
   C   sV  | j j| jjds"| j d d S | j  r8| j d y| j j| jjd}W n6 tk
r } z| j dt|  W d d }~X Y nX d}t	| jj
| jj| jjd}| jjd kr|d d |d< n$|d d | jjks| jj|d< d	}| jj
d kr|d d
 |d
< n*|d d
 | jj
ks0| jj
|d d
< d	}| jjdkr| jjd krhi |d< |d d rd	}n,|d d pxi | jjkr| jj|d< d	}n| jjdkrt	|d d pi |d< | jjd k	rx>| jj D ].\}}|d ||kr||d |< d	}qW | jjd k	rx| jjD ]}| jjd k	r| jj|sl|d |r|d | d	}n| j jdt|  n"|d |r&|d | d	}q&W |d	kr>| jsy"| j j|d |d d |d W n8 tk
r } z| j dt|  W d d }~X Y nX | j j|d d| jd< || jd< n|| jd< || jd< d S )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LabelsZSpecr*   Tr)   replacer+   mergez\Label '%s' listed both in 'labels' and 'labels_to_remove'. Keeping the assigned label value.ZIDVersionZIndex)r(   version	node_speczFailed to update node : %snodechanged)r   Zcheck_if_swarm_noder&   hostnamefailZcheck_if_swarm_node_is_downZinspect_noder   r	   dictr   r   r   r   r   getr   popr   warnr%   Zupdate_nodeZget_node_inspectr$   )r   Z	node_infoexcr2   r0   r   r   r   r   r   r'      s~    
&


&
zSwarmNodeManager.node_update)r   r    r!   r   r'   r"   r   r   )r   r   r#      s   r#   c           
   C   s   t t dddt ddt ddddgdt d	dd
t ddddgdt dddgdd} t| dddd}y&t dd}t|| |jjf | W nr tk
r } z|jd|t	 d W d d }~X Y n: t
k
r } z|jd|t	 d W d d }~X Y nX d S )NstrT)typerequiredr5   )r;   r-   r,   )r;   defaultchoiceslist)r;   elementsactivepauseZdrain)r;   r>   Zworkermanager)r3   r   r   r   r   r   z2.4.0z1.25)argument_specZsupports_check_modeZmin_docker_versionZmin_docker_api_versionF)r2   z(An unexpected docker error occurred: {0})	exceptionz\An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0})r5   r
   r#   r   Z	exit_jsonr   r4   format	traceback
format_excr   )rD   r   r$   er   r   r   main  s*    


(rJ   __main__)
__future__r   r   r   r;   __metaclass__ZDOCUMENTATIONZEXAMPLESZRETURNrG   Zdocker.errorsr   r   ImportErrorZ@ansible_collections.community.docker.plugins.module_utils.commonr   r   Zansible.module_utils._textr	   Z?ansible_collections.community.docker.plugins.module_utils.swarmr
   r   r#   rJ   r   r   r   r   r   <module>   s"   J%b