/
event.py
380 lines (343 loc) · 12.3 KB
/
event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
'''
Manage events
'''
# Events are all fired off via a zeromq pub socket, and listened to with
# local subscribers. The event messages are comprised of two parts delimited
# at the 20 char point. The first 20 characters are used for the zeromq
# subscriber to match publications and 20 characters were chosen because it is
# a few more characters than the length of a jid. The 20 characters
# are padded with "|" chars so that the msgpack component can be predictably
# extracted. All of the formatting is self contained in the event module, so
# we should be able to modify the structure in the future since the same module
# to read is the same module to fire off events.
# Import python libs
import os
import fnmatch
import glob
import hashlib
import errno
import logging
import multiprocessing
from multiprocessing import Process
# Import third party libs
import zmq
# Import salt libs
import salt.payload
import salt.loader
import salt.state
from salt._compat import string_types
log = logging.getLogger(__name__)
class SaltEvent(object):
'''
The base class used to manage salt events
'''
def __init__(self, node, sock_dir=None, **kwargs):
self.serial = salt.payload.Serial({'serial': 'msgpack'})
self.context = zmq.Context()
self.poller = zmq.Poller()
self.cpub = False
self.cpush = False
self.puburi, self.pulluri = self.__load_uri(sock_dir, node, **kwargs)
def __load_uri(self, sock_dir, node, **kwargs):
'''
Return the string uri for the location of the pull and pub sockets to
use for firing and listening to events
'''
id_hash = hashlib.md5(kwargs.get('id', '')).hexdigest()
if node == 'master':
puburi = 'ipc://{0}'.format(os.path.join(
sock_dir,
'master_event_pub.ipc'
))
pulluri = 'ipc://{0}'.format(os.path.join(
sock_dir,
'master_event_pull.ipc'
))
else:
if kwargs.get('ipc_mode', '') == 'tcp':
puburi = 'tcp://127.0.0.1:{0}'.format(
kwargs.get('tcp_pub_port', 4510)
)
pulluri = 'tcp://127.0.0.1:{0}'.format(
kwargs.get('tcp_pull_port', 4511)
)
else:
puburi = 'ipc://{0}'.format(os.path.join(
sock_dir,
'minion_event_{0}_pub.ipc'.format(id_hash)
))
pulluri = 'ipc://{0}'.format(os.path.join(
sock_dir,
'minion_event_{0}_pull.ipc'.format(id_hash)
))
log.debug(
'{0} PUB socket URI: {1}'.format(self.__class__.__name__, puburi)
)
log.debug(
'{0} PULL socket URI: {1}'.format(self.__class__.__name__, pulluri)
)
return puburi, pulluri
def connect_pub(self):
'''
Establish the publish connection
'''
self.sub = self.context.socket(zmq.SUB)
self.sub.connect(self.puburi)
self.poller.register(self.sub, zmq.POLLIN)
self.cpub = True
def connect_pull(self):
'''
Establish a connection with the event pull socket
'''
self.push = self.context.socket(zmq.PUSH)
self.push.connect(self.pulluri)
self.cpush = True
def get_event(self, wait=5, tag='', full=False):
'''
Get a single publication
'''
wait = wait * 1000
if not self.cpub:
self.connect_pub()
self.sub.setsockopt(zmq.SUBSCRIBE, tag)
while True:
socks = dict(self.poller.poll(wait))
if self.sub in socks and socks[self.sub] == zmq.POLLIN:
raw = self.sub.recv()
data = self.serial.loads(raw[20:])
if full:
ret = {'data': data,
'tag': raw[:20].rstrip('|')}
return ret
return data
else:
return None
def iter_events(self, tag='', full=False):
'''
Creates a generator that continuously listens for events
'''
try:
while True:
data = self.get_event(tag=tag, full=full)
if data is None:
continue
yield data
finally:
self.destroy()
def fire_event(self, data, tag=''):
'''
Send a single event into the publisher
'''
if not self.cpush:
self.connect_pull()
tag = '{0:|<20}'.format(tag)
event = '{0}{1}'.format(tag, self.serial.dumps(data))
self.push.send(event)
return True
def destroy(self):
if self.cpub:
# Wait at most 2.5 secs to send any remaining messages in the
# socket or the context.term() bellow will hang indefinitely.
# See https://github.com/zeromq/pyzmq/issues/102
self.sub.setsockopt(zmq.LINGER, 2500)
self.sub.close()
if self.cpush:
self.push.setsockopt(zmq.LINGER, 2500)
self.push.close()
# If socket's are not unregistered from a poller, nothing which touches
# that poller get's garbage collected. The Poller itself, it's
# registered sockets and the Context
for socket in self.poller.sockets.keys():
if not socket.closed:
# Should already be closed from above, but....
socket.setsockopt(zmq.LINGER, 2500)
socket.close()
self.poller.unregister(socket)
self.context.term()
def __del__(self):
self.destroy()
class MasterEvent(SaltEvent):
'''
Create a master event management object
'''
def __init__(self, sock_dir):
super(MasterEvent, self).__init__('master', sock_dir)
self.connect_pub()
class MinionEvent(SaltEvent):
'''
Create a master event management object
'''
def __init__(self, **kwargs):
super(MinionEvent, self).__init__('minion', **kwargs)
class EventPublisher(Process):
'''
The interface that takes master events and republishes them out to anyone
who wants to listen
'''
def __init__(self, opts):
super(EventPublisher, self).__init__()
self.opts = opts
def run(self):
'''
Bind the pub and pull sockets for events
'''
# Set up the context
self.context = zmq.Context(1)
# Prepare the master event publisher
self.epub_sock = self.context.socket(zmq.PUB)
epub_uri = 'ipc://{0}'.format(
os.path.join(self.opts['sock_dir'], 'master_event_pub.ipc')
)
# Prepare master event pull socket
self.epull_sock = self.context.socket(zmq.PULL)
epull_uri = 'ipc://{0}'.format(
os.path.join(self.opts['sock_dir'], 'master_event_pull.ipc')
)
# Start the master event publisher
self.epub_sock.bind(epub_uri)
self.epull_sock.bind(epull_uri)
# Restrict access to the sockets
pub_mode = 448
if self.opts.get('client_acl') or self.opts.get('external_auth'):
pub_mode = 511
os.chmod(
os.path.join(self.opts['sock_dir'],
'master_event_pub.ipc'),
pub_mode
)
os.chmod(
os.path.join(self.opts['sock_dir'],
'master_event_pull.ipc'),
448
)
try:
while True:
# Catch and handle EINTR from when this process is sent
# SIGUSR1 gracefully so we don't choke and die horribly
try:
package = self.epull_sock.recv()
self.epub_sock.send(package)
except zmq.ZMQError as exc:
if exc.errno == errno.EINTR:
continue
raise exc
except KeyboardInterrupt:
# Wait at most 2.5 secs to send any remaining messages in the
# socket or the context.term() bellow will hang indefinitely.
# See https://github.com/zeromq/pyzmq/issues/102
self.epub_sock.setsockopt(zmq.LINGER, 2500)
self.epub_sock.close()
self.epull_sock.setsockopt(zmq.LINGER, 2500)
self.epull_sock.close()
finally:
self.context.term()
class Reactor(multiprocessing.Process, salt.state.Compiler):
'''
Read in the reactor configuration variable and compare it to events
processed on the master.
The reactor has the capability to execute pre-programmed executions
as reactions to events
'''
def __init__(self, opts):
multiprocessing.Process.__init__(self)
salt.state.Compiler.__init__(self, opts)
self.event = SaltEvent('master', self.opts['sock_dir'])
self.wrap = ReactWrap(self.opts)
def render_reaction(self, glob_ref, tag, data):
'''
Execute the render system against a single reaction file and return
the data structure
'''
react = {}
for fn_ in glob.glob(glob_ref):
react.update(self.render_template(
fn_,
tag=tag,
data=data))
return react
def list_reactors(self, tag):
'''
Take in the tag from an event and return a list of the reactors to
process
'''
log.debug('Gathering rections for tag {0}'.format(tag))
reactors = []
for ropt in self.opts['reactor']:
if not isinstance(ropt, dict):
continue
if not len(ropt) == 1:
continue
key = ropt.keys()[0]
val = ropt[key]
if fnmatch.fnmatch(tag, key):
if isinstance(val, string_types):
reactors.append(val)
elif isinstance(val, list):
reactors.extend(val)
return reactors
def reactions(self, tag, data, reactors):
'''
Render a list of reactor files and returns a reaction struct
'''
log.debug('Compiling reactions for tag {0}'.format(tag))
high = {}
chunks = []
for fn_ in reactors:
high.update(self.render_reaction(fn_, tag, data))
if high:
errors = self.verify_high(high)
if errors:
return errors
chunks = self.order_chunks(self.compile_high_data(high))
return chunks
def call_reactions(self, chunks):
'''
Execute the reaction state
'''
for chunk in chunks:
self.wrap.run(chunk)
def run(self):
'''
Enter into the server loop
'''
for data in self.event.iter_events(full=True):
reactors = self.list_reactors(data['tag'])
if not reactors:
continue
chunks = self.reactions(data['tag'], data['data'], reactors)
if chunks:
self.call_reactions(chunks)
class ReactWrap(object):
'''
Create a wrapper that executes low data for the reaction system
'''
def __init__(self, opts):
self.opts = opts
def run(self, low):
'''
Execute the specified function in the specified state by passing the
LowData
'''
l_fun = getattr(self, low['state'])
f_call = salt.utils.format_call(l_fun, low)
ret = l_fun(*f_call.get('args', ()), **f_call.get('kwargs', {}))
return ret
def cmd(self, *args, **kwargs):
'''
Wrap LocalClient for running :ref:`execution modules <all-salt.modules>`
'''
local = salt.client.LocalClient(self.opts['conf_file'])
return local.cmd_async(*args, **kwargs)
def runner(self, fun, **kwargs):
'''
Wrap RunnerClient for executing :ref:`runner modules <all-salt.runners>`
'''
runner = salt.runner.RunnerClient(self.opts)
return runner.low(fun, kwargs)
def wheel(self, fun, **kwargs):
'''
Wrap Wheel to enable executing :ref:`wheel modules <all-salt.wheel>`
'''
kwargs['fun'] = fun
wheel = salt.wheel.Wheel(self.opts)
return wheel.master_call(**kwargs)