mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Bug 62940 fix cron inconsistency in _write_cron_lines
This commit is contained in:
parent
609e5e1837
commit
0fe1954b91
1 changed files with 18 additions and 6 deletions
|
@ -266,11 +266,8 @@ def _write_cron_lines(user, lines):
|
|||
"""
|
||||
lines = [salt.utils.stringutils.to_str(_l) for _l in lines]
|
||||
path = salt.utils.files.mkstemp()
|
||||
if _check_instance_uid_match("root") or __grains__.get("os_family") in (
|
||||
"Solaris",
|
||||
"AIX",
|
||||
):
|
||||
# In some cases crontab command should be executed as user rather than root
|
||||
# Some OS' do not support specifying user via the `crontab` command
|
||||
if __grains__.get("os_family") in ("Solaris", "AIX"):
|
||||
with salt.utils.files.fpopen(
|
||||
path, "w+", uid=__salt__["file.user_to_uid"](user), mode=0o600
|
||||
) as fp_:
|
||||
|
@ -278,10 +275,25 @@ def _write_cron_lines(user, lines):
|
|||
ret = __salt__["cmd.run_all"](
|
||||
_get_cron_cmdstr(path), runas=user, python_shell=False
|
||||
)
|
||||
else:
|
||||
# If Salt is running from same user as requested in cron module we don't need any user switch
|
||||
elif _check_instance_uid_match(user):
|
||||
with salt.utils.files.fpopen(path, "w+", mode=0o600) as fp_:
|
||||
fp_.writelines(lines)
|
||||
ret = __salt__["cmd.run_all"](_get_cron_cmdstr(path), python_shell=False)
|
||||
# If Salt is running from root user it could modify any user's crontab
|
||||
elif _check_instance_uid_match("root"):
|
||||
with salt.utils.files.fpopen(path, "w+", mode=0o600) as fp_:
|
||||
fp_.writelines(lines)
|
||||
ret = __salt__["cmd.run_all"](_get_cron_cmdstr(path, user), python_shell=False)
|
||||
# Edge cases here, let's try do a runas
|
||||
else:
|
||||
with salt.utils.files.fpopen(
|
||||
path, "w+", uid=__salt__["file.user_to_uid"](user), mode=0o600
|
||||
) as fp_:
|
||||
fp_.writelines(lines)
|
||||
ret = __salt__["cmd.run_all"](
|
||||
_get_cron_cmdstr(path), runas=user, python_shell=False
|
||||
)
|
||||
os.remove(path)
|
||||
return ret
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue