diff --git a/actionsmap/yunohost.yml b/actionsmap/yunohost.yml index 436104f1d..55d973a69 100644 --- a/actionsmap/yunohost.yml +++ b/actionsmap/yunohost.yml @@ -1161,6 +1161,10 @@ hook: arguments: action: help: Action name + -n: + full: --hooks + help: List of hooks names to execute + nargs: '*' -a: full: --args help: Ordered list of arguments to pass to the script diff --git a/app.py b/app.py index a51612aca..f0049b1b7 100644 --- a/app.py +++ b/app.py @@ -591,7 +591,7 @@ def app_addaccess(auth, apps, users): new_users = new_users +','+ allowed_user app_setting(app, 'allowed_users', new_users.strip()) - hook_callback('post_app_addaccess', [app, new_users]) + hook_callback('post_app_addaccess', args=[app, new_users]) app_ssowatconf(auth) @@ -644,7 +644,7 @@ def app_removeaccess(auth, apps, users): new_users=new_users+','+user['username'] app_setting(app, 'allowed_users', new_users.strip()) - hook_callback('post_app_removeaccess', [app, new_users]) + hook_callback('post_app_removeaccess', args=[app, new_users]) app_ssowatconf(auth) @@ -677,7 +677,7 @@ def app_clearaccess(auth, apps): if 'allowed_users' in app_settings: app_setting(app, 'allowed_users', delete=True) - hook_callback('post_app_clearaccess', [app]) + hook_callback('post_app_clearaccess', args=[app]) app_ssowatconf(auth) diff --git a/backup.py b/backup.py index 031ab9a28..874a34e52 100644 --- a/backup.py +++ b/backup.py @@ -146,7 +146,7 @@ def backup_create(name=None, description=None, output_directory=None, # Run hooks msignals.display(m18n.n('backup_running_hooks')) - hook_callback('backup', [tmp_dir]) + hook_callback('backup', args=[tmp_dir]) # Create backup info file with open("%s/info.json" % tmp_dir, 'w') as f: @@ -278,7 +278,7 @@ def backup_restore(name, ignore_apps=False, force=False): # Run hooks msignals.display(m18n.n('restore_running_hooks')) - hook_callback('restore', [tmp_dir]) + hook_callback('restore', args=[tmp_dir]) # Remove temporary directory os.system('rm -rf %s' % tmp_dir) diff --git a/firewall.py b/firewall.py index 9abea8ec2..08b63b3bc 100644 --- a/firewall.py +++ b/firewall.py @@ -186,7 +186,8 @@ def firewall_reload(): for port in firewall['ipv4'][protocol]: os.system("iptables -A INPUT -p %s --dport %d -j ACCEPT" % (protocol, port)) - hook_callback('post_iptable_rules', [upnp, os.path.exists("/proc/net/if_inet6")]) + hook_callback('post_iptable_rules', + args=[upnp, os.path.exists("/proc/net/if_inet6")]) os.system("iptables -A INPUT -i lo -j ACCEPT") os.system("iptables -A INPUT -p icmp -j ACCEPT") diff --git a/hook.py b/hook.py index 94b5e5cb3..4c7595de2 100644 --- a/hook.py +++ b/hook.py @@ -165,20 +165,40 @@ def hook_list(action, list_by='name', show_info=False): return { 'hooks': result } -def hook_callback(action, args=None): +def hook_callback(action, hooks=[], args=None): """ Execute all scripts binded to an action Keyword argument: action -- Action name + hooks -- List of hooks names to execute args -- Ordered list of arguments to pass to the script """ result = { 'succeed': list(), 'failed': list() } + hooks_dict = {} - # Retrieve hooks by priority - hooks = hook_list(action, list_by='priority', show_info=True)['hooks'] + # Retrieve hooks if not hooks: + hooks_dict = hook_list(action, list_by='priority', + show_info=True)['hooks'] + else: + hooks_names = hook_list(action, list_by='name', + show_info=True)['hooks'] + # Iterate over given hooks names list + for n in hooks: + try: + hl = hooks_names[n] + except KeyError: + raise MoulinetteError(errno.EINVAL, + m18n.n('hook_name_unknown', n)) + # Iterate over hooks with this name + for h in hl: + # Update hooks dict + d = hooks_dict.get(h['priority'], dict()) + d.update({ n: { 'path': h['path'] }}) + hooks_dict[h['priority']] = d + if not hooks_dict: return result # Format arguments @@ -188,16 +208,17 @@ def hook_callback(action, args=None): args = [args] # Iterate over hooks and execute them - for priority in sorted(hooks): - for name, info in iter(hooks[priority].items()): + for priority in sorted(hooks_dict): + for name, info in iter(hooks_dict[priority].items()): + filename = '%s-%s' % (priority, name) try: hook_exec(info['path'], args=args) except: logger.exception("error while executing hook '%s'", info['path']) - result['failed'].append(name) + result['failed'].append(filename) else: - result['succeed'].append(name) + result['succeed'].append(filename) return result diff --git a/locales/en.json b/locales/en.json index 14283c2c6..ae3229f3f 100644 --- a/locales/en.json +++ b/locales/en.json @@ -79,6 +79,7 @@ "firewall_reloaded" : "Firewall successfully reloaded", "hook_list_by_invalid" : "Invalid property to list hook by", + "hook_name_unknown" : "Unknown hook name '{:s}'", "hook_choice_invalid" : "Invalid choice '{:s}'", "hook_argument_missing" : "Missing argument '{:s}'", diff --git a/locales/fr.json b/locales/fr.json index 2280a3de3..827ff34d1 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -79,6 +79,7 @@ "firewall_reloaded" : "Pare-feu rechargé avec succès", "hook_list_by_invalid" : "Propriété pour lister les scripts incorrecte", + "hook_name_unknown" : "Nom de script '{:s}' inconnu", "hook_choice_invalid" : "Choix incorrect : '{:s}'", "hook_argument_missing" : "Argument manquant : '{:s}'", diff --git a/user.py b/user.py index 15ac62d2d..ed922a322 100644 --- a/user.py +++ b/user.py @@ -186,7 +186,8 @@ def user_create(auth, username, firstname, lastname, mail, password): app_ssowatconf(auth) #TODO: Send a welcome mail to user msignals.display(m18n.n('user_created'), 'success') - hook_callback('post_user_create', [username, mail, password, firstname, lastname]) + hook_callback('post_user_create', + args=[username, mail, password, firstname, lastname]) return { 'fullname' : fullname, 'username' : username, 'mail' : mail }