diff --git a/objets.py b/objets.py index a3f8990..918e2ac 100644 --- a/objets.py +++ b/objets.py @@ -52,7 +52,6 @@ import lc_ldap import crans_utils import attributs import ldap_locks -import services import variables ## import de /usr/scripts/ @@ -238,7 +237,7 @@ class CransLdapObject(object): self.conn.lockholder.purge(id(self)) # Services à relancer - services.services_to_restart(self.conn, {}, self._modifs) + services.services_to_restart(self.conn, {}, self._modifs, created_object=[self]) self._post_creation() # Vérifications après insertion. @@ -271,7 +270,7 @@ class CransLdapObject(object): self.conn.delete_s(self.dn) self.conn.lockholder.purge(id(self)) self._post_deletion() - services.services_to_restart(self.conn, self.attrs, {}) + services.services_to_restart(self.conn, self.attrs, {}, deleted_object=[self]) def save(self): """Sauvegarde dans la base les modifications apportées à l'objet. @@ -619,7 +618,7 @@ class proprio(CransLdapObject): machine.delete(comm, login) self.bury(comm, login) self.conn.delete_s(self.dn) - services.services_to_restart(self.conn, self.attrs, {}) + services.services_to_restart(self.conn, self.attrs, {}, deleted_object=[self]) class machine(CransLdapObject): u""" Une machine """ @@ -945,3 +944,5 @@ class facture(CransLdapObject): class service(CransLdapObject): ldap_name = "service" + +import services diff --git a/services.py b/services.py index 0e9aad5..70f6ea5 100644 --- a/services.py +++ b/services.py @@ -23,12 +23,16 @@ services_to_attrs['ports'] = [ attributs.portUDPout, attributs.portUDPin, attrib services_to_attrs['droits'] = [ attributs.droits ] services_to_attrs['mail_ajout_droits'] = [ attributs.droits ] services_to_attrs['dhcp'] = services_to_attrs['macip'] -#services_to_attrs['home'] = [ attributs.homeDirectory ] ## Vérifier que ça ne fasse pas de caca -services_to_attrs['del_user'] = [] -services_to_attrs['mail_bienvenue'] = [] services_to_attrs['mail_modif'] = [] +services_to_objects={} +services_to_objects['delete']={} +services_to_objects['create']={} +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] + # génération des arguments du service à redémarrer (par defaut []) services_to_args={} @@ -39,15 +43,22 @@ services_to_args['port']=lambda x: [str(x)] if isinstance(x, attributs.ipHostNum 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['mail_ajout_droits'] = lambda x: "%s:%s" % (x.parent['uid'][0], x) -# Quand redémarrer le service (par defaut 0) +# Quand redémarrer le service (par defaut [0]), doit returner un iterable avec potentiellement plusieurs dates services_to_time={} -services_to_time['blacklist']=lambda x: isinstance(x, attributs.blacklist) and (0 if x.value['debut'] == '-' else x.value['debut']) or 0 +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] attrs_to_services = {} for (key, values) in services_to_attrs.items(): for value in values: attrs_to_services[value] = attrs_to_services.get(value, []) + [key] +objects_to_services={} +for status in ['delete', 'create']: + objects_to_services[status]={} + for (key, values) in services_to_objects[status].items(): + for value in values: + objects_to_services[status][value]=objects_to_services[status].get(value, []) + [key] + # Identique à celle dans ldap_crans.py def preattr(val): """ @@ -184,7 +195,7 @@ def service_to_restart(conn, new=None, args=[], start=0): # Existe déja => rien à faire pass -def services_to_restart(conn, old_attrs={}, new_attrs={}): +def services_to_restart(conn, old_attrs={}, new_attrs={}, created_object=[], deleted_object=[]): """Détermine quels sont les services à reconfigurer""" 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) @@ -203,9 +214,16 @@ def services_to_restart(conn, old_attrs={}, new_attrs={}): for service in attrs_to_services.get(attr.__class__, []): services_to_restart[service] = services_to_restart.get(service, []) + [attr] + for obj in created_object: + for service in objects_to_services['create'].get(obj.__class__, []): + services_to_restart[service] = services_to_restart.get(service, []) + + for obj in deleted_object: + for service in objects_to_services['delete'].get(obj.__class__, []): + services_to_restart[service] = services_to_restart.get(service, []) + for service in services_to_restart.keys(): for attr in services_to_restart[service]: - start = 0 arg = set() if service in services_to_args.keys(): arg = arg.union(services_to_args[service](attr)) @@ -232,9 +250,12 @@ def services_to_restart(conn, old_attrs={}, new_attrs={}): pass if service in services_to_time.keys(): - start = services_to_time[service](attr) - #print "%s,%s,%s" % (service, arg, start) - service_to_restart(conn, service, list(arg), start) + for start in services_to_time[service](attr): + service_to_restart(conn, service, list(arg), start) + else: + service_to_restart(conn, service, list(arg), 0) + else: + service_to_restart(conn, service, [], 0)