a ”ˆ«hÊ)ã@sÞddlmZddlZddlmZddlTddlZddlZddlZ ddl m Z e j   ¡Ze ƒZGdd„dƒZGdd „d ƒZGd d „d eƒZGd d „d eƒZGdd„deƒZGdd„deƒZGdd„deƒZGdd„dejƒZdS)é)ÚbaseéN)Ú*)Úcommandsc@seZdZddd„ZdS)ÚServiceNcCs||_||_||_||_dS©N)ÚenableÚstartÚcfg_fileÚrunlevel)Úselfr rr r ©r ú@/usr/lib/python3.9/site-packages/tuned/plugins/plugin_service.pyÚ__init__szService.__init__)NNNN)Ú__name__Ú __module__Ú __qualname__rr r r rr src@s4eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd S) Ú InitHandlercCs(t dg¡\}}|dkr$| ¡dSdS)Nr réÿÿÿÿ)ÚcmdÚexecuteÚsplit©r ÚretcodeÚoutr r rÚ runlevel_getszInitHandler.runlevel_getcCst ddg¡dS)NZtelinitÚq©rr©r r r rÚ daemon_reloadszInitHandler.daemon_reloadcCsdSrr ©r Únamer r r rÚ cfg_installszInitHandler.cfg_installcCsdSrr r r r rÚ cfg_uninstallszInitHandler.cfg_uninstallcCsdSrr r r r rÚ cfg_verify"szInitHandler.cfg_verifyN)rrrrrr"r#r$r r r rrs rc@s<eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd S)ÚSysVBasicHandlercCst d|dg¡dS)NÚservicer r©r r!r r rr 'szSysVBasicHandler.startcCst d|dg¡dS)Nr&Ústoprr'r r rr(*szSysVBasicHandler.stopcCs tƒ‚dSr©ÚNotImplementedError©r r!r r r rr-szSysVBasicHandler.enablecCs tƒ‚dSrr)r+r r rÚdisable0szSysVBasicHandler.disablecCs"tjd|dgdgd\}}|dkS)Nr&Ústatusr©Z no_errorsr©r r!rrr r rÚ is_running3szSysVBasicHandler.is_runningcCs tƒ‚dSrr)r+r r rÚ is_enabled7szSysVBasicHandler.is_enabledN) rrrr r(rr,r0r1r r r rr%&s r%c@s$eZdZdd„Zdd„Zdd„ZdS)Ú SysVHandlercCst dd||dg¡dS)NÚ chkconfigú--levelÚonrr+r r rr;szSysVHandler.enablecCst dd||dg¡dS)Nr3r4Úoffrr+r r rr,>szSysVHandler.disablecCsBt dd|g¡\}}|dkr>| dt|ƒ¡ddd…dkSdS)Nr3ú--listrú%s:rér5©rrrÚstr©r r!r rrr r rr1AszSysVHandler.is_enabledN©rrrrr,r1r r r rr2:sr2c@s$eZdZdd„Zdd„Zdd„ZdS)Ú SysVRCHandlercCst dd||dg¡dS)Nú sysv-rc-confr4r5rr+r r rrFszSysVRCHandler.enablecCst dd||dg¡dS)Nr?r4r6rr+r r rr,IszSysVRCHandler.disablecCsBt dd|g¡\}}|dkr>| dt|ƒ¡ddd…dkSdS)Nr?r7rr8rr9r5r:r<r r rr1LszSysVRCHandler.is_enabledNr=r r r rr>Esr>c@sDeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dS)Ú OpenRCHandlercCs&t ddg¡\}}|dkr"| ¡SdS)Nz rc-statusz-rr©rrÚstriprr r rrQszOpenRCHandler.runlevel_getcCst d|dg¡dS)Nú rc-servicer rr'r r rr UszOpenRCHandler.startcCst d|dg¡dS)NrCr(rr'r r rr(XszOpenRCHandler.stopcCst dd||g¡dS)Nú rc-updateÚaddrr+r r rr[szOpenRCHandler.enablecCst dd||g¡dS)NrDÚdelrr+r r rr,^szOpenRCHandler.disablecCs"tjd|dgdgd\}}|dkS)NrCr-rr.rr/r r rr0aszOpenRCHandler.is_runningcCs2t dd|g¡\}}tt dt |¡d|¡ƒS)NrDZshowz\b)rrÚboolÚreÚsearchÚescaper<r r rr1eszOpenRCHandler.is_enabledN) rrrrr r(rr,r0r1r r r rr@Psr@c@s\eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dS)ÚSystemdHandlercCsdS)NÚr rr r rrkszSystemdHandler.runlevel_getcCst dd|g¡dS)NÚ systemctlZrestartrr'r r rr nszSystemdHandler.startcCst dd|g¡dS)NrMr(rr'r r rr(qszSystemdHandler.stopcCst dd|g¡dS)NrMrrr+r r rrtszSystemdHandler.enablecCst dd|g¡dS)NrMr,rr+r r rr,wszSystemdHandler.disablecCs"tjdd|gdgd\}}|dkS)NrMz is-activerr.rr/r r rr0zszSystemdHandler.is_runningcCs>tjdd|gdgd\}}| ¡}|dkr.dS|dkr:dSdS) NrMz is-enabledrr.ZenabledTZdisabledFrA)r r!r rrr-r r rr1~szSystemdHandler.is_enabledc Cs¢t d||f¡tj |¡s0t d|¡dStj|}ztj|tj ddWn8t yˆ}z t d||f¡WYd}~dSd}~00t   ||¡|  ¡dS)NzCinstalling service configuration overlay file '%s' for service '%s'z)Unable to find service configuration '%s'T)Úexist_okz#Unable to create directory '%s': %s)ÚlogÚinfoÚosÚpathÚexistsÚerrorÚconstsÚSERVICE_SYSTEMD_CFG_PATHÚmakedirsZDEF_SERVICE_CFG_DIR_MODEÚOSErrorrÚcopyr)r r!r ÚdirpathÚer r rr"ƒs   zSystemdHandler.cfg_installc Cslt d||f¡tj|}d|tj |¡f}t |¡|  ¡zt  |¡Wnt t fyfYn0dS)NzEuninstalling service configuration overlay file '%s' for service '%s'ú%s/%s) rOrPrUrVrQrRÚbasenamerÚunlinkrÚrmdirÚFileNotFoundErrorrX)r r!r rZrRr r rr#‘s  zSystemdHandler.cfg_uninstallcCs†|dur dSdtj|tj |¡f}tj |¡sHt d||f¡dStj |¡sjt d||f¡dSt  |¡}t  |¡}||kS)Nr\z.Unable to find service '%s' configuration '%s'Fz0Service '%s' configuration not installed in '%s') rUrVrQrRr]rSrOrTrZ sha256sum)r r!r rRZ sha256sum1Z sha256sum2r r rr$s    zSystemdHandler.cfg_verifyN) rrrrr r(rr,r0r1r"r#r$r r r rrKis rKcsneZdZdZ‡fdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z e jfdd„Z‡ZS)Ú ServicePlugina  Plug-in for handling sysvinit, sysv-rc, openrc and systemd services. The syntax is as follows: [subs="quotes"] ---- [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. ==== cs*tt|ƒj|i|¤Žd|_| ¡|_dS)NT)ÚsuperrarZ_has_dynamic_optionsÚ_detect_init_systemÚ _init_handler)r ÚargsÚkwargs©Ú __class__r rrÓszServicePlugin.__init__cCstj|dgd\}}|dkS)Nrr.r)r Zcommandrrr r rÚ _check_cmdØszServicePlugin._check_cmdcCs„| ddg¡rt d¡tƒS| dg¡r:t d¡tƒS| ddg¡rXt d¡tƒS| d dg¡rvt d ¡tƒSt d ¡‚dS) NrMr-zdetected systemdr3zdetected generic sysvinitz update-rc.dz-hzdetected sysv-rcrDzdetected openrcz8Unable to detect your init system, disabling the plugin.) rirOÚdebugrKr2r>r@Ú exceptionsZNotSupportedPluginExceptionrr r rrcÜs     z!ServicePlugin._detect_init_systemcCs’t d|¡}tƒ}|D]v}|dkr*d|_q|dkr:d|_q|dkrJd|_q|dkrZd|_q|dd…d krz|dd…|_qt d ||f¡q|S) Nz \s*[,;]\s*rTr,Fr r(ézfile:z*service '%s': invalid service option: '%s')rHrrrr r rOrT)r r!ÚvalÚlr&Úir r rÚ_parse_service_optionsìs z$ServicePlugin._parse_service_optionscs6d|_d|_t ‡fdd„|j ¡Dƒ¡ˆ_i|_dS)NFTc sTg|]L\}}|dd…dkrt|ƒdkr|dd…ˆ |dd…ˆj |¡¡f‘qS)Nézservice.)ÚlenrpZ _variablesÚexpand)Ú.0ÚoptionÚvaluerr rÚ s þ ÿz0ServicePlugin._instance_init..)Z_has_dynamic_tuningZ_has_static_tuningÚ collectionsÚ OrderedDictÚoptionsÚitemsÚ _servicesÚ_services_original©r Úinstancer rrÚ_instance_initþs ÿ zServicePlugin._instance_initcCsdSrr r~r r rÚ_instance_cleanupszServicePlugin._instance_cleanupcCsT|r|j |¡n|dur&|j |¡|r:|j ||¡n|durP|j ||¡dSr)rdr r(rr,)r r!r rr r r rÚ_process_service s zServicePlugin._process_servicecCs¶|j ¡}|dur t d¡dS|j ¡D]†}|j |d|¡}|j |d¡}t|||dj |ƒ|j |d<|dj r|j  |d|dj ¡|  |d|dj |dj|¡q*dS)NzCannot detect runlevelrr)rdrrOrTr|r{r1r0rr r}r"r‚r r)r rr r&r1r0r r rÚ_instance_apply_statics   z$ServicePlugin._instance_apply_staticc CsB|j ¡}|dur&t tjd¡dSd}|j ¡D]}|j |d|dj ¡}|rzt  tj d|d|dj f¡nR|durªt tjd|d|dj f¡d}n"t  tj d|d|dj f¡|j  |d|¡}|j |d¡} | d |d|dj| |¡durd}| d |d|dj||¡dur4d}q4|S) Nzcannot detect runlevelFTrrz'service '%s' configuration '%s' matchesz'service '%s' configuration '%s' differszservice '%s' configuration '%s'z %s runningz %s enabled)rdrrOrTrUZSTR_VERIFY_PROFILE_FAILr|r{r$r rPZSTR_VERIFY_PROFILE_OKZ STR_VERIFY_PROFILE_VALUE_MISSINGr1r0Z _verify_valuer r) r rZignore_missingZdevicesr Úretr&Zret_cfg_verifyr1r0r r rÚ_instance_verify_static"s( $""$"z%ServicePlugin._instance_verify_staticcCsHt|j ¡ƒD]4\}}|jr,|j ||j¡| ||j|j|j ¡qdSr) Úlistr}r{r rdr#r‚r rr )r rZrollbackr!rvr r rÚ_instance_unapply_static:sz&ServicePlugin._instance_unapply_static)rrrÚ__doc__rrircrpr€rr‚rƒr…rUZ ROLLBACK_SOFTr‡Ú __classcell__r r rgrra«s'   ra)rLrrxZ tuned.constsrUZ decoratorsrQrHZ tuned.logsZtunedZtuned.utils.commandsrZlogsÚgetrOrrrr%r2r>r@rKZPluginrar r r rÚs"      B