
    Vh4                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZmZmZmZ d dlmZ d dlmZ d	 Z G d
 de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: proxysql_scheduler
author: "Ben Mildren (@bmildren)"
short_description: Adds or removes schedules from proxysql admin interface
description:
   - The M(community.proxysql.proxysql_scheduler) module adds or removes schedules using the
     proxysql admin interface.
options:
  active:
    description:
      - A schedule with I(active) set to C(False) will be tracked in the
        database, but will be never loaded in the in-memory data structures.
    type: bool
    default: true
  interval_ms:
    description:
      - How often (in millisecond) the job will be started. The minimum value
        for I(interval_ms) is 100 milliseconds.
    type: int
    default: 10000
  filename:
    description:
      - Full path of the executable to be executed.
    type: str
    required: true
  arg1:
    description:
      - Argument that can be passed to the job.
    type: str
  arg2:
    description:
      - Argument that can be passed to the job.
    type: str
  arg3:
    description:
      - Argument that can be passed to the job.
    type: str
  arg4:
    description:
      - Argument that can be passed to the job.
    type: str
  arg5:
    description:
      - Argument that can be passed to the job.
    type: str
  comment:
    description:
      - Text field that can be used for any purposed defined by the user.
    type: str
  state:
    description:
      - When C(present) - adds the schedule, when C(absent) - removes the
        schedule.
    type: str
    choices: [ "present", "absent" ]
    default: present
  force_delete:
    description:
      - By default we avoid deleting more than one schedule in a single batch,
        however if you need this behaviour and you are not concerned about the
        schedules deleted, you can set I(force_delete) to C(True).
    type: bool
    default: false
extends_documentation_fragment:
- community.proxysql.proxysql.managing_config
- community.proxysql.proxysql.connectivity
notes:
- Supports C(check_mode).
a  
---
# This example adds a schedule, it saves the scheduler config to disk, but
# avoids loading the scheduler config to runtime (this might be because
# several servers are being added and the user wants to push the config to
# runtime in a single batch using the community.general.proxysql_manage_config
# module).  It uses supplied credentials to connect to the proxysql admin
# interface.

- name: Add a schedule
  community.proxysql.proxysql_scheduler:
    login_user: 'admin'
    login_password: 'admin'
    interval_ms: 1000
    filename: "/opt/maintenance.py"
    state: present
    load_to_runtime: false

# This example removes a schedule, saves the scheduler config to disk, and
# dynamically loads the scheduler config to runtime.  It uses credentials
# in a supplied config file to connect to the proxysql admin interface.

- name: Remove a schedule
  community.proxysql.proxysql_scheduler:
    config_file: '~/proxysql.cnf'
    filename: "/opt/old_script.py"
    state: absent
a  
stdout:
    description: The schedule modified or removed from proxysql.
    returned: On create/update will return the newly modified schedule, on
              delete it will return the deleted record.
    type: dict
    "sample": {
        "changed": true,
        "filename": "/opt/test.py",
        "msg": "Added schedule to scheduler",
        "schedules": [
            {
                "active": "1",
                "arg1": null,
                "arg2": null,
                "arg3": null,
                "arg4": null,
                "arg5": null,
                "comment": "",
                "filename": "/opt/test.py",
                "id": "1",
                "interval_ms": "10000"
            }
        ],
        "state": "present"
    }
)AnsibleModule)mysql_connectmysql_driverproxysql_common_argument_specsave_config_to_diskload_config_to_runtime)	iteritems)	to_nativec                 r    | j                   d   dk  s| j                   d   dkD  r| j                  d       y y )Ninterval_msd   i z,interval_ms must between 100ms & 100000000msmsg)params	fail_json)modules    y/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/proxysql/plugins/modules/proxysql_scheduler.pyperform_checksr      s?    }}]#c)
--
&
2> 	 	
 3    c                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
ProxySQLSchedulec                 X   j                   d   | _        j                   d   | _        j                   d   | _        j                   d   | _        j                   d   | _        j                   d   | _        j                   d   | _        g d}t        fd	|D              | _	        y )
Nstateforce_deletesave_to_diskload_to_runtimeactiver   filename)arg1arg2arg3arg4arg5commentc              3   @   K   | ]  }|j                   |   f  y w)N)r   ).0kr   s     r   	<genexpr>z,ProxySQLSchedule.__init__.<locals>.<genexpr>   s'       ;$% "#FMM!$4 5  ;s   )
r   r   r   r   r   r    r   r!   dictconfig_data)selfr   config_data_keyss    ` r   __init__zProxySQLSchedule.__init__   s    ]]7+
"MM.9"MM.9%}}->?mmH-!==7j1'    ;)9 ; ;r   c                 $   d}| j                   | j                  | j                  g}t        | j                        D ]$  \  }}|	|j                  |       |d|z   dz   z  }& |j                  ||       |j                         }t        |d         S )NzSELECT count(*) AS `schedule_count`
               FROM scheduler
               WHERE active = %s
                 AND interval_ms = %s
                 AND filename = %s
  AND  = %sschedule_count)	r    r   r!   r   r-   appendexecutefetchoneintr.   cursorquery_string
query_datacolvalcheck_counts          r   check_schedule_configz&ProxySQLSchedule.check_schedule_config   s    & 	 [[]] 	
 "$"2"23 	;HC!!#&
S 07 ::	;
 	|Z0oo';/011r   c                    d}| j                   | j                  | j                  g}t        | j                        D ]$  \  }}|	|j                  |       |d|z   dz   z  }& |j                  ||       |j                         }|S )NzSELECT *
               FROM scheduler
               WHERE active = %s
                 AND interval_ms = %s
                 AND filename = %sr2   r3   )r    r   r!   r   r-   r5   r6   fetchall)r.   r:   r;   r<   r=   r>   schedules          r   get_schedule_configz$ProxySQLSchedule.get_schedule_config   s    & 	 [[]] 	
 "$"2"23 	;HC!!#&
S 07 ::	;
 	|Z0??$r   c                    d}d}| j                   | j                  | j                  g}t        | j                        D ]&  \  }}|	|dz  }|j                  |       |d|z   z  }( |dd|z  z   dz   z  }|j                  ||       y)	NzbINSERT INTO scheduler (
               active,
               interval_ms,
               filenamer      z,
z)
VALUES (%s, %s, %sz, %s)T)r    r   r!   r   r-   r5   r6   )r.   r:   r;   colsr<   r=   r>   s          r   create_schedule_configz'ProxySQLSchedule.create_schedule_config   s     	 [[]] 	
 "$"2"23 	,HC	!!#&+		, 	"d] 	 	|Z0r   c                    d}| j                   | j                  | j                  g}t        | j                        D ]$  \  }}|	|j                  |       |d|z   dz   z  }& |j                  ||       |j                  }dt        |      fS )NzDELETE FROM scheduler
               WHERE active = %s
                 AND interval_ms = %s
                 AND filename = %sr2   r3   T)	r    r   r!   r   r-   r5   r6   rowcountr8   r9   s          r   delete_schedule_configz'ProxySQLSchedule.delete_schedule_config   s    & 	 [[]] 	
 "$"2"23 	;HC!!#&
S 07 ::	;
 	|Z0ooS%%%r   c                 l    |r2| j                   rt        |d       | j                  rt        |d       y y y )N	SCHEDULER)r   r
   r   r   )r.   r:   r   s      r   manage_configzProxySQLSchedule.manage_config  s5      #FK8##&v{; $ r   c                     |sC| j                  |      |d<   d|d<   | j                  |      |d<   | j                  ||d          y d|d<   d|d<   y )NchangedzAdded schedule to schedulerr   	schedulesTzKSchedule would have been added to scheduler, however check_mode is enabled.)rI   rD   rO   r.   
check_moderesultr:   s       r   create_schedulez ProxySQLSchedule.create_schedule  sm    ++F3 99F5M((0 ;v%i02 !%F9,F5Mr   c                     |sC| j                  |      |d<   | j                  |      |d<   d|d<   | j                  ||d          y d|d<   d|d<   y )NrR   rQ   zDeleted schedule from schedulerr   TzOSchedule would have been deleted from scheduler, however check_mode is enabled.)rD   rL   rO   rS   s       r   delete_schedulez ProxySQLSchedule.delete_schedule)  so    ((0 ; ++F3 9=F5Mv%i02 !%F9)F5Mr   N)__name__
__module____qualname__r0   r@   rD   rI   rL   rO   rV   rX    r   r   r   r      s*    ;&2,,8&*<-*r   r   c                     t               } | j                  t        dd      t        dd      t        dd      t        d      t        d      t        d      t        d      t        d      t        d      t        d	d	d
g      t        dd      t        dd      t        dd             t        d|       }t	        |       |j
                  d   }|j
                  d   }|j
                  d   }d }	 t        ||||d      \  }}}t        |      }	i }
|	j                  |
d<   |	j                  |
d<   |	j                  d	k(  rR	 |	j                  |      dk  r|	j                  |j                   |
|       nd|
d<   d|
d<   |	j#                  |      |
d<   nr|	j                  d
k(  rc	 |	j                  |      }|dkD  rB|dk(  s|	j$                  r|	j'                  |j                   |
|       n|j                  d       n
d|
d<   d |
d<    |j(                  d"i |
 y # t        j                  $ r)}|j                  dt        |      z         Y d }~Hd }~ww xY w# t        j                  $ r(}|j                  dt        |      z         Y d }~d }~ww xY w# t        j                  $ r(}|j                  d!t        |      z         Y d }~d }~ww xY w)#NTbool)defaulttypei'  r8   str)requiredr`   )r`   presentabsent)r_   choicesF)r    r   r!   r"   r#   r$   r%   r&   r'   r   r   r   r   )supports_check_modeargument_spec
login_userlogin_passwordconfig_file
DictCursor)cursor_classz/unable to connect to ProxySQL Admin Module.. %sr   r   r!   r   rQ   z;The schedule already exists and doesn't need to be updated.r   rR   zunable to modify schedule.. %srF   zIOperation would delete multiple records use force_delete to override thisz<The schedule is already absent from the memory configurationzunable to remove schedule.. %sr\   )r	   updater,   r   r   r   r   r   Errorr   r   r   r   r!   r@   rV   rT   rD   r   rX   	exit_json)rg   r   rh   ri   rj   r:   db_connversioneproxysql_schedulerU   existing_scheduless               r   mainru   =  s
   13MDv.U3t%0uuuuu% 9y/7/9 :%f5$V4T7  "  #F
 6|,J]]#34N--.KF	
#01;1?1<>J	$L  )0F'--F7O*33F:)+	 66v>!C!11&2C2C2828: %*y!"8u &99&A {# 
	 	 H	,	!77? !A%%*.?.L.L%55f6G6G6<6<> $$B % 
 %*y!"9u Fvg  
AIaLP 	 	
 	

. !! 	4y|C   	. !! 	4y|C   	sJ   <H! AI  ,A"J !I4II J3JJK1KK__main__N)
__future__r   r   r   r`   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Aansible_collections.community.proxysql.plugins.module_utils.mysqlr   r   r	   r
   r   ansible.module_utils.sixr   ansible.module_utils._textr   r   objectr   ru   rY   r\   r   r   <module>r      sp    A @EN:
8 5  / 0
U*v U*xXv zF r   