SaltStack源码分析之service模块
2015-05-12 12:04
567 查看
/usr/lib/python2.6/site-packages/salt/modules/service.py
这里定义两个字典,__func_alias__ 和 _GRAINMAP.
在python中,如果一个变量是以单个下划线开头的话,表示这个变量是私有变量。例如_var。
如果是以两个下划线开头的话,就表示这个变量是严格的私有变量,例如__var 。模块内部访问,外部无法直接调用。
如果是以两个下划线开头和结尾的话,就表示这个变量是python语言级别的变量,如__var__
单个下划线结尾是为了避免与python关键字冲突。如var_
Master在向minion发送命令之前会执行一些操作,例如返回静态grain的值或者动态grain的函数
获取所有服务名称
参考资料:
http://docs.saltstack.com/en/latest/topics/development/dunder_dictionaries.html#grains
本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1650560
-*- coding: utf-8 -*- ''' The default service module, if not otherwise specified salt will fall back to this basic module ''' # Import python libs import os __func_alias__ = { 'reload_': 'reload' } _GRAINMAP = { 'Arch': '/etc/rc.d', 'Arch ARM': '/etc/rc.d' }
这里定义两个字典,__func_alias__ 和 _GRAINMAP.
在python中,如果一个变量是以单个下划线开头的话,表示这个变量是私有变量。例如_var。
如果是以两个下划线开头的话,就表示这个变量是严格的私有变量,例如__var 。模块内部访问,外部无法直接调用。
如果是以两个下划线开头和结尾的话,就表示这个变量是python语言级别的变量,如__var__
单个下划线结尾是为了避免与python关键字冲突。如var_
def __virtual__(): ''' Only work on systems which exclusively use sysvinit ''' # Disable on these platforms, specific service modules exist: disable = set(( 'RedHat', 'CentOS', 'Amazon', 'ScientificLinux', 'CloudLinux', 'Fedora', 'Gentoo', 'Ubuntu', 'Debian', 'Arch', 'Arch ARM', 'ALT', 'SUSE Enterprise Server', 'OEL', 'Linaro', 'elementary OS', 'McAfee OS Server' )) if __grains__.get('os', '') in disable: return False # Disable on all non-Linux OSes as well if __grains__['kernel'] != 'Linux': return False # Suse >=12.0 uses systemd if __grains__.get('os_family', '') == 'Suse': try: if int(__grains__.get('osrelease', '').split('.')[0]) >= 12: return False except ValueError: return False return 'service'
Master在向minion发送命令之前会执行一些操作,例如返回静态grain的值或者动态grain的函数
def start(name): ''' Start the specified service CLI Example: .. code-block:: bash salt '*' service.start <service name> ''' cmd = os.path.join( _GRAINMAP.get(__grains__.get('os'), '/etc/init.d'), name ) + ' start' return not __salt__['cmd.retcode'](cmd)
$ sudo salt '*' service.start httpd jidong-fileserver: True localhost.localdomain: False gintama-qa-server: False jialebi-qa-server: False
def stop(name): ''' Stop the specified service CLI Example: .. code-block:: bash salt '*' service.stop <service name> ''' cmd = os.path.join( _GRAINMAP.get(__grains__.get('os'), '/etc/init.d'), name ) + ' stop' return not __salt__['cmd.retcode'](cmd)
$ sudo salt '*' service.stop httpd jidong-fileserver: True localhost.localdomain: True gintama-qa-server: True jialebi-qa-server: True
def restart(name): ''' Restart the specified service CLI Example: .. code-block:: bash salt '*' service.restart <service name> ''' cmd = os.path.join( _GRAINMAP.get(__grains__.get('os'), '/etc/init.d'), name ) + ' restart' return not __salt__['cmd.retcode'](cmd)
$ sudo salt '*' service.restart httpd jidong-fileserver: True localhost.localdomain: False gintama-qa-server: False jialebi-qa-server: False
def status(name, sig=None): ''' Return the status for a service, returns the PID or an empty string if the service is running or not, pass a signature to use to find the service via ps CLI Example: .. code-block:: bash salt '*' service.status <service name> [service signature] ''' return __salt__['status.pid'](sig if sig else name)
$ sudo salt '*' service.status httpd jidong-fileserver: True localhost.localdomain: False gintama-qa-server: False jialebi-qa-server: False
def reload_(name): ''' Refreshes config files by calling service reload. Does not perform a full restart. CLI Example: .. code-block:: bash salt '*' service.reload <service name> ''' cmd = os.path.join( _GRAINMAP.get(__grains__.get('os'), '/etc/init.d'), name ) + ' reload' return not __salt__['cmd.retcode'](cmd)
$ sudo salt '*' service.reload httpd jidong-fileserver: False localhost.localdomain: False gintama-qa-server: False jialebi-qa-server: False
def available(name): ''' Returns ``True`` if the specified service is available, otherwise returns ``False``. CLI Example: .. code-block:: bash salt '*' service.available sshd ''' return name in get_all()
$ sudo salt '*' service.available httpd jidong-fileserver: True localhost.localdomain: True gintama-qa-server: True jialebi-qa-server: True
def missing(name): ''' The inverse of service.available. Returns ``True`` if the specified service is not available, otherwise returns ``False``. CLI Example: .. code-block:: bash salt '*' service.missing sshd ''' return name not in get_all()
$ sudo salt '*' service.missing httpd jidong-fileserver: False localhost.localdomain: False gintama-qa-server: False jialebi-qa-server: False
def get_all(): ''' Return a list of all available services CLI Example: .. code-block:: bash salt '*' service.get_all ''' if not os.path.isdir(_GRAINMAP.get(__grains__.get('os'), '/etc/init.d')): return [] return sorted(os.listdir(_GRAINMAP.get(__grains__.get('os'), '/etc/init.d')))
获取所有服务名称
参考资料:
http://docs.saltstack.com/en/latest/topics/development/dunder_dictionaries.html#grains
本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1650560
相关文章推荐
- SaltStack源码分析之group状态模块
- 一个普通的 Zepto 源码分析(三) - event 模块
- 二 spring源码分析---资源模块接口设计
- ADB模块源码分析(二)——adb server的启动
- Flume NG源码分析(二)支持运行时动态修改配置的配置模块
- Cloud Foundry Service Node源码分析及实现【附下载】
- ANR源码分析之Service Timeout
- Mangos源码分析(14):Mangos预编译头文件及模块划分随想
- Android 5.0源码分析---startService的过程
- 看书 Python 源码分析笔记 (十一) 模块加载机制
- Android之AlarmManagerService源码分析
- Android服务之PackageManagerService启动源码分析
- ActivityManagerService服务线程启动源码分析【转】
- Jimmy的文档:Glusterfs之rpc模块源码分析之RPC概述(第一部分)
- jQuery源码分析系列:事件模块概述
- WebRTC源码分析二:音频模块结构
- android ActivityManagerService 源码分析----Activity管理(二)
- Android线程IntentService源码分析
- IntentService源码分析