
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
À<×eá)  ã               @   sÜ   d dl mZ ddlZddljZd dlT ddlZddlZddlZ	ddl
mZ e	jjƒ Zeƒ ZG dd„ dƒZG dd	„ d	ƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZdS )é   )Úbaseé    N)Ú*)Úcommandsc               @   s   e Zd Zddd„ZdS )ÚServiceNc             C   s   || _ || _|| _|| _d S )N)ÚenableÚstartÚcfg_fileÚrunlevel)Úselfr   r   r	   r
   © r   ú$/usr/lib/python3.6/plugin_service.pyÚ__init__   s    zService.__init__)NNNN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚInitHandlerc             C   s(   t jdgƒ\}}|dkr$|jƒ d S d S )Nr
   r   r   éÿÿÿÿ)ÚcmdÚexecuteÚsplit)r   ÚretcodeÚoutr   r   r   Úrunlevel_get   s    zInitHandler.runlevel_getc             C   s   t jddgƒ d S )NZtelinitÚq)r   r   )r   r   r   r   Údaemon_reload   s    zInitHandler.daemon_reloadc             C   s   d S )Nr   )r   Únamer	   r   r   r   Úcfg_install   s    zInitHandler.cfg_installc             C   s   d S )Nr   )r   r   r	   r   r   r   Úcfg_uninstall   s    zInitHandler.cfg_uninstallc             C   s   d S )Nr   )r   r   r	   r   r   r   Ú
cfg_verify"   s    zInitHandler.cfg_verifyN)r   r   r   r   r   r   r   r   r   r   r   r   r      s
   r   c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚSysVBasicHandlerc             C   s   t jd|dgƒ d S )NÚservicer   )r   r   )r   r   r   r   r   r   '   s    zSysVBasicHandler.startc             C   s   t jd|dgƒ d S )Nr!   Ústop)r   r   )r   r   r   r   r   r"   *   s    zSysVBasicHandler.stopc             C   s
   t ƒ ‚d S )N)ÚNotImplementedError)r   r   r
   r   r   r   r   -   s    zSysVBasicHandler.enablec             C   s
   t ƒ ‚d S )N)r#   )r   r   r
   r   r   r   Údisable0   s    zSysVBasicHandler.disablec             C   s"   t jd|dgdgd�\}}|dkS )Nr!   Ústatusr   )Ú	no_errors)r   r   )r   r   r   r   r   r   r   Ú
is_running3   s    zSysVBasicHandler.is_runningc             C   s
   t ƒ ‚d S )N)r#   )r   r   r
   r   r   r   Ú
is_enabled7   s    zSysVBasicHandler.is_enabledN)	r   r   r   r   r"   r   r$   r'   r(   r   r   r   r   r    &   s   r    c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSysVHandlerc             C   s   t jdd||dgƒ d S )NÚ	chkconfigz--levelÚon)r   r   )r   r   r
   r   r   r   r   ;   s    zSysVHandler.enablec             C   s   t jdd||dgƒ d S )Nr*   z--levelÚoff)r   r   )r   r   r
   r   r   r   r$   >   s    zSysVHandler.disablec             C   sB   t jdd|gƒ\}}|dkr>|jdt|ƒ ƒd d d… dkS d S )Nr*   z--listr   z%s:r   é   r+   )r   r   r   Ústr)r   r   r
   r   r   r   r   r   r(   A   s    zSysVHandler.is_enabledN)r   r   r   r   r$   r(   r   r   r   r   r)   :   s   r)   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSysVRCHandlerc             C   s   t jdd||dgƒ d S )Nzsysv-rc-confz--levelr+   )r   r   )r   r   r
   r   r   r   r   F   s    zSysVRCHandler.enablec             C   s   t jdd||dgƒ d S )Nzsysv-rc-confz--levelr,   )r   r   )r   r   r
   r   r   r   r$   I   s    zSysVRCHandler.disablec             C   sB   t jdd|gƒ\}}|dkr>|jdt|ƒ ƒd d d… dkS d S )Nzsysv-rc-confz--listr   z%s:r   r-   r+   )r   r   r   r.   )r   r   r
   r   r   r   r   r   r(   L   s    zSysVRCHandler.is_enabledN)r   r   r   r   r$   r(   r   r   r   r   r/   E   s   r/   c               @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚOpenRCHandlerc             C   s&   t jddgƒ\}}|dkr"|jƒ S d S )Nz	rc-statusz-rr   )r   r   Ústrip)r   r   r   r   r   r   r   Q   s    zOpenRCHandler.runlevel_getc             C   s   t jd|dgƒ d S )Nz
