MCPcopy Create free account
hub / github.com/saltstack/salt / exec_code_all

Function exec_code_all

salt/modules/cmdmod.py:3494–3548  ·  view source on GitHub ↗

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

(lang, code, cwd=None, args=None, **kwargs)

Source from the content-addressed store, hash-verified

3492
3493
3494def exec_code_all(lang, code, cwd=None, args=None, **kwargs):
3495 """
3496 Pass in two strings, the first naming the executable language, aka -
3497 python2, python3, ruby, perl, lua, etc. the second string containing
3498 the code you wish to execute. All cmd artifacts (stdout, stderr, retcode, pid)
3499 will be returned.
3500
3501 All parameters from :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` except python_shell can be used.
3502
3503 CLI Example:
3504
3505 .. code-block:: bash
3506
3507 salt '*' cmd.exec_code_all ruby 'puts "cheese"'
3508 salt '*' cmd.exec_code_all ruby 'puts "cheese"' args='["arg1", "arg2"]' env='{"FOO": "bar"}'
3509 """
3510 powershell = lang.lower().startswith("powershell")
3511
3512 if powershell:
3513 codefile = salt.utils.files.mkstemp(suffix=".ps1")
3514 else:
3515 codefile = salt.utils.files.mkstemp()
3516
3517 with salt.utils.files.fopen(codefile, "w+t", binary=False) as fp_:
3518 fp_.write(salt.utils.stringutils.to_str(code))
3519
3520 if powershell:
3521 cmd = [lang, "-File", codefile]
3522 else:
3523 cmd = [lang, codefile]
3524
3525 if isinstance(args, str):
3526 cmd.append(args)
3527 elif isinstance(args, list):
3528 cmd += args
3529
3530 def _cleanup_tempfile(path):
3531 try:
3532 __salt__["file.remove"](path)
3533 except (SaltInvocationError, CommandExecutionError) as exc:
3534 log.error(
3535 "cmd.exec_code_all: Unable to clean tempfile '%s': %s",
3536 path,
3537 exc,
3538 exc_info_on_loglevel=logging.DEBUG,
3539 )
3540
3541 runas = kwargs.get("runas")
3542 if runas is not None:
3543 if not salt.utils.platform.is_windows():
3544 os.chown(codefile, __salt__["file.user_to_uid"](runas), -1)
3545
3546 ret = run_all(cmd, cwd=cwd, python_shell=False, **kwargs)
3547 _cleanup_tempfile(codefile)
3548 return ret
3549
3550
3551def tty(device, echo=""):

Callers 1

exec_codeFunction · 0.85

Calls 5

run_allFunction · 0.70
_cleanup_tempfileFunction · 0.70
writeMethod · 0.45
appendMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected