
    Vh6                    |    d dl m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ed
k(  r e        yy)    )annotationsa
  
module: sysvinit
author:
    - "Ansible Core Team"
version_added: "2.6"
short_description:  Manage SysV services.
description:
    - Controls services on target hosts that use the SysV init system.
options:
    name:
        required: true
        description:
            - Name of the service.
        type: str
        aliases: ['service']
    state:
        choices: [ 'started', 'stopped', 'restarted', 'reloaded' ]
        description:
            - V(started)/V(stopped) are idempotent actions that will not run commands unless necessary.
              Not all init scripts support V(restarted) nor V(reloaded) natively, so these will both trigger a stop and start as needed.
        type: str
    enabled:
        type: bool
        description:
            - Whether the service should start on boot. At least one of O(state) and O(enabled) are required.
    sleep:
        default: 1
        description:
            - If the service is being V(restarted) or V(reloaded) then sleep this many seconds between the stop and start command.
              This helps to workaround badly behaving services.
        type: int
    pattern:
        description:
            - A substring to look for as would be found in the output of the I(ps) command as a stand-in for a status result.
            - If the string is found, the service will be assumed to be running.
            - "This option is mainly for use with init scripts that don't support the C(status) option."
        type: str
    runlevels:
        description:
            - The runlevels this script should be enabled/disabled from.
            - Use this to override the defaults set by the package or init script itself.
        type: list
        elements: str
    arguments:
        description:
            - Additional arguments provided on the command line that some init scripts accept.
        type: str
        aliases: [ 'args' ]
    daemonize:
        type: bool
        description:
            - Have the module daemonize as the service itself might not do so properly.
            - This is useful with badly written init scripts or daemons, which
              commonly manifests as the task hanging as it is still holding the
              tty or the service dying when the task is over as the connection
              closes the session.
        default: no
extends_documentation_fragment: action_common_attributes
attributes:
    check_mode:
        support: full
    diff_mode:
        support: none
    platform:
        platforms: posix
notes:
    - One option other than name is required.
    - The service names might vary by specific OS/distribution.
requirements:
    - That the service managed has a corresponding init script.
a  
- name: Make sure apache2 is started
  ansible.builtin.sysvinit:
      name: apache2
      state: started
      enabled: yes

- name: Sleep for 5 seconds between stop and start command of badly behaving service
  ansible.builtin.sysvinit:
    name: apache2
    state: restarted
    sleep: 5

- name: Make sure apache2 is started on runlevels 3 and 5
  ansible.builtin.sysvinit:
      name: apache2
      state: started
      enabled: yes
      runlevels:
        - 3
        - 5
a  
results:
    description: results from actions taken
    returned: always
    type: complex
    contains:
      name:
        description: Name of the service
        type: str
        returned: always
        sample: "apache2"
      status:
        description: Status of the service
        type: dict
        returned: changed
        sample: {
          "enabled": {
             "changed": true,
             "rc": 0,
             "stderr": "",
             "stdout": ""
          },
          "stopped": {
             "changed": true,
             "rc": 0,
             "stderr": "",
             "stdout": "Stopping web server: apache2.\n"
          }
        }
N)sleep)AnsibleModule)sysv_is_enabledget_sysv_scriptsysv_existsfail_if_missingget_ps	daemonizec                    t        t        t        dddg      t        g dd      t        d      t        d	d
      t        d      t        ddg      t        dd      t        dd            dddgg      j                  d   j                  d   j                  d   } j                  d   }j                  d   }j                  d   }d}dx}}di d}t        t	                     t              g d}g d}	i }
i }|	D ]  }j                  ||      ||<    |r-|D ]'  }|
j                  |i        t        |      |
|   d<   ) nt              |
d<   d}d}|rt        |      x}}n|j                  d      r|d   d d!}nrd"z  }nj                  d#$       j                        \  }}}|d%k(  sd&k(  rd'|v rd}d}|si|j                  d(      d
k  rU|j                         j                  j                         d      }d)D ]
  }||v sd} n |sd*D ]  }||v sd+|vsd}d} n |s|d,v rd}|s+|dk(  rd}d}n!t              rd}d}j!                  d-       |sj!                  d.       |d/   j                  di        d|d/   d   d0<   d |d/   d   d1<   d |d/   d   d2<   d |d/   d   d3<   |rN||d/   d   d<   |D ]  }| |
