diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index 493ad2c35..05e706660 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -320,7 +320,7 @@ def hook_callback( 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 @@ -331,6 +331,7 @@ def hook_exec( raise_on_error -- Raise if the script returns a non-zero exit code chdir -- The directory from where the script will be executed env -- Dictionnary of environment variables to export + user -- User with which to run the command """ # Validate hook path @@ -372,7 +373,7 @@ def hook_exec( returncode, returndata = _hook_exec_python(path, args, env, loggers) else: 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 @@ -388,7 +389,7 @@ def hook_exec( 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 @@ -416,17 +417,23 @@ def _hook_exec_bash(path, args, chdir, env, return_format, loggers): f.write("") 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 env["BASH_XTRACEFD"] = "7" 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.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 try: