Events&Reactor系统 EVENT SYSTEM

The Salt Event System is used to fire off events enabling third party applications or external processes to react to behavior within Salt. The event system uses a publish-subscribe pattern, otherwise know as pub/sub.

Salt事件系统用于触发第三方应用或外部过程对Salt内行为做出反应的事件。事件系统使用发布订阅模式,否则称为PUB/SUB。

event bus

The event system is comprised of a two primary components, which make up the concept of an Event Bus:

事件系统由两个主要组成部分组成,它们构成事件总线的概念:

  • The event sockets which publishes events. 发布事件的事件套接字。
  • The event library which can listen to events and send events into the salt system. 事件库,它可以侦听事件并将事件发送到salt系统

Events are published onto the event bus and event bus subscribers listen for the published events.

事件发布到事件总线上,事件总线订阅服务器侦听已发布事件。

The event bus is used for both inter-process communication as well as network transport in Salt. Inter-process communication is provided through UNIX domain sockets (UDX).

事件总线既用于进程间通信,又用于网络传输。进程间通信是通过UNIX域套接字(UDX)提供的。

The Salt Master and each Salt Minion has their own event bus.

Salt Master和每个Salt Minion都有他们自己的事件bus。

event types

Salt Master Events

listening for events

Salt's event system is used heavily within Salt and it is also written to integrate heavily with existing tooling and scripts. There is a variety of ways to consume it.

Salt的事件系统在Salt中大量使用,它也被写为与现有的工具和脚本紧密结合。有各种各样的方式来消费它。

from the cli

The quickest way to watch the event bus is by calling the state.event runner:

查看事件总线的最快方法是调用 state.event runner:

salt-run state.event pretty=True

That runner is designed to interact with the event bus from external tools and shell scripts. See the documentation for more examples.

该转发器被设计为与外部总线和外壳脚本交互的事件总线。有关更多示例,请参阅文档。

remotely via the rest api 通过REST API远程访问

Salt's event bus can be consumed salt.netapi.rest_cherrypy.app.Events as an HTTP stream from external tools or services.

可以将Salt事件总线作为来自外部工具或服务的HTTP流来消耗salt.netapi.rest_cherrypy.app.Events

curl -SsNk https://salt-api.example.com:8000/events?token=05A3

from python

Python scripts can access the event bus only as the same system user that Salt is running as.

Python脚本只能像salt一样运行的系统用户访问事件总线。

The event system is accessed via the event library and can only be accessed by the same system user that Salt is running as. To listen to events a SaltEvent object needs to be created and then the get_event function needs to be run. The SaltEvent object needs to know the location that the Salt Unix sockets are kept. In the configuration this is the sock_dir option. The sock_dir option defaults to "/var/run/salt/master" on most systems.

事件系统是通过事件库访问的,并且只能由同一系统用户访问,即salt正在运行。要侦听事件,需要创建一个saltevent对象,然后需要运行getyengenter函数。saltever对象需要知道salt unix插座的位置。在配置中,这是sokkddir选项。sokkydir选项默认为大多数系统上的“/var/run/salt/master”。

The following code will check for a single event:

下面的代码将检查单个事件:

import salt.config
import salt.utils.event opts = salt.config.client_config('/etc/salt/master') event = salt.utils.event.get_event(
'master',
sock_dir=opts['sock_dir'],
transport=opts['transport'],
opts=opts) data = event.get_event()

Events will also use a "tag". Tags allow for events to be filtered by prefix. By default all events will be returned. If only authentication events are desired, then pass the tag "salt/auth".

事件也会使用“标签”。标签允许事件按照前缀过滤。默认情况下,所有的事件将被返回。如果只需要认证事件,然后传“salt/认证”事件。

The get_event method has a default poll time assigned of 5 seconds. To change this time set the "wait" option.

get_event方法的默认轮询时间分配为5秒。 要更改此时间,请设置“wait”选项。

The following example will only listen for auth events and will wait for 10 seconds instead of the default 5.

