您好,登錄后才能下訂單哦!
1.salt 命令的開始腳本 [root@zhu1 ~]# which salt /usr/bin/salt [root@zhu1 ~]# cat /usr/bin/salt #!/usr/bin/python2.6 ''' Publish commands to the salt system from the command line on the master. ''' from salt.scripts import salt_main if __name__ == '__main__': salt_main() 可以看到salt命令實際上是調(diào)用的是: salt.scripts文件中的salt_main()函數(shù)而已
2.salt_main函數(shù)如下
client = salt.cli.salt.SaltCMD() client.run() salt_main函數(shù)建立一個salt.cli.salt.SaltCMD實例client,然后執(zhí)行該實例的run方法 run方法執(zhí)行內(nèi)容如下: self.parse_args() #解析命令行選項 local = salt.client.get_local_client(self.get_config_file_path(), skip_perm_errors=skip_perm_errors) 生成一個字典kwargs ;該字典稍后會作為參數(shù)傳遞給執(zhí)行函數(shù) 以 salt '*' test.ping 為例 kwargs = { 'tgt': '*', 'show_timeout': True, 'delimiter': ':', 'expr_form': 'glob', 'timeout': 5, 'arg': [], 'fun': 'test.ping', 'show_jid': False } 執(zhí)行: local.cmd_cli(**kwargs) 在看cmd_cli方法執(zhí)行內(nèi)容前,需看local實例具體做的那些事情
3.local = salt.client.get_local_client(self.get_config_file_path(),skip_perm_errors=skip_perm_errors)
執(zhí)行內(nèi)容:
local = salt.client.get_local_client('/etc/salt/master',False) salt.client.get_local_client 是一個函數(shù),該函數(shù)返回LocalClient()實例
4.LocalClient 類實例化時
salt 執(zhí)行命令工作流程(以 salt '*' test.ping 為例): 1.調(diào)用self.parse_args()方法解析命令行參數(shù) 2.實例化 local = salt.client.get_local_client('/etc/salt/master',False) salt.client.get_local_client 是一個函數(shù),該函數(shù)返回LocalClient()實例 3.實例化 LocalClient() 4.初始化 self.event = salt.utils.event.get_event('master',/var/run/salt/master,zeromq,'self.opts','listen=False') 5.salt.utils.event.get_event()函數(shù)返回salt.utils.event.SaltEvent()類 self.puburi = /var/run/salt/master/master_event_pub.ipc self.pulluri = /var/run/salt/master/master_event_pull.ipc 初始化 self.utils = salt.loader.utils(self.opts) 初始化 self.functions = salt.loader.minion_mods(self.opts, utils=self.utils) 初始化 self.returners = salt.loader.returners(self.opts, self.functions) 6.生成字典 kwargs = {'tgt': 'zhu1', 'show_timeout': True, 'delimiter': ':', 'expr_form': 'glob', 'timeout': 5, 'arg': [], 'fun': 'test.ping', 'show_jid': False} 7.開始執(zhí)行: local.cmd_cli(**kwargs)方法 8.在local.cmd_cli方法中調(diào)用self.run_job方法 9.在local.run_job方法中調(diào)用self.pub方法 10.在local.pub方法中調(diào)用self._prep_pub方法 11.self._prep_pub方法返回一個字典,該字典內(nèi)容為發(fā)送給salt-master的參數(shù)格式: payload_kwargs = {'tgt_type': 'glob', 'jid': '', 'key': 'rtXXpQ75m9tp0RgzNxYJuhX0ulKOHW4qKO3cMerv++4a3HDCTArXW+F1UNbZAqmc9MXAdHbHl/U=', 'tgt': 'zhu1', 'arg': [], 'fun': 'test.ping', 'kwargs': {'delimiter': ':', 'show_timeout': True, 'show_jid': False}, 'cmd': 'publish', 'ret': '', 'user': 'root'} 12.salt發(fā)送的master的地址為 master_uri = tcp://127.0.0.1:4506 13.實例化channel = salt.transport.Channel.factory(self.opts,crypt='clear',master_uri=tcp://127.0.0.1:4506) salt.transport.Channel.factory 函數(shù)返回salt.transport.client.ReqChannel.factory() salt.transport.client.ReqChannel.factory()返回SyncWrapper(AsyncReqChannel.factory, (opts,), kwargs) AsyncReqChannel.factory返回 salt.transport.zeromq.AsyncZeroMQReqChannel(opts, **kwargs) AsyncZeroMQReqChannel繼承于salt.transport.client.ReqChannel AsyncZeroMQReqChannel在創(chuàng)建類時會執(zhí)行self.message_client = AsyncReqMessageClient(self.opts,self.master_uri,io_loop=self._io_loop) 在AsyncReqMessageClient類實例化時執(zhí)行如下部分代碼: self.context = zmq.Context() self.socket = self.context.socket(zmq.REQ) self.socket.connect(self.addr) 14.執(zhí)行self.event.connect_pub(timeout=timeout) 15.在connect_pub中執(zhí)行self.subscriber = salt.transport.ipc.IPCMessageSubscriber(/var/run/salt/master/master_event_pub.ipc) 16.IPCMessageSubscriber連接------------->/var/run/salt/master/master_event_pub.ipc 17.執(zhí)行payload = channel.send(payload_kwargs, timeout=timeout) 執(zhí)行發(fā)送: channel.send()--->AsyncZeroMQReqChannel.send() 執(zhí)行發(fā)送: self.message_client.send() 執(zhí)行發(fā)送 AsyncReqMessageClient.send() 18.發(fā)送tcp://127.0.0.1:4506后返回的payload = {'load': {'jid': '20161227141413152668', 'minions': ['zhu1']}, 'enc': 'clear'} 19.在local.run_job函數(shù)中執(zhí)行self.pub返回的數(shù)據(jù): pub_data ={'jid': '20161227141413152668', 'minions': ['zhu1']} 20.在local.run_job函數(shù)中執(zhí)行self._check_pub_data(pub_data)檢查返回的數(shù)據(jù) opts.get('order_masters') = False 21.開始執(zhí)行 self.event.subscribe('salt/job/20161227141413152668')根據(jù)jid訂閱匹配的事件 返回pub_data = {'jid': '20161227141413152668', 'minions': ['zhu1']} 22.在cmd_cli函數(shù)中 self.run_job方法返回數(shù)據(jù) pub_data = {'jid': '20161227141413152668', 'minions': ['zhu1']} 23.在cmd_cli方法中 self.get_cli_event_returns一定會執(zhí)行 24.開始執(zhí)行self.get_cli_event_returns(20161227141413152668,['zhu1'],5,zhu1,glob,False,False) 25.開始執(zhí)行: get_iter_returns(20161227141413152668,['zhu1']) 26.開始執(zhí)行g(shù)et_iter_returns(20161227141413152668,['zhu1'])方法;一直監(jiān)聽事件系統(tǒng)直到所等待的job數(shù)據(jù)到來 27.執(zhí)行returners = self.returners['local_cache.get_load'] <class 'salt.loader.LazyLoader'> jid_dir=/var/cache/salt/master/jobs/bc/21bfd15870cdc50f82a8555eba79e7 load_fn=/var/cache/salt/master/jobs/bc/21bfd15870cdc50f82a8555eba79e7/.load.p minions_cache = ['/var/cache/salt/master/jobs/bc/21bfd15870cdc50f82a8555eba79e7/.minions.p'] 28. 開始執(zhí)行 self.get_returns_no_block('salt/job/20161227141413152668') ret = {'tag': '20161227141413152668', 'data': {'_stamp': '2016-12-27T06:14:13.156325', 'minions': ['zhu1']}} ret = {'tag': 'salt/job/20161227141413152668/new', 'data': {'tgt_type': 'glob', 'jid': '20161227141413152668', 'tgt': 'zhu1', '_stamp': '2016-12-27T06:14:13.167890', 'user': 'root', 'arg': [], 'fun': 'test.ping', 'minions': ['zhu1']}} ret = {'tag': 'salt/job/20161227141413152668/ret/zhu1', 'data': {'fun_args': [], 'jid': '20161227141413152668', 'return': True, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2016-12-27T06:14:13.294696', 'fun': 'test.ping', 'id': 'zhu1'}} fn_ret={'zhu1': {'retcode': 0, 'ret': True}} full_ret={'zhu1': {'retcode': 0, 'ret': True}} zhu1: True 開始執(zhí)行: self._clean_up_subscriptions(20161227141413152668)
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。