cubicweb/misc/scripts/ldap_change_base_dn.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 05 Oct 2016 15:30:10 +0200
changeset 11774 51c160677afe
parent 11057 0b59724cb3f2
child 12146 d540defa0591
permissions -rw-r--r--
[repository] Drop the entities.extid column and associated cache This was not necessary anymore with promoted usage of the new data import API. Turn repository's _type_extid_cache to _type_cache with only the entity's type as key. This introduces an backward incompatible change: entity_metas dict doesn't contains anymore the extid key, but it doesn't seem used at all anywhere, so this sounds acceptable. Closes #15538317
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
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    17
for eid, olduserdn in rql("Any X, XURI WHERE X cwuri XURI, X cw_source S, S name %(name)s",
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    18
                          {'name': uri}):
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
    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)
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    22
        sql("UPDATE cw_cwuser SET cw_cwuri='%s' WHERE cw_eid=%s" % (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')