[enh] Allow to select hooks names to execute in hook_callback

This commit is contained in:
Jérôme Lebleu 2014-12-12 12:10:45 +01:00
parent ba02319915
commit d1b31d5f33
8 changed files with 43 additions and 14 deletions

View file

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

6
app.py
View file

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

View file

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

View file

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

35
hook.py
View file

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

View file

@ -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}'",

View file

@ -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}'",

View file

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