From 7ce7d6fc15bc22ee812eeeef5f946eac0f61540a Mon Sep 17 00:00:00 2001 From: Julien Malik Date: Mon, 14 Mar 2016 11:04:14 +0100 Subject: [PATCH] [enh] Support passing env var to hook_exec --- src/yunohost/hook.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index 3ee11947a..bfad4fcd5 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -294,7 +294,7 @@ def hook_callback(action, hooks=[], args=None, no_trace=False, chdir=None, def hook_exec(path, args=None, raise_on_error=False, no_trace=False, - chdir=None): + chdir=None, env=None): """ Execute hook from a file with arguments @@ -304,6 +304,7 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, raise_on_error -- Raise if the script returns a non-zero exit code no_trace -- Do not print each command that will be executed chdir -- The directory from where the script will be executed + env -- Dictionnary of environment variables to export """ from moulinette.utils.process import call_async_output @@ -328,14 +329,18 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, else: cmd_script = path + envcli = '' + if env is not None and isinstance(env, dict): + envcli = ' '.join([ '{key}="{val}"'.format(key=key, val=val) for key,val in env.items()]) + # Construct command to execute command = ['sudo', '-n', '-u', 'admin', '-H', 'sh', '-c'] if no_trace: - cmd = '/bin/bash "{script}" {args}' + cmd = '{envcli} /bin/bash "{script}" {args}' else: # use xtrace on fd 7 which is redirected to stdout - cmd = 'BASH_XTRACEFD=7 /bin/bash -x "{script}" {args} 7>&1' - command.append(cmd.format(script=cmd_script, args=cmd_args)) + cmd = '{envcli} BASH_XTRACEFD=7 /bin/bash -x "{script}" {args} 7>&1' + command.append(cmd.format(envcli=envcli, script=cmd_script, args=cmd_args)) if logger.isEnabledFor(log.DEBUG): logger.info(m18n.n('executing_command', command=' '.join(command)))