3 ܓio@s&ddlZddlmZGdddZdS)N)sos_get_command_outputc@sdeZdZdZdddZddZddZd d Zdd d ZddZ ddZ ddZ ddZ ddZ dS) InitSystemaEncapsulates 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`` NcCsBi|_||_|jd|pd|_|jd|p4d|_||_dS)zInitialize a new InitSystem() N)servicesinit_cmdlist_cmd query_cmdchroot)selfrrrr r /usr/lib/python3.6/__init__.py__init__&s zInitSystem.__init__cCs&|jr"||jkr"|j|ddkSdS)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`` configZenabledF)r)r namer r r is_enabled1s zInitSystem.is_enabledcCs&|jr"||jkr"|j|ddkSdS)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`` rZdisabledF)r)r rr r r is_disabled>szInitSystem.is_disabledcCs ||jkS)aChecks 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 rr r r is_serviceJs zInitSystem.is_serviceTcCs|S)aChecks 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 rdefaultr r r is_runningWszInitSystem.is_runningcCstdS)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 N)NotImplementedError)r r r r load_all_serviceskszInitSystem.load_all_servicesc Cs:|jr6yt|jd||jdStk r4dSXdS)zQuery an individual servicer)r N)rrr Exception)r rr r r _query_serviceus zInitSystem._query_servicecCs|S)aParses 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 outputr r r parse_querys zInitSystem.parse_querycs"tj|tjfdd|jDS)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`` csg|]}j|r|qSr )match).0s)regr r sz0InitSystem.get_service_names..)recompileIr)r Zregexr )rr get_service_namesszInitSystem.get_service_namescCs||ddd}||jkr|Sd|j|kr2|j|S|j|}|dk rx|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` Zmissing)rstatusrr%Nr)rrr)r rZ_defaultZsvcr r r get_service_statuss     zInitSystem.get_service_status)NNNN)T)__name__ __module__ __qualname____doc__r rrrrrrrr#r&r r r r rs       r)r Z sos.utilitiesrrr r r r  s