Merge pull request #42991 from DarkKnightCZ/exec-code-all-fix

Allow cmdmod.exec_code_all to utilize cmdmod.run_all parameters
This commit is contained in:
Mike Place 2017-08-25 15:25:31 -06:00 committed by GitHub
commit cfde26562b
2 changed files with 43 additions and 4 deletions

View file

@ -2403,33 +2403,39 @@ def has_exec(cmd):
return which(cmd) is not None
def exec_code(lang, code, cwd=None):
def exec_code(lang, code, cwd=None, args=None, **kwargs):
'''
Pass in two strings, the first naming the executable language, aka -
python2, python3, ruby, perl, lua, etc. the second string containing
the code you wish to execute. The stdout will be returned.
All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.
CLI Example:
.. code-block:: bash
salt '*' cmd.exec_code ruby 'puts "cheese"'
salt '*' cmd.exec_code ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
'''
return exec_code_all(lang, code, cwd)['stdout']
return exec_code_all(lang, code, cwd, args, **kwargs)['stdout']
def exec_code_all(lang, code, cwd=None):
def exec_code_all(lang, code, cwd=None, args=None, **kwargs):
'''
Pass in two strings, the first naming the executable language, aka -
python2, python3, ruby, perl, lua, etc. the second string containing
the code you wish to execute. All cmd artifacts (stdout, stderr, retcode, pid)
will be returned.
All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.
CLI Example:
.. code-block:: bash
salt '*' cmd.exec_code_all ruby 'puts "cheese"'
salt '*' cmd.exec_code_all ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
'''
powershell = lang.lower().startswith("powershell")
@ -2446,7 +2452,12 @@ def exec_code_all(lang, code, cwd=None):
else:
cmd = [lang, codefile]
ret = run_all(cmd, cwd=cwd, python_shell=False)
if isinstance(args, six.string_types):
cmd.append(args)
elif isinstance(args, list):
cmd += args
ret = run_all(cmd, cwd=cwd, python_shell=False, **kwargs)
os.remove(codefile)
return ret

View file

@ -187,6 +187,34 @@ class CMDModuleTest(ModuleCase):
code]).rstrip(),
'cheese')
def test_exec_code_with_single_arg(self):
'''
cmd.exec_code
'''
code = textwrap.dedent('''\
import sys
sys.stdout.write(sys.argv[1])''')
arg = 'cheese'
self.assertEqual(self.run_function('cmd.exec_code',
[AVAILABLE_PYTHON_EXECUTABLE,
code],
args=arg).rstrip(),
arg)
def test_exec_code_with_multiple_args(self):
'''
cmd.exec_code
'''
code = textwrap.dedent('''\
import sys
sys.stdout.write(sys.argv[1])''')
arg = 'cheese'
self.assertEqual(self.run_function('cmd.exec_code',
[AVAILABLE_PYTHON_EXECUTABLE,
code],
args=[arg, 'test']).rstrip(),
arg)
def test_quotes(self):
'''
cmd.run with quoted command