saltstack源码详解一

拥有回忆 提交于 2021-01-31 04:57:34

@(python之路)[saltstack源码详解一] saltstack源码详解

[TOC]

初识源码流程

环境准备:

# 安装
yum install salt-ssh
pip3 install salt-ssh

# 命令
salt-ssh
# 配置
/etc/salt/roster
# 功能
salt-ssh "*" cmd.run "ls"

下面我们查看源码

入口

yum install salt-ssh后,他就会把代码放到python中的lib中。他会触发这个脚本

from salt.scripts import salt_ssh

if __name__ == '__main__':
    salt_ssh()

他会调用这里边的东西并执行。 解析salt-ssh "*" cmd.run "ls" -i 命令 源码流程:

  • 启动命令时python脚本
  • 读取默认配置文件 obj = salt.clissh.SaltSSH() obj.run()
  • 执行命令 obj = SSH(配置文件) obj.run()
  • 创建进程间供享的队列;[]
  • 读取配置文件中的主机,给每个符合条件的主机创建一个进程; host = next(target_iter) routine = MultiprocessingProcess(target=self.handle_routine,args=args) 每个进程执行得到结果后,将结果放入队列中 ret = self.handle_routing(……)  触发Single   触发Shell   触发terminal  que.put(ret)

salt日常使用

from salt.client.ssh.shell import Shell
shell = Shell({_ssh_version:(4,9 )})
result = shell.exec_cmd("ls")
print(result)

预留python接口:

from salt.client.ssh.client import SSHClient

client = SSHClient()

ret = client.cmd('*','cmd.run',('ls',),30)
print(ret)

1.grains.items

salt-ssh '*' grains.items grains一般用于获取服务的静态信息 自定义

# /srv/salt/_grains/xx.py
预留python接口
from salt.client.ssh.client import SSHClient
client = SSHClient()
ret = client.cmd("*",'grains.items')
print(ret)

2.pillar.items

salt-ssh "*" pillar.items pillar一般用于获取服务器动态信息 方式一

# /srv/pillar/top.sls
base:
  "*":          # "*" 表示所有的分组
    - xxx       # 文件名

# /srv/pillar/xxx.sls
x1:
	{% if grains['os_family'] == 'Debian' %}
	  xxx1: 111111111111111
	  {% elif grains['os_family'] == 'RedHat' %}
	  xxx1: 222222222222222
	  {% elif grains['os'] == 'Arch' %}
	  xxx1: 333333333333333
	{% endif %}

2/3: 是否可以用python脚本实现

# /etc/salt/master
ext_pillar:
  - xiaohua:
	     api: 123
  - tom:
 
 # /var/cache/salt/master/extmods/pillar/tom.py  # [**推荐使用**]
def ext_pillar(minion_id,pillar,*args,**kwargs):
	import time
	return {'alexcccccccccccccctime':str(time.time())}

# /usr/lib/python2.7/site-packages/salt/pillar/wupeiqi.py
def ext_pillar(minion_id,pillar,*args,**kwargs):
	import time
	return {'alexcccccccccccccctime':str(time.time())}

总结pillar源码分析:

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
			
			# ['xiaohua','tom','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 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!