saltstack源码-启动2-parsers.py选项初始化1

class MasterOptionParser(OptionParser, ConfigDirMixIn, MergeConfigMixIn,
                         LogLevelMixIn, RunUserMixin, DaemonMixIn,
                         PidfileMixin):#这些差不多全是salt初始化的类,有的minion也会调用

    __metaclass__ = OptionParserMeta #指定他的元类

    description = ‘The Salt master, used to control the Salt minions.‘

    # ConfigDirMixIn config filename attribute
    _config_filename_ = ‘master‘#默认配置文件名
    # LogLevelMixIn attributes
    _default_logging_logfile_ = os.path.join(syspaths.LOGS_DIR, ‘master‘) #这里是得到他的日志文件路径/var/log/salt/master

    def setup_config(self):
        return config.master_config(self.get_config_file_path())

接下来就是查找他的元类
python类在初始化的时候,经历了两个阶段,第一个阶段是分配空间__new__(),第二个阶段是初始化值__init__()。当类被创建时,python会首先寻找__metaclass__是否存在,
如果存在则调用__metaclass__。如果此类没定义__metaclass__就去看父类,父类没有就去模块里找(全局变量__metaclass__),
模块里再没有就把__metaclass__ = type作为类的metaclass。
mcs代表调用__new__函数的class,name代表对象的__name__值,也就是名称,bases代表对象的父类元组,attrs代表类的属性字典.

class MixInMeta(type):
    # This attribute here won‘t actually do anything. But, if you need to
    # specify an order or a dependency within the mix-ins, please define the
    # attribute on your own MixIn
    _mixin_prio_ = 0

    def __new__(mcs, name, bases, attrs):
        instance = super(MixInMeta, mcs).__new__(mcs, name, bases, attrs)
        if not hasattr(instance, ‘_mixin_setup‘):
            raise RuntimeError(
                ‘Don\‘t subclass {0} in {1} if you\‘re not going to use it ‘
                ‘as a salt parser mix-in.‘.format(mcs.__name__, name)
            )
        return instance

class OptionParserMeta(MixInMeta):
    def __new__(mcs, name, bases, attrs):
        instance = super(OptionParserMeta, mcs).__new__(mcs,
                                                        name,
                                                        bases,
                                                        attrs)
        #上面调用父类的MinInMeta.__new__方法
    if not hasattr(instance, ‘_mixin_setup_funcs‘):
            instance._mixin_setup_funcs = []
        if not hasattr(instance, ‘_mixin_process_funcs‘):
            instance._mixin_process_funcs = []
        if not hasattr(instance, ‘_mixin_after_parsed_funcs‘):
            instance._mixin_after_parsed_funcs = []
        #这条语句分别创建了3个空列表

        for base in _sorted(bases + (instance,)):
        #这里是把基类和子类进行排序
            func = getattr(base, ‘_mixin_setup‘, None)#得到所有类_mixin_setup属性方法,没有就赋值为None
            if func is not None and func not in instance._mixin_setup_funcs:
                instance._mixin_setup_funcs.append(func)
                #for循环过后得到的列表 _mixin_setup_funcs[ConfigDirMixIn._mixin_setup,
                #                                          LogLevelMixIn._mixin_setup,
                #                                         RunUserMixin._mixin_setup,
                #                                         DaemonMixIn_mixin_setup,
                #                                         PidfileMixin._mixin_setup,
                #                                         ]
                #这些类方法是初始化salt的选项命令

            func = getattr(base, ‘_mixin_after_parsed‘, None)##得到所有类_mixin_after_parsed属性方法,没有就赋值为None
            if func is not None and func not in                     instance._mixin_after_parsed_funcs:
                instance._mixin_after_parsed_funcs.append(func)
            #这个列表如果是启动的话在这是空列表,不过会在OptionParser的parse_args方法里面实现添加

            # Mark process_<opt> functions with the base priority for sorting
            for func in dir(base):
                if not func.startswith(‘process_‘):
                    continue

                func = getattr(base, func)
                if getattr(func, ‘_mixin_prio_‘, None) is not None:
                    # Function already has the attribute set, don‘t override it
                    continue

                func.__func__._mixin_prio_ = getattr(
                    base, ‘_mixin_prio_‘, 1000
                )
            #这个for循环把所有没_mixin_prio属性的基类 设置属性值1000

        return instance #返回实例 

接下来就是调用OptionParser的__init__()方法来初始化了
接着调用的OptionParser的parse_args方法

    def parse_args(self, args=None, values=None):
        options, args = optparse.OptionParser.parse_args(self, args, values)
#上面这句options的值是{‘daemon‘: False, ‘log_level‘: None, ‘versions_report‘: None, ‘user‘: None, ‘log_file‘: None, ‘config_dir‘: ‘/etc/salt‘, ‘pidfile‘: ‘/var/run/salt.pid‘, ‘log_level_logfile‘: None}
#惭愧 到现在都还没找到这些值是怎么来的,找了好久都没找到,后来干脆就不找了,现往下看完再说,别吊死在这
        if options.versions_report:
            self.print_versions_report()

        self.options, self.args = options, args

        # Let‘s get some proper sys.stderr logging as soon as possible!!!
        # This logging handler will be removed once the proper console or
        # logfile logging is setup.
        log.setup_temp_logger(
            getattr(self.options, ‘log_level‘, ‘error‘)
        )

        # Gather and run the process_<option> functions in the proper order
        process_option_funcs = []
        for option_key in options.__dict__.keys():
            process_option_func = getattr(
                self, ‘process_{0}‘.format(option_key), None
            )
            if process_option_func is not None:
                process_option_funcs.append(process_option_func)
