码迷,mamicode.com
首页 > 其他好文 > 详细

salt-call源码分析

时间:2015-05-27 16:00:41      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:salt-call

使用salt-call可以在minion本机执行salt命令

/usr/bin/salt-call 

#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: ‘salt==2014.7.0‘,‘console_scripts‘,‘salt-call‘
__requires__ = ‘salt==2014.7.0‘
import sys
from pkg_resources import load_entry_point

if __name__ == ‘__main__‘:
    sys.exit(
        load_entry_point(‘salt==2014.7.0‘, ‘console_scripts‘, ‘salt-call‘)()
    )


/usr/lib/python2.6/site-packages/salt/scripts.py

def salt_call():
    ‘‘‘
    Directly call a salt command in the modules, does not require a running
    salt minion to run.
    ‘‘‘
    if ‘‘ in sys.path:
        sys.path.remove(‘‘)
    client = None
    try:
        client = salt.cli.SaltCall()
        client.run()
    except KeyboardInterrupt, err:
        trace = traceback.format_exc()
        try:
            hardcrash = client.options.hard_crash
        except (AttributeError, KeyError):
            hardcrash = False
        _handle_interrupt(
            SystemExit(‘\nExiting gracefully on Ctrl-c‘),
            err,
            hardcrash, trace=trace)


client=salt.cli.SaltCall() 一行,调用了/usr/lib/python2.6/site-packages/salt/cli/__init__.py  中的SaltCall类

class SaltCall(parsers.SaltCallOptionParser):
    ‘‘‘
    Used to locally execute a salt command
    ‘‘‘

    def run(self):
        ‘‘‘
        Execute the salt call!
        ‘‘‘
        self.parse_args()

        if self.config[‘verify_env‘]:
            verify_env([
                    self.config[‘pki_dir‘],
                    self.config[‘cachedir‘],
                ],
                self.config[‘user‘],
                permissive=self.config[‘permissive_pki_access‘],
                pki_dir=self.config[‘pki_dir‘],
            )
            if not self.config[‘log_file‘].startswith((‘tcp://‘,
                                                       ‘udp://‘,
                                                       ‘file://‘)):
                # Logfile is not using Syslog, verify
                verify_files(
                    [self.config[‘log_file‘]],
                    self.config[‘user‘]
                )

        if self.options.file_root:
            # check if the argument is pointing to a file on disk
            file_root = os.path.abspath(self.options.file_root)
            self.config[‘file_roots‘] = {‘base‘: [file_root]}

        if self.options.pillar_root:
            # check if the argument is pointing to a file on disk
            pillar_root = os.path.abspath(self.options.pillar_root)
            self.config[‘pillar_roots‘] = {‘base‘: [pillar_root]}

        if self.options.local:
            self.config[‘file_client‘] = ‘local‘
        if self.options.master:
            self.config[‘master‘] = self.options.master

        # Setup file logging!
        self.setup_logfile_logger()

        #caller = salt.cli.caller.Caller(self.config)
        caller = salt.cli.caller.Caller.factory(self.config)

        if self.options.doc:
            caller.print_docs()
            self.exit(os.EX_OK)

        if self.options.grains_run:
            caller.print_grains()
            self.exit(os.EX_OK)

        caller.run()






本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1655646

salt-call源码分析

标签:salt-call

原文地址:http://john88wang.blog.51cto.com/2165294/1655646

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!