本文共 8304 字,大约阅读时间需要 27 分钟。
前沿:
以前都是调取一个url的接口,把结果post过去,然后接收post请求的那个函数,会把结果send到指定的websocket客户端,也可以是所有的websocket客户端。
但总是觉得频繁的调用url,然后再send,觉得有些多此一举,还不如用python websocket client直接链接ws长链接,然后直接send就可以了。
saltstack的event是什么?
我也不长篇大论了,他是saltstack提供的一个事件机制,咱们通过saltstack做的一些实例操作,在event可以体现出现出来。比如,调用调用test.ping 、 cmd.run 。
最明显的log是,创建任务,然后找jid的结果,没有的话,再查。
[root@devops-ruifengyun bj_lvs2 ]$ python event.py {'tag': '20140528090741247990', 'data': {'_stamp': '2014-05-28_09:07:41.248175', 'minions': ['10.150.145.51']}}------{'tag': 'new_job', 'data': {'tgt_type': 'glob', 'jid': '20140528090741247990', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:07:41.248224', 'user': 'root', 'arg': ['ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}------{'tag': 'salt/job/20140528090741247990/new', 'data': {'tgt_type': 'glob', 'jid': '20140528090741247990', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:07:41.248250', 'user': 'root', 'arg': ['ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}------{'tag': '20140528090741247990', 'data': {'fun_args': ['ip a'], 'jid': '20140528090741247990', 'return': '1: lo:mtu 16436 qdisc noqueue state UNKNOWN \n link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n inet 127.0.0.1/8 scope host lo\n inet6 ::1/128 scope host \n valid_lft forever preferred_lft forever\n2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000\n link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n inet 10.150.145.55/32 scope global eth0\n inet 10.150.145.100/32 scope global eth0\n inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:07:41.393589', 'fun': 'cmd.run', 'id': '10.150.145.51'}}------{'tag': 'salt/job/20140528090741247990/ret/10.150.145.51', 'data': {'fun_args': ['ip a'], 'jid': '20140528090741247990', 'return': '1: lo: mtu 16436 qdisc noqueue state UNKNOWN \n link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n inet 127.0.0.1/8 scope host lo\n inet6 ::1/128 scope host \n valid_lft forever preferred_lft forever\n2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000\n link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n inet 10.150.145.55/32 scope global eth0\n inet 10.150.145.100/32 scope global eth0\n inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:07:41.393740', 'fun': 'cmd.run', 'id': '10.150.145.51'}}------{'tag': '20140528090742530725', 'data': {'_stamp': '2014-05-28_09:07:42.530881', 'minions': ['10.150.145.51']}}------{'tag': 'new_job', 'data': {'tgt_type': 'glob', 'jid': '20140528090742530725', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:07:42.530922', 'user': 'root', 'arg': ['ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}------{'tag': 'salt/job/20140528090742530725/new', 'data': {'tgt_type': 'glob', 'jid': '20140528090742530725', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:07:42.530946', 'user': 'root', 'arg': ['ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}------{'tag': '20140528090742530725', 'data': {'fun_args': ['ip a'], 'jid': '20140528090742530725', 'return': '1: lo: mtu 16436 qdisc noqueue state UNKNOWN \n link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n inet 127.0.0.1/8 scope host lo\n inet6 ::1/128 scope host \n valid_lft forever preferred_lft forever\n2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000\n link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n inet 10.150.145.55/32 scope global eth0\n inet 10.150.145.100/32 scope global eth0\n inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:07:42.655764', 'fun': 'cmd.run', 'id': '10.150.145.51'}}------{'tag': 'salt/job/20140528090742530725/ret/10.150.145.51', 'data': {'fun_args': ['ip a'], 'jid': '20140528090742530725', 'return': '1: lo: mtu 16436 qdisc noqueue state UNKNOWN \n link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n inet 127.0.0.1/8 scope host lo\n inet6 ::1/128 scope host \n valid_lft forever preferred_lft forever\n2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000\n link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n inet 10.150.145.55/32 scope global eth0\n inet 10.150.145.100/32 scope global eth0\n inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:07:42.655909', 'fun': 'cmd.run', 'id': '10.150.145.51'}}------{'tag': '20140528090829833342', 'data': {'_stamp': '2014-05-28_09:08:29.833516', 'minions': ['10.150.145.51']}}------{'tag': 'new_job', 'data': {'tgt_type': 'glob', 'jid': '20140528090829833342', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:08:29.833566', 'user': 'root', 'arg': ['sleep 3;ip a'], 'fun': 'cmd.run', 'minions': ['10.150.145.51']}}------
在这里也可以看到他获取结果的思路:
创建一个任务及jid,然后每次去看看刚才(sleep 30;ip a ) 的结果,每隔五秒轮寻一次结果。每次去saltutil.find_job的jid是相同的,不同的是saltutil.find_job他本身的任务号。
原文:
------{'tag': 'new_job', 'data': {'tgt_type': 'glob', 'jid': '20140528092216402264', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:22:16.402889', 'user': 'root', 'arg': ['20140528092151158538', {'__kwarg__': True}], 'fun': 'saltutil.find_job', 'minions': ['10.150.145.51']}}------{'tag': 'salt/job/20140528092216402264/new', 'data': {'tgt_type': 'glob', 'jid': '20140528092216402264', 'tgt': '10.150.145.51', '_stamp': '2014-05-28_09:22:16.402975', 'user': 'root', 'arg': ['20140528092151158538', {'__kwarg__': True}], 'fun': 'saltutil.find_job', 'minions': ['10.150.145.51']}}------{'tag': '20140528092216402264', 'data': {'fun_args': ['20140528092151158538', {'__kwarg__': True}], 'jid': '20140528092216402264', 'return': {'tgt_type': 'glob', 'jid': '20140528092151158538', 'tgt': '10.150.145.51', 'pid': 30325, 'ret': '', 'user': 'root', 'arg': ['sleep 30;ip a'], 'fun': 'cmd.run'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:22:16.470647', 'fun': 'saltutil.find_job', 'id': '10.150.145.51'}}------{'tag': 'salt/job/20140528092216402264/ret/10.150.145.51', 'data': {'fun_args': ['20140528092151158538', {'__kwarg__': True}], 'jid': '20140528092216402264', 'return': {'tgt_type': 'glob', 'jid': '20140528092151158538', 'tgt': '10.150.145.51', 'pid': 30325, 'ret': '', 'user': 'root', 'arg': ['sleep 30;ip a'], 'fun': 'cmd.run'}, 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:22:16.470789', 'fun': 'saltutil.find_job', 'id': '10.150.145.51'}}------{'tag': '20140528092151158538', 'data': {'fun_args': ['sleep 30;ip a'], 'jid': '20140528092151158538', 'return': '1: lo:mtu 16436 qdisc noqueue state UNKNOWN \n link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n inet 127.0.0.1/8 scope host lo\n inet6 ::1/128 scope host \n valid_lft forever preferred_lft forever\n2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000\n link/ether 00:0c:29:fe:a4:a1 brd ff:ff:ff:ff:ff:ff\n inet 10.150.145.51/16 brd 10.150.255.255 scope global eth0\n inet 10.150.145.55/32 scope global eth0\n inet 10.150.145.100/32 scope global eth0\n inet6 fe80::20c:29ff:fefe:a4a1/64 scope link \n valid_lft forever preferred_lft forever', 'retcode': 0, 'success': True, 'cmd': '_return', '_stamp': '2014-05-28_09:22:21.281017', 'fun': 'cmd.run', 'id': '10.150.145.51'}}
对于websocket把结果打到前端,咱们就直接用websocket客户端搞~
websocket模块的地址在,
pip install
cd webso*
python setup.py install
from websocket import create_connectionws = create_connection("ws://localhost:8888/ws")print "Sending 'Hello, World'..."ws.send("Hello, World")print "Sent"print "Reeiving..."result = ws.recv()print "Received '%s'" % resultws.close()
原文:
event和websocket结合就可以了,event的结果有些乱,可以预先把任务的jid放到list里面,然后每次去遍历数据,send到前端。
这篇文章,写的有些泛泛,大家图个新鲜就好 ~
转载地址:http://bmlyx.baihongyu.com/