[services] Commentaires, réarangement du code, paramettrage des derniers services
This commit is contained in:
parent
60d49b1f49
commit
2940dae3b8
1 changed files with 118 additions and 10 deletions
128
services.py
128
services.py
|
@ -23,29 +23,124 @@ services_to_attrs['ports'] = [ attributs.portUDPout, attributs.portUDPin, attrib
|
||||||
services_to_attrs['droits'] = [ attributs.droits ]
|
services_to_attrs['droits'] = [ attributs.droits ]
|
||||||
services_to_attrs['mail_ajout_droits'] = [ attributs.droits ]
|
services_to_attrs['mail_ajout_droits'] = [ attributs.droits ]
|
||||||
services_to_attrs['dhcp'] = services_to_attrs['macip']
|
services_to_attrs['dhcp'] = services_to_attrs['macip']
|
||||||
|
services_to_attrs['mail_bienvenue'] = [ attributs.mail, attributs.canonicalAlias ]
|
||||||
services_to_attrs['mail_modif'] = []
|
services_to_attrs['mail_modif'] = [ attributs.droits, attributs.exempt ] + services_to_attrs['ports'] + services_to_attrs['macip']
|
||||||
|
|
||||||
services_to_objects={}
|
services_to_objects={}
|
||||||
services_to_objects['delete']={}
|
services_to_objects['delete']={}
|
||||||
services_to_objects['create']={}
|
services_to_objects['create']={}
|
||||||
services_to_objects['create']['home'] = [objets.adherent, objets.club]
|
services_to_objects['create']['home'] = [objets.adherent, objets.club]
|
||||||
services_to_objects['create']['mail_bienvenue'] = [objets.adherent]
|
|
||||||
services_to_objects['delete']['del_user'] = [objets.adherent, objets.club]
|
services_to_objects['delete']['del_user'] = [objets.adherent, objets.club]
|
||||||
|
|
||||||
|
|
||||||
|
def services_to_args_mail_modif(x):
|
||||||
|
if isinstance(x, attributs.droits):
|
||||||
|
return [ "uid=%s" % x.parent['uid'][0] ]
|
||||||
|
elif isinstance(x, attributs.Attr) and x.__class__ in [ attributs.exempt ] + services_to_attrs['ports']:
|
||||||
|
return [ "mid=%s" % x.parent['mid'][0] ]
|
||||||
|
elif isinstance(x.parent, objets.machine) and isinstance(x.parent.proprio(), objets.AssociationCrans):
|
||||||
|
return [ "mid=%s" % x.parent['mid'][0] ]
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def services_to_args_macip(x):
|
||||||
|
if isinstance(x.parent, objets.machine):
|
||||||
|
if isinstance(x, attributs.ipHostNumber):
|
||||||
|
return [str(x)]
|
||||||
|
else:
|
||||||
|
[ str(ip) for ip in x.parent.get('ipHostNumber',[]) ]
|
||||||
|
elif isinstance(x.parent, objets.proprio):
|
||||||
|
return [ str(ip) for m in x.parent.machines() for ip in m.get('ipHostNumber',[])]
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def services_to_args_port(x):
|
||||||
|
if isinstance(x, attributs.ipHostNumber) or isinstance(x, attributs.ip6HostNumber):
|
||||||
|
[str(x)]
|
||||||
|
else:
|
||||||
|
[ str(ip) for ip in x.parent.get('ipHostNumber',[]) ]
|
||||||
|
|
||||||
|
# Trouver comment faire le cas où on ajoute une redirection mail (il faut alors retourner un quadruplet "homedir,uidNumber,uid,mail")
|
||||||
|
def services_to_args_home(x):
|
||||||
|
if isinstance(x, attributs.Attr):
|
||||||
|
proprio=x.parent
|
||||||
|
elif isinstance(x, objets.proprio):
|
||||||
|
proprio=x
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
if u'cransAccount' in [ str(o) for o in proprio.parent['objectClass']]:
|
||||||
|
return [ "%s,%s,%s" % (proprio['homeDirectory'][0],proprio['uidNumber'][0],proprio['uid'][0]) ]
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def services_to_args_del_user(x):
|
||||||
|
if isinstance(x, attributs.Attr):
|
||||||
|
proprio=x.parent
|
||||||
|
elif isinstance(x, objets.proprio):
|
||||||
|
proprio=x
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
if u'cransAccount' in [ str(o) for o in proprio.parent['objectClass']]:
|
||||||
|
return [ "%s,%s" % (proprio['uid'][0], proprio['homeDirectory'][0]) ]
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def services_to_args_blacklist(x):
|
||||||
|
if isinstance(x.parent, objets.machine):
|
||||||
|
return [ str(ip) for m in x.parent.proprio().machines() for ip in m['ipHostNumber'] ]
|
||||||
|
elif isinstance(x.parent, objets.proprio):
|
||||||
|
return [ str(ip) for m in x.parent.machines() for ip in m['ipHostNumber'] ]
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def services_to_args_mail_bienvenue(x):
|
||||||
|
if isinstance(x, attributs.Attr) and isinstance(x.parent, objets.adherent):
|
||||||
|
adh=x.parent
|
||||||
|
elif isinstance(x, objets.adherent):
|
||||||
|
adh=x
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
if u'cransAccount' in [ str(o) for o in adh.parent['objectClass']]:
|
||||||
|
return [ "%s" % adh['canonicalAlias'][0] ]
|
||||||
|
else:
|
||||||
|
return [ "%s" % adh['mail'][0] ]
|
||||||
|
|
||||||
|
def services_to_time_blacklist(x):
|
||||||
|
if isinstance(x, attributs.blacklist):
|
||||||
|
# Un set à au plus deux valeur : la date de début et la date de fin de la blacklist
|
||||||
|
return set([(0 if x.value['debut'] == '-' else x.value['debut']), (0 if x.value['fin'] == '-' else x.value['fin'])])
|
||||||
|
else:
|
||||||
|
return [0]
|
||||||
|
|
||||||
|
def services_to_time_mail_bienvenue(x):
|
||||||
|
if isinstance(x, attributs.Attr) and isinstance(x.parent, objets.adherent):
|
||||||
|
adh=x.parent
|
||||||
|
elif isinstance(x, objets.adherent):
|
||||||
|
adh=x
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
if u'cransAccount' in [ str(o) for o in adh.parent['objectClass']]:
|
||||||
|
return [ time.time() + 660 ]
|
||||||
|
else:
|
||||||
|
return [ 0 ]
|
||||||
|
|
||||||
# génération des arguments du service à redémarrer (par defaut [])
|
# génération des arguments du service à redémarrer (par defaut [])
|
||||||
services_to_args={}
|
services_to_args={}
|
||||||
services_to_args['macip']=lambda x: issubclass(type(x.parent), objets.machine) and ([str(x)] if isinstance(x, attributs.ipHostNumber) else [ str(ip) for ip in x.parent.get('ipHostNumber',[]) ]) or ([ str(ip) for m in x.parent.machines() for ip in m.get('ipHostNumber',[])] if issubclass(type(x.parent), objets.proprio) else [])
|
services_to_args['macip']=services_to_args_macip
|
||||||
## Inutile pour blackliste pour le moment
|
## Inutile pour blackliste pour le moment
|
||||||
#services_to_args['blacklist']=lambda x: issubclass(type(x.parent), lc_ldap.machine) and [ str(ip) for m in x.parent.proprio().machines() for ip in m['ipHostNumber'] ] or [ str(ip) for m in x.parent.machines() for ip in m['ipHostNumber'] ]
|
#services_to_args['blacklist']=services_to_args_blacklist
|
||||||
services_to_args['port']=lambda x: [str(x)] if isinstance(x, attributs.ipHostNumber) or isinstance(x, attributs.ip6HostNumber) else [ str(ip) for ip in x.parent.get('ipHostNumber',[]) ]
|
services_to_args['port']=services_to_args_port
|
||||||
services_to_args['home']=lambda x: u'cransAccount' in [ str(o) for o in x.parent['objectClass']] and [ "%s,%s,%s" % (x.parent['homeDirectory'][0],x.parent['uidNumber'][0],x.parent['uid'][0]) ] or [ "%s,%s,%s,%s" % (x.parent['homeDirectory'][0],x.parent['uidNumber'][0],x.parent['uid'][0],x.parent['mail'][0]) ]
|
services_to_args['home']=services_to_args_home
|
||||||
services_to_args['mail_ajout_droits'] = lambda x: "%s:%s" % (x.parent['uid'][0], x)
|
services_to_args['mail_ajout_droits'] = lambda x: "%s:%s" % (x.parent['uid'][0], x)
|
||||||
|
services_to_args['del_user']=services_to_args_del_user
|
||||||
|
services_to_args['mail_bienvenue']=services_to_args_mail_bienvenue
|
||||||
|
services_to_args['mail_modif']=services_to_args_mail_modif
|
||||||
|
|
||||||
# Quand redémarrer le service (par defaut [0]), doit returner un iterable avec potentiellement plusieurs dates
|
# Quand redémarrer le service (par defaut [0]), doit returner un iterable avec potentiellement plusieurs dates
|
||||||
services_to_time={}
|
services_to_time={}
|
||||||
services_to_time['blacklist']=lambda x: isinstance(x, attributs.blacklist) and set([(0 if x.value['debut'] == '-' else x.value['debut']), (0 if x.value['fin'] == '-' else x.value['fin'])]) or [0]
|
services_to_time['blacklist']=services_to_time_blacklist
|
||||||
|
services_to_time['mail_bienvenue']=services_to_time_mail_bienvenue
|
||||||
|
|
||||||
attrs_to_services = {}
|
attrs_to_services = {}
|
||||||
for (key, values) in services_to_attrs.items():
|
for (key, values) in services_to_attrs.items():
|
||||||
|
@ -197,11 +292,16 @@ def service_to_restart(conn, new=None, args=[], start=0):
|
||||||
|
|
||||||
def services_to_restart(conn, old_attrs={}, new_attrs={}, created_object=[], deleted_object=[]):
|
def services_to_restart(conn, old_attrs={}, new_attrs={}, created_object=[], deleted_object=[]):
|
||||||
"""Détermine quels sont les services à reconfigurer"""
|
"""Détermine quels sont les services à reconfigurer"""
|
||||||
|
|
||||||
|
# On tranforme les dico *_attrs du type string -> attr en *_attrs_c du type class -> attr
|
||||||
old_attrs_c = dict((attributs.AttributeFactory.get(key), value) for key,value in old_attrs.items() if attributs.AttributeFactory.get(key, fallback=None) != None and value)
|
old_attrs_c = dict((attributs.AttributeFactory.get(key), value) for key,value in old_attrs.items() if attributs.AttributeFactory.get(key, fallback=None) != None and value)
|
||||||
new_attrs_c = dict((attributs.AttributeFactory.get(key), value) for key,value in new_attrs.items() if attributs.AttributeFactory.get(key, fallback=None) != None and value)
|
new_attrs_c = dict((attributs.AttributeFactory.get(key), value) for key,value in new_attrs.items() if attributs.AttributeFactory.get(key, fallback=None) != None and value)
|
||||||
|
|
||||||
|
# Les attributs crée
|
||||||
created_attr = [ attr for name in new_attrs_c.keys() if not name in old_attrs_c.keys() for attr in new_attrs_c[name]]
|
created_attr = [ attr for name in new_attrs_c.keys() if not name in old_attrs_c.keys() for attr in new_attrs_c[name]]
|
||||||
|
# Les attributs supprimé
|
||||||
deleted_attr = [ attr for name in old_attrs_c.keys() if not name in new_attrs_c.keys() for attr in old_attrs_c[name]]
|
deleted_attr = [ attr for name in old_attrs_c.keys() if not name in new_attrs_c.keys() for attr in old_attrs_c[name]]
|
||||||
|
# Les attributs mis à jour : liste de (ancien, nouveau, ancien\nouveau, nouveau\ancien) pour les types d'attibut a la fois dans old_attrs et new_attrs
|
||||||
updated_attr = [ (old_attrs_c[name], new_attrs_c[name],
|
updated_attr = [ (old_attrs_c[name], new_attrs_c[name],
|
||||||
[ i for i in old_attrs_c[name] if i.value not in [ j.value for j in new_attrs_c[name]]],
|
[ i for i in old_attrs_c[name] if i.value not in [ j.value for j in new_attrs_c[name]]],
|
||||||
[ i for i in new_attrs_c[name] if i.value not in [ j.value for j in old_attrs_c[name]]],
|
[ i for i in new_attrs_c[name] if i.value not in [ j.value for j in old_attrs_c[name]]],
|
||||||
|
@ -209,6 +309,8 @@ def services_to_restart(conn, old_attrs={}, new_attrs={}, created_object=[], del
|
||||||
updated_attr_new = [ i for j in updated_attr for i in j[3]]
|
updated_attr_new = [ i for j in updated_attr for i in j[3]]
|
||||||
updated_attr_old = [ i for j in updated_attr for i in j[2]]
|
updated_attr_old = [ i for j in updated_attr for i in j[2]]
|
||||||
|
|
||||||
|
|
||||||
|
# Génération du dico "nom de service à redémarrer" -> "attribut dont dépende le service qui ont été modifier"
|
||||||
services_to_restart = {}
|
services_to_restart = {}
|
||||||
for attr in [ attr for l in [created_attr, deleted_attr, updated_attr_new ] for attr in l]:
|
for attr in [ attr for l in [created_attr, deleted_attr, updated_attr_new ] for attr in l]:
|
||||||
for service in attrs_to_services.get(attr.__class__, []):
|
for service in attrs_to_services.get(attr.__class__, []):
|
||||||
|
@ -216,21 +318,25 @@ def services_to_restart(conn, old_attrs={}, new_attrs={}, created_object=[], del
|
||||||
|
|
||||||
for obj in created_object:
|
for obj in created_object:
|
||||||
for service in objects_to_services['create'].get(obj.__class__, []):
|
for service in objects_to_services['create'].get(obj.__class__, []):
|
||||||
services_to_restart[service] = services_to_restart.get(service, [])
|
services_to_restart[service] = services_to_restart.get(service, []) + [obj]
|
||||||
|
|
||||||
for obj in deleted_object:
|
for obj in deleted_object:
|
||||||
for service in objects_to_services['delete'].get(obj.__class__, []):
|
for service in objects_to_services['delete'].get(obj.__class__, []):
|
||||||
services_to_restart[service] = services_to_restart.get(service, [])
|
services_to_restart[service] = services_to_restart.get(service, []) + [obj]
|
||||||
|
|
||||||
for service in services_to_restart.keys():
|
for service in services_to_restart.keys():
|
||||||
|
# ok, la variable s'appel attr, mais ça paut aussi être un objet
|
||||||
for attr in services_to_restart[service]:
|
for attr in services_to_restart[service]:
|
||||||
arg = set()
|
arg = set()
|
||||||
|
# Il y a une fonction pour générer des arguements, ils sont donc nécessaire
|
||||||
if service in services_to_args.keys():
|
if service in services_to_args.keys():
|
||||||
arg = arg.union(services_to_args[service](attr))
|
arg = arg.union(services_to_args[service](attr))
|
||||||
if attr in updated_attr_new:
|
if attr in updated_attr_new:
|
||||||
for old_attr in updated_attr_old:
|
for old_attr in updated_attr_old:
|
||||||
if attr.__class__ == old_attr.__class__:
|
if attr.__class__ == old_attr.__class__:
|
||||||
arg = arg.union(services_to_args[service](old_attr))
|
arg = arg.union(services_to_args[service](old_attr))
|
||||||
|
if not arg: # services_to_args retour une liste vide d'arguments, on passe à l'attribut suivant
|
||||||
|
continue
|
||||||
|
|
||||||
# Cas du dhcp
|
# Cas du dhcp
|
||||||
if attr.__class__ in services_to_attrs['dhcp']:
|
if attr.__class__ in services_to_attrs['dhcp']:
|
||||||
|
@ -249,9 +355,11 @@ def services_to_restart(conn, old_attrs={}, new_attrs={}, created_object=[], del
|
||||||
except socket.error:
|
except socket.error:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Il y a une fonction pour dire quand il faut redémarrer le service
|
||||||
if service in services_to_time.keys():
|
if service in services_to_time.keys():
|
||||||
for start in services_to_time[service](attr):
|
for start in services_to_time[service](attr):
|
||||||
service_to_restart(conn, service, list(arg), start)
|
service_to_restart(conn, service, list(arg), start)
|
||||||
|
# Sinon, on le redémarre tout de suite
|
||||||
else:
|
else:
|
||||||
service_to_restart(conn, service, list(arg), 0)
|
service_to_restart(conn, service, list(arg), 0)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue