
    Vh*                         d dl mZmZmZ eZdZdZd dlZdZ		 d dl
Z
dZd dlmZ d dlmZ d d	lmZ d d
lmZmZmZ d dlZd Zd ZddZd Zedk(  r e        yy# e$ r  ej                         Z	dZY Uw xY w)    )absolute_importdivisionprint_functiona<	  
module: redis
short_description: Various redis commands, replica and flush
description:
  - Unified utility to interact with redis instances.
extends_documentation_fragment:
  - community.general.redis
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  command:
    description:
      - The selected redis command.
      - V(config) ensures a configuration setting on an instance.
      - V(flush) flushes all the instance or a specified db.
      - V(replica) sets a redis instance in replica or master mode. (V(slave) is an alias for V(replica)).
    choices: [config, flush, replica, slave]
    type: str
  tls:
    default: false
    version_added: 4.6.0
  login_user:
    version_added: 4.6.0
  validate_certs:
    version_added: 4.6.0
  ca_certs:
    version_added: 4.6.0
  master_host:
    description:
      - The host of the master instance [replica command].
    type: str
  master_port:
    description:
      - The port of the master instance [replica command].
    type: int
  replica_mode:
    description:
      - The mode of the redis instance [replica command].
      - V(slave) is an alias for V(replica).
    default: replica
    choices: [master, replica, slave]
    type: str
    aliases:
      - slave_mode
  db:
    description:
      - The database to flush (used in DB mode) [flush command].
    type: int
  flush_mode:
    description:
      - Type of flush (all the DBs in a redis instance or a specific one) [flush command].
    default: all
    choices: [all, db]
    type: str
  name:
    description:
      - A redis config key.
    type: str
  value:
    description:
      - A redis config value. When memory size is needed, it is possible to specify it in the usual form of 1KB, 2M, 400MB
        where the base is 1024. Units are case insensitive, in other words 1m = 1mb = 1M = 1MB.
    type: str

notes:
  - Requires the C(redis-py) Python package on the remote host. You can install it with pip (C(pip install redis)) or with
    a package manager. U(https://github.com/andymccurdy/redis-py).
  - If the redis master instance you are making replica of is password protected this needs to be in the C(redis.conf) in
    the C(masterauth) variable.
seealso:
  - module: community.general.redis_info
requirements: [redis]
author: "Xabier Larrakoetxea (@slok)"
a  
- name: Set local redis instance to be a replica of melee.island on port 6377
  community.general.redis:
    command: replica
    master_host: melee.island
    master_port: 6377

- name: Deactivate replica mode
  community.general.redis:
    command: replica
    replica_mode: master

- name: Flush all the redis db
  community.general.redis:
    command: flush
    flush_mode: all

- name: Flush only one db in a redis instance
  community.general.redis:
    command: flush
    db: 1
    flush_mode: db

- name: Configure local redis to have 10000 max clients
  community.general.redis:
    command: config
    name: maxclients
    value: 10000

- name: Configure local redis maxmemory to 4GB
  community.general.redis:
    command: config
    name: maxmemory
    value: 4GB

- name: Configure local redis to have lua time limit of 100 ms
  community.general.redis:
    command: config
    name: lua-time-limit
    value: 100

- name: Connect using TLS and certificate authentication
  community.general.redis:
    command: config
    name: lua-time-limit
    value: 100
    tls: true
    ca_certs: /etc/redis/certs/ca.crt
    client_cert_file: /etc/redis/certs/redis.crt
    client_key_file: /etc/redis/certs/redis.key
NTF)AnsibleModule)human_to_bytes)	to_native)fail_importsredis_auth_argument_specredis_auth_paramsc                 F    	 | j                  ||      S # t        $ r Y yw xY wNFslaveof	Exception)clientmaster_hostmaster_ports      k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/redis.pyset_replica_moder      s*    ~~k;77 s    	  c                 B    	 | j                         S # t        $ r Y yw xY wr   r   )r   s    r   set_master_moder      s&    ~~ s    	c                     	 t        |t              s| j                         S | j                         S # t        $ r Y yw xY wr   )
isinstanceintflushallflushdbr   )r   dbs     r   flushr      s?    "c"??$$ >>## s   2 2 	>>c                      t        d      } t        t        dg d      t        d      t        d      t        ddg d	d
g      t        d      t        ddddg      t        d      t        d            }|j                  |        t        |d      }t	        ||j
                  d          t        |      }|j
                  d   }|dk(  rd}|dk(  r|j
                  d   }|j
                  d   }|j
                  d   }|dk(  rd}|dk(  r(|s|j                  d       |s|j                  d       t        j                  d2i |}	 |j                          |j                         }
