每天saltsack源码之旅-01
首先看下salt命令:
1 [root@localhost ~]# which salt 2 /usr/bin/salt
查看下命令内容:
1 [root@localhost ~]# cat /usr/bin/salt 2 #!/usr/bin/python2.6 3 ''' 4 Publish commands to the salt system from the command line on the master. 5 ''' 6 7 from salt.scripts import salt_main 8 9 10 if __name__ == '__main__': 11 salt_main()
在我们执行命令的时候,实际上执行 是导入salt.scripts中的salt_main的方法。然后执行该方法。
让我们查看这个方法,看下这方法都执行了什么?
因为执行的python中的salt_main()方法,所以查看当前系统的salt安装路径:
1 [root@localhost ~]# python 2 Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 3 [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2 4 Type "help", "copyright", "credits" or "license" for more information. 5 >>> import salt 6 >>> print salt.__file__ 7 /usr/lib/python2.6/site-packages/salt/__init__.pyc
所以我们去目录:/usr/lib/python2.6/site-packages/salt/ ,由于salt命令导入的salt下的script的模块 中的salt_main的方法,我们一起看下script的模块中的salt_main方法。
1 def salt_main(): 2 ''' 3 Publish commands to the salt system from the command line on the 4 master. 5 ''' 6 import salt.cli.salt##导入salt的cli模块中的salt方法。 7 if '' in sys.path:#判断当前的python导入模块判断导入目录是否包含有当前目录,如果有,将当前目录移除。只读取系统中的目录不读取当前写的模块。 8 sys.path.remove('') 9 client = None#赋值变量client 10 try: 11 client = salt.cli.salt.SaltCMD() 12 client.run() 13 except KeyboardInterrupt as err: 14 trace = traceback.format_exc() 15 try: 16 hardcrash = client.options.hard_crash 17 except (AttributeError, KeyError): 18 hardcrash = False 19 _handle_interrupt( 20 SystemExit('\nExiting gracefully on Ctrl-c'), 21 err, 22 hardcrash, trace=trace)
salt_main方法是将命令行的命令发布到salt系统中。
学习是一种态度,坚持是质变的利器!

浙公网安备 33010602011771号