-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathsaltutil.py
More file actions
619 lines (501 loc) · 16.3 KB
/
Copy pathsaltutil.py
File metadata and controls
619 lines (501 loc) · 16.3 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
# -*- coding: utf-8 -*-
'''
The Saltutil module is used to manage the state of the salt minion itself. It is used to manage minion modules as well as automate updates to the salt minion.
:depends: - esky Python module for update functionality
'''
# Import python libs
import os
import hashlib
import shutil
import signal
import logging
import fnmatch
import time
import sys
import copy
import threading
# Import salt libs
import salt.payload
import salt.state
import salt.client
import salt.utils
import salt.utils.process
import salt.transport
from salt.exceptions import SaltReqTimeoutError
from salt._compat import string_types
__proxyenabled__ = ['*']
# Import third party libs
try:
import esky
HAS_ESKY = True
except ImportError:
HAS_ESKY = False
log = logging.getLogger(__name__)
def _sync(form, saltenv=None):
'''
Sync the given directory in the given environment
'''
if saltenv is None:
# No environment passed, detect them based on gathering the top files
# from the master
st_ = salt.state.HighState(__opts__)
top = st_.get_top()
if top:
saltenv = st_.top_matches(top).keys()
if not saltenv:
saltenv = 'base'
if isinstance(saltenv, string_types):
saltenv = saltenv.split(',')
ret = []
remote = set()
source = os.path.join('salt://_{0}'.format(form))
mod_dir = os.path.join(__opts__['extension_modules'], '{0}'.format(form))
if not os.path.isdir(mod_dir):
log.info('Creating module dir {0!r}'.format(mod_dir))
os.makedirs(mod_dir)
for sub_env in saltenv:
log.info('Syncing {0} for environment {1!r}'.format(form, sub_env))
cache = []
log.info('Loading cache from {0}, for {1})'.format(source, sub_env))
cache.extend(__salt__['cp.cache_dir'](source, sub_env))
local_cache_dir = os.path.join(
__opts__['cachedir'],
'files',
sub_env,
'_{0}'.format(form)
)
log.debug('Local cache dir: {0!r}'.format(local_cache_dir))
for fn_ in cache:
if __opts__.get('file_client', '') == 'local':
for fn_root in __opts__['file_roots'].get(sub_env, []):
if fn_.startswith(fn_root):
relpath = os.path.relpath(fn_, fn_root)
relpath = relpath[relpath.index('/') + 1:]
relname = os.path.splitext(relpath)[0].replace(
os.sep,
'.')
remote.add(relpath)
dest = os.path.join(mod_dir, relpath)
else:
relpath = os.path.relpath(fn_, local_cache_dir)
relname = os.path.splitext(relpath)[0].replace(os.sep, '.')
remote.add(relpath)
dest = os.path.join(mod_dir, relpath)
log.info('Copying {0!r} to {1!r}'.format(fn_, dest))
if os.path.isfile(dest):
# The file is present, if the sum differs replace it
srch = hashlib.md5(
salt.utils.fopen(fn_, 'r').read()
).hexdigest()
dsth = hashlib.md5(
salt.utils.fopen(dest, 'r').read()
).hexdigest()
if srch != dsth:
# The downloaded file differs, replace!
shutil.copyfile(fn_, dest)
ret.append('{0}.{1}'.format(form, relname))
else:
dest_dir = os.path.dirname(dest)
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
shutil.copyfile(fn_, dest)
ret.append('{0}.{1}'.format(form, relname))
touched = bool(ret)
if __opts__.get('clean_dynamic_modules', True):
current = set(_listdir_recursively(mod_dir))
for fn_ in current - remote:
full = os.path.join(mod_dir, fn_)
if os.path.isfile(full):
touched = True
os.remove(full)
#cleanup empty dirs
while True:
emptydirs = _list_emptydirs(mod_dir)
if not emptydirs:
break
for emptydir in emptydirs:
touched = True
os.rmdir(emptydir)
#dest mod_dir is touched? trigger reload if requested
if touched:
mod_file = os.path.join(__opts__['cachedir'], 'module_refresh')
with salt.utils.fopen(mod_file, 'a+') as ofile:
ofile.write('')
return ret
def _listdir_recursively(rootdir):
file_list = []
for root, dirs, files in os.walk(rootdir):
for filename in files:
relpath = os.path.relpath(root, rootdir).strip('.')
file_list.append(os.path.join(relpath, filename))
return file_list
def _list_emptydirs(rootdir):
emptydirs = []
for root, dirs, files in os.walk(rootdir):
if not files and not dirs:
emptydirs.append(root)
return emptydirs
def update(version=None):
'''
Update the salt minion from the URL defined in opts['update_url']
This feature requires the minion to be running a bdist_esky build.
The version number is optional and will default to the most recent version
available at opts['update_url'].
Returns details about the transaction upon completion.
CLI Example:
.. code-block:: bash
salt '*' saltutil.update 0.10.3
'''
if not HAS_ESKY:
return 'Esky not available as import'
if not getattr(sys, 'frozen', False):
return 'Minion is not running an Esky build'
if not __salt__['config.option']('update_url'):
return '"update_url" not configured on this minion'
app = esky.Esky(sys.executable, __opts__['update_url'])
oldversion = __grains__['saltversion']
try:
if not version:
version = app.find_update()
if not version:
return 'No updates available'
app.fetch_version(version)
app.install_version(version)
app.cleanup()
except Exception as err:
return err
restarted = {}
for service in __opts__['update_restart_services']:
restarted[service] = __salt__['service.restart'](service)
return {'comment': 'Updated from {0} to {1}'.format(oldversion, version),
'restarted': restarted}
def sync_modules(saltenv=None, refresh=True):
'''
Sync the modules from the _modules directory on the salt master file
server. This function is environment aware, pass the desired environment
to grab the contents of the _modules directory, base is the default
environment.
CLI Example:
.. code-block:: bash
salt '*' saltutil.sync_modules
'''
ret = _sync('modules', saltenv)
if refresh:
refresh_modules()
return ret
def sync_states(saltenv=None, refresh=True):
'''
Sync the states from the _states directory on the salt master file
server. This function is environment aware, pass the desired environment
to grab the contents of the _states directory, base is the default
environment.
CLI Example:
.. code-block:: bash
salt '*' saltutil.sync_states
'''
ret = _sync('states', saltenv)
if refresh:
refresh_modules()
return ret
def sync_grains(saltenv=None, refresh=True):
'''
Sync the grains from the _grains directory on the salt master file
server. This function is environment aware, pass the desired environment
to grab the contents of the _grains directory, base is the default
environment.
CLI Example:
.. code-block:: bash
salt '*' saltutil.sync_grains
'''
ret = _sync('grains', saltenv)
if refresh:
refresh_modules()
refresh_pillar()
return ret
def sync_renderers(saltenv=None, refresh=True):
'''
Sync the renderers from the _renderers directory on the salt master file
server. This function is environment aware, pass the desired environment
to grab the contents of the _renderers directory, base is the default
environment.
CLI Example:
.. code-block:: bash
salt '*' saltutil.sync_renderers
'''
ret = _sync('renderers', saltenv)
if refresh:
refresh_modules()
return ret
def sync_returners(saltenv=None, refresh=True):
'''
Sync the returners from the _returners directory on the salt master file
server. This function is environment aware, pass the desired environment
to grab the contents of the _returners directory, base is the default
environment.
CLI Example:
.. code-block:: bash
salt '*' saltutil.sync_returners
'''
ret = _sync('returners', saltenv)
if refresh:
refresh_modules()
return ret
def sync_outputters(saltenv=None, refresh=True):
'''
Sync the outputters from the _outputters directory on the salt master file
server. This function is environment aware, pass the desired environment
to grab the contents of the _outputters directory, base is the default
environment.
CLI Example:
.. code-block:: bash
salt '*' saltutil.sync_outputters
'''
ret = _sync('outputters', saltenv)
if refresh:
refresh_modules()
return ret
def sync_all(saltenv=None, refresh=True):
'''
Sync down all of the dynamic modules from the file server for a specific
environment
CLI Example:
.. code-block:: bash
salt '*' saltutil.sync_all
'''
log.debug('Syncing all')
ret = {}
ret['modules'] = sync_modules(saltenv, False)
ret['states'] = sync_states(saltenv, False)
ret['grains'] = sync_grains(saltenv, False)
ret['renderers'] = sync_renderers(saltenv, False)
ret['returners'] = sync_returners(saltenv, False)
ret['outputters'] = sync_outputters(saltenv, False)
if refresh:
refresh_modules()
return ret
def refresh_pillar():
'''
Signal the minion to refresh the pillar data.
CLI Example:
.. code-block:: bash
salt '*' saltutil.refresh_pillar
'''
__salt__['event.fire']({}, 'pillar_refresh')
def refresh_modules():
'''
Signal the minion to refresh the module and grain data
CLI Example:
.. code-block:: bash
salt '*' saltutil.refresh_modules
'''
__salt__['event.fire']({}, 'module_refresh')
def is_running(fun):
'''
If the named function is running return the data associated with it/them.
The argument can be a glob
CLI Example:
.. code-block:: bash
salt '*' saltutil.is_running state.highstate
'''
run = running()
ret = []
for data in run:
if fnmatch.fnmatch(data.get('fun', ''), fun):
ret.append(data)
return ret
def running():
'''
Return the data on all running salt processes on the minion
CLI Example:
.. code-block:: bash
salt '*' saltutil.running
'''
ret = []
serial = salt.payload.Serial(__opts__)
pid = os.getpid()
current_thread = threading.currentThread().name
proc_dir = os.path.join(__opts__['cachedir'], 'proc')
if not os.path.isdir(proc_dir):
return []
for fn_ in os.listdir(proc_dir):
path = os.path.join(proc_dir, fn_)
with salt.utils.fopen(path, 'rb') as fp_:
buf = fp_.read()
fp_.close()
if buf:
data = serial.loads(buf)
else:
# Proc file is empty, remove
os.remove(path)
continue
if not isinstance(data, dict):
# Invalid serial object
continue
if not salt.utils.process.os_is_running(data['pid']):
# The process is no longer running, clear out the file and
# continue
os.remove(path)
continue
if __opts__['multiprocessing']:
if data.get('pid') == pid:
continue
else:
if data.get('pid') != pid:
os.remove(path)
continue
if data.get('jid') == current_thread:
continue
if not data.get('jid') in [x.name for x in threading.enumerate()]:
os.remove(path)
continue
ret.append(data)
return ret
def find_job(jid):
'''
Return the data for a specific job id
CLI Example:
.. code-block:: bash
salt '*' saltutil.find_job <job id>
'''
for data in running():
if data['jid'] == jid:
return data
return {}
def signal_job(jid, sig):
'''
Sends a signal to the named salt job's process
CLI Example:
.. code-block:: bash
salt '*' saltutil.signal_job <job id> 15
'''
for data in running():
if data['jid'] == jid:
try:
os.kill(int(data['pid']), sig)
return 'Signal {0} sent to job {1} at pid {2}'.format(
int(sig),
jid,
data['pid']
)
except OSError:
path = os.path.join(__opts__['cachedir'], 'proc', str(jid))
if os.path.isfile(path):
os.remove(path)
return ('Job {0} was not running and job data has been '
' cleaned up').format(jid)
return ''
def term_job(jid):
'''
Sends a termination signal (SIGTERM 15) to the named salt job's process
CLI Example:
.. code-block:: bash
salt '*' saltutil.term_job <job id>
'''
return signal_job(jid, signal.SIGTERM)
def kill_job(jid):
'''
Sends a kill signal (SIGKILL 9) to the named salt job's process
CLI Example:
.. code-block:: bash
salt '*' saltutil.kill_job <job id>
'''
return signal_job(jid, signal.SIGKILL)
def regen_keys():
'''
Used to regenerate the minion keys.
CLI Example:
.. code-block:: bash
salt '*' saltutil.regen_keys
'''
for fn_ in os.listdir(__opts__['pki_dir']):
path = os.path.join(__opts__['pki_dir'], fn_)
try:
os.remove(path)
except os.error:
pass
time.sleep(60)
sreq = salt.payload.SREQ(__opts__['master_uri'])
auth = salt.crypt.SAuth(__opts__)
def revoke_auth():
'''
The minion sends a request to the master to revoke its own key.
Note that the minion session will be revoked and the minion may
not be able to return the result of this command back to the master.
CLI Example:
.. code-block:: bash
salt '*' saltutil.revoke_auth
'''
# sreq = salt.payload.SREQ(__opts__['master_uri'])
auth = salt.crypt.SAuth(__opts__)
tok = auth.gen_token('salt')
load = {'cmd': 'revoke_auth',
'id': __opts__['id'],
'tok': tok}
sreq = salt.transport.Channel.factory(__opts__)
try:
sreq.send(load)
# return auth.crypticle.loads(
# sreq.send('aes', auth.crypticle.dumps(load), 1))
except SaltReqTimeoutError:
return False
return False
def cmd(tgt,
fun,
arg=(),
timeout=None,
expr_form='glob',
ret='',
kwarg=None,
ssh=False,
**kwargs):
'''
Assuming this minion is a master, execute a salt command
CLI Example:
.. code-block:: bash
salt '*' saltutil.cmd
'''
if ssh:
client = salt.client.SSHClient(__opts__['conf_file'])
else:
client = salt.client.LocalClient(__opts__['conf_file'])
ret = {}
for ret_comp in client.cmd_iter(
tgt,
fun,
arg,
timeout,
expr_form,
ret,
kwarg,
**kwargs):
ret.update(ret_comp)
return ret
def cmd_iter(tgt,
fun,
arg=(),
timeout=None,
expr_form='glob',
ret='',
kwarg=None,
ssh=False,
**kwargs):
'''
Assuming this minion is a master, execute a salt command
CLI Example:
.. code-block:: bash
salt '*' saltutil.cmd
'''
if ssh:
client = salt.client.SSHClient(__opts__['conf_file'])
else:
client = salt.client.LocalClient(__opts__['conf_file'])
for ret in client.cmd_iter(
tgt,
fun,
arg,
timeout,
expr_form,
ret,
kwarg,
**kwargs):
yield ret