[enh] Support passing env var to hook_exec

This commit is contained in:
Julien Malik 2016-03-14 11:04:14 +01:00 committed by Jérôme Lebleu
parent 7223a627af
commit 7ce7d6fc15

View file

@ -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)))