以下示例只会侦听auth事件,并会等待10秒而不是默认值5。

data = event.get_event(wait=10, tag='salt/auth')

To retrieve the tag as well as the event data, pass full=True:

要检索标签以及事件数据,请传递full = True:

evdata = event.get_event(wait=10, tag='salt/job', full=True)
tag, data = evdata['tag'], evdata['data']

Instead of looking for a single event, the iter_events method can be used to make a generator which will continually yield salt events.

iter_events方法可以用来生成一个不断产生salt事件的生成器,而不是寻找一个单个事件。

The iter_events method also accepts a tag but not a wait time:

iter_events方法也接受一个标签,但不是等待时间:

for data in event.iter_events(tag='salt/auth'):
print(data)

And finally event tags can be globbed, such as they can be in the Reactor, using the fnmatch library.

最后,事件标记可以使用fnmatch库进行globbed,例如它们可以在Reactor中。

 import fnmatch

 import salt.config
import salt.utils.event opts = salt.config.client_config('/etc/salt/master') sevent = salt.utils.event.get_event(
'master',
sock_dir=opts['sock_dir'],
transport=opts['transport'],
opts=opts) while True:
ret = sevent.get_event(full=True)
if ret is None:
continue if fnmatch.fnmatch(ret['tag'], 'salt/job/*/ret/*'):
do_something_with_job_return(ret['data'])

firing events

It is possible to fire events on either the minion's local bus or to fire events intended for the master.

可以在minion发射事件给local bus或者发送事件给master。

To fire a local event from the minion on the command line call the event.fire execution function:

在minion端的命令行上发射一个本地事件,请调用event.fire函数。

salt-call event.fire '{"data": "message to be sent in the event"}' 'tag'

To fire an event to be sent up to the master from the minion call the event.send execution function. Remember YAML can be used at the CLI in function arguments:

从minion发送一个事件发送给master,调用event.send执行函数。 记住YAML可以在函数参数的CLI中使用:

salt-call event.send 'myco/mytag/success' '{success: True, message: "It works!"}'

If a process is listening on the minion, it may be useful for a user on the master to fire an event to it:

如果minion上某个进程正在侦听,这有助于,master服务器上的用户能向其发送事件:

 # Job on minion
import salt.utils.event event = salt.utils.event.MinionEvent(**__opts__) for evdata in event.iter_events(tag='customtag/'):
return evdata # do your processing here...
salt minionname event.fire '{"data": "message for the minion"}' 'customtag/african/unladen'

firing events from python

from salt execution modules salt执行模块

Events can be very useful when writing execution modules, in order to inform various processes on the master when a certain task has taken place. This is easily done using the normal cross-calling syntax:

在编写执行模块时,事件可能非常有用,以便在发生特定任务时通知主服务器上的各种进程。 这很容易使用正常的交叉调用语法完成:

 # /srv/salt/_modules/my_custom_module.py

 def do_something():
'''
Do something and fire an event to the master when finished CLI Example:: salt '*' my_custom_module:do_something
'''
# do something!
__salt__['event.send']('myco/my_custom_module/finished', {
'finished': True,
'message': "The something is finished!",
})

from custom python scripts

Firing events from custom Python code is quite simple and mirrors how it is done at the CLI:

从自定义Python代码触发事件非常简单,并反映了在CLI中完成事件的方式:

 import salt.client

 caller = salt.client.Caller()

 caller.sminion.functions['event.send'](
'myco/myevent/success',
{
'success': True,
'message': "It works!",
}
)

