cubicweb/misc/scripts/ldap_change_base_dn.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Wed, 05 Apr 2017 14:59:09 +0200
branch3.25
changeset 12146 d540defa0591
parent 11774 51c160677afe
permissions -rw-r--r--
[server] Add source_by_eid and source_by_uri methods to repository Most of the times we only need to retrieve one source (either by uri or eid) and querying sources_by_eid and sources_by_uri properties on repository just for one item is costly. So these methods query what's needed. We issue a ValueError (instead of KeyError for sources_by_{eid,uri} dict) in case the key is not found.
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
12146
d540defa0591 [server] Add source_by_eid and source_by_uri methods to repository
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11774
diff changeset
    11
olddn = repo.source_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')