reverting https://github.com/saltstack/salt/pull/60890 and taking a different approach to fixing the Mac brew failures.

This commit is contained in:
Gareth J. Greenaway 2021-09-21 12:48:37 -07:00
parent fb10707877
commit ba66308561
3 changed files with 39 additions and 50 deletions

View file

@ -434,27 +434,23 @@ def _run(
cmd = " ".join(map(_cmd_quote, cmd))
# Ensure directory is correct before running command
cmd = "cd -- {dir} && {{ {cmd}; }}".format(dir=_cmd_quote(cwd), cmd=cmd)
cmd = "cd -- {dir} && {{ {cmd}\n }}".format(dir=_cmd_quote(cwd), cmd=cmd)
# Ensure environment is correct for a newly logged-in user by running
# the command under the user's login shell
# the command under bash as a login shell
try:
valid_shells = shells()
user_shell = __salt__["user.info"](runas)["shell"]
if user_shell not in valid_shells:
raise CommandExecutionError(
"Shell '{}' for '{}' is not valid".format(user_shell, runas)
if re.search("bash$", user_shell):
cmd = "{shell} -l -c {cmd}".format(
shell=user_shell, cmd=_cmd_quote(cmd)
)
cmd = "{shell} -l -c {cmd}".format(shell=user_shell, cmd=_cmd_quote(cmd))
except KeyError:
pass
# Ensure the login is simulated correctly (note: su runs sh, not bash,
# which causes the environment to be initialised incorrectly, which is
# fixed by the previous line of code)
cmd = "sudo -i -n -H -u {} -- {}".format(_cmd_quote(runas), cmd)
cmd = "su -l {} -c {}".format(_cmd_quote(runas), _cmd_quote(cmd))
# Set runas to None, because if you try to run `su -l` after changing
# user, su will prompt for the password of the user and cause salt to

View file

@ -110,8 +110,15 @@ def _call_brew(*cmd, failhard=True):
"""
user = __salt__["file.get_user"](_homebrew_bin())
runas = user if user != __opts__["user"] else None
_cmd = []
if runas:
_cmd = ["sudo -i -n -H -u {} -- ".format(runas)]
_cmd = _cmd + [salt.utils.path.which("brew")] + list(cmd)
_cmd = " ".join(_cmd)
runas = None
result = __salt__["cmd.run_all"](
[salt.utils.path.which("brew")] + list(cmd),
cmd=_cmd,
runas=runas,
output_loglevel="trace",
python_shell=False,

View file

@ -395,51 +395,37 @@ def test_shell_properly_handled_on_macOS():
with patch("pwd.getpwnam") as getpwnam_mock:
with patch("salt.utils.timed_subprocess.TimedProc", mock_proc):
with patch.object(
cmdmod,
"shells",
MagicMock(return_value=["/usr/local/bin/bash", "/bin/zsh"]),
# User default shell is '/usr/local/bin/bash'
user_default_shell = "/usr/local/bin/bash"
with patch.dict(
cmdmod.__salt__,
{"user.info": MagicMock(return_value={"shell": user_default_shell})},
):
# User default shell is '/usr/local/bin/bash'
user_default_shell = "/usr/local/bin/bash"
with patch.dict(
cmdmod.__salt__,
{
"user.info": MagicMock(
return_value={"shell": user_default_shell}
)
},
):
cmd_handler.clear()
cmdmod._run(
"ls", cwd=tempfile.gettempdir(), runas="foobar", use_vt=False
)
cmd_handler.clear()
cmdmod._run(
"ls", cwd=tempfile.gettempdir(), runas="foobar", use_vt=False
)
assert re.search(
"{} -l -c".format(user_default_shell), cmd_handler.cmd
), "cmd invokes right bash session on macOS"
assert re.search(
"{} -l -c".format(user_default_shell), cmd_handler.cmd
), "cmd invokes right bash session on macOS"
# User default shell is '/bin/zsh'
user_default_shell = "/bin/zsh"
with patch.dict(
cmdmod.__salt__,
{"user.info": MagicMock(return_value={"shell": user_default_shell})},
):
# User default shell is '/bin/zsh'
user_default_shell = "/bin/zsh"
with patch.dict(
cmdmod.__salt__,
{
"user.info": MagicMock(
return_value={"shell": user_default_shell}
)
},
):
cmd_handler.clear()
cmdmod._run(
"ls", cwd=tempfile.gettempdir(), runas="foobar", use_vt=False
)
cmd_handler.clear()
cmdmod._run(
"ls", cwd=tempfile.gettempdir(), runas="foobar", use_vt=False
)
assert not re.search(
"bash -l -c", cmd_handler.cmd
), "cmd does not invoke user shell on macOS"
assert not re.search(
"bash -l -c", cmd_handler.cmd
), "cmd does not invoke user shell on macOS"
def test_run_cwd_doesnt_exist_issue_7154():