
    Vh'                     \    d dl mZmZmZ eZdZdZd dlZd dl	m
Z
mZ d Zedk(  r e        yy)    )absolute_importdivisionprint_functiona	  
module: supervisorctl
short_description: Manage the state of a program or group of programs managed by C(supervisord)
description:
  - Manage the state of a program or group of programs managed by C(supervisord).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    type: str
    description:
      - The name of the supervisord program or group to manage.
      - The name will be taken as group name when it ends with a colon V(:).
      - If O(name=all), all programs and program groups will be managed.
    required: true
  config:
    type: path
    description:
      - The supervisor configuration file path.
  server_url:
    type: str
    description:
      - URL on which supervisord server is listening.
  username:
    type: str
    description:
      - Username to use for authentication.
  password:
    type: str
    description:
      - Password to use for authentication.
  state:
    type: str
    description:
      - The desired state of program/group.
    required: true
    choices: ["present", "started", "stopped", "restarted", "absent", "signalled"]
  stop_before_removing:
    type: bool
    description:
      - Use O(stop_before_removing=true) to stop the program/group before removing it.
    required: false
    default: false
    version_added: 7.5.0
  signal:
    type: str
    description:
      - The signal to send to the program/group, when combined with the 'signalled' state. Required when l(state=signalled).
  supervisorctl_path:
    type: path
    description:
      - Path to C(supervisorctl) executable.
notes:
  - When O(state=present), the module will call C(supervisorctl reread) then C(supervisorctl add) if the program/group does
    not exist.
  - When O(state=restarted), the module will call C(supervisorctl update) then call C(supervisorctl restart).
  - When O(state=absent), the module will call C(supervisorctl reread) then C(supervisorctl remove) to remove the target program/group.
    If the program/group is still running, the action will fail. If you want to stop the program/group before removing, use
    O(stop_before_removing=true).
requirements: ["supervisorctl"]
author:
  - "Matt Wright (@mattupstate)"
  - "Aaron Wang (@inetfuture) <inetfuture@gmail.com>"
a  
- name: Manage the state of program to be in started state
  community.general.supervisorctl:
    name: my_app
    state: started

- name: Manage the state of program group to be in started state
  community.general.supervisorctl:
    name: 'my_apps:'
    state: started

- name: Restart my_app, reading supervisorctl configuration from a specified file
  community.general.supervisorctl:
    name: my_app
    state: restarted
    config: /var/opt/my_project/supervisord.conf

- name: Restart my_app, connecting to supervisord with credentials and server URL
  community.general.supervisorctl:
    name: my_app
    state: restarted
    username: test
    password: testpass
    server_url: http://localhost:9001

- name: Send a signal to my_app via supervisorctl
  community.general.supervisorctl:
    name: my_app
    state: signalled
    signal: USR1

- name: Restart all programs and program groups
  community.general.supervisorctl:
    name: all
    state: restarted
N)AnsibleModuleis_executablec                  n   t        t        dd      t        d      t        d      t        d      t        dd      t        d      t        ddg d      t        d	d
      t        d      	      } t        | ddddgfg      j                  d   d
j                  d      rdj	                  d      j                  d   j                  j                  d      }j                  j                  d      }j                  j                  d      }j                  j                  d      }j                  j                  d      }j                  j                  d      }j                  j                  d      }t        dddd      _        |rDt        j                  j                  |      rt        |      r|gn)j                  d|z         nj                  dd      g|rj                  d|g       |rj                  d|g       |rj                  d |g       |rj                  d!|g       dAfd"	fd#}dBfd$	}	d%k(  rB d&d'      \  }
}} |       }t        |      d(k(  rj                  d)*        |	|d+ d,d-        |       }d.k(  rt        |      d(k(  rj                  d
/       |r |	|d0 d1d2d
3       j                   rj                  d4        d5d'        d6      \  }
}}d7z  |v rj                  d/       nj                  |8       d9k(  rt        |      d(kD  rj                  d
/       j                   rj                  d4        d5d'        d:      \  }}}d;z  |v rj                  d/       nj                  |8       t        |      d(k(  rj                  d)*       d-k(  r |	|d< d=d-       d2k(  r |	|d> d1d2       dk(  r |	|d? d@|z  d       y y )CNstrT)typerequiredpath)r
   )r
   no_log)presentstarted	restartedstoppedabsent	signalled)r
   r   choicesboolF)r
   default)	nameconfig
