saltstack 的源码大概分析

下面是简单示例:

                            salt/script.py 
                                client = salt.cli.ssh.SaltSSH()
                                client.run()

                            salt/cli/ssh.py 
                                # self.config是读取的配置文件
                                ssh = salt.client.ssh.SSH(self.config)
                                ssh.run()
                            
                            salt/client/ssh/__init__.py 
                                class SSH(object):
                                    def run(self, jid=None):
                                        if self.opts.get('raw_shell', False):
                                            fun = 'ssh._raw'
                                            args = argv
                                        else:
                                            fun = argv[0] if argv else ''
                                            args = argv[1:]
                                        for ret in self.handle_ssh():
                                            pass 
                                    
                                    def handle_ssh():
                                        routine = MultiprocessingProcess(target=self.handle_routine,args=args)
                                        routine.start()
                                    def handle_routine(self, que, opts, host, target, mine=False):
                                        single = Single(...)
                                        single.run()
                            
                            salt/client/ssh/__init__.py                             
                                class Single(object):
                                    def run(self, deploy_attempted=False):
                                        stdout, retcode = self.run_wfunc()
                                        return stdout, stderr, retcode
                                    def run_wfunc(self):
                                        
                                        # 实例化Pillar对象
                                        pillar = salt.pillar.Pillar(
                                                opts_pkg,
                                                opts_pkg['grains'],
                                                opts_pkg['id'],
                                                opts_pkg.get('environment', 'base')
                                                )
                                        pillar_dirs = {}
                                        
                                        # pillar对象.pillar => 去三个地方获取所有pillar
                                        pillar_data = pillar.compile_pillar(pillar_dirs=pillar_dirs)
                                        
                                        print(pillar_data)
                                
                            salt/pillar/__init__.py 
                                class Pillar(object):
                                    
                                    def __init__(...):
                                        # 去中找自定义pillar
                                        # /var/cache/salt/master/extmods/pillar/alex.py  # [**推荐使用**]
                                        # /usr/lib/python2.7/site-packages/salt/pillar/wupeiqi.py
                                        
                                        # ['wupeqii','alex','cmd_json',.....]
                                        self.ext_pillars = salt.loader.pillars(ext_pillar_opts, self.functions)
                                        
                                def compile_pillar(self, ext=True, pillar_dirs=None):
                                    
                                    # 1. top.sls 
                                    # 去中找自定义pillar
                                    # 2. /var/cache/salt/master/extmods/pillar/alex.py  # [**推荐使用**]
                                    # 3. /usr/lib/python2.7/site-packages/salt/pillar/wupeiqi.py
                                    
                                    pillar1 = self.render_pillar('top.cls')
                                    pillar2 = self.ext_pillar(pillar1,...)
                                    
                                    return pillar1 + pillar2
                                        
                                def self.ext_pillar(...):
                                    return self._external_pillar_data(....)
                                
                                def _external_pillar_data(self, pillar, val, pillar_dirs, key):
                                    ext = self.ext_pillars[key](self.minion_id,pillar,*val)
                                    return ext 

 

posted @ 2019-03-25 15:59  auxiaoliu  阅读(381)  评论(0编辑  收藏  举报