|dk(  r|
d   dk(  r|j!                  d|       y |dk(  r:|
d   dk(  r2|
d   |k(  r*|
d   |k(  r"t        |||       }|j!                  d|       y |dk(  rV|j"                  st%        |||      r*|j                         }
|||d }|j!                  d|       y |j                  d!       y |j"                  st'        |      r|j!                  d|       y |j                  d"       y |d#k(  r|j
                  d   }|j
                  d$   }|dk(  r||j                  d%       t        j                  d2d|i|}	 |j                          |dk(  r>|j"                  st)        |      r|j!                  dd&       y |j                  d'       y |j"                  st)        ||      r|j!                  dd|(       y |j                  d)|z         y |d*k(  r|j
                  d+   }	 t+        j,                  d,|j
                  d-   j/                               r0t1        t3        |j
                  d-   j/                                     }n|j
                  d-   }t        j                  d2i |}	 |j                          	 |j7                  |      |   }|k7  }|j"                  s|s|j!                  |||/       y 	 |j9                  ||       |j!                  |||/       y |j                  d1       y # t        $ r<}	|j                  dt        |	      z  t        j                                Y d }	~	Wd }	~	ww xY w# t        $ r<}	|j                  dt        |	      z  t        j                                Y d }	~	2d }	~	ww xY w# t4        $ r |j
                  d-   }Y Iw xY w# t        $ r<}	|j                  dt        |	      z  t        j                                Y d }	~	gd }	~	ww xY w# t        $ r<}	|j                  d.t        |	      z  t        j                                Y d }	~	d }	~	ww xY w# t        $ r<}	|j                  d0t        |	      z  t        j                                Y d }	~	d }	~	ww xY w)3NF)tls_defaultstr)configr   replicaslave)typechoices)r%   r   r#   )masterr#   r$   
slave_mode)r%   defaultr&   aliasesallr   )r%   r)   r&   )commandr   r   replica_moder   
flush_modenamevalueT)argument_specsupports_check_modetlsr,   r$   r   r   r-   z,In replica mode master host must be provided)msgz,In replica mode master port must be providedz!unable to connect to database: %s)r4   	exceptionr'   role)changedmode)statusr   r   zUnable to set replica modezUnable to set master moder   r.   z)In db mode the db number must be provided)r7   flushedzUnable to flush all databases)r7   r:   r   zUnable to flush '%d' databaser"   r/   z"^\s*(\d*\.?\d*)\s*([A-Za-z]+)?\s*$r0   zunable to read config: %s)r7   r/   r0   zunable to write config: %sz A valid command must be provided )r
   dictupdater   r	   paramsr   	fail_jsonredisStrictRedispingr   r   	traceback
format_excinfo	exit_json
check_moder   r   r   rematchupperr!   r   
ValueError
config_get
config_set)redis_auth_argsmodule_argsmoduleredis_paramsr,   r   r   r8   rerE   r9   r   r/   r0   	old_valuer7   s                    r   mainrU      s   .5AO%)PQe$e$uiA_#/.2UUEE4=Iu
K '! F
 u-.$V,LmmI&G' )mmM2mmM2}}^,7?D 9  %S T  %S T --	wFFH
 vvx8V 8U6Y4<7#:tM?RVa?afjkxfy  ~I  gI''F
 U8
 y $$(8K(U668D"&'2'2F
 $$T$?$$)E$F $$(:$$T$=$$)D$E 
G	]]4 }}\* 4<z  %P Q 44|4	wFFH 5=  E!H  t <  %D E   E!RL  t C  %Dr%I J	H	}}V$	+xx=v}}W?U?[?[?]^N6==+A+G+G+IJKg. --	wFFH	oT*40I u$GW4uEtT5) W4uE?@K  	w!DyQR|!S_h_s_s_uvv	wf  	w!DyQR|!S_h_s_s_uvv	w2  	+MM'*E	+  	w!DyQR|!S_h_s_s_uvv	w
  	o!<y|!KW`WkWkWmnn	o  t  %AIaL%P\e\p\p\r ssts   Q9 ?S +A0T	 1T( U0 ?V8 9	R>1R99R>	T
1TT	T%$T%(	U-11U((U-0	V591V00V58	W=1W88W=__main__)N)
__future__r   r   r   r%   __metaclass__DOCUMENTATIONEXAMPLESrC   REDIS_IMP_ERRr@   redis_foundImportErrorrD   ansible.module_utils.basicr   +ansible.module_utils.common.text.formattersr   +ansible.module_utils.common.text.convertersr   @ansible_collections.community.general.plugins.module_utils.redisr	   r
   r   rH   r   r   r   rU   __name__r;       r   <module>rd      s    A @M^2h 
 K 4 F A? ? 	TAn zF A  (I((*MKs   A A43A4