#下面分别执行这3个方法process_config_dir  process_log_level    process_log_file

        for process_option_func in _sorted(process_option_funcs):
            try:
                process_option_func()
            except Exception as err:
                logging.getLogger(__name__).exception(err)
                self.error(
                    ‘Error while processing {0}: {1}‘.format(
                        process_option_func, traceback.format_exc(err)
                    )
                )
#首先看一下ConfigDirMixIn.process_config_dir 方法
    def process_config_dir(self):
        if not os.path.isdir(self.options.config_dir):
            # No logging is configured yet
            sys.stderr.write(
                ‘WARNING: {0!r} directory does not exist.\n‘.format(
                    self.options.config_dir
                )
            )

        # Make sure we have an absolute path
        self.options.config_dir = os.path.abspath(self.options.config_dir)

        if hasattr(self, ‘setup_config‘):
            try:
                self.config = self.setup_config()
                #这里调用子类的MasterOptionParser.setup_config 方法,其方法原型如下:
                #def setup_config(self):
                 #   return config.master_config(self.get_config_file_path())
            except (IOError, OSError) as exc:
                self.error(
                    ‘Failed to load configuration: {0}‘.format(exc)
                )

那个get_config_file_path()返回配置文件的绝对路径然后作为参数传至config.py里面的master_config()方法
草!又调到config.py了,4个编辑器完全不够我用的...妈的继续往下去看

时间: 2024-10-06 00:55:19

saltstack源码-启动2-parsers.py选项初始化1的相关文章

saltstack源码-启动1

决定看salt的源码了.干脆就从最基本的看起来,先看它的启动过程开始第一步用/etc/init.d/salt-master start 启动找到那个文件,发现有3种启动方式,suse,debian,centos的启动方式各不一样,我测试机和线上环境都是centos的,所以直接就看Centos的 ...... PYTHON=/usr/bin/python SALTMASTER=/usr/bin/salt-master MASTER_ARGS="" ...... stat() { ....

saltstack源码-启动3-config.py配置文件加载

#目标文件位置/usr/lib/python2.6/site-packages/salt/config.py#这个文件加载配置文件的模块.master和minion的配置文件加载都是在这个模块里面完成的#master的启动在这模块里面只涉及到方法和属性只有几个 master和minion的默认配置属性也在这个文件里面定义 DEFAULT_MASTER_OPTS = { 'interface': '0.0.0.0', 'publish_port': '4505', 'pub_hwm': 1000,

saltstack 系列(四)centos7使用saltstack源码安装nginx

使用saltstack源码安装nginx,首先先看一下我nginx的目录  tree一下,我们只需要关系nginx-install.sls 和nignx-service.sls.clu-vhost是我用python写的自动添加集群和自动更新踢出集群,后面会讲到. nginx ├── files │   ├── clu-vhost │   │   ├── 11.py │   │   ├── content.txt │   │   ├── epel-release-latest-7.noarch.r

saltstack源码安装nrpe

最近弄saltstack弄上瘾了,今天又弄了saltstack源码安装nrpe,安装nrpe之前需要安装nagios-plugins,首先是top.sls文件: [[email protected] salt]# cat top.sls base: '*': - nagios [[email protected] salt]# tree nagios   #目录结构 nagios ├── conf.sls ├── files │?? ├── conf.sh │?? ├── nagios-plug

《python源码剖析》笔记 python环境初始化

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.线程环境初始化 Py_InitializeEx,Python会首先调用 PyInterpreterState_New创建一个崭新的PyInterpreterState对象. 创建了PyInterpreterState(进程状态)对象之后,Python会调用PyThreadState_New创建PyThreadState(线程状态)对象 全局变量_PyThreadState_Curren

saltstack源码安装zabbix_agent

公司现有的监控使用zabbix,saltstack客户端服务端安装好后,客户端不需要每台机器逐台安装,当然修改zabbix配置文件,直接用salt完成,今天测试了salt源码安装zabbix_agent,效果还行: 老规矩,top.sls [[email protected] salt]# cat top.sls base: '*': - zabbix #目录树 [[email protected] salt]# tree zabbix/ zabbix/ ├── conf.sls ├── fil

Centos下用Saltstack源码安装Mysql

简介: SaltStack是 一个服务器基础架构集中化管理平台,具备配置管理.远程执行.监控等功能,一般可以理解为简化版的puppet和加强版的func.SaltStack基 于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq.PyCrypto.Pyjinjia2. python-msgpack和PyYAML等)构建. 最近工作比较闲,所以继续学习saltstack,今天就用源码来安装Mysql数据库,学saltstack还是得多实践,废话不多说,上干

saltstack源码安装nginx

[首先配置好saltstack基础环境,确保master能远程minion,这里就不列举了]  因为涉及到的目录较多,因此先规划好目录结构 [[email protected] dev]# tree /srv/dev/ /srv/dev/nginx_install │       ├  initpkg.sls │          ├  initall .sls │       ├  nginx_init.sls │          ├  files │           └── nginx

shell脚本 &nbsp; http源码启动脚本

源码包http启动脚本,仅供参考......下面有文本的. #!/bin/bash #Author: wangergui              Email:[email protected]         Date:2016-06-01 #Function: source apache start script PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root:/bin export PATH [