mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Add a new 'plain_dict' return type for hook_exec, and use it for app config
This commit is contained in:
parent
3e75957f96
commit
b1634785fb
2 changed files with 27 additions and 34 deletions
|
@ -1606,31 +1606,12 @@ def app_config_show_panel(app):
|
||||||
"YNH_APP_INSTANCE_NAME": app,
|
"YNH_APP_INSTANCE_NAME": app,
|
||||||
"YNH_APP_INSTANCE_NUMBER": str(app_instance_nb),
|
"YNH_APP_INSTANCE_NUMBER": str(app_instance_nb),
|
||||||
}
|
}
|
||||||
parsed_values = {}
|
|
||||||
|
|
||||||
# I need to parse stdout to communicate between scripts because I can't
|
return_code, parsed_values = hook_exec(config_script,
|
||||||
# read the child environment :( (that would simplify things so much)
|
|
||||||
# after hours of research this is apparently quite a standard way, another
|
|
||||||
# option would be to add an explicite pipe or a named pipe for that
|
|
||||||
# a third option would be to write in a temporary file but I don't like
|
|
||||||
# that because that could expose sensitive data
|
|
||||||
def parse_stdout(line):
|
|
||||||
line = line.rstrip()
|
|
||||||
logger.info(line)
|
|
||||||
|
|
||||||
if line.strip().startswith("YNH_CONFIG_") and "=" in line:
|
|
||||||
# XXX error handling?
|
|
||||||
# XXX this might not work for multilines stuff :( (but echo without
|
|
||||||
# formatting should do it no?)
|
|
||||||
key, value = line.strip().split("=", 1)
|
|
||||||
logger.debug("config script declared: %s -> %s", key, value)
|
|
||||||
parsed_values[key] = value
|
|
||||||
|
|
||||||
return_code = hook_exec(config_script,
|
|
||||||
args=["show"],
|
args=["show"],
|
||||||
env=env,
|
env=env,
|
||||||
stdout_callback=parse_stdout,
|
return_format="plain_dict"
|
||||||
)[0]
|
)
|
||||||
|
|
||||||
if return_code != 0:
|
if return_code != 0:
|
||||||
raise Exception("script/config show return value code: %s (considered as an error)", return_code)
|
raise Exception("script/config show return value code: %s (considered as an error)", return_code)
|
||||||
|
|
|
@ -298,7 +298,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,
|
def hook_exec(path, args=None, raise_on_error=False, no_trace=False,
|
||||||
chdir=None, env=None, user="root", stdout_callback=None,
|
chdir=None, env=None, user="root", stdout_callback=None,
|
||||||
stderr_callback=None):
|
stderr_callback=None, return_format="json"):
|
||||||
"""
|
"""
|
||||||
Execute hook from a file with arguments
|
Execute hook from a file with arguments
|
||||||
|
|
||||||
|
@ -401,19 +401,31 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False,
|
||||||
try:
|
try:
|
||||||
with open(stdreturn, 'r') as f:
|
with open(stdreturn, 'r') as f:
|
||||||
raw_content = f.read()
|
raw_content = f.read()
|
||||||
|
returncontent = {}
|
||||||
|
|
||||||
|
if return_format == "json":
|
||||||
if raw_content != '':
|
if raw_content != '':
|
||||||
returnjson = read_json(stdreturn)
|
try:
|
||||||
else:
|
returncontent = read_json(stdreturn)
|
||||||
returnjson = {}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise YunohostError('hook_json_return_error', path=path, msg=str(e),
|
raise YunohostError('hook_json_return_error',
|
||||||
|
path=path, msg=str(e),
|
||||||
raw_content=raw_content)
|
raw_content=raw_content)
|
||||||
|
|
||||||
|
elif return_format == "plain_dict":
|
||||||
|
for line in raw_content.split("\n"):
|
||||||
|
if "=" in line:
|
||||||
|
key, value = line.strip().split("=", 1)
|
||||||
|
returncontent[key] = value
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise YunohostError("Excepted value for return_format is either 'json' or 'plain_dict', got '%s'" % return_format)
|
||||||
finally:
|
finally:
|
||||||
stdreturndir = os.path.split(stdreturn)[0]
|
stdreturndir = os.path.split(stdreturn)[0]
|
||||||
os.remove(stdreturn)
|
os.remove(stdreturn)
|
||||||
os.rmdir(stdreturndir)
|
os.rmdir(stdreturndir)
|
||||||
|
|
||||||
return returncode, returnjson
|
return returncode, returncontent
|
||||||
|
|
||||||
|
|
||||||
def _extract_filename_parts(filename):
|
def _extract_filename_parts(filename):
|
||||||
|
|
Loading…
Add table
Reference in a new issue