misc/scripts/ldapuser2ldapfeed.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 31 Jan 2012 21:43:24 +0100
changeset 8188 1867e252e487
child 8468 f52bb4226020
child 8483 4ba11607d84a
permissions -rw-r--r--
[repository] ldap-feed source. Closes #2086984 datafeed based source which copy a subtree of the ldap directory into the system database. Authentication still go through ldap though. Pros: * don't need temporary tables and such for multi-sources RQL queries execution * much more flexible to enhance / configure behaviour (you simply have to replace the parser) * run better when ldap isn't reachable Cons: * no more 'on the fly' discovery of users (though a user authenticating itself will be automatically added if it doesn't exist in the db yet) * synchronization may be heavy if there are a lot of users A new cw.server.ldaputils containing code in common between former ldapuser and new ldapfeed sources has been introduced. Also ldapuser source now uses url instead of custom host/protocol option so it looks like a datafeed source (could be improved).

"""turn a pyro source into a datafeed source

Once this script is run, execute c-c db-check to cleanup relation tables.
"""
import sys

try:
    source_name, = __args__
    source = repo.sources_by_uri[source_name]
except ValueError:
    print('you should specify the source name as script argument (i.e. after --'
          ' on the command line)')
    sys.exit(1)
except KeyError:
    print '%s is not an active source' % source_name
    sys.exit(1)

# check source is reachable before doing anything
if not source.get_connection().cnx:
    print '%s is not reachable. Fix this before running this script' % source_name
    sys.exit(1)

raw_input('Ensure you have shutdown all instances of this application before continuing.'
          ' Type enter when ready.')

system_source = repo.system_source

from datetime import datetime
from cubicweb.server.edition import EditedEntity


session.mode = 'write' # hold on the connections set

print '******************** backport entity content ***************************'

todelete = {}
for entity in rql('Any X WHERE X cw_source S, S eid %(s)s', {'s': source.eid}).entities():
        etype = entity.__regid__
        if not source.support_entity(etype):
            print "source doesn't support %s, delete %s" % (etype, entity.eid)
        else:
            try:
                entity.complete()
            except Exception:
                print '%s %s much probably deleted, delete it (extid %s)' % (
                    etype, entity.eid, entity.cw_metainformation()['extid'])
            else:
                print 'get back', etype, entity.eid
                entity.cw_edited = EditedEntity(entity, **entity.cw_attr_cache)
                if not entity.creation_date:
                    entity.cw_edited['creation_date'] = datetime.now()
                if not entity.modification_date:
                    entity.cw_edited['modification_date'] = datetime.now()
                if not entity.upassword:
                    entity.cw_edited['upassword'] = u''
                if not entity.cwuri:
                    entity.cw_edited['cwuri'] = '%s/?dn=%s' % (
                        source.urls[0], entity.cw_metainformation()['extid'])
                print entity.cw_edited
                system_source.add_entity(session, entity)
                sql("UPDATE entities SET source='system' "
                    "WHERE eid=%(eid)s", {'eid': entity.eid})
                continue
        todelete.setdefault(etype, []).append(entity)

# only cleanup entities table, remaining stuff should be cleaned by a c-c
# db-check to be run after this script
for entities in todelete.values():
    system_source.delete_info_multi(session, entities, source_name)


source_ent = rql('CWSource S WHERE S eid %(s)s', {'s': source.eid}).get_entity(0, 0)
source_ent.set_attributes(type=u"ldapfeed", parser=u"ldapfeed")


commit()