This article analyzes the running process of saltstack from the perspective of sal command line execution. The code analysis process is parsed by running logs.
Command Execution Process
Enter salt '*' cmd. run ls command on the terminal
#salt "*" cmd.run lscloudstack-agent_4.2.0_all.debcloudstack-common_4.2.0_all.debzmq
Let's take a look at the output log of the salt master.
14:28:36, 087 [master. py] [_ handle_clear] [640] [master] [-1221166336] [MainThread] [salt. master] [INFO] Clear payload received ed with command publish 14:28:36, 497 [master. py] [publish] [2520] [master] [-1221166336] [MainThread] [salt. master] [INFO] User root Published command cmd. run with jid 20140103142836087085 14:28:36, 499 [master. py] [publish] [2530] [master] [-1221166336] [MainThread] [salt. master] [DEBUG] Published command details {'tgt _ type': 'glob', 'jid': '000000', 'tgt ': 'stack', 'ret ': '', 'user': 'root', 'arg ': ['LS'], 'fun': 'cmd. run '} 14:28:38, 617 [master. py] [_ handle_aes] [664] [master] [-1221166336] [MainThread] [salt. master] [INFO] AES payload received ed with command _ return 14:28:38, 622 [master. py] [_ return] [1236] [master] [-1221166336] [MainThread] [salt. master] [INFO] Got return from stack for job 20140103142836087085 14:28:39, 201 [master. py] [_ handle_clear] [640] [master] [-1221166336] [MainThread] [salt. master] [INFO] Clear payload received ed with command _ auth 14:28:39, 203 [verify. py] [check_max_open_files] [392] [verify] [-1221166336] [MainThread] [salt. utils. verify] [DEBUG] This salt-master instance has accepted 1 minion keys. 14:28:39, 205 [master. py] [_ auth] [1795] [master] [-1221166336] [MainThread] [salt. master] [INFO] Authentication request from stack 14:28:39, 208 [master. py] [_ auth] [1922] [master] [-1221166336] [MainThread] [salt. master] [INFO] Authentication accepted from stack 14:28:40, 371 [master. py] [_ handle_aes] [664] [master] [-1221166336] [MainThread] [salt. master] [INFO] AES payload received ed with command _ return 14:28:40, 374 [master. py] [_ return] [1236] [master] [-1221166336] [MainThread] [salt. master] [INFO] Got return from stack for job 20140103142836087085 |
Check the running log of the minion client.
14:28:36, 522 [minion. py] [_ handle_aes] [612] [minion] [-1221879040] [MainThread] [salt. minion] [INFO] User root Executing command cmd. run with jid 20140103142836087085 14:28:36, 527 [minion. py] [_ handle_aes] [618] [minion] [-1221879040] [MainThread] [salt. minion] [DEBUG] Command details {'tgt _ type': 'glob', 'jid': '000000', 'tgt ': 'stack', 'ret ': '', 'user': 'root', 'arg ': ['LS'], 'fun': 'cmd. run '} 14:28:37, 790 [minion. py] [parse_args_and_kwargs] [165] [minion] [-1221879040] [MainThread] [salt. minion] [DEBUG] Parsed args: ['LS'] 14:28:37, 842 [minion. py] [parse_args_and_kwargs] [166] [minion] [-1221879040] [MainThread] [salt. minion] [DEBUG] Parsed kwargs: {'_ pub_user': 'root', '_ pub_arg': ['LS'], '_ pub_fun': 'cmd. run', '_ pub_jid': '000000', '_ pub_tgt': 'stack', '_ pub_tgt_type': 'glob', '_ pub_ret ': ''} 14:28:37, 913 [invalid Mod. py] [_ run] [283] [demomod] [-1221879040] [MainThread] [salt.loaded.int. module. cmdmod] [INFO] Executing command 'LS' in directory '/root' 14:28:38, 607 [demomod. py] [run] [501] [demomod] [-1221879040] [MainThread] [salt.loaded.int. module. demomod] [DEBUG] output: cloudstack-agent_4.2.0_all.deb Cloudstack-common_4.2.0_all.deb Zmq 14:28:38, 610 [minion. py] [_ return_pub] [828] [minion] [-1221879040] [MainThread] [salt. minion] [INFO] Returning information for job: 20140103142836087085 |
Execution Process Parsing
1. The Master and Minion of the Salt stack transmit messages through ZeroMq and use the publishing-subscription mode of ZeroMq. The connection modes include tcp and ipc.
2. run the command cmd. run ls from salt. client. LocalClient. cmd_cli to the master. Obtain a Jodid and obtain the command execution result based on jobid.
3. After the master receives the command, the command to be executed is sent to the client minion.
4. minion receives the command to be processed from the message bus and submits it to minion. _ handle_aes for processing.
5. minion. _ handle_aes initiates a local thread to call javasmod to execute the ls command. After the thread finishes ls execution, it calls the minion. _ return_pub method and returns the execution result to the master through the message bus.
6. The master receives the result returned by the client and calls the master. _ handle_aes method to write the result to the file.
7. salt. client. LocalClient. cmd_cli obtains the Job execution result through polling and outputs the result to the terminal.
TIPS:
1. Salt masterAnd minion communicate with each other through zeromq to transmit messages. Read the salt source code and learn how to use zeromq.
2. salt uses the pub-sub mode and req-rep mode of zeromq.
3. salt uses the tcp and ipc communication modes