
    Vh                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ d dlmZ  G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona
  
module: runit
author:
  - James Sumners (@jsumners)
short_description: Manage runit services
description:
  - Controls runit services on remote hosts using the sv utility.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of the service to manage.
    type: str
    required: true
  state:
    description:
      - V(started)/V(stopped) are idempotent actions that will not run commands unless necessary. V(restarted) will always
        bounce the service (sv restart) and V(killed) will always bounce the service (sv force-stop). V(reloaded) will send
        a HUP (sv reload). V(once) will run a normally downed sv once (sv once), not really an idempotent operation.
    type: str
    choices: [killed, once, reloaded, restarted, started, stopped]
  enabled:
    description:
      - Whether the service is enabled or not, if disabled it also implies stopped.
    type: bool
  service_dir:
    description:
      - Directory runsv watches for services.
    type: str
    default: /var/service
  service_src:
    description:
      - Directory where services are defined, the source of symlinks to O(service_dir).
    type: str
    default: /etc/sv
a  
- name: Start sv dnscache, if not running
  community.general.runit:
    name: dnscache
    state: started

- name: Stop sv dnscache, if running
  community.general.runit:
    name: dnscache
    state: stopped

- name: Kill sv dnscache, in all cases
  community.general.runit:
    name: dnscache
    state: killed

- name: Restart sv dnscache, in all cases
  community.general.runit:
    name: dnscache
    state: restarted

- name: Reload sv dnscache, in all cases
  community.general.runit:
    name: dnscache
    state: reloaded

- name: Use alternative sv directory location
  community.general.runit:
    name: dnscache
    state: reloaded
    service_dir: /run/service
N)AnsibleModule)	to_nativec                   v    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zy)Svz
    Main class that handles daemontools, can be subclassed and overridden in case
    we want to use a 'derivative' like encore, s6, etc
    c                    g | _         g d| _        || _        |j                  d   | _        |j                  d   | _        |j                  d   | _        d | _        d | _        d | _	        d | _
        d | _        |j                  d| j                   d      | _        |j                  d| j                         | _        d	j                  | j
                  | j                  g      | _        d	j                  | j                  | j                  g      | _        t$        j&                  j)                  | j                         | _        | j                  r| j+                          y d
| _	        y )N)stateenabledsvc_fullsrc_fullpidduration
full_statenameservice_dirservice_srcsvT)opt_dirsrequired)r   /stopped)extra_pathsreport_varsmoduleparamsr   r   r   r   r   r   r   r   get_bin_pathsvc_cmd
svstat_cmdjoinr   r   ospathlexists
get_status)selfr   s     k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/runit.py__init__zSv.__init__d   s   hMM&)	!==7!==7
**4$:J:JUY*Z --dT=M=M-N$"2"2DII!>?$"2"2DII!>?wwt}}5<<OO"DJ    c                    t         j                  j                  | j                        r,	 t        j                  | j                  | j
                         y | j                  j                  d| j                  z         y # t        $ r=}| j                  j                  | j                  dt        |      z         Y d }~y d }~ww xY w)NzError while linking: %sr#   msgz1Could not find source for service to enable (%s).r,   )
r"   r#   existsr   symlinkr   OSErrorr   	fail_jsonr   r&   es     r'   enablez	Sv.enable~   s    77>>$--(h

