ENV: Version: 2016.11.0, Transport: tcp, Multi-Master
1. 类图和重要方法说明
2. 源码解析
2.1 初始化(salt.master.Master.__init__)
1 | # salt/master.py |
- 检查zmq版本
- 调用SMaster.init(self, opts),进行opts、master_key和key的初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# salt/master.py
class SMaster(object):
'''
Create a simple salt-master, this will generate the top-level master
'''
secrets = {} # mapping of key -> {'secret': multiprocessing type, 'reload': FUNCTION}
def __init__(self, opts):
'''
Create a salt master server instance
:param dict opts: The salt options dictionary
'''
self.opts = opts
self.master_key = salt.crypt.MasterKeys(self.opts)
self.key = self.__prep_key()
2.2 启动(salt.master.Master.start)
1.启动前检查,Master._pre_flight
- os.chdir(‘/‘)
- 根据fileserver_backend配置,创建Fileserver。这里用到了LazyLoader,先创建空的module,在实际调用的时候才进行加载,而不是在启动时全部import
1
2
3
4
5
6
7# salt/loader.py
class LazyLoader(salt.utils.lazy.LazyDict):
'''
A pseduo-dictionary which has a set of keys which are the
name of the module and function, delimited by a dot. When
the value of the key is accessed, the function is then loaded
from disk and into memory.
1 | # salt/loader.py |
1 | def __getattr__(self, mod_name): |
- 创建pillar_cache文件夹
- 检查git_pillar
This external pillar allows for a Pillar top file and Pillar SLS files to be sourced from a git repository.
1 | ext_pillar: |
2.注册信号处理程序
enable_sigusr1_handler。打印stack trace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# salt/utils/debug.py
def _handle_sigusr1(sig, stack):
'''
Signal handler for SIGUSR1, only available on Unix-like systems
'''
# When running in the foreground, do the right thing
# and spit out the debug info straight to the console
if sys.stderr.isatty():
output = sys.stderr
_makepretty(output, stack)
else:
filename = 'salt-debug-{0}.log'.format(int(time.time()))
destfile = os.path.join(tempfile.gettempdir(), filename)
with salt.utils.fopen(destfile, 'w') as output:
_makepretty(output, stack)enable_sigusr2_handler。YAPPI开关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21# salt/utils/debug.py
def _handle_sigusr2(sig, stack):
'''
Signal handler for SIGUSR2, only available on Unix-like systems
'''
try:
import yappi
except ImportError:
return
if yappi.is_running():
yappi.stop()
filename = 'callgrind.salt-{0}-{1}'.format(int(time.time()), os.getpid())
destfile = os.path.join(tempfile.gettempdir(), filename)
yappi.get_func_stats().save(destfile, type='CALLGRIND')
if sys.stderr.isatty():
sys.stderr.write('Saved profiling data to: {0}\n'.format(destfile))
yappi.clear_stats()
else:
if sys.stderr.isatty():
sys.stderr.write('Profiling started\n')
yappi.start()
3.设置max open files,Master.__set_max_open_files
4.初始化SMaster.secrets[‘aes’],在进行消息的发送时加密使用
5.创建ProcessManager,后续创建的各组件进程都会添加进来
1 | # salt/master.py |
6.创建publisher process。用于分发消息给minion
1 | # salt/master.py |
7.创建event publisher process。Master内部的消息总线,进行信息交换
1 | # salt/master.py |
8.创建reactor。
1 | # salt/master.py |
Salt’s Reactor system gives Salt the ability to trigger actions in response to an event. It is a simple interface to watching Salt’s event bus for event tags that match a given pattern and then running one or more commands in response.
This system binds sls files to event tags on the master. These sls files then define reactions. This means that the reactor system has two parts. First, the reactor option needs to be set in the master configuration file. The reactor option allows for event tags to be associated with sls reaction files. Second, these reaction files use highdata (like the state system) to define reactions to be executed.
1 | engines: |
9.创建maintenance process。A generalized maintenance process which performs maintenance routines.
1 | # salt/master.py |
1 | # salt/master.py |
10.创建event return process。
1 | # salt/master.py |
All Salt commands will return the command data back to the master. Specifying returners will ensure that the data is also sent to the specified returner interfaces.
11.创建ext_processes process
1 | # salt/master.py |
In addition to the processes that the Salt master automatically spawns, it is possible to configure it to start additional custom processes.
This is useful if a dedicated process is needed that should run throughout the life of the Salt master. For periodic independent tasks, a scheduled runner may be more appropriate.
Processes started in this way will be restarted if they die and will be killed when the Salt master is shut down.
1 | ext_processes: |
12.创建halite process。DEPRECATED: A client-side web application interface to a running Salt infrastructure
1 | # salt/master.py |
13.创建concache process
1 | # salt/master.py |
con_cache
Default: False
If max_minions is used in large installations, the master might experience high-load situations because of having to check the number of connected minions for every authentication. This cache provides the minion-ids of all connected minions to all MWorker-processes and greatly improves the performance of max_minions.
14.创建request server process。处理salt命令和minion处理结果
1 | # salt/master.py |
创建ReqServerChannel和MWorker