server_urlusernamepasswordsupervisorctl_pathstatestop_before_removingsignalr   r   r   )argument_specsupports_check_moderequired_ifr   :r   r   r   r   r   r   C)LANGLC_ALLLC_MESSAGESLC_CTYPEzEProvided path to supervisorctl does not exist or isn't executable: %smsgsupervisorctlz-cz-sz-uz-pc                     t              }|j                  |        |r|j                  |        j                  |fi |S N)listappendrun_command)cmdr   kwargsargsmodulesupervisorctl_argss       s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/supervisorctl.pyrun_supervisorctlzmain.<locals>.run_supervisorctl   sA    &'CKK!v!!$1&11    c                  2   g }  d      \  }}}|j                         D ]o  }|j                  d      D cg c]
  }|dk7  s	| }}|d   }|d   }
rd|v r|j                  d      d   }	|	k7  rQR|k7  rdk7  r]| j                  ||f       q | S c c}w )Nstatus  r      r#   all)
splitlinessplitr/   )matchedrcouterrlinefieldfieldsprocess_namer:   groupis_groupr   r7   s             r6   get_matched_processesz#main.<locals>.get_matched_processes   s    (2CNN$ 	3D *.CHERKeHFH!!9LAYF,&(..s3A6E} 4'DEMNNL&12-	3. # Is   
BBc                 r   g }| D ]  \  }} ||      s|j                  |       ! t        |      dk(  r|sy j                  d       j                  r|sy j                  d       |D ]-  } ||d      \  }}	}
|d||	vsj	                  |	       / |rj                  d|	       y y )
Nr   Fchangedr   r   TrN   check_rcz: r)   )rN   r   r   affected)r/   len	exit_json
check_mode	fail_json)	processesstatus_filteractionexpected_resultexit_moduleto_take_action_onrH   r:   rB   rC   rD   r4   r   r7   r   s              r6   take_action_on_processesz&main.<locals>.take_action_on_processes   s    $- 	7 L&V$!((6	7  !Q&UUCT*- 	*L,V\DQLBS'9D  S )	*
 TEL]^ r8   r   updaterP   r   zERROR (no such process))r   r*   c                      y)NT ss    r6   <lambda>zmain.<locals>.<lambda>   s    r8   restartr   r   rM   c                 
    | dv S N)RUNNINGSTARTINGr`   ra   s    r6   rc   zmain.<locals>.<lambda>   s    !?V:V r8   stopr   )r[   rO   rereadremovez%s: removed process group)r*   r   r   r   addz%s: added process groupc                 
    | dvS rf   r`   ra   s    r6   rc   zmain.<locals>.<lambda>  s    a?V6V r8   startc                 
    | dv S rf   r`   ra   s    r6   rc   zmain.<locals>.<lambda>  s    a;R6R r8   c                 
    | dv S )N)rg   r`   ra   s    r6   rc   zmain.<locals>.<lambda>  s    a<6G r8   z	signal %sr-   )T)dictr   paramsendswithrstripgetrun_command_environ_updateosr   existsr   rV   get_bin_pathextendrS   rT   rU   )arg_specr   r   r   r   r   r   r   rK   r]   rB   rC   rD   rW   dummyrJ   r4   r   r7   r   r5   s                  @@@@@@r6   mainr}   {   sE   ut, U#5!5.V,6{|!vu=
H  {XJ78F == DH}}S{{3MM'"E!==,,-CD]]x(F""<0J}}  ,H}}  ,H**+?@]]x(F )-#cs]`(aF%77>>,--@R2S"4!5[^pp  r %11/4HI!!4.1!!4"45!!4"23!!4"2328_ _, (DAC)+	y>Q$,EF NIyQ%'Iy>QUUC$Y0VX^`iw|}T*(T2(48C&-4TEB4u=	y>AUUCT*(T2-eT:sE$t+s2TEB4u= 9~d(AB	 ,VX_ajk	 ,RTZ\ef ,GW]I]_jk r8   __main__)
__future__r   r   r   r
   __metaclass__DOCUMENTATIONEXAMPLESrw   ansible.module_utils.basicr   r   r}   __name__r`   r8   r6   <module>r      sH    A @DL#J 
 CalH zF r8   