rc-servicer   )r   r   )r   r   r   r   r   r   U   s    zOpenRCHandler.startc             C   s   t jd|dgƒ d S )Nz
rc-servicer"   )r   r   )r   r   r   r   r   r"   X   s    zOpenRCHandler.stopc             C   s   t jdd||gƒ d S )Nz	rc-updateÚadd)r   r   )r   r   r
   r   r   r   r   [   s    zOpenRCHandler.enablec             C   s   t jdd||gƒ d S )Nz	rc-updateÚdel)r   r   )r   r   r
   r   r   r   r$   ^   s    zOpenRCHandler.disablec             C   s"   t jd|dgdgd�\}}|dkS )Nz
rc-servicer%   r   )r&   )r   r   )r   r   r   r   r   r   r   r'   a   s    zOpenRCHandler.is_runningc             C   s2   t jdd|gƒ\}}ttjdtj|ƒ d |ƒƒS )Nz	rc-updateZshowz\b)r   r   ÚboolÚreÚsearchÚescape)r   r   r
   r   r   r   r   r   r(   e   s    zOpenRCHandler.is_enabledN)
r   r   r   r   r   r"   r   r$   r'   r(   r   r   r   r   r0   P   s   r0   c               @   s\   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚSystemdHandlerc             C   s   dS )NÚ r   )r   r   r   r   r   k   s    zSystemdHandler.runlevel_getc             C   s   t jdd|gƒ d S )NÚ	systemctlZrestart)r   r   )r   r   r   r   r   r   n   s    zSystemdHandler.startc             C   s   t jdd|gƒ d S )Nr:   r"   )r   r   )r   r   r   r   r   r"   q   s    zSystemdHandler.stopc             C   s   t jdd|gƒ d S )Nr:   r   )r   r   )r   r   r
   r   r   r   r   t   s    zSystemdHandler.enablec             C   s   t jdd|gƒ d S )Nr:   r$   )r   r   )r   r   r
   r   r   r   r$   w   s    zSystemdHandler.disablec             C   s"   t jdd|gdgd�\}}|dkS )Nr:   z	is-activer   )r&   )r   r   )r   r   r   r   r   r   r   r'   z   s    zSystemdHandler.is_runningc             C   s>   t jdd|gdgd�\}}|jƒ }|dkr.dS |dkr:dS d S )	Nr:   z
is-enabledr   )r&   ZenabledTZdisabledF)r   r   r1   )r   r   r
   r   r   r%   r   r   r   r(   ~   s    zSystemdHandler.is_enabledc             C   s˜   t jd||f ƒ tjj|ƒs0t jd| ƒ d S tj| }ytj|tj	ƒ W n2 t
k
r~ } zt jd||f ƒ d S d }~X nX tj||ƒ | jƒ  d S )NzCinstalling service configuration overlay file '%s' for service '%s'z)Unable to find service configuration '%s'z#Unable to create directory '%s': %s)ÚlogÚinfoÚosÚpathÚexistsÚerrorÚconstsÚSERVICE_SYSTEMD_CFG_PATHÚmakedirsZDEF_SERVICE_CFG_DIR_MODEÚOSErrorr   Úcopyr   )r   r   r	   ÚdirpathÚer   r   r   r   ƒ   s    
zSystemdHandler.cfg_installc             C   sn   t jd||f ƒ tj| }d|tjj|ƒf }tj|ƒ | j	ƒ  ytj
|ƒ W n ttfk
rh   Y nX d S )NzEuninstalling service configuration overlay file '%s' for service '%s'z%s/%s)r;   r<   rA   rB   r=   r>   Úbasenamer   Úunlinkr   ÚrmdirÚFileNotFoundErrorrD   )r   r   r	   rF   r>   r   r   r   r   ‘   s    

zSystemdHandler.cfg_uninstallc             C   s†   |d krd S dt j| tjj|ƒf }tjj|ƒsHtjd||f ƒ dS tjj|ƒsjtjd||f ƒ dS tj	|ƒ}tj	|ƒ}||kS )Nz%s/%sz.Unable to find service '%s' configuration '%s'Fz0Service '%s' configuration not installed in '%s')
rA   rB   r=   r>   rH   r?   r;   r@   r   Z	sha256sum)r   r   r	   r>   Z
sha256sum1Z
sha256sum2r   r   r   r   �   s    

zSystemdHandler.cfg_verifyN)r   r   r   r   r   r"   r   r$   r'   r(   r   r   r   r   r   r   r   r8   i   s   r8   c                   sn   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zejfdd„Z‡  ZS )ÚServicePlugina1  
	`service`::
	
	Plug-in for handling sysvinit, sysv-rc, openrc and systemd services.
	+
	The syntax is as follows:
	+
	[subs="+quotes,+macros"]
	----
	[service]
	service.__service_name__=__commands__[,file:__file__]
	----
	+
	Supported service-handling `_commands_` are `start`, `stop`, `enable`
	and `disable`. The optional `file:__file__` directive installs an overlay
	configuration file `__file__`. Multiple commands must be comma (`,`)
	or semicolon (`;`) separated. If the directives conflict, the last
	one is used.
	+
	The service plugin supports configuration overlays only for systemd.
	In other init systems, this directive is ignored. The configuration
	overlay files are copied to `/etc/systemd/system/__service_name__.service.d/`
	directories. Upon profile unloading, the directory is removed if it is empty.
	+
	With systemd, the `start` command is implemented by `restart` in order
	to allow loading of the service configuration file overlay.
	+
	NOTE: With non-systemd init systems, the plug-in operates on the
	current runlevel only.
	+
	.Start and enable the `sendmail` service with an overlay file
	====
	----
	[service]
	service.sendmail=start,enable,file:${i:PROFILE_DIR}/tuned-sendmail.conf
	----
	The internal variable `${i:PROFILE_DIR}` points to the directory
	from which the profile is loaded.
	====
	c                s&   t t| ƒj||Ž d| _| jƒ | _d S )NT)ÚsuperrL   r   Z_has_dynamic_optionsÚ_detect_init_systemÚ_init_handler)r   ÚargsÚkwargs)Ú	__class__r   r   r   Õ   s    zServicePlugin.__init__c             C   s   t j|dgd�\}}|dkS )Nr   )r&   )r   r   )r   Zcommandr   r   r   r   r   Ú
_check_cmdÚ   s    zServicePlugin._check_cmdc             C   s„   | j ddgƒrtjdƒ tƒ S | j dgƒr:tjdƒ tƒ S | j ddgƒrXtjdƒ tƒ S | j d	dgƒrvtjd
ƒ tƒ S tjdƒ‚d S )Nr:   r%   zdetected systemdr*   zdetected generic sysvinitzupdate-rc.dz-hzdetected sysv-rcz	rc-updatezdetected openrcz8Unable to detect your init system, disabling the plugin.)	rS   r;   Údebugr8   r)   r/   r0   Ú
exceptionsZNotSupportedPluginException)r   r   r   r   rN   Þ   s    



z!ServicePlugin._detect_init_systemc             C   s–   t jd|ƒ}tƒ }x~|D ]v}|dkr,d|_q|dkr<d|_q|dkrLd|_q|dkr\d|_q|d d… d	kr||dd … |_qtjd
||f ƒ qW |S )Nz
\s*[,;]\s*r   Tr$   Fr   r"   é   zfile:z*service '%s': invalid service option: '%s')r5   r   r   r   r   r	   r;   r@   )r   r   ÚvalÚlr!   Úir   r   r   Ú_parse_service_optionsî   s    
z$ServicePlugin._parse_service_optionsc                s6   d|_ d|_tj‡ fdd„|jjƒ D ƒƒˆ _i |_d S )NFTc                sT   g | ]L\}}|d d… dkrt |ƒdkr|dd … ˆ j|dd … ˆ jj|ƒƒf‘qS )Né   zservice.)ÚlenrZ   Z
_variablesÚexpand)Ú.0ZoptionÚvalue)r   r   r   ú
<listcomp>  s   z0ServicePlugin._instance_init.<locals>.<listcomp>)Z_has_dynamic_tuningZ_has_static_tuningÚcollectionsÚOrderedDictZoptionsÚitemsÚ	_servicesÚ_services_original)r   Úinstancer   )r   r   Ú_instance_init   s
    zServicePlugin._instance_initc             C   s   d S )Nr   )r   rf   r   r   r   Ú_instance_cleanup	  s    zServicePlugin._instance_cleanupc             C   sT   |r| j j|ƒ n|d k	r&| j j|ƒ |r:| j j||ƒ n|d k	rP| j j||ƒ d S )N)rO   r   r"   r   r$   )r   r   r   r   r
   r   r   r   Ú_process_service  s    zServicePlugin._process_servicec             C   sº   | j jƒ }|d kr tjdƒ d S x”| jjƒ D ]†}| j j|d |ƒ}| j j|d ƒ}t|||d j	|ƒ|j
|d < |d j	r’| j j|d |d j	ƒ | j|d |d j|d j|ƒ q,W d S )NzCannot detect runlevelr   r   )rO   r   r;   r@   rd   rc   r(   r'   r   r	   re   r   ri   r   r   )r   rf   r
   r!   r(   r'   r   r   r   Ú_instance_apply_static  s    


z$ServicePlugin._instance_apply_staticc       
      C   sH  | j jƒ }|d kr&tjtjd ƒ dS d}�x| jjƒ D �]}| j j|d |d j	ƒ}|r~tj
tjd |d |d j	f ƒ nR|d k	r®tjtjd |d |d j	f ƒ d}n"tj
tjd |d |d j	f ƒ | j j|d |ƒ}| j j|d ƒ}	| jd	|d  |d j|	|ƒdk�rd}| jd
|d  |d j||ƒdkr8d}q8W |S )Nzcannot detect runlevelFTr   r   z'service '%s' configuration '%s' matchesz'service '%s' configuration '%s' differszservice '%s' configuration '%s'z
%s runningz
%s enabled)rO   r   r;   r@   rA   ZSTR_VERIFY_PROFILE_FAILrd   rc   r   r	   r<   ZSTR_VERIFY_PROFILE_OKZ STR_VERIFY_PROFILE_VALUE_MISSINGr(   r'   Z_verify_valuer   r   )
r   rf   Zignore_missingZdevicesr
   Úretr!   Zret_cfg_verifyr(   r'   r   r   r   Ú_instance_verify_static$  s(    
$""$"z%ServicePlugin._instance_verify_staticc             C   sL   xFt |jjƒ ƒD ]4\}}|jr.| jj||jƒ | j||j|j|j	ƒ qW d S )N)
Úlistre   rc   r	   rO   r   ri   r   r   r
   )r   rf   Zrollbackr   r_   r   r   r   Ú_instance_unapply_static<  s    z&ServicePlugin._instance_unapply_static)r   r   r   Ú__doc__r   rS   rN   rZ   rg   rh   ri   rj   rl   rA   ZROLLBACK_SOFTrn   Ú__classcell__r   r   )rR   r   rL   «   s   (	
rL   )r9   r   ra   Ztuned.constsrA   Z
decoratorsr=   r5   Z
tuned.logsZtunedZtuned.utils.commandsr   ZlogsÚgetr;   r   r   r   r    r)   r/   r0   r8   ZPluginrL   r   r   r   r   Ú<module>   s"   

B