【SaltStack官方版】—— Events&Reactor系统—EVENT SYSTEM的更多相关文章

  1. 【SaltStack官方版】—— Events&Reactor系统—BEACONS

    Events&Reactor系统—BEACONS Beacons let you use the Salt event system to monitor non-Salt processes ...

  2. 【SaltStack官方版】—— EVENTS & REACTOR指南

    EVENTS & REACTOR Event System Event Bus Event types Salt Master Events Authentication events Sta ...

  3. 【SaltStack官方版】—— STORING JOB RESULTS IN AN EXTERNAL SYSTEM

    STORING JOB RESULTS IN AN EXTERNAL SYSTEM After a job executes, job results are returned to the Salt ...

  4. 【SaltStack官方版】—— returners——返回器

    ETURNERS 返回器 By default the return values of the commands sent to the Salt minions are returned to t ...

  5. 【SaltStack官方版】—— MANAGING THE JOB CACHE

    MANAGING THE JOB CACHE The Salt Master maintains a job cache of all job executions which can be quer ...

  6. 【SaltStack官方版】—— job management

    JOB MANAGEMENT New in version 0.9.7. Since Salt executes jobs running on many systems, Salt needs to ...

  7. 【SaltStack官方版】—— states教程, part 3 - 定义,包括,延伸

    STATES TUTORIAL, PART 3 - TEMPLATING, INCLUDES, EXTENDS 本教程建立在第1部分和第2部分涵盖的主题上.建议您从此开始.这章教程我们将讨论更多  s ...

  8. 【SaltStack官方版】—— states教程, part 4 - states 说明

    STATES TUTORIAL, PART 4 本教程建立在第1部分.第2部分.第3部分涵盖的主题上.建议您从此开始.这章教程我们将讨论更多 sls 文件的扩展模板和配置技巧. This part o ...

  9. 【SaltStack官方版】—— states教程, part 2 - 更复杂的states和必要的事物

    states tutorial, part 2 - more complex states, requisites 本教程建立在第1部分涵盖的主题上.建议您从此处开始. 在Salt States教程的 ...

随机推荐

  1. 2019 ccpc 秦皇岛

    D 如果1/n是有限小数,不停乘以10,一定在有限次之后成为一个整数. 10的质因子只有2和5,只要保证分母的质因子只有2和5即可 #include <iostream> #include ...

  2. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_5 Mybatis的CRUD-查询返回一行一列和占位符分析

    聚合函数 模糊查询的另外一种写法 如果用户这种方式里面的value是固定的 因为在源码分析中,绑定的就是固定的value值 所以这里传参数的 没必要在用百分号了 删掉后 xml里面应该用这种方式来注释 ...

  3. Linux几个命令的升级替代品

    grep => ack, agack和ag是两个文本搜索工具,比自带的grep要好用得多.在指定目录下搜索文本时,它们不需要像grep那样指定各种命令行选项,输出结果也会包含文件名和行号,并且会 ...

  4. 【ABAP系列】SAP 系统的消息类型分析 MESSAGE TYPE

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 系统的消息类型分析 ME ...

  5. 【Linux开发】【Qt开发】交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别

    一. 什么是ABI和EABI1) ABI: 二进制应用程序接口(Application Binary Interface (ABI) for the ARM Architecture)在计算机中,应用 ...

  6. TS学习笔记----(一)基础类型

    布尔值: boolean let isDone: boolean = false; 数字: number 和JavaScript一样,TS里的所有数字都是浮点数. 支持十进制和十六进制字面量,TS还支 ...

  7. CentOS7配置NFS网络文件系统

    NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件.通过使用N ...

  8. 【WPS/Visio】WPS word无法复制或编辑Visio对象

    前言 Win10,WPS2019,Visio2016. 好像是有一次设置 .vsdx 的默认打开方式为Visio,之后每次在WPS里复制Visio对象,或双击编辑WPS word中以前的Visio对象 ...

  9. 快速查看php文档技巧

    在php源码中看到注释中的相关链接后 Ctrl+鼠标,浏览器打开 将输入栏的“en”改为“zh”即可变为中文文档,其他语言类推

  10. 基于 Redux + Redux Persist 进行状态管理的 Flutter 应用示例

    好久没在 SegmentFault 写东西,唉,也不知道 是忙还是懒,以后有时间 再慢慢写起来吧,最近开始学点新东西,有的写了,个人博客跟这里同步. 一直都在自己的 React Native 应用中使 ...