Merge pull request #1264 from YunoHost/add-user-in-hook-exec-again

add user in hook exec again
This commit is contained in:
Alexandre Aubin 2021-08-08 18:56:23 +02:00 committed by GitHub
commit 922bd842fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -320,7 +320,7 @@ def hook_callback(
def hook_exec( def hook_exec(
path, args=None, raise_on_error=False, chdir=None, env=None, return_format="json" path, args=None, raise_on_error=False, chdir=None, env=None, user="root", return_format="json"
): ):
""" """
Execute hook from a file with arguments Execute hook from a file with arguments
@ -331,6 +331,7 @@ def hook_exec(
raise_on_error -- Raise if the script returns a non-zero exit code raise_on_error -- Raise if the script returns a non-zero exit code
chdir -- The directory from where the script will be executed chdir -- The directory from where the script will be executed
env -- Dictionnary of environment variables to export env -- Dictionnary of environment variables to export
user -- User with which to run the command
""" """
# Validate hook path # Validate hook path
@ -372,7 +373,7 @@ def hook_exec(
returncode, returndata = _hook_exec_python(path, args, env, loggers) returncode, returndata = _hook_exec_python(path, args, env, loggers)
else: else:
returncode, returndata = _hook_exec_bash( returncode, returndata = _hook_exec_bash(
path, args, chdir, env, return_format, loggers path, args, chdir, env, user, return_format, loggers
) )
# Check and return process' return code # Check and return process' return code
@ -388,7 +389,7 @@ def hook_exec(
return returncode, returndata return returncode, returndata
def _hook_exec_bash(path, args, chdir, env, return_format, loggers): def _hook_exec_bash(path, args, chdir, env, user, return_format, loggers):
from moulinette.utils.process import call_async_output from moulinette.utils.process import call_async_output
@ -416,17 +417,23 @@ def _hook_exec_bash(path, args, chdir, env, return_format, loggers):
f.write("") f.write("")
env["YNH_STDRETURN"] = stdreturn env["YNH_STDRETURN"] = stdreturn
# Construct command to execute
if user == "root":
command = ['sh', '-c']
else:
command = ['sudo', '-n', '-u', user, '-H', 'sh', '-c']
# use xtrace on fd 7 which is redirected to stdout # use xtrace on fd 7 which is redirected to stdout
env["BASH_XTRACEFD"] = "7" env["BASH_XTRACEFD"] = "7"
cmd = '/bin/bash -x "{script}" {args} 7>&1' cmd = '/bin/bash -x "{script}" {args} 7>&1'
cmd = cmd.format(script=cmd_script, args=cmd_args) command.append(cmd.format(script=cmd_script, args=cmd_args))
logger.debug("Executing command '%s'" % cmd) logger.debug("Executing command '%s'" % command)
_env = os.environ.copy() _env = os.environ.copy()
_env.update(env) _env.update(env)
returncode = call_async_output(cmd, loggers, shell=True, cwd=chdir, env=_env) returncode = call_async_output(command, loggers, shell=False, cwd=chdir, env=_env)
raw_content = None raw_content = None
try: try: