Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Slot args port #55674

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
65 changes: 63 additions & 2 deletions doc/ref/states/requisites.rst
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,34 @@ For example:
In the above case, ``some_check`` will be run prior to _each_ name -- once for
``first_deploy_cmd`` and a second time for ``second_deploy_cmd``.

.. versionchanged:: Neon
The ``unless`` requisite can take a module as a dictionary field in unless.
The dictionary must contain an argument ``fun`` which is the module that is
being run, and everything else must be passed in under the args key or will
be passed as individual kwargs to the module function.

.. code-block:: yaml

install apache on debian based distros:
cmd.run:
- name: make install
- cwd: /path/to/dir/whatever-2.1.5/
- unless:
- fun: file.file_exists
path: /usr/local/bin/whatever

.. code-block:: yaml

set mysql root password:
debconf.set:
- name: mysql-server-5.7
- data:
'mysql-server/root_password': {'type': 'password', 'value': {{pillar['mysql.pass']}} }
- unless:
- fun: pkg.version
args:
- mysql-server-5.7

.. _onlyif-requisite:

Onlyif
Expand Down Expand Up @@ -978,8 +1006,41 @@ concept of ``True`` and ``False``.
The above example ensures that the stop_volume and delete modules only run
if the gluster commands return a 0 ret value.

Listen/Listen_in
----------------
.. versionchanged:: Neon
The ``onlyif`` requisite can take a module as a dictionary field in onlyif.
The dictionary must contain an argument ``fun`` which is the module that is
being run, and everything else must be passed in under the args key or will
be passed as individual kwargs to the module function.

.. code-block:: yaml

install apache on redhat based distros:
pkg.latest:
- name: httpd
- onlyif:
- fun: match.grain
tgt: 'os_family: RedHat'

install apache on debian based distros:
pkg.latest:
- name: apache2
- onlyif:
- fun: match.grain
tgt: 'os_family: Debian'

.. code-block:: yaml

arbitrary file example:
file.touch:
- name: /path/to/file
- onlyif:
- fun: file.search
args:
- /etc/crontab
- 'entry1'

runas
~~~~~

.. versionadded:: 2014.7.0

Expand Down
49 changes: 49 additions & 0 deletions doc/topics/releases/neon.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@
Salt Release Notes - Codename Neon
==================================

Unless and onlyif Enhancements
==============================

The ``unless`` and ``onlyif`` requisites can now be operated with salt modules.
The dictionary must contain an argument ``fun`` which is the module that is
being run, and everything else must be passed in under the args key or will be
passed as individual kwargs to the module function.

.. code-block:: yaml

install apache on debian based distros:
cmd.run:
- name: make install
- cwd: /path/to/dir/whatever-2.1.5/
- unless:
- fun: file.file_exists
path: /usr/local/bin/whatever

.. code-block:: yaml

set mysql root password:
debconf.set:
- name: mysql-server-5.7
- data:
'mysql-server/root_password': {'type': 'password', 'value': {{pillar['mysql.pass']}} }
- unless:
- fun: pkg.version
args:
- mysql-server-5.7

Keystore State and Module
=========================
Expand Down Expand Up @@ -78,6 +107,26 @@ The slot syntax has been updated to support parsing dictionary responses and to
Duration: 1.229 ms
Changes:

Also, slot parsing is now supported inside of nested state data structures (dicts, lists, unless/onlyif args):

.. code-block:: yaml

demo slot parsing for nested elements:
file.managed:
- name: /tmp/slot.txt
- source: salt://template.j2
- template: jinja
- context:
# Slot inside of the nested context dictionary
variable: __slot__:salt:test.echo(a_value)
- unless:
- fun: file.search
args:
# Slot as unless argument
- __slot__:salt:test.echo(/tmp/slot.txt)
- "DO NOT OVERRIDE"
ignore_if_missing: True


State Changes
=============
Expand Down
81 changes: 65 additions & 16 deletions salt/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,17 @@ def _run_check(self, low_data):

