/usr/lib/python2.6/site-packages/salt/modules/service.py

 -*- 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