cubicweb/misc/scripts/ldap_change_base_dn.py
author Simon Chabot <simon.chabot@logilab.fr>
Thu, 05 Mar 2020 17:50:00 +0100
branch3.26
changeset 12909 a3d682abd5c3
parent 12146 d540defa0591
permissions -rw-r--r--
Added tag 3.26.17, debian/3.26.17-1 for changeset 5c6d242069b6
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')