From b8d8f72e36b014fd21cd63a39c3aff7357fc1e18 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 29 Oct 2017 23:19:52 +0100 Subject: [PATCH 1/4] [enh] Add an optional stdinfo output for stuff ran with hook_exec --- src/yunohost/hook.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index 1f971edb6..489cebd5d 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -26,6 +26,7 @@ import os import re import errno +import tempfile from glob import iglob from moulinette import m18n @@ -297,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, - chdir=None, env=None, user="admin"): + chdir=None, env=None, user="admin", enable_stdinfo=False): """ Execute hook from a file with arguments @@ -336,6 +337,12 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, env = {} env['YNH_CWD'] = chdir + if enable_stdinfo: + stdinfo = os.path.join(tempfile.mkdtemp(), "stdinfo") + env['YNH_STDINFO'] = stdinfo + else: + stdinfo = None + # Construct command to execute if user == "root": command = ['sh', '-c'] @@ -361,11 +368,17 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, # Define output callbacks and call command callbacks = ( - lambda l: logger.info(l.rstrip()), - lambda l: logger.warning(l.rstrip()), + lambda l: logger.debug(l.rstrip()), # Stdout + lambda l: logger.warning(l.rstrip()), # Stderr ) + + if stdinfo: + callbacks = ( callbacks[0], callbacks[1], + lambda l: logger.info(l.rstrip())) + returncode = call_async_output( - command, callbacks, shell=False, cwd=chdir + command, callbacks, shell=False, cwd=chdir, + stdinfo=stdinfo ) # Check and return process' return code From 5cd1563014ade84ffa96cac66212697b0002b6fd Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 29 Oct 2017 23:20:10 +0100 Subject: [PATCH 2/4] [enh] Enable stdinfo when running app script --- src/yunohost/app.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 403e76cc4..24229571d 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -489,7 +489,9 @@ def app_change_url(auth, app, domain, path): os.system('chmod +x %s' % os.path.join(os.path.join(APP_TMP_FOLDER, "scripts", "change_url"))) # XXX journal - if hook_exec(os.path.join(APP_TMP_FOLDER, 'scripts/change_url'), args=args_list, env=env_dict, user="root") != 0: + if hook_exec(os.path.join(APP_TMP_FOLDER, 'scripts/change_url'), + args=args_list, env=env_dict, user="root", + enable_stdinfo=True) != 0: logger.error("Failed to change '%s' url." % app) # restore values modified by app_checkurl @@ -603,7 +605,9 @@ def app_upgrade(auth, app=[], url=None, file=None): # Execute App upgrade script os.system('chown -hR admin: %s' % INSTALL_TMP) - if hook_exec(extracted_app_folder + '/scripts/upgrade', args=args_list, env=env_dict, user="root") != 0: + if hook_exec(extracted_app_folder + '/scripts/upgrade', + args=args_list, env=env_dict, user="root", + enable_stdinfo=True) != 0: logger.error(m18n.n('app_upgrade_failed', app=app_instance_name)) else: now = int(time.time()) @@ -738,7 +742,7 @@ def app_install(auth, app, label=None, args=None, no_remove_on_failure=False): try: install_retcode = hook_exec( os.path.join(extracted_app_folder, 'scripts/install'), - args=args_list, env=env_dict, user="root") + args=args_list, env=env_dict, user="root", enable_stdinfo=True) except (KeyboardInterrupt, EOFError): install_retcode = -1 except: @@ -755,7 +759,8 @@ def app_install(auth, app, label=None, args=None, no_remove_on_failure=False): # Execute remove script remove_retcode = hook_exec( os.path.join(extracted_app_folder, 'scripts/remove'), - args=[app_instance_name], env=env_dict_remove, user="root") + args=[app_instance_name], env=env_dict_remove, user="root", + enable_stdinfo=True) if remove_retcode != 0: logger.warning(m18n.n('app_not_properly_removed', app=app_instance_name)) @@ -826,7 +831,8 @@ def app_remove(auth, app): env_dict["YNH_APP_INSTANCE_NAME"] = app env_dict["YNH_APP_INSTANCE_NUMBER"] = str(app_instance_nb) - if hook_exec('/tmp/yunohost_remove/scripts/remove', args=args_list, env=env_dict, user="root") == 0: + if hook_exec('/tmp/yunohost_remove/scripts/remove', args=args_list, + env=env_dict, user="root", enable_stdinfo=True) == 0: logger.success(m18n.n('app_removed', app=app)) if os.path.exists(app_setting_path): From 7c28ec1c1b51dee9c39c6beab8f5df16b62ee632 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 29 Oct 2017 23:37:51 +0100 Subject: [PATCH 3/4] [enh] Adding an app helper to display infos --- data/helpers.d/print | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/data/helpers.d/print b/data/helpers.d/print index 36f4a120e..740933acb 100644 --- a/data/helpers.d/print +++ b/data/helpers.d/print @@ -5,6 +5,14 @@ ynh_die() { exit "${2:-1}" } +# Display a message in the 'INFO' logging category +# +# usage: ynh_info "Some message" +ynh_info() +{ + echo "$1" >>"$YNH_STDINFO" +} + # Ignore the yunohost-cli log to prevent errors with conditionals commands # usage: ynh_no_log COMMAND # Simply duplicate the log, execute the yunohost command and replace the log without the result of this command From 8e8d8e54ab359e2a6e0dc5356ed251cb71a4797e Mon Sep 17 00:00:00 2001 From: ljf Date: Mon, 11 Jun 2018 11:56:54 +0200 Subject: [PATCH 4/4] [enh] Enable ynh_info by default --- src/yunohost/app.py | 13 +++++-------- src/yunohost/hook.py | 9 +++------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 24229571d..e0467d7a7 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -490,8 +490,7 @@ def app_change_url(auth, app, domain, path): # XXX journal if hook_exec(os.path.join(APP_TMP_FOLDER, 'scripts/change_url'), - args=args_list, env=env_dict, user="root", - enable_stdinfo=True) != 0: + args=args_list, env=env_dict, user="root") != 0: logger.error("Failed to change '%s' url." % app) # restore values modified by app_checkurl @@ -606,8 +605,7 @@ def app_upgrade(auth, app=[], url=None, file=None): # Execute App upgrade script os.system('chown -hR admin: %s' % INSTALL_TMP) if hook_exec(extracted_app_folder + '/scripts/upgrade', - args=args_list, env=env_dict, user="root", - enable_stdinfo=True) != 0: + args=args_list, env=env_dict, user="root") != 0: logger.error(m18n.n('app_upgrade_failed', app=app_instance_name)) else: now = int(time.time()) @@ -742,7 +740,7 @@ def app_install(auth, app, label=None, args=None, no_remove_on_failure=False): try: install_retcode = hook_exec( os.path.join(extracted_app_folder, 'scripts/install'), - args=args_list, env=env_dict, user="root", enable_stdinfo=True) + args=args_list, env=env_dict, user="root") except (KeyboardInterrupt, EOFError): install_retcode = -1 except: @@ -759,8 +757,7 @@ def app_install(auth, app, label=None, args=None, no_remove_on_failure=False): # Execute remove script remove_retcode = hook_exec( os.path.join(extracted_app_folder, 'scripts/remove'), - args=[app_instance_name], env=env_dict_remove, user="root", - enable_stdinfo=True) + args=[app_instance_name], env=env_dict_remove, user="root") if remove_retcode != 0: logger.warning(m18n.n('app_not_properly_removed', app=app_instance_name)) @@ -832,7 +829,7 @@ def app_remove(auth, app): env_dict["YNH_APP_INSTANCE_NUMBER"] = str(app_instance_nb) if hook_exec('/tmp/yunohost_remove/scripts/remove', args=args_list, - env=env_dict, user="root", enable_stdinfo=True) == 0: + env=env_dict, user="root") == 0: logger.success(m18n.n('app_removed', app=app)) if os.path.exists(app_setting_path): diff --git a/src/yunohost/hook.py b/src/yunohost/hook.py index 489cebd5d..94ba0de16 100644 --- a/src/yunohost/hook.py +++ b/src/yunohost/hook.py @@ -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, - chdir=None, env=None, user="admin", enable_stdinfo=False): + chdir=None, env=None, user="admin"): """ Execute hook from a file with arguments @@ -337,11 +337,8 @@ def hook_exec(path, args=None, raise_on_error=False, no_trace=False, env = {} env['YNH_CWD'] = chdir - if enable_stdinfo: - stdinfo = os.path.join(tempfile.mkdtemp(), "stdinfo") - env['YNH_STDINFO'] = stdinfo - else: - stdinfo = None + stdinfo = os.path.join(tempfile.mkdtemp(), "stdinfo") + env['YNH_STDINFO'] = stdinfo # Construct command to execute if user == "root":