-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
range.py
73 lines (53 loc) · 1.99 KB
/
range.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
# -*- coding: utf-8 -*-
'''
This roster resolves targets from a range server.
:depends: seco.range, https://github.com/ytoolshed/range
When you want to use a range query for target matching, use ``--roster range``. For example:
.. code-block:: bash
salt-ssh --roster range '%%%example.range.cluster' test.ping
'''
from __future__ import absolute_import
import fnmatch
import logging
log = logging.getLogger(__name__)
# Try to import range from https://github.com/ytoolshed/range
HAS_RANGE = False
try:
import seco.range
HAS_RANGE = True
except ImportError:
log.error('Unable to load range library')
# pylint: enable=import-error
def __virtual__():
return HAS_RANGE
def targets(tgt, tgt_type='range', **kwargs):
'''
Return the targets from a range query
'''
r = seco.range.Range(__opts__['range_server'])
log.debug('Range connection to \'{0}\' established'.format(__opts__['range_server']))
hosts = []
try:
log.debug('Querying range for \'{0}\''.format(tgt))
hosts = r.expand(tgt)
except seco.range.RangeException as err:
log.error('Range server exception: %s', err)
return {}
log.debug('Range responded with: \'{0}\''.format(hosts))
# Currently we only support giving a raw range entry, no target filtering supported other than what range returns :S
tgt_func = {
'range': target_range,
'glob': target_range,
# 'glob': target_glob,
}
log.debug('Filtering using tgt_type: \'{0}\''.format(tgt_type))
try:
targeted_hosts = tgt_func[tgt_type](tgt, hosts)
except KeyError:
raise NotImplementedError
log.debug('Targeting data for salt-ssh: \'{0}\''.format(targeted_hosts))
return targeted_hosts
def target_range(tgt, hosts):
return dict((host, {'host': host, 'user': __opts__['ssh_user']}) for host in hosts)
def target_glob(tgt, hosts):
return dict((host, {'host': host, 'user': __opts__['ssh_user']}) for host in hosts if fnmatch.fnmatch(tgt, host))