misc/scripts/ldap_change_base_dn.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 02 Jul 2013 17:07:34 +0200
changeset 9460 a2a0bc984863
parent 5969 caea22e82d83
child 10589 7c23b7de2b8d
permissions -rw-r--r--
[config] cleanup/refactor server sources file values handling * kill former `sources` method, misnamed since we've only the system source configuration in the sources file (and some default admin account information) * introduce new system_source_config and default_admin_config properties to access to the two information in this file Then use one or the other when appropriate
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
     1
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
     2
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
     3
    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
     4
except ValueError:
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
    print 'USAGE: cubicweb-ctl shell <instance> ldap_change_base_dn.py -- <ldap source uri> <new dn>'
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
    print
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
    print 'you should not have updated your sources file yet'
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5969
diff changeset
     9
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
    10
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
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
    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
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
    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
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
    16
    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
    17
    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
    18
    if newuserdn != olduserdn:
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
        print olduserdn, '->', newuserdn
5969
caea22e82d83 [ldap] fix update script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5968
diff changeset
    20
        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
    21
2e039d1e150c [c-c shell] script to update the base dn of an ldap source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
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
    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
print 'you can now update the sources file to the new dn and restart the instance'