4==$--8 KK!!&Y\`\i\i&i!j  h%%4==>WZcdeZf>f%gghs   *B   	C	3CCc                 &   | j                  | j                  d| j                  g       	 t        j                  | j
                         y # t        $ r=}| j                  j                  | j
                  dt        |      z         Y d }~y d }~ww xY w)N
force-stopzError while unlinking: %sr+   )
execute_commandr   r   r"   unlinkr   r0   r   r1   r   r2   s     r'   disablez
Sv.disable   sr    dllL$--HI	fIIdmm$ 	fKK!!t}}:UXabcXd:d!ee	fs   A
 
	B3BBc                 
   | j                  | j                  d| j                  g      \  }}}||r|x| _        | _        y || _        | j                  j                  d      d   }t        j                  d|      }|r|j                  d      | _	        t        j                  d|      }|r|j                  d      | _
        t        j                  d|      rd| _        y t        j                  d	|      rd
| _        y d| _        y )Nstatusz; r   z\(pid (\d+)\)   z (\d+)sz^run:startedz^down:r   unknown)r7   r    r   r   r   splitresearchgroupr   r   )r&   rcouterrfull_state_no_loggerms         r'   r%   zSv.get_status   s    --t$--.XYS#?s+..DOdj!DO $(??#8#8#>q#A 		*,@AA771:		*&:;A !
yy#78&
9&:;&
&
r)   c                 "    | j                         S N)startr&   s    r'   r=   z
Sv.started   s    zz|r)   c                 R    | j                  | j                  d| j                  g      S )NrJ   r7   r   r   rK   s    r'   rJ   zSv.start   s"    ##T\\7DMM$JKKr)   c                 "    | j                         S rI   )stoprK   s    r'   r   z
Sv.stopped       yy{r)   c                 R    | j                  | j                  d| j                  g      S )NrO   rM   rK   s    r'   rO   zSv.stop   "    ##T\\64==$IJJr)   c                 R    | j                  | j                  d| j                  g      S )NoncerM   rK   s    r'   rT   zSv.once   rR   r)   c                 "    | j                         S rI   )reloadrK   s    r'   reloadedzSv.reloaded   s    {{}r)   c                 R    | j                  | j                  d| j                  g      S )NrV   rM   rK   s    r'   rV   z	Sv.reload   s"    ##T\\8T]]$KLLr)   c                 "    | j                         S rI   )restartrK   s    r'   	restartedzSv.restarted   s    ||~r)   c                 R    | j                  | j                  d| j                  g      S )NrZ   rM   rK   s    r'   rZ   z
Sv.restart   s"    ##T\\9dmm$LMMr)   c                 "    | j                         S rI   )killrK   s    r'   killedz	Sv.killed   rP   r)   c                 R    | j                  | j                  d| j                  g      S )Nr6   rM   rK   s    r'   r^   zSv.kill   s"    ##T\\<$OPPr)   c                     	 | j                   j                  |      \  }}}fS # t        $ r2}| j                   j                  dt	        |      z         Y d }~;d }~ww xY w)Nzfailed to execute: %sr-   )r   run_command	Exceptionr1   r   )r&   cmdrC   rD   rE   r3   s         r'   r7   zSv.execute_command   sf    	N![[44S9NRc 3|  	NKK!!&=	!&L!MM	Ns   & 	A!(AA!c                 p    | j                          i }| j                  D ]  }| j                  |   ||<    |S rI   )r%   r   __dict__)r&   statesks      r'   reportz	Sv.report   s=    !! 	)Aa(F1I	)r)   N)__name__
__module____qualname____doc__r(   r4   r9   r%   r=   rJ   r   rO   rT   rW   rV   r[   rZ   r_   r^   r7   ri    r)   r'   r	   r	   ^   sd    
#4kf8LKKMNQr)   r	   c                     t        t        t        dd      t        dg d      t        d      t        dd	      t        dd
	            d      } t        dddd      | _        | j                  d   }| j                  d   }t	        |       }d}|A||j
                  k7  r2d}| j                  s$	 |r|j                          n|j                          |.||j                  k7  rd}| j                  s t        ||              | j                  ||j!                                y # t        t        f$ r(}| j                  dt        |      z         Y d }~d }~ww xY w)NstrT)typer   )r_   rT   rW   r[   r=   r   )rq   choicesbool)rq   z/var/service)rq   defaultz/etc/sv)r   r   r   r   r   )argument_specsupports_check_modeC)LANGLC_ALLLC_MESSAGESLC_CTYPEr   r   Fz!Could not change service link: %sr-   )changedr   )r   dictrun_command_environ_updater   r	   r   
check_moder4   r9   r0   IOErrorr1   r   r   getattr	exit_jsonri   )r   r   r   r   r|   r3   s         r'   mainr      sE   540E+lmf%%@%;
 !	F )-#cs]`(aF%MM'"EmmI&G	FBGw"**4  YIIKJJL Ubhh.  GB 
W5 W% Y  %H9UV<%W XXYs   3#D( (E7EE__main__)
__future__r   r   r   rq   __metaclass__DOCUMENTATIONEXAMPLESr"   r@   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   objectr	   r   rj   rn   r)   r'   <module>r      sZ    A @)VB 
 	 4 Ay yx$6N zF r)   