Skip to content
This repository has been archived by the owner. It is now read-only.

Turn off sync cleaning by default, reduce verbosity of grep failures in logs #238

Merged
merged 3 commits into from
Aug 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ In order to run the audits once daily, you can use the following schedule:
show_profile: True
returner: splunk_nova_return
return_job: False
run_on_start: False

.. _nova_configuration:

Expand Down
12 changes: 7 additions & 5 deletions _modules/hubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def top(topfile='top.nova',
return results


def sync():
def sync(clean=False):
'''
Sync the nova audit modules and profiles from the saltstack fileserver.

Expand All @@ -433,8 +433,9 @@ def sync():

Returns a boolean representing success

NOTE: This function will also clean out existing files at the cached
location, as cp.cache_dir doesn't clean out old files
NOTE: This function will optionally clean out existing files at the cached
location, as cp.cache_dir doesn't clean out old files. Pass ``clean=True``
to enable this behavior

CLI Examples:

Expand All @@ -451,8 +452,9 @@ def sync():
saltenv = __salt__['config.get']('hubblestack:nova:saltenv', 'base')

# Clean previously synced files
for nova_dir in _hubble_dir():
__salt__['file.remove'](nova_dir)
if clean:
for nova_dir in _hubble_dir():
__salt__['file.remove'](nova_dir)

synced = []
for i, nova_dir in enumerate((nova_module_dir, nova_profile_dir)):
Expand Down
64 changes: 61 additions & 3 deletions hubblestack_nova_modules/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ def audit(data_list, tags, verbose=False, show_profile=False, debug=False):
if isinstance(grep_args, str):
grep_args = [grep_args]

grep_ret = __salt__['file.grep'](name,
tag_data['pattern'],
*grep_args).get('stdout')
grep_ret = _grep(name,
tag_data['pattern'],
*grep_args).get('stdout')

found = False
if grep_ret:
Expand Down Expand Up @@ -278,3 +278,61 @@ def _get_tags(data):
formatted_data.pop('data')
ret[tag].append(formatted_data)
return ret


def _grep(path,
pattern,
*opts):
'''
Grep for a string in the specified file

.. note::
This function's return value is slated for refinement in future
versions of Salt

path
Path to the file to be searched

.. note::
Globbing is supported (i.e. ``/var/log/foo/*.log``, but if globbing
is being used then the path should be quoted to keep the shell from
attempting to expand the glob expression.

pattern
Pattern to match. For example: ``test``, or ``a[0-5]``

opts
Additional command-line flags to pass to the grep command. For example:
``-v``, or ``-i -B2``

.. note::
The options should come after a double-dash (as shown in the
examples below) to keep Salt's own argument parser from
interpreting them.

CLI Example:

.. code-block:: bash

salt '*' file.grep /etc/passwd nobody
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i
salt '*' file.grep /etc/sysconfig/network-scripts/ifcfg-eth0 ipaddr -- -i -B2
salt '*' file.grep "/etc/sysconfig/network-scripts/*" ipaddr -- -i -l
'''
path = os.path.expanduser(path)

split_opts = []
for opt in opts:
try:
opt = salt.utils.shlex_split(opt)
except AttributeError:
opt = salt.utils.shlex_split(str(opt))
split_opts.extend(opt)

cmd = ['grep'] + split_opts + [pattern, path]
try:
ret = __salt__['cmd.run_all'](cmd, python_shell=False, ignore_retcode=True)
except (IOError, OSError) as exc:
raise CommandExecutionError(exc.strerror)

return ret