
    -_go                     ,    d dl Z d dlmZ  G d d      Zy)    N)sos_get_command_outputc                   T    e Zd ZdZ	 	 ddZd Zd Zd ZddZd Z	d	 Z
d
 Zd Zd Zy)
InitSystema  Encapsulates an init system to provide service-oriented functions to
    sos.

    This should be used to query the status of services, such as if they are
    enabled or disabled on boot, or if the service is currently running.

    :param init_cmd: The binary used to interact with the init system
    :type init_cmd: ``str``

    :param list_cmd: The list subcmd given to `init_cmd` to list services
    :type list_cmd: ``str``

    :param query_cmd: The query subcmd given to `query_cmd` to query the
                      status of services
    :type query_cmd: ``str``

    :param chroot:  Location to chroot to for any command execution, i.e. the
                    sysroot if we're running in a container
    :type chroot:   ``str`` or ``None``

    Nc                     i | _         || _        | j                   d| xs d| _        | j                   d| xs d| _        || _        y)zInitialize a new InitSystem() N)servicesinit_cmdlist_cmd	query_cmdchroot)selfr	   r
   r   r   s        D/usr/lib/python3/dist-packages/sos/policies/init_systems/__init__.py__init__zInitSystem.__init__&   sO      ==/8*5= MM?!I;7?4    c                 b    | j                   r#|| j                   v r| j                   |   d   dk(  S y)zCheck if given service name is enabled

        :param name: The name of the service
        :type name: ``str``

        :returns: ``True`` if the service is enabled, else ``False``
        :rtype: ``bool``
        configenabledFr   r   names     r   
is_enabledzInitSystem.is_enabled1   s2     ==TT]]2==&x0I==r   c                 b    | j                   r#|| j                   v r| j                   |   d   dk(  S y)zCheck if a given service name is disabled
        :param name: The name of the service
        :type name: ``str``

        :returns: ``True`` if the service is disabled, else ``False``
        :rtype: ``bool``
        r   disabledFr   r   s     r   is_disabledzInitSystem.is_disabled>   s2     ==TT]]2==&x0J>>r   c                     || j                   v S )a  Checks if the given service name exists on the system at all, this
        does not check for the service status

        :param name: The name of the service
        :type name: ``str``

        :returns: ``True`` if the service exists, else ``False``
        :rtype: ``bool``
        r   r   s     r   
is_servicezInitSystem.is_serviceJ   s     t}}$$r   c                     |S )a  Checks if the given service name is in a running state.

        This should be overridden by initsystems that subclass InitSystem

        :param name: The name of the service
        :type name: ``str``

        :param default: The default response in case the check fails
        :type default:  ``bool`

        :returns: ``True`` if the service is running, else ``default``
        :rtype: ``bool``
         )r   r   defaults      r   
is_runningzInitSystem.is_runningW   s	    $ r   c                     t         )a:  This loads all services known to the init system into a dict.
        The dict should be keyed by the service name, and contain a dict of the
        name and service status

        This must be overridden by anything that subclasses `InitSystem` in
        order for service methods to function properly
        )NotImplementedError)r   s    r   load_all_serviceszInitSystem.load_all_servicesk   s
     "!r   c                     | j                   r'	 t        | j                    d| | j                        S y# t        $ r Y yw xY w)zQuery an individual servicer   )r   N)r   r   r   	Exceptionr   s     r   _query_servicezInitSystem._query_serviceu   sN    >>-~~&av.;;    s   %5 	A Ac                     |S )a  Parses the output returned by the query command to make a
        determination of what the state of the service is

        This should be overriden by anything that subclasses InitSystem

        :param output: The raw output from querying the service with the
                       configured `query_cmd`
        :type output: ``str``

        :returns: A state for the service, e.g. 'active', 'disabled', etc...
        :rtype: ``str``
        r   )r   outputs     r   parse_queryzInitSystem.parse_query   s	     r   c                     t        j                  |t         j                        }| j                  D cg c]  }|j	                  |      s| c}S c c}w )zGet a list of all services discovered on the system that match the
        given regex.

        :param regex: The service name regex to match against
        :type regex: ``str``
        )recompileIr   match)r   regexregss       r   get_service_nameszInitSystem.get_service_names   s:     jj%==9aCIIaL999s   A
Ac                 (   |ddd}|| j                   vr|S d| j                   |   v r| j                   |   S | j                  |      }|H| j                  |d         | j                   |   d<   |d   | j                   |   d<   | j                   |   S |S )a=  Get the status for the given service name along with the output
        of the query command

        :param name: The name of the service
        :type name: ``str``

        :returns: Service status and query_cmd output from the init system
        :rtype: ``dict`` with keys `name`, `status`, and `output`
        missing )r   statusr(   r6   r(   )r   r&   r)   )r   r   _defaultsvcs       r   get_service_statuszInitSystem.get_service_status   s     

 t}}$Ot}}T**==&&!!$'?,0,<,<S],KDMM$),/MDMM$)==&&r   )NNNN)T)__name__
__module____qualname____doc__r   r   r   r   r    r#   r&   r)   r2   r9   r   r   r   r   r      sC    , @D	

%("
:r   r   )r+   sos.utilitiesr   r   r   r   r   <module>r?      s    
 0d dr   