|   d   k7  sd|d0<   d|d/   d   d0<     j"                  s|d0   r| r|j                  d4      r1j                  |d4   d d5d j%                  |            \  }}}n|j                  d6      rj                  |d6   d7dj%                  |      d d8      \  }}}nq|j                  d4      r1j                  |d4   d d9d j%                  |            \  }}}n/|j                  d6      rj                  |d6   d7dj%                  |      d d:      \  }}}n| | |
d   k7  rd|d0<   d|d/   d   d0<   j"                  s|d0   r| r`|j                  d4      rj                  |d4   d d;      \  }}}n|j                  d6      r~j                  |d6   d d8      \  }}}n_|j                  d4      rj                  |d4   d d<      \  }}}n/|j                  d6      rj                  |d6   d d:      \  }}}j"                  sV|d/   d   d0   rK||d/   d   d1<   ||d/   d   d2<   ||d/   d   d3<   d=\  }}}d>|d/   d   d3   v r j                  dGd?d@z  i| |d/   j                  j                  d   i        d|d/   j                  d      d0<   d |d/   j                  d      d1<   d |d/   j                  d      d2<   d |d/   j                  d      d3<   rjt'        j(                  dAdj                               fdB}dCk(  rKd|d0<   d|d/   j                  d      d0<   j"                  sdDD ]  } ||      \  }}}|st+        |        n{|dEk(  k7  r6d|d0<   d|d/   j                  d      d0<   j"                  sJ |      \  }}}n=|dFk(  k(  r5d|d0<   d|d/   j                  d      d0<   j"                  s |      \  }}}j"                  sf|d/   j                  d      d0   rN||d/   j                  d      d1<   ||d/   j                  d      d2<   ||d/   j                  d      d3<   d=\  }}} j,                  dGi | y )HNTstrservice)requiredtypealiases)startedstopped	restartedreloaded)choicesr   bool)r   int   )r   defaultargs)r   r   list)r   elementsF)namestateenabledr   pattern	arguments	runlevelsr   r   r    )argument_specsupports_check_moderequired_one_ofr   r#   r!   r   r    )r   changedstatus)z/sbinz	/usr/sbinz/binz/usr/bin)	chkconfigupdate-rc.dinsservr   )opt_dirs)runlevel z statusz	%s statusz"Unable to determine service status)msgiptablesACCEPT
)stopzis dead z	dead but zcould not access pid fileinactive)runstartactiveznot )r            E   zTUsed ps output to match service name and determine it is up, this is very unreliablez9Unable to determine if service is up, assuming it is downr)   r(   rcstdoutstderrr+   z enable r*   z	 --level z onz	 disable z offz	 defaultsz disable)NNNzillegal runlevel specifiedr0   z=Illegal runlevel specified for enable operation on service %szp?ed$c                    j                   d   }	d| d|dn|}j                   d   rt        |      \  }}}nj                  |      \  }}}|dk7  rj                  dd|||       |||fS )	Nr"   r/   r'   r   r   z
Failed to z
 service: )r0   r>   r?   r@   )paramsr   run_command	fail_json)
doitr   cmdr>   outerractionmoduler   scripts
         H/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/sysvinit.pyrunmezmain.<locals>.runmeB  s    ==-D &DLbd.JKC }}[)!*63!7S#!'!3!3C!8S# Qw  64%PUW`clo pS>!    restart)r5   r8   r8   r5    )r   dictrB   r	   r   r   get_bin_path
setdefaultr   r
   getrD   rC   countlowerreplacewarn
check_modejoinresubr   	exit_json)r    r#   r!   	sleep_forr>   rG   rH   resultpathsbinariesrunlevel_statuslocationbinaryrl
is_startedworkedrF   cleanoutr   started_statusrM   dothisrI   rJ   r   rK   s                         @@@@rL   mainrk      s   t%)ENUZ[f%E1-e$x876	
 !!9-.F == D]]7#FmmI&Gk*ImmI&Gg&I	
BNC#F FK-t4T"F 7EAH OH G!..v.FG  	PB&&r2.-<TB-OOB	*	P &5T%:	" JF $VW55<<	"$,Y$7>C&C!EF++C0S#Rxz!h#o!
 ciio299;..tzz|R@i G(*!%
 *D ")X5&:P)-J%)F!	" b$44Qw!
%!
rsOP 8	2.-2F8Y	*(,F8Y%,0F8Y),0F8Y)3<x#K0 	>B/"-i88$(y!9=x +I6	>
   VI%6<<.%+%7%7XVcMdfjloltltu~l  9A  &BNRc\\+.%+%7%7QYZeQfhjhohopyhz  }A  9B  &CNRc<<.%+%7%7hWdNegkmpmumuv  nA  9B  &CNRc\\+.%+%7%7RZ[fRgikipipqzi{  ~B  9C  &DNRc7oi.H#H $F959F8Y'	2  VI%6<<.%+%7%7HUbLcei8j%kNRc\\+.%+%7%7h{F[]a8b%cNRc<<.%+%7%78TaKbdh8i%jNRc\\+.%+%7%7xP[G\^b8c%dNRc !1)!<Y!G,.x#D)03x#H-03x#H-'C'6(+;I+Fx+PPFr!`cg!grkqr 8g 6;:?F8V]]7+,Y759F8V]]7+,T29=F8V]]7+,X69=F8V]]7+,X6"flln5	"" Y $F9BFF8V]]734Y?$$ 0 )F%*6]NRc i()
 Fg-. $F9BFF8V]]734Y?$$$V}CFf,- $F9BFF8V]]734Y?$$$V}C  VH%5fmmG6L%Mi%X=?F8V]]734T:ADF8V]]734X>ADF8V]]734X>+LBS FvrN   __main__)
__future__r   DOCUMENTATIONEXAMPLESRETURNr[   timer   ansible.module_utils.basicr   ansible.module_utils.servicer   r   r   r	   r
   r   rk   __name__rP   rN   rL   <module>ru      sM    #FP.
> 
  4 z zdN zF rN   