return ret

def _run_check_function(self, entry):
"""Format slot args and run unless/onlyif function."""
fun = entry.pop('fun')
args = entry.pop('args') if 'args' in entry else []
cdata = {
'args': args,
'kwargs': entry
}
self.format_slots(cdata)
return self.functions[fun](*cdata['args'], **cdata['kwargs'])

def _run_check_onlyif(self, low_data, cmd_opts):
'''
Check that unless doesn't return 0, and that onlyif returns a 0.
Expand All @@ -880,20 +891,40 @@ def _run_check_onlyif(self, low_data, cmd_opts):
low_data_onlyif = [low_data['onlyif']]
else:
low_data_onlyif = low_data['onlyif']
for entry in low_data_onlyif:
if not isinstance(entry, six.string_types):
ret.update({'comment': 'onlyif execution failed, bad type passed', 'result': False})
return ret
cmd = self.functions['cmd.retcode'](
entry, ignore_retcode=True, python_shell=True, **cmd_opts)
log.debug('Last command return code: %s', cmd)

def _check_cmd(cmd):
if cmd != 0 and ret['result'] is False:
ret.update({'comment': 'onlyif condition is false',
'skip_watch': True,
'result': True})
return ret
elif cmd == 0:
ret.update({'comment': 'onlyif condition is true', 'result': False})

for entry in low_data_onlyif:
if isinstance(entry, six.string_types):
cmd = self.functions['cmd.retcode'](
entry, ignore_retcode=True, python_shell=True, **cmd_opts)
log.debug('Last command return code: %s', cmd)
_check_cmd(cmd)
elif isinstance(entry, dict):
if 'fun' not in entry:
ret['comment'] = 'no `fun` argument in onlyif: {0}'.format(entry)
log.warning(ret['comment'])
return ret

result = self._run_check_function(entry)
if self.state_con.get('retcode', 0):
_check_cmd(self.state_con['retcode'])
elif not result:
ret.update({'comment': 'onlyif condition is false',
'skip_watch': True,
'result': True})
else:
ret.update({'comment': 'onlyif condition is true',
'result': False})

else:
ret.update({'comment': 'onlyif execution failed, bad type passed', 'result': False})
return ret

def _run_check_unless(self, low_data, cmd_opts):
Expand All @@ -906,20 +937,38 @@ def _run_check_unless(self, low_data, cmd_opts):
low_data_unless = [low_data['unless']]
else:
low_data_unless = low_data['unless']
for entry in low_data_unless:
if not isinstance(entry, six.string_types):
ret.update({'comment': 'unless condition is false, bad type passed', 'result': False})
return ret
cmd = self.functions['cmd.retcode'](
entry, ignore_retcode=True, python_shell=True, **cmd_opts)
log.debug('Last command return code: %s', cmd)

def _check_cmd(cmd):
if cmd == 0 and ret['result'] is False:
ret.update({'comment': 'unless condition is true',
'skip_watch': True,
'result': True})
elif cmd != 0:
ret.update({'comment': 'unless condition is false', 'result': False})
return ret

for entry in low_data_unless:
if isinstance(entry, six.string_types):
cmd = self.functions['cmd.retcode'](entry, ignore_retcode=True, python_shell=True, **cmd_opts)
log.debug('Last command return code: %s', cmd)
_check_cmd(cmd)
elif isinstance(entry, dict):
if 'fun' not in entry:
ret['comment'] = 'no `fun` argument in unless: {0}'.format(entry)
log.warning(ret['comment'])
return ret

result = self._run_check_function(entry)
if self.state_con.get('retcode', 0):
_check_cmd(self.state_con['retcode'])
elif result:
ret.update({'comment': 'unless condition is true',
'skip_watch': True,
'result': True})
else:
ret.update({'comment': 'unless condition is false',
'result': False})
else:
ret.update({'comment': 'unless condition is false, bad type passed', 'result': False})

# No reason to stop, return ret
return ret
Expand Down
Loading