SaltStack runners模块分析之jobs模块

/usr/lib/python2.6/site-packages/salt/runners/jobs.py

jobs runner模块用于方便管理正在运行或者处于活动状态的job



返回所有正在运行的job的报告


def active():
    '''
    Return a report on all actively running jobs from a job id centric
    perspective

    CLI Example:

    .. code-block:: bash

        salt-run jobs.active
    '''
    ret = {}
    client = salt.client.get_local_client(__opts__['conf_file'])
    active_ = client.cmd('*', 'saltutil.running', timeout=__opts__['timeout'])
    for minion, data in active_.items():
        if not isinstance(data, list):
            continue
        for job in data:
            if not job['jid'] in ret:
                ret[job['jid']] = _format_job_instance(job)
                ret[job['jid']].update({'Running': [{minion: job.get('pid', None)}], 'Returned': []})
            else:
                ret[job['jid']]['Running'].append({minion: job['pid']})

    mminion = salt.minion.MasterMinion(__opts__)
    for jid in ret:
        returner = _get_returner((__opts__['ext_job_cache'], __opts__['master_job_cache']))
        data = mminion.returners['{0}.get_jid'.format(returner)](jid)
        for minion in data:
            if minion not in ret[jid]['Returned']:
                ret[jid]['Returned'].append(minion)

    salt.output.display_output(ret, opts=__opts__)
    return ret
$ sudo salt-run jobs.active
20150529112431115487:
    ----------
    Arguments:
    Function:
        state.highstate
    Returned:
        - jialebi-qa-server
        - jidong-fileserver
        - gintama-qa-server
        - localhost.localdomain
    Running:
        |_
          ----------
          jialebi-qa-server:
              30690
        |_
          ----------
          jidong-fileserver:
              23521
        |_
          ----------
          gintama-qa-server:
              16806
        |_
          ----------
          localhost.localdomain:
              10146
    Target:
        *
    Target-type:
        glob
    User:
        sudo_caribbean


返回之前一个job的输出

def lookup_jid(jid, ext_source=None, missing=False):
    '''
    Return the printout from a previously executed job

    CLI Example:

    .. code-block:: bash

        salt-run jobs.lookup_jid 20130916125524463507
    '''
    ret = {}
    mminion = salt.minion.MasterMinion(__opts__)
    returner = _get_returner((__opts__['ext_job_cache'], ext_source, __opts__['master_job_cache']))

    data = mminion.returners['{0}.get_jid'.format(returner)](jid)
    for minion in data:
        if u'return' in data[minion]:
            ret[minion] = data[minion].get(u'return')
        else:
            ret[minion] = data[minion].get('return')
        if 'out' in data[minion]:
            out = data[minion]['out']
    if missing:
        ckminions = salt.utils.minions.CkMinions(__opts__)
        exp = ckminions.check_minions(data['tgt'], data['tgt_type'])
        for minion_id in exp:
            if minion_id not in data:
                ret[minion_id] = 'Minion did not return'
    salt.output.display_output(ret, opts=__opts__)
    return ret


$ sudo salt-run jobs.list_job 20150529112431115487
Arguments:
Function:
    state.highstate
Minions:
    - localhost.localdomain
    - jialebi-qa-server
    - jidong-fileserver
    - zg2-develp-server
    - gintama-qa-server
Result:
    ----------
    gintama-qa-server:
        ----------
        out:
            highstate
        return:
            ----------
            file_|-/etc/httpd/conf/httpd.conf_|-/etc/httpd/conf/httpd.conf_|-managed:
                ----------
                __run_num__:
                    1
                changes:
                    ----------
                comment:
                    File /etc/httpd/conf/httpd.conf is in the correct state
                name:
                    /etc/httpd/conf/httpd.conf
                result:
                    True
            group_|-apache_|-apache_|-present:
                ----------
                __run_num__:
                    2
                changes:
                    ----------
                comment:
                    No change
                name:
                    apache
                result:
                    True
                retcode:
                    2
            pkg_|-apache_|-httpd_|-installed:
                ----------
                __run_num__:
                    0
                changes:
                    ----------
                comment:
                    Package httpd is already installed
                name:
                    httpd
                result:
                    True
            pkg_|-mod_python_|-mod_python_|-installed:
                ----------
                __run_num__:
                    4
                changes:
                    ----------
                comment:
                    Package mod_python is already installed
                name:
                    mod_python
                result:
                    True
                retcode:
                    2



列出所有可以检测到的jobs

def list_jobs(ext_source=None):
    '''
    List all detectable jobs and associated functions

    CLI Example:

    .. code-block:: bash

        salt-run jobs.list_jobs
    '''
    returner = _get_returner((__opts__['ext_job_cache'], ext_source, __opts__['master_job_cache']))
    mminion = salt.minion.MasterMinion(__opts__)

    ret = mminion.returners['{0}.get_jids'.format(returner)]()
    salt.output.display_output(ret, opts=__opts__)

    return ret
$ sudo salt-run jobs.list_jobs
20150528154601461997:
    ----------
    Arguments:
    Function:
        state.highstate
    StartTime:
        2015, May 28 15:46:01.461997
    Target:
        jialebi-qa-server
    Target-type:
        glob
    User:
        root
20150528154733362383:
    ----------
    Arguments:
    Function:
        state.highstate
    StartTime:
        2015, May 28 15:47:33.362383
    Target:
        jialebi-qa-server
    Target-type:
        glob
    User:
        root





def print_job(jid, ext_source=None):
    '''
    Print job available details, including return data.

    CLI Example:

    .. code-block:: bash

        salt-run jobs.print_job
    '''
    ret = {}

    returner = _get_returner((__opts__['ext_job_cache'], ext_source, __opts__['master_job_cache']))
    mminion = salt.minion.MasterMinion(__opts__)

    job = mminion.returners['{0}.get_load'.format(returner)](jid)
    ret[jid] = _format_jid_instance(jid, job)
    ret[jid]['Result'] = mminion.returners['{0}.get_jid'.format(returner)](jid)
    salt.output.display_output(ret, opts=__opts__)

    return ret
$ sudo salt-run jobs.print_job 20150529112431115487
20150529112431115487:
    ----------
    Arguments:
    Function:
        state.highstate
    Minions:
        - localhost.localdomain
        - jialebi-qa-server
        - jidong-fileserver
        - zg2-develp-server
        - gintama-qa-server
    Result:
        ----------
        gintama-qa-server:
            ----------
            out:
                highstate
            return:
                ----------
                file_|-/etc/httpd/conf/httpd.conf_|-/etc/httpd/conf/httpd.conf_|-managed:
                    ----------
                    __run_num__:
                        1
                    changes:
                        ----------
                    comment:
                        File /etc/httpd/conf/httpd.conf is in the correct state
                    name:
                        /etc/httpd/conf/httpd.conf
                    result:
                        True
                group_|-apache_|-apache_|-present:
                    ----------
                    __run_num__:
                        2
                    changes:
                        ----------
                    comment:
                        No change
                    name:
                        apache
                    result:
                        True
                    retcode:
                        2
                pkg_|-apache_|-httpd_|-installed:
                    ----------
                    __run_num__:
                        0
                    changes:
                        ----------
                    comment:
                        Package httpd is already installed
                    name:
                        httpd
                    result:
                        True


你可能感兴趣的:(jobs,saltstack,模块分析)