Merge pull request #1323 from YunoHost/fix-action

Fix action
This commit is contained in:
Alexandre Aubin 2021-09-14 19:13:12 +02:00 committed by GitHub
commit f38b59a590
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 15 deletions

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
YNH_APP_BASEDIR=$(realpath $([[ "$(basename $0)" =~ ^backup|restore$ ]] && echo '../settings' || echo '..')) YNH_APP_BASEDIR=$(realpath $([[ "$(basename $0)" =~ ^backup|restore$ ]] && echo '../settings' || [[ -n "$YNH_ACTION" ]] && echo '.' || echo '..' ))
# Handle script crashes / failures # Handle script crashes / failures
# #

View file

@ -1651,25 +1651,34 @@ def app_action_run(operation_logger, app, action, args=None):
) )
env_dict["YNH_ACTION"] = action env_dict["YNH_ACTION"] = action
_, path = tempfile.mkstemp() tmp_workdir_for_app = _make_tmp_workdir_for_app(app=app)
_, action_script = tempfile.mkstemp(dir=tmp_workdir_for_app)
with open(path, "w") as script: with open(action_script, "w") as script:
script.write(action_declaration["command"]) script.write(action_declaration["command"])
os.chmod(path, 700)
if action_declaration.get("cwd"): if action_declaration.get("cwd"):
cwd = action_declaration["cwd"].replace("$app", app) cwd = action_declaration["cwd"].replace("$app", app)
else: else:
cwd = os.path.join(APPS_SETTING_PATH, app) cwd = tmp_workdir_for_app
# FIXME: this should probably be ran in a tmp workdir... try:
retcode = hook_exec( retcode = hook_exec(
path, action_script,
env=env_dict, env=env_dict,
chdir=cwd, chdir=cwd,
user=action_declaration.get("user", "root"), user=action_declaration.get("user", "root"),
)[0] )[0]
# Calling hook_exec could fail miserably, or get
# manually interrupted (by mistake or because script was stuck)
# In that case we still want to delete the tmp work dir
except (KeyboardInterrupt, EOFError, Exception):
retcode = -1
import traceback
logger.error(m18n.n("unexpected_error", error="\n" + traceback.format_exc()))
finally:
shutil.rmtree(tmp_workdir_for_app)
if retcode not in action_declaration.get("accepted_return_codes", [0]): if retcode not in action_declaration.get("accepted_return_codes", [0]):
msg = "Error while executing action '%s' of app '%s': return code %s" % ( msg = "Error while executing action '%s' of app '%s': return code %s" % (
@ -1680,8 +1689,6 @@ def app_action_run(operation_logger, app, action, args=None):
operation_logger.error(msg) operation_logger.error(msg)
raise YunohostError(msg, raw_msg=True) raise YunohostError(msg, raw_msg=True)
os.remove(path)
operation_logger.success() operation_logger.success()
return logger.success("Action successed!") return logger.success("Action successed!")