cubicweb/misc/scripts/ldap_change_base_dn.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 10589 misc/scripts/ldap_change_base_dn.py@7c23b7de2b8d
child 11774 51c160677afe
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     1
from __future__ import print_function
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     2
5968
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
from base64 import b64decode, b64encode
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
try:
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
    uri, newdn = __args__
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
except ValueError:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     7
    print('USAGE: cubicweb-ctl shell <instance> ldap_change_base_dn.py -- <ldap source uri> <new dn>')
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     8
    print()
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     9
    print('you should not have updated your sources file yet')
5968
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5969
diff changeset
    11
olddn = repo.sources_by_uri[uri].config['user-base-dn']
5968
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
assert olddn != newdn
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
raw_input("Ensure you've stopped the instance, type enter when done.")
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
for eid, extid in sql("SELECT eid, extid FROM entities WHERE source='%s'" % uri):
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
    olduserdn = b64decode(extid)
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
    newuserdn = olduserdn.replace(olddn, newdn)
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
    if newuserdn != olduserdn:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
    21
        print(olduserdn, '->', newuserdn)
5969
caea22e82d83 [ldap] fix update script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5968
diff changeset
    22
        sql("UPDATE entities SET extid='%s' WHERE eid=%s" % (b64encode(newuserdn), eid))
5968
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
commit()
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
    26
print('you can now update the sources file to the new dn and restart the instance')