
    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 d dlmZ d dlmZmZmZmZmZmZ d	 Zd
 Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: mongodb_stepdown
short_description: Step down the MongoDB node from a PRIMARY state.
description: >
  Step down the MongoDB node from the PRIMARY state if it has that status.
  Returns OK immediately if the member is already in the SECONDARY or ARBITER states.
  Will wait until a timeout for the member state to reach SECONDARY or PRIMARY,
  if the member state is currently STARTUP, RECOVERING, STARTUP2 or ROLLBACK,
  before taking any needed action.
author: Rhys Campbell (@rhysmeister)
version_added: "1.0.0"

extends_documentation_fragment:
  - community.mongodb.login_options
  - community.mongodb.ssl_options

options:
  poll:
    description:
      - The maximum number of times query for the member status.
    type: int
    default: 1
  interval:
    description:
      - The number of seconds to wait between poll executions.
    type: int
    default: 30
  stepdown_seconds:
    description:
      - The number of seconds to step down the primary, during which time the stepdown member is ineligible for becoming primary.
    type: int
    default: 60
  secondary_catch_up:
    description:
      - The secondaryCatchUpPeriodSecs parameter for the stepDown command.
      - The number of seconds that mongod will wait for an electable secondary to catch up to the primary.
    type: int
    default: 10
  force:
    description:
      - Optional. A boolean that determines whether the primary steps down if no electable and up-to-date secondary exists within the wait period.
    type: bool
    default: false
notes:
  - Requires the pymongo Python package on the remote host, version 4+.. This
    can be installed using pip or the OS package manager.
    @see U(http://api.mongodb.org/python/current/installation.html)
requirements:
  - pymongo
a`  
- name: Step down the current MongoDB member
  community.mongodb.mongodb_stepdown:
    login_user: admin
    login_password: secret

- name: Step down the current MongoDB member, poll a maximum of 5 times if member state is recovering
  community.mongodb.mongodb_stepdown:
    login_user: admin
    login_password: secret
    poll: 5
    interval: 10
a  
failed:
  description: If the module had failed or not.
  returned: always
  type: bool
iteration:
  description: Number of times the module has queried the replicaset status.
  returned: always
  type: int
msg:
  description: Status message.
  returned: always
  type: str
N)AnsibleModule)	to_native)missing_required_libmongodb_common_argument_spec
mongo_authPYMONGO_IMP_ERRpymongo_foundget_mongodb_clientc                     d}| j                   j                  d      }|d   D ]  }d|j                         v s|d   } |S )zs
    Return the member status string
    # https://docs.mongodb.com/manual/reference/command/replSetGetStatus/
    NreplSetGetStatusmembersselfstateStr)admincommandkeys)client
myStateStrrsmembers       v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/mongodb/plugins/modules/mongodb_stepdown.pymember_statusr   j   sQ    
 J			0	1BY- ,V[[]"
+J,     c                    	 ddl m} d}d}|j                  d   }|j                  d   }|j                  d   }|j                  d	   }	|j                  d
   }
i }d}||k  r	 |dz  }||d<   t        |       }|dk(  rS|j                  rd|d<   d|d<   d}n d|fd|	fd
|
fg      }	 | j                  j                  |       d|d<   d}d|d<   n]|dv rdj                  |      |d<   d|d<   d}n=|dv rt        j                  |       ndj                  |      |d<   d|d<   d}n	 ||k  r||d   |fS # t        $ rG}	 ddlm} n4# t        $ r(}|j	                  dt        |      z         Y d}~nd}~ww xY wY d}~vd}~ww xY w# t        $ r}t        |      dk(  rn|Y d}~d}~ww xY w# t        $ rH}|dz  }d|d<   d|d<   t        |      |d<   d}||k(  rY d}~t        j                  |       Y d}~d}~ww xY w)zD
    client - MongoDB Client
    module - Ansible module object
    r   )OrderedDictz[Cannot import OrderedDict class. You can probably install with: pip install ordereddict: %smsgNpollintervalstepdown_secondssecondary_catch_upforce   
iterationsPRIMARYzmember was stepped downr    TchangedreplSetStepDownsecondaryCatchUpPeriodSecszconnection closed)	SECONDARYARBITERzmember was already at {0} stateF)STARTUP
RECOVERINGSTARTUP2ROLLBACKzUnexpected member state {0}failed)collectionsr   ImportErrorordereddict	fail_jsonr   paramsr   
check_moder   r   	Exceptionstrformattimesleep)r   moduler   excepr'   failuresr!   r"   r#   r$   r%   
return_docstatusr   cmd_doces                   r   member_stepdownrE   w   s   1+ JH== D}}Z(H}}%78';<MM'"EJF
t
4	%!OJ'1J|$&v.JY&$$(AJu%,0Jy)!F)*,<=57IJ %(+ G
	(,,W5 -1Jy)!F(AJu%77$E$L$LZ$X
5!(-
9%PP

8$$A$H$H$T
5!(-
9% %M t
n :e$j00Q  1	1/ 	1!~(/"0 1 1	11J % ( u:)<< "'K !(0  		%MH#'Jx $)Jy! #AJuFT!

8$$		%s   D3 !2F- F- &F F- F- .5F- 3	F=EE>	E5E0+E>0E55E>>F	F*F% F- %F**F- -	G>6$G9G99G>c            
         t               } | j                  t        dd      t        dd      t        dd      t        dd      t        dd      	       t        | d
ddgg      }t        s |j                  t        d      t               t        d      }	 t        |d
      }t        ||d
      }	 t        |      \  }}}|d   }|d   }	du r|j                  	       y |j                  	       y # t        $ r(}|j                  dt        |      z         Y d }~ud }~ww xY w# t        $ r(}|j                  dt        |      z         Y d }~d }~ww xY w)NboolF)typedefaultint   r&   
   <   )r%   r"   r!   r$   r#   T
login_userlogin_password)argument_specsupports_check_moderequired_togetherpymongo)r    	exception)r2   )directConnectionz!Unable to connect to database: %sr   r'   r)   z$Unable to query replica_set info: %s)r    r'   r)   )r	   updatedictr   r   r6   r   r   r   r
   r9   r   rE   r:   	exit_json)
rP   r>   resultr   rD   rB   r    rA   r'   r)   s
             r   mainrZ      s   02M.5"-ua(UB75"5   # (*:;<F
 1)<#2 	 	4 FQ#FTBFFTBN"1&&"AZ-
Y' SZISZI  Q@9Q<OPPQ  NCc!fLMMNs0   D  8D4  	D1	D,,D14	E%=E  E%__main__)
__future__r   r   r   rH   __metaclass__DOCUMENTATIONEXAMPLESRETURNr<   ansible.module_utils.basicr   ansible.module_utils._textr   Iansible_collections.community.mongodb.plugins.module_utils.mongodb_commonr   r	   r
   r   r   r   r   rE   rZ   __name__ r   r   <module>rf      se    A @2h
   4 0 
P1p&JR zF r   