Repository navigation
Expand file tree
/
Copy pathevent.py
More file actions
711 lines (626 loc) · 24.5 KB
/
Copy pathevent.py
File metadata and controls
711 lines (626 loc) · 24.5 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# -*- coding: utf-8 -*-
'''
Manage events
Events are all fired off via a zeromq 'pub' socket, and listened to with
local zeromq 'sub' sockets
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
used to read events is the same module used to fire off events.
Old style event messages were 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 was chosen because it was at
the time a few more characters than the length of a jid (Job ID).
Any tags of length less than 20 characters were padded with "|" chars out to 20 characters.
Although not explicit, the data for an event comprised a python dict that was serialized by
msgpack.
New style event messages support event tags longer than 20 characters while still
being backwards compatible with old style tags.
The longer tags better enable name spaced event tags which tend to be longer.
Moreover, the constraint that the event data be a python dict is now an explicit
constraint and fire-event will now raise a ValueError if not. Tags must be
ascii safe strings, that is, have values less than 0x80
Since the msgpack dict (map) indicators have values greater than or equal to 0x80
it can be unambiguously determined if the start of data is at char 21 or not.
In the new style:
When the tag is longer than 20 characters, an end of tag string
is appended to the tag given by the string constant TAGEND, that is, two line feeds '\n\n'.
When the tag is less than 20 characters then the tag is padded with pipes
"|" out to 20 characters as before.
When the tag is exactly 20 characters no padded is done.
The get_event method intelligently figures out if the tag is longer than 20 characters.
The convention for namespacing is to use dot characters "." as the name space delimeter.
The name space "salt" is reserved by SaltStack for internal events.
For example:
Namspaced tag
'salt.runner.manage.status.start'
'''
# Import python libs
import os
import fnmatch
import glob
import hashlib
import errno
import logging
import time
import datetime
import multiprocessing
from multiprocessing import Process
from collections import MutableMapping
# Import third party libs
try:
import zmq
except ImportError:
# Local mode does not need zmq
pass
import yaml
# Import salt libs
import salt.payload
import salt.loader
import salt.state
import salt.utils
from salt._compat import string_types
log = logging.getLogger(__name__)
# The SUB_EVENT set is for functions that require events fired based on
# component executions, like the state system
SUB_EVENT = set([
'state.highstate',
'state.sls',
])
TAGEND = '\n\n' # long tag delimeter
TAGPARTER = '/' # name spaced tag delimeter
SALT = 'salt' # base prefix for all salt/ events
# dict map of namespaced base tag prefixes for salt events
TAGS = {
'auth': 'auth', # prefix for all salt/auth events
'job': 'job', # prefix for all salt/job events (minion jobs)
'key': 'key', # prefix for all salt/key events
'minion': 'minion', # prefix for all salt/minion events (minion sourced events)
'syndic': 'syndic', # prefix for all salt/syndic events (syndic minion sourced events)
'run': 'run', # prefix for all salt/run events (salt runners)
'wheel': 'wheel', # prefix for all salt/wheel events
'cloud': 'cloud', # prefix for all salt/cloud events
'fileserver': 'fileserver', # prefix for all salt/fileserver events
}
def tagify(suffix='', prefix='', base=SALT):
'''
convenience function to build a namespaced event tag string
from joinning with the TABPART character the base, prefix and suffix
If string prefix is a valid key in TAGS Then use the value of key prefix
Else use prefix string
If suffix is a list Then join all string elements of suffix individually
Else use string suffix
'''
parts = [base, TAGS.get(prefix, prefix)]
if hasattr(suffix, 'append'): # list so extend parts
parts.extend(suffix)
else: # string so append
parts.append(suffix)
return TAGPARTER.join([part for part in parts if part])
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)
self.pending_events = []
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
'''
hash_type = getattr(hashlib, kwargs.get('hash_type', 'md5'))
# Substr the first 10 chars off, because some algorithms produce
# longer hashes than others, and may exceed the IPC maximum length
# for UNIX sockets.
id_hash = hash_type(kwargs.get('id', '')).hexdigest()
if kwargs.get('hash_type', 'md5') == 'sha256':
id_hash = id_hash[:10]
if node == 'master':
puburi = 'ipc://{0}'.format(os.path.join(
sock_dir,
'master_event_pub.ipc'
))
salt.utils.check_ipc_path_max_len(puburi)
pulluri = 'ipc://{0}'.format(os.path.join(
sock_dir,
'master_event_pull.ipc'
))
salt.utils.check_ipc_path_max_len(pulluri)
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)
))
salt.utils.check_ipc_path_max_len(puburi)
pulluri = 'ipc://{0}'.format(os.path.join(
sock_dir,
'minion_event_{0}_pull.ipc'.format(id_hash)
))
salt.utils.check_ipc_path_max_len(pulluri)
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 subscribe(self, tag=None):
'''
Subscribe to events matching the passed tag.
'''
if not self.cpub:
self.connect_pub()
def unsubscribe(self, tag=None):
'''
Un-subscribe to events matching the passed tag.
'''
return
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.sub.setsockopt(zmq.SUBSCRIBE, '')
self.cpub = True
def connect_pull(self, timeout=1000):
'''
Establish a connection with the event pull socket
Set the send timeout of the socket options to timeout (in milliseconds)
Default timeout is 1000 ms
The linger timeout must be at least as long as this timeout
'''
self.push = self.context.socket(zmq.PUSH)
try:
# bug in 0MQ default send timeout of -1 (inifinite) is not infinite
self.push.setsockopt(zmq.SNDTIMEO, timeout)
except AttributeError:
# This is for ZMQ < 2.2 (Caught when ssh'ing into the Jenkins
# CentOS5, which still uses 2.1.9)
pass
self.push.connect(self.pulluri)
self.cpush = True
@classmethod
def unpack(cls, raw, serial=None):
if serial is None:
serial = salt.payload.Serial({'serial': 'msgpack'})
if ord(raw[20]) >= 0x80: # old style
mtag = raw[0:20].rstrip('|')
mdata = raw[20:]
else: # new style
mtag, sep, mdata = raw.partition(TAGEND) # split tag from data
data = serial.loads(mdata)
return mtag, data
def get_event(self, wait=5, tag='', full=False, use_pending=False):
'''
Get a single publication.
IF no publication available THEN block for upto wait seconds
AND either return publication OR None IF no publication available.
IF wait is 0 then block forever.
use_pending
Defines whether to keep all unconsumed events in a pending_events
list, or to discard events that don't match the requested tag. If
set to True, MAY CAUSE MEMORY LEAKS.
'''
self.subscribe()
if use_pending:
for evt in [x for x in self.pending_events
if x['tag'].startswith(tag)]:
self.pending_events.remove(evt)
if full:
return evt
else:
return evt['data']
start = time.time()
timeout_at = start + wait
while not wait or time.time() <= timeout_at:
socks = dict(self.poller.poll(wait * 1000)) # convert to milliseconds
if self.sub in socks and socks[self.sub] == zmq.POLLIN:
raw = self.sub.recv()
else:
continue
mtag, data = self.unpack(raw, self.serial)
ret = {'data': data,
'tag': mtag}
if not mtag.startswith(tag): # tag not match
if use_pending:
self.pending_events.append(ret)
wait = timeout_at - time.time()
continue
if full:
return ret
return data
return None
def iter_events(self, tag='', full=False):
'''
Creates a generator that continuously listens for events
'''
while True:
data = self.get_event(tag=tag, full=full)
if data is None:
continue
yield data
def fire_event(self, data, tag, timeout=1000):
'''
Send a single event into the publisher with paylod dict "data" and event
identifier "tag"
Supports new style long tags.
The 0MQ push timeout on the send is set to timeout in milliseconds
The default is 1000 ms
Note the linger timeout must be at least as long as this timeout
'''
if not str(tag): # no empty tags allowed
raise ValueError('Empty tag.')
if not isinstance(data, MutableMapping): # data must be dict
raise ValueError('Dict object expected, not "{0!r}".'.format(data))
if not self.cpush:
self.connect_pull(timeout=timeout)
data['_stamp'] = datetime.datetime.now().isoformat()
tagend = ''
if len(tag) <= 20: # old style compatible tag
tag = '{0:|<20}'.format(tag) # pad with pipes '|' to 20 character length
else: # new style longer than 20 chars
tagend = TAGEND
event = '{0}{1}{2}'.format(tag, tagend, self.serial.dumps(data))
try:
self.push.send(event)
except Exception as ex:
log.debug(ex)
raise
return True
def destroy(self, linger=5000):
if self.cpub is True and self.sub.closed is False:
# 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, linger)
self.sub.close()
if self.cpush is True and self.push.closed is False:
self.push.setsockopt(zmq.LINGER, linger)
self.push.close()
# If sockets are not unregistered from a poller, nothing which touches
# that poller gets garbage collected. The Poller itself, its
# registered sockets and the Context
if isinstance(self.poller.sockets, dict):
for socket in self.poller.sockets.keys():
if socket.closed is False:
socket.setsockopt(zmq.LINGER, linger)
socket.close()
self.poller.unregister(socket)
else:
for socket in self.poller.sockets:
if socket[0].closed is False:
socket[0].setsockopt(zmq.LINGER, linger)
socket[0].close()
self.poller.unregister(socket[0])
if self.context.closed is False:
self.context.term()
# Hardcore destruction
if hasattr(self.context, 'destroy'):
self.context.destroy(linger=1)
# https://github.com/zeromq/pyzmq/issues/173#issuecomment-4037083
# Assertion failed: get_load () == 0 (poller_base.cpp:32)
time.sleep(0.025)
def fire_ret_load(self, load):
'''
Fire events based on information in the return load
'''
if load.get('retcode') and load.get('fun'):
# Minion fired a bad retcode, fire an event
if load['fun'] in SUB_EVENT:
try:
for tag, data in load.get('return', {}).items():
data['retcode'] = load['retcode']
tags = tag.split('_|-')
if data.get('result') is False:
self.fire_event(
data,
'{0}.{1}'.format(tags[0], tags[-1])) # old dup event
data['jid'] = load['jid']
data['id'] = load['id']
data['success'] = False
data['return'] = 'Error: {0}.{1}'.format(tags[0], tags[-1])
data['fun'] = load['fun']
data['user'] = load['user']
self.fire_event(
data,
tagify([load['jid'],
'sub',
load['id'],
'error',
load['fun']],
'job'))
except Exception:
pass
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 LocalClientEvent(MasterEvent):
'''
This class is just used to differentiate who is handling the events,
specially on logs, but it's the same as MasterEvent.
'''
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
'''
linger = 5000
# 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')
)
salt.utils.check_ipc_path_max_len(epub_uri)
# 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')
)
salt.utils.check_ipc_path_max_len(epull_uri)
# Start the master event publisher
old_umask = os.umask(0177)
try:
self.epull_sock.bind(epull_uri)
self.epub_sock.bind(epub_uri)
if self.opts.get('client_acl') or self.opts.get('external_auth'):
os.chmod(
os.path.join(self.opts['sock_dir'],
'master_event_pub.ipc'),
0666
)
finally:
os.umask(old_umask)
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:
if self.epub_sock.closed is False:
self.epub_sock.setsockopt(zmq.LINGER, linger)
self.epub_sock.close()
if self.epull_sock.closed is False:
self.epull_sock.setsockopt(zmq.LINGER, linger)
self.epull_sock.close()
if self.context.closed is False:
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.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):
try:
react.update(self.render_template(
fn_,
tag=tag,
data=data))
except Exception:
log.error('Failed to render "{0}"'.format(fn_))
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 reactors for tag {0}'.format(tag))
reactors = []
if isinstance(self.opts['reactor'], basestring):
try:
with salt.utils.fopen(self.opts['reactor']) as fp_:
react_map = yaml.safe_load(fp_.read())
except (OSError, IOError):
log.error(
'Failed to read reactor map: "{0}"'.format(
self.opts['reactor']
)
)
except Exception:
log.error(
'Failed to parse YAML in reactor map: "{0}"'.format(
self.opts['reactor']
)
)
else:
react_map = self.opts['reactor']
for ropt in react_map:
if not isinstance(ropt, dict):
continue
if 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
'''
self.event = SaltEvent('master', self.opts['sock_dir'])
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)
try:
ret = l_fun(*f_call.get('args', ()), **f_call.get('kwargs', {}))
except Exception:
log.error(
'Failed to execute {0}: {1}\n'.format(low['state'], l_fun),
exc_info=True
)
return False
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>`
'''
wheel = salt.wheel.Wheel(self.opts)
return wheel.call_func(fun, **kwargs)
class StateFire(object):
'''
Evaluate the data from a state run and fire events on the master and minion
for each returned chunk that is not "green"
This object is made to only run on a minion
'''
def __init__(self, opts, auth=None):
self.opts = opts
self.event = SaltEvent(opts, 'minion')
if not auth:
self.auth = salt.crypt.SAuth(self.opts)
else:
self.auth = auth
def fire_master(self, data, tag, preload=None):
'''
Fire an event off on the master server
CLI Example::
salt '*' event.fire_master 'stuff to be in the event' 'tag'
'''
load = {}
if preload:
load.update(preload)
load.update({'id': self.opts['id'],
'tag': tag,
'data': data,
'cmd': '_minion_event',
'tok': self.auth.gen_token('salt')})
sreq = salt.transport.Channel.factory(self.opts)
try:
sreq.send(load)
except Exception:
pass
return True
def fire_running(self, running):
'''
Pass in a state "running" dict, this is the return dict from a state
call. The dict will be processed and fire events.
By default yellows and reds fire events on the master and minion, but
this can be configured.
'''
load = {'id': self.opts['id'],
'events': [],
'cmd': '_minion_event'}
for stag in sorted(
running,
key=lambda k: running[k].get('__run_num__', 0)):
if running[stag]['result'] and not running[stag]['changes']:
continue
tag = 'state_{0}_{1}'.format(
str(running[stag]['result']),
'True' if running[stag]['changes'] else 'False')
load['events'].append(
{'tag': tag,
'data': running[stag]}
)
sreq = salt.transport.Channel.factory(self.opts)
try:
sreq.send(load)
except Exception:
pass
return True