Skip to content

Instantly share code, notes, and snippets.

@dereckson
Last active September 5, 2021 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dereckson/dfa6553e9c42bd225a9e0c8010091a99 to your computer and use it in GitHub Desktop.
Save dereckson/dfa6553e9c42bd225a9e0c8010091a99 to your computer and use it in GitHub Desktop.
Allow cmd.* to work on FreeBSD with runas
--- modules/cmdmod.py.orig 2021-07-30 19:55:37.000000000 +0000
+++ modules/cmdmod.py 2021-09-05 07:59:54.531476000 +0000
@@ -69,9 +69,7 @@
log = logging.getLogger(__name__)
-DEFAULT_SHELL = salt.grains.extra.shell()["shell"]
-
# Overwriting the cmd python module makes debugging modules with pdb a bit
# harder so lets do it this way instead.
def __virtual__():
@@ -97,6 +95,17 @@
return lambda x: x
+def _shell_default():
+ try:
+ if __grains__['os'] in ['FreeBSD']:
+ return "/bin/sh"
+ except NameError:
+ # We've been called as an helper when __grains__ isn't available.
+ pass
+
+ return salt.grains.extra.shell()['shell']
+
+
def _python_shell_default(python_shell, __pub_jid):
"""
Set python_shell default based on remote execution and __opts__['cmd_safe']
@@ -262,7 +271,7 @@
log_callback=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=False,
env=None,
clean_env=False,
@@ -290,6 +299,8 @@
"""
if "pillar" in kwargs and not pillar_override:
pillar_override = kwargs["pillar"]
+ if not shell:
+ shell = _shell_default()
if output_loglevel != "quiet" and _is_valid_shell(shell) is False:
log.warning(
"Attempt to run a shell command with what may be an invalid shell! "
@@ -499,10 +510,7 @@
env_cmd.extend(["-u", runas])
if group:
env_cmd.extend(["-g", group])
- if shell != DEFAULT_SHELL:
- env_cmd.extend(["-s", "--", shell, "-c"])
- else:
- env_cmd.extend(["-i", "--"])
+ env_cmd.extend(["-s", "--", shell, "-c"])
env_cmd.extend([sys.executable])
elif __grains__["os"] in ["FreeBSD"]:
env_cmd = (
@@ -867,7 +875,7 @@
stdin=None,
output_encoding=None,
runas=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=False,
env=None,
template=None,
@@ -910,7 +918,7 @@
cwd=None,
stdin=None,
runas=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=False,
env=None,
template=None,
@@ -961,7 +969,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=None,
env=None,
clean_env=False,
@@ -1262,7 +1270,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
env=None,
clean_env=False,
template=None,
@@ -1503,7 +1511,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=None,
env=None,
clean_env=False,
@@ -1719,7 +1727,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=None,
env=None,
clean_env=False,
@@ -1935,7 +1943,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=None,
env=None,
clean_env=False,
@@ -2199,7 +2207,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=None,
env=None,
clean_env=False,
@@ -2400,7 +2408,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=False,
env=None,
clean_env=False,
@@ -2454,7 +2462,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=None,
env=None,
template=None,
@@ -2742,7 +2750,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=None,
env=None,
template="jinja",
@@ -3053,7 +3061,7 @@
stdin=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=True,
binds=None,
env=None,
@@ -4195,7 +4203,7 @@
cwd=None,
runas=None,
group=None,
- shell=DEFAULT_SHELL,
+ shell=None,
python_shell=None,
env=None,
clean_env=False,
--- states/cmd.py.orig 2021-07-30 19:55:37.000000000 +0000
+++ states/cmd.py 2021-09-05 08:09:20.445459000 +0000
@@ -245,6 +245,13 @@
log = logging.getLogger(__name__)
+def _shell_default():
+ if __grains__["os"] in ["FreeBSD"]:
+ return "/bin/sh"
+
+ return __grains__["shell"]
+
+
def _reinterpreted_state(state):
"""
Re-interpret the state returned by salt.state.run using our protocol.
@@ -784,7 +791,7 @@
"root": root,
"runas": runas,
"use_vt": use_vt,
- "shell": shell or __grains__["shell"],
+ "shell": shell or _shell_default(),
"env": env,
"prepend_path": prepend_path,
"umask": umask,
@@ -908,6 +915,9 @@
shell
The shell to use for execution. The default is set in grains['shell']
+ .. note::
+ On FreeBSD, the default is set to ``/bin/sh``..
+
env
A list of environment variables to be set prior to execution.
Example:
@@ -1060,7 +1070,7 @@
{
"runas": runas,
"password": password,
- "shell": shell or __grains__["shell"],
+ "shell": shell or _shell_default(),
"env": env,
"cwd": cwd,
"template": template,
@@ -1078,7 +1088,7 @@
run_check_cmd_kwargs = {
"cwd": cwd,
"runas": runas,
- "shell": shell or __grains__["shell"],
+ "shell": shell or _shell_default(),
}
# Change the source to be the name arg if it is not specified
@@ -1165,7 +1175,7 @@
cmd_kwargs = {
"cwd": kwargs.get("cwd"),
"runas": kwargs.get("user"),
- "shell": kwargs.get("shell") or __grains__["shell"],
+ "shell": kwargs.get("shell") or _shell_default(),
"env": kwargs.get("env"),
"use_vt": use_vt,
"output_loglevel": output_loglevel,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment