|
最近在看saltstack的源代码,了解里面的流程,看了下salt命令的执行流程。想想自己能不能改造下源代码,于是自己在那想需求。后来想想能不能在执行salt的时候指定jid啊。
先说下需求:
指定salt命令的jid
类似这样的:
开始动手实现。
第一步
修改salt.utils.parsers.SaltCMDOptionParser类,增加命令行选项。
1
2
3
4
5
6
7
| self.add_option(
'-j','--jid',
dest='jid',
default='',
help=('Set the job\'s id,'
'like 20140903143821048459')
)
|
第二步
修改salt.utils.parsers.OptionParser类,用于判断jid的合法性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| # 在parse_args方法返回前,调用这个方法
if hasattr(options,'jid'):
self._check_jid(options.jid)
return options, args
# 检查jid的合法性
def _check_jid(self,jid=''):
try:
if jid == '':
pass
elif len(jid) != 20:
raise optparse.OptionValueError('jid needs 20 length and is numberic str,like "20140903143821048459"')
else:
import re
if not re.match(r'\d{20}',jid):
raise optparse.OptionValueError('jid needs 20 length and is numberic str,like "20140903143821048459"')
except optparse.OptionValueError,e:
print(e)
sys.exit(1)
|
第三步
修改salt.cli.SaltCMD类
1
2
3
4
5
6
7
8
| try:
local = salt.client.LocalClient(self.get_config_file_path(),self.options.jid) #修改
except SaltClientError as exc:
self.exit(2, '{0}\n'.format(exc))
return
if self.options.batch:
batch = salt.cli.batch.Batch(self.config,jid=self.options.jid) # 修改
|
第四步
修改salt.client.LocalClient类
1
2
3
4
5
6
7
8
9
10
| def __init__(self,
c_path=os.path.join(syspaths.CONFIG_DIR, 'master'),
jid='',mopts=None): # 增加jid参数
......
self.jid = jid # 增加属性
self.serial = salt.payload.Serial(self.opts)
def run_job(self,tgt,fun,....):
# jid = ''
jid = self.jid
|
第五步
修改salt.cli.batch.Batch类
1
2
3
4
5
| def __init__(self, opts, quiet=False,jid=''): # 增加jid
self.opts = opts
self.quiet = quiet
self.local = salt.client.LocalClient(opts['conf_file'],jid) # 增加jid
self.minions = self.__gather_minions()
|
所有步骤完成,重启master和minion。
当当当。。。
修改成功。
为什么要加这个功能,因为后台管理系统在执行任务的时候,需要先由web页面产生一个jid。通过这个jid去查询一些信息。
|
|
|