Merge pull request #12853 from cachedout/12408

Better sysctl test detection.
This commit is contained in:
Thomas S Hatch 2014-05-17 11:59:14 -05:00
commit 8c11967309
3 changed files with 37 additions and 11 deletions

View File

@ -257,7 +257,7 @@ def _run(cmd,
if not _is_valid_shell(shell):
log.warning(
'Attempt to run a shell command with what may be an invalid shell! '
'Check to ensure that she shell <{0}> is valid for this user.'
'Check to ensure that the shell <{0}> is valid for this user.'
.format(shell))
# Set the default working directory to the home directory of the user

View File

@ -66,24 +66,38 @@ def default_config():
return '/etc/sysctl.conf'
def show():
def show(config_file=False):
'''
Return a list of sysctl parameters for this minion
config: Pull the data from the system configuration file
instead of the live data.
CLI Example:
.. code-block:: bash
salt '*' sysctl.show
'''
cmd = 'sysctl -a'
ret = {}
out = __salt__['cmd.run_stdout'](cmd, output_loglevel='trace')
for line in out.splitlines():
if not line or ' = ' not in line:
continue
comps = line.split(' = ', 1)
ret[comps[0]] = comps[1]
if config_file:
config_file_path = default_config()
try:
for line in salt.utils.fopen(config_file_path):
if not line.startswith('#') and '=' in line:
key, value = line.split(' = ', 1)
ret[key] = value
except OSError:
log.error('Could not open sysctl file')
return None
else:
cmd = 'sysctl -a'
out = __salt__['cmd.run_stdout'](cmd, output_loglevel='trace')
for line in out.splitlines():
if not line or ' = ' not in line:
continue
comps = line.split(' = ', 1)
ret[comps[0]] = comps[1]
return ret

View File

@ -53,8 +53,9 @@ def present(name, value, config=None):
config = '/etc/sysctl.conf'
current = __salt__['sysctl.show']()
configured = __salt__['sysctl.show'](config_file=True)
if __opts__['test']:
if name in current:
if name in current and name not in configured:
if re.sub(' +|\t+', ' ', current[name]) != re.sub(' +|\t+', ' ', str(value)):
ret['result'] = None
ret['comment'] = (
@ -62,8 +63,19 @@ def present(name, value, config=None):
).format(name, value)
return ret
else:
ret['comment'] = 'Sysctl value {0} = {1} is already set'.format(name, value)
ret['result'] = None
ret['comment'] = 'Sysctl value is currently set on the running system but not in a config file.\n'\
'Sysctl option {0} set to be changed to {1} in config file.'.format(name, value)
return ret
elif name in configured and name not in current:
ret['result'] = None
ret['comment'] = 'Sysctl value {0} is present in configuration file but is not present in the running config.\n'\
'The value {0} is set to be changed to {1} '
return ret
elif name not in configured and name not in current:
ret['result'] = None
ret['comment'] = 'Sysctl option {0} set to be changed to {1}'.format(name, value)
return ret
else:
ret['result'] = False
ret['comment'] = 'Invalid sysctl option {0} = {1}'.format(name, value)