mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Merge pull request #1264 from YunoHost/add-user-in-hook-exec-again
add user in hook exec again
This commit is contained in:
commit
922bd842fb
1 changed files with 13 additions and 6 deletions
|
@ -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:
|
||||||
|
|
Loading…
Add table
Reference in a new issue