Skip to content

Commit ad91ad5

Browse files
authored
Merge pull request #98 from mirceaulinic/develop
Release 2020.3.0rc2
2 parents 4a62da1 + 8165b1c commit ad91ad5

File tree

10 files changed

+282
-87
lines changed

10 files changed

+282
-87
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright 2019 Mircea Ulinic
189+
Copyright 2019-2020 Mircea Ulinic
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,42 @@ and your (Proxy) Minions at the same time.
2121

2222
> This is NOT a SaltStack product.
2323
24+
Why ``salt-sproxy``
25+
-------------------
26+
27+
``salt-sproxy`` can be used as a standalone tool to manage your devices without
28+
having any further requirements, as well as an extension to your existing Salt
29+
environment (if you already have). In other words, if you have a Salt
30+
installation where you manage some network devices and servers, installing
31+
``salt-sproxy`` on your Master will allow you to run any Salt command as always,
32+
e.g., executing ``salt \* test.ping`` and ``salt-sproxy \* test.ping`` will have
33+
the exact same effect, and result. On top of that, using ``salt-sproxy`` allows
34+
you to manage other devices for which you don't run (Proxy) Minions for.
35+
36+
Of course, if you don't already have Salt, no problem, you can start managing
37+
your devices straight away, check out the [quick
38+
start steps](https://github.com/mirceaulinic/salt-sproxy/blob/develop/docs/quick_start.rst).
39+
40+
In brief, here are some benefits you can get by using *salt-sproxy*:
41+
42+
- Say goodbye to the burden of managing hundreds of system services for the
43+
Proxy Minion processes.
44+
- You can run it locally, on your own computer.
45+
- Python programming made a breeze - might go well with the
46+
[ISalt](https://github.com/mirceaulinic/isalt) package.
47+
- Integrates easily with your existing Salt environment (if you have), by
48+
installing the package on your Salt Master.
49+
- Can continue to leverage the event-driven automation and orchestration
50+
methodologies.
51+
- REST API, see also
52+
[the Salt REST API](https://salt-sproxy.readthedocs.io/en/latest/salt_api.html)
53+
documentation.
54+
- By sending events to a Salt Master, you are able to implement whatever
55+
auditing you need (e.g., what command was executed by who and when, etc.).
56+
- Benefit from inheriting _all_ the native Salt features and integrations
57+
contributed by thousands of users, and tested in hundreds of different
58+
environments, over almost a decade of development.
59+
2460
Prerequisites
2561
-------------
2662

@@ -198,8 +234,8 @@ Salt has natively available an HTTP API. You can read more at
198234
[https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#a-rest-api-for-salt](
199235
https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#a-rest-api-for-salt)
200236
if you haven't used it before. The usage is very simple; for salt-sproxy
201-
specifically you can follow the notes from [https://salt-sproxy.readthedocs.io/en/latest/salt_api.html](
202-
https://salt-sproxy.readthedocs.io/en/latest/salt_api.html) how to set it up and
237+
specifically you can follow the notes from [https://salt-sproxy.readthedocs.io/en/latest/salt_sapi.html](
238+
https://salt-sproxy.readthedocs.io/en/latest/salt_sapi.html) how to set it up and
203239
use. Usage example - apply a small configuration change on a Juniper device, by
204240
executing an HTTP request via the Salt API:
205241
@@ -208,12 +244,10 @@ $ curl -sS localhost:8080/run -H 'Accept: application/x-yaml' \
208244
-d eauth='pam' \
209245
-d username='mircea' \
210246
-d password='pass' \
211-
-d client='runner' \
212-
-d fun='proxy.execute' \
247+
-d client='sproxy' \
213248
-d tgt='juniper-router' \
214-
-d function='net.load_config' \
215-
-d text='set system ntp server 10.10.10.1' \
216-
-d sync=True
249+
-d fun='net.load_config' \
250+
-d text='set system ntp server 10.10.10.1'
217251
return:
218252
- juniper-router:
219253
already_configured: false
@@ -226,8 +260,8 @@ return:
226260
result: true
227261
```
228262
229-
See the [documentation](https://salt-sproxy.readthedocs.io/en/latest/salt_api.html)
230-
for explanation, and [this example](https://salt-sproxy.readthedocs.io/en/latest/examples/salt_api.html)
263+
See the [documentation](https://salt-sproxy.readthedocs.io/en/latest/salt_sapi.html)
264+
for explanation, and [this example](https://salt-sproxy.readthedocs.io/en/latest/examples/salt_sapi.html)
231265
for a quick start.
232266
233267
What's included
@@ -240,13 +274,13 @@ backwards compatibility with older Salt versions:
240274
241275
```
242276
|-- cli.py
243-
|-- __init__.py
244277
|-- parsers.py
245278
|-- _roster/
246279
| |-- ansible.py
247-
| `-- netbox.py
280+
| |-- file.py
281+
| |-- netbox.py
282+
| `-- pillar.py
248283
|-- _runners/
249-
| |-- __init__.py
250284
| `-- proxy.py
251285
|-- scripts.py
252286
`-- version.py

README.rst

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ and your (Proxy) Minions at the same time.
4242

4343
This is NOT a SaltStack product.
4444

45+
Why ``salt-sproxy``
46+
-------------------
47+
48+
``salt-sproxy`` can be used as a standalone tool to manage your devices without
49+
having any further requirements, as well as an extension to your existing Salt
50+
environment (if you already have). In other words, if you have a Salt
51+
installation where you manage some network devices and servers, installing
52+
``salt-sproxy`` on your Master will allow you to run any Salt command as always,
53+
e.g., executing ``salt \* test.ping`` and ``salt-sproxy \* test.ping`` will have
54+
the exact same effect, and result. On top of that, using ``salt-sproxy`` allows
55+
you to manage other devices for which you don't run (Proxy) Minions for.
56+
57+
Of course, if you don't already have Salt, no problem, you can start managing
58+
your devices straight away, check out the `quick
59+
start steps <https://github.com/mirceaulinic/salt-sproxy/blob/develop/docs/quick_start.rst>`__.
60+
61+
In brief, here are some benefits you can get by using *salt-sproxy*:
62+
63+
- Say goodbye to the burden of managing hundreds of system services for the
64+
Proxy Minion processes.
65+
- You can run it locally, on your own computer.
66+
- Python programming made a breeze - might go well with the
67+
`ISalt <https://github.com/mirceaulinic/isalt>`__ package.
68+
- Integrates easily with your existing Salt environment (if you have), by
69+
installing the package on your Salt Master.
70+
- Can continue to leverage the event-driven automation and orchestration
71+
methodologies.
72+
- REST API, see also
73+
`the Salt REST API <https://salt-sproxy.readthedocs.io/en/latest/salt_api.html>`__
74+
documentation.
75+
- By sending events to a Salt Master, you are able to implement whatever
76+
auditing you need (e.g., what command was executed by who and when, etc.).
77+
- Benefit from inheriting _all_ the native Salt features and integrations
78+
contributed by thousands of users, and tested in hundreds of different
79+
environments, over almost a decade of development.
80+
81+
4582
Prerequisites
4683
-------------
4784

@@ -50,8 +87,8 @@ would like to install it on your computer, you might want to run it under a
5087
`virtual environment <https://docs.python-guide.org/dev/virtualenvs/>`__.
5188

5289
Besides the CLI, the usage remains the same as when you're running a Salt
53-
environment with Proxy or regular Minions. See the following documents on how
54-
to get started and fully unleash the power of Salt:
90+
environment with Proxy or regular Minions. For example, see the following
91+
documents on how to get started and fully unleash the power of Salt:
5592

5693
- `Salt in 10 minutes
5794
<https://docs.saltstack.com/en/latest/topics/tutorials/walkthrough.html>`__.
@@ -230,7 +267,7 @@ Salt has natively available an HTTP API. You can read more at
230267
https://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#a-rest-api-for-salt
231268
if you haven't used it before. The usage is very simple; for salt-sproxy
232269
specifically you can follow the notes from
233-
https://salt-sproxy.readthedocs.io/en/latest/salt_api.html how to set it up and
270+
https://salt-sproxy.readthedocs.io/en/latest/salt_sapi.html how to set it up and
234271
use. Usage example - apply a small configuration change on a Juniper device, by
235272
executing an HTTP request via the Salt API:
236273

@@ -240,12 +277,10 @@ executing an HTTP request via the Salt API:
240277
-d eauth='pam' \
241278
-d username='mircea' \
242279
-d password='pass' \
243-
-d client='runner' \
244-
-d fun='proxy.execute' \
280+
-d client='sproxy' \
245281
-d tgt='juniper-router' \
246-
-d function='net.load_config' \
247-
-d text='set system ntp server 10.10.10.1' \
248-
-d sync=True
282+
-d fun='net.load_config' \
283+
-d text='set system ntp server 10.10.10.1'
249284
return:
250285
- juniper-router:
251286
already_configured: false
@@ -258,8 +293,8 @@ executing an HTTP request via the Salt API:
258293
result: true
259294
260295
See the `documentation
261-
<https://salt-sproxy.readthedocs.io/en/latest/salt_api.html>`__ for explanation,
262-
and `this example <https://salt-sproxy.readthedocs.io/en/latest/examples/salt_api.html>`__
296+
<https://salt-sproxy.readthedocs.io/en/latest/salt_sapi.html>`__ for explanation,
297+
and `this example <https://salt-sproxy.readthedocs.io/en/latest/examples/salt_sapi.html>`__
263298
for a quick start.
264299

265300
What's included
@@ -273,13 +308,13 @@ backwards compatibility with older Salt versions:
273308
.. code-block:: text
274309
275310
|-- cli.py
276-
|-- __init__.py
277311
|-- parsers.py
278312
|-- _roster/
313+
| |-- file.py
279314
| |-- ansible.py
280-
| `-- netbox.py
315+
| |-- netbox.py
316+
| `-- pillar.py
281317
|-- _runners/
282-
| |-- __init__.py
283318
| `-- proxy.py
284319
|-- scripts.py
285320
`-- version.py

docs/index.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,39 @@ and your (Proxy) Minions at the same time.
1414

1515
This is NOT a SaltStack product.
1616

17+
Why ``salt-sproxy``
18+
-------------------
19+
20+
``salt-sproxy`` can be used as a standalone tool to manage your devices without
21+
having any further requirements, as well as an extension to your existing Salt
22+
environment (if you already have). In other words, if you have a Salt
23+
installation where you manage some network devices and servers, installing
24+
``salt-sproxy`` on your Master will allow you to run any Salt command as always,
25+
e.g., executing ``salt \* test.ping`` and ``salt-sproxy \* test.ping`` will have
26+
the exact same effect, and result. On top of that, using ``salt-sproxy`` allows
27+
you to manage other devices for which you don't run (Proxy) Minions for.
28+
29+
Of course, if you don't already have Salt, no problem, you can start managing
30+
your devices straight away, check out the :ref:`quick-start` steps.
31+
32+
In brief, here are some benefits you can get by using *salt-sproxy*:
33+
34+
- Say goodbye to the burden of managing hundreds of system services for the
35+
Proxy Minion processes.
36+
- You can run it locally, on your own computer.
37+
- Python programming made a breeze - might go well with the
38+
`ISalt <https://github.com/mirceaulinic/isalt>`__ package.
39+
- Integrates easily with your existing Salt environment (if you have), by
40+
installing the package on your Salt Master.
41+
- Can continue to leverage the event-driven automation and orchestration
42+
methodologies.
43+
- REST API, see also :ref:`salt-api` documentation.
44+
- By sending events to a Salt Master, you are able to implement whatever
45+
auditing you need (e.g., what command was executed by who and when, etc.).
46+
- Benefit from inheriting _all_ the native Salt features and integrations
47+
contributed by thousands of users, and tested in hundreds of different
48+
environments, over almost a decade of development.
49+
1750
Install
1851
-------
1952

docs/opts.rst

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,23 @@ already familiar with a vast majority of them from the `salt
124124

125125
Cache the collected Grains. Beware that this option overwrites the existing
126126
Grains. This may be helpful when using the ``salt-sproxy`` only, but may
127-
lead to unexpected results when running in :ref:`mixed-environments`.
127+
lead to unexpected results when running in :ref:`mixed-environments`. That
128+
said, when running together with ``--use-existing-proxy``, there shouldn't
129+
be any issues, as *salt-sproxy* will attemtp to use the existing (Proxy)
130+
Minion if any, otherwise it will write the collected Grains to the cache,
131+
which is a safe operation in this case (i.e., it won't overwrite the Grains
132+
of an existing Minion).
128133

129134
.. option:: --cache-pillar
130135

131136
Cache the collected Pillar. Beware that this option overwrites the existing
132137
Pillar. This may be helpful when using the ``salt-sproxy`` only, but may
133-
lead to unexpected results when running in :ref:`mixed-environments`.
138+
lead to unexpected results when running in :ref:`mixed-environments`. That
139+
said, when running together with ``--use-existing-proxy``, there shouldn't
140+
be any issues, as *salt-sproxy* will attemtp to use the existing (Proxy)
141+
Minion if any, otherwise it will write the compiled Pillar to the cache,
142+
which is a safe operation in this case (i.e., it won't overwrite the cached
143+
Pillar of an existing Minion).
134144

135145
.. option:: --no-cached-grains
136146

@@ -356,6 +366,10 @@ already familiar with a vast majority of them from the `salt
356366

357367
Avoid loading the list of targets from the cache.
358368

369+
.. versionchanged:: 2020.3.0
370+
371+
This option now defaults to ``True``.
372+
359373
.. option:: --pillar-root
360374

361375
.. versionadded:: 2020.2.0
@@ -532,3 +546,29 @@ Output Options
532546

533547
Override the configured state_verbose value for minion
534548
output. Set to True or False. Default: none.
549+
550+
Configuration file options
551+
--------------------------
552+
553+
All the previous options can be provided via the CLI, as in-line arguments, as
554+
well as configured in the configuration file. There are however options that
555+
are available only through the configuration file:
556+
557+
.. option:: ``target_use_cache_grains``
558+
559+
.. versionadded: 2020.3.0
560+
561+
Whether targeting should look up into the existing cache to compute the
562+
list of matching devices. This option may be particularly useful when using
563+
one of the following targeting mechanisms: ``-G`` (grain), ``-P`` (grain
564+
PCRE), or ``-C`` (compound). Default: ``True`` (it will check the cache).
565+
566+
567+
.. option:: ``target_use_cache_pillar``
568+
569+
.. versionadded: 2020.3.0
570+
571+
Whether targeting should look up into the existing cache to compute the
572+
list of matching devices. This option may be particularly useful when using
573+
one of the following targeting mechanisms: ``-I`` (pillar), ``-J`` (pillar
574+
PCRE), or ``-C`` (compound). Default:: ``True`` (it will check the cache).

salt_sproxy/_roster/__init__.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fnmatch
88
import logging
99

10+
import salt.cache
1011
from salt.ext import six
1112
import salt.utils.minions
1213

@@ -26,6 +27,15 @@ def load_cache(pool, __runner__, opts, tgt, tgt_type=None):
2627
Load the Pillar and Grain cache, as required, and merge the Roster Grains
2728
and Pillar into.
2829
'''
30+
if opts.get('grains'):
31+
for device, device_opts in six.iteritems(pool):
32+
if 'minion_opts' not in device_opts:
33+
device_opts['minion_opts'] = {}
34+
if 'grains' not in device_opts['minion_opts']:
35+
device_opts['minion_opts']['grains'] = {}
36+
device_opts['minion_opts']['grains'] = salt.utils.dictupdate.merge(
37+
opts['grains'], device_opts['minion_opts']['grains'], merge_lists=True,
38+
)
2939
if tgt_type in ('glob', 'pcre', 'list'):
3040
# When the target type is glob, pcre, or list, we don't require grains
3141
# or pillar loaded from the cache, because the targeting won't depend on
@@ -39,38 +49,40 @@ def load_cache(pool, __runner__, opts, tgt, tgt_type=None):
3949
# Runners as they rely on fetching data from the Master, for Minions that
4050
# are accepted. What we're doing here is reading straight from the cache.
4151
log.debug('Loading cached and merging into the Roster data')
42-
cache_pool = __runner__['cache.list']('minions')
52+
cache = salt.cache.factory(opts)
53+
cache_pool = cache.list('minions')
4354
for device in cache_pool:
4455
if device not in pool:
56+
log.trace('%s has cache, but is not in the Roster pool', device)
4557
continue
4658
if 'minion_opts' not in pool[device]:
4759
pool[device]['minion_opts'] = {'grains': {}, 'pillar': {}}
4860
cache_key = 'minions/{}/data'.format(device)
49-
if opts.get('use_cached_grains', True) and tgt_type in (
61+
if opts.get('target_use_cached_grains', True) and tgt_type in (
5062
'compound',
5163
'grain',
5264
'grain_pcre',
5365
'nodegroup',
5466
):
5567
log.debug('Fetching cached Grains for %s', device)
56-
cache_grains = __runner__['cache.fetch'](cache_key, 'grains')
57-
if cache_grains:
68+
cached_grains = cache.fetch(cache_key, 'grains')
69+
if cached_grains:
5870
pool[device]['minion_opts']['grains'] = salt.utils.dictupdate.merge(
59-
cache_grains,
71+
cached_grains,
6072
pool[device]['minion_opts'].get('grains', {}),
6173
merge_lists=True,
6274
)
63-
if opts.get('use_cached_pillar', True) and tgt_type in (
75+
if opts.get('target_use_cached_pillar', True) and tgt_type in (
6476
'compound',
6577
'pillar',
6678
'pillar_pcre',
6779
'nodegroup',
6880
):
6981
log.debug('Fetching cached Pillar for %s', device)
70-
cache_pillar = __runner__['cache.fetch'](cache_key, 'pillar')
71-
if cache_pillar:
82+
cached_pillar = cache.fetch(cache_key, 'pillar')
83+
if cached_pillar:
7284
pool[device]['minion_opts']['pillar'] = salt.utils.dictupdate.merge(
73-
cache_pillar,
85+
cached_pillar,
7486
pool[device]['minion_opts'].get('pillar', {}),
7587
merge_lists=True,
7688
)

0 commit comments

Comments
 (0)