From af003742d599cc8b7b85ad6273377c5b9e8c70f0 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 17 Nov 2020 12:27:52 +0100 Subject: [PATCH] Start migration of structural entries --- lib/ldap_migration/__init__.py | 85 ++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 4 deletions(-) diff --git a/lib/ldap_migration/__init__.py b/lib/ldap_migration/__init__.py index 9478511..3a6dc8b 100644 --- a/lib/ldap_migration/__init__.py +++ b/lib/ldap_migration/__init__.py @@ -36,7 +36,7 @@ from fb_tools.errors import FbAppError from .config import LDAPMigrationConfiguration from .idict import CaseInsensitiveDict -__version__ = '0.6.0' +__version__ = '0.6.1' LOG = logging.getLogger(__name__) CFG_BASENAME = 'ldap-migration.ini' @@ -758,6 +758,84 @@ class LDAPMigrationApplication(BaseApplication): return count_dns + # ------------------------------------------------------------------------- + def migrate_entries(self): + """The main routine if this application.""" + + print() + LOG.info("Migrating all entries from source to target LDAP cluster.") + print() + + self.migrate_structural_entries() + + # ------------------------------------------------------------------------- + def migrate_structural_entries(self): + + LOG.info("Migrating all structural from source to target LDAP cluster.") + + self._migrate_entries(self.struct_dns, is_root=True, with_acl=False) + + # ------------------------------------------------------------------------- + def _migrate_entries(self, cur_hash, is_root=False, with_acl=False): + + if not is_root: + + src_dn = cur_hash['dn'] + LOG.debug("Migrating source DN {dn!r}.".format(dn=src_dn)) + + src_entry = None + tgt_entry = None + + sfilter = '(objectClass=*)' + src_attrs = [ALL_ATTRIBUTES] + if with_acl: + src_attrs = ['aci', ALL_ATTRIBUTES] + tgt_attrs = [ALL_ATTRIBUTES] + + src_status, src_result, src_response, _ = self.source.search( + search_base=src_dn, search_scope=BASE, search_filter=sfilter, + get_operational_attributes=True, attributes=src_attrs, + time_limit=self.config.timeout) + + if src_status: + + src_entry = src_response[0] + + if self.verbose > 2: + LOG.debug("Result of searching for source DN {dn!r}:\n{res}".format( + dn=src_dn, res=pp(src_result))) + if self.verbose > 2: + LOG.debug("Response of searching for source DN {dn!r}:\n{res}".format( + dn=src_dn, res=pp(src_entry))) + + tgt_dn = self.mangle_dn(src_dn) + if self.verbose > 1: + LOG.debug("Searching for target DN {dn!r}.".format(dn=tgt_dn)) + tgt_status, tgt_result, tgt_response, _ = self.target.search( + search_base=tgt_dn, search_scope=BASE, search_filter=sfilter, + get_operational_attributes=with_acl, attributes=tgt_attrs, + time_limit=self.config.timeout) + + target_entry = None + if tgt_status: + target_entry = tgt_response[0] + if self.verbose > 2: + LOG.debug("Result of searching for target DN {dn!r}:\n{res}".format( + dn=tgt_dn, res=pp(tgt_result))) + if self.verbose > 2: + LOG.debug("Response of searching for target DN {dn!r}:\n{res}".format( + dn=tgt_dn, res=pp(target_entry))) + else: + if self.verbose > 2: + LOG.debug("Target DN {dn!r} not found.".format(dn=tgt_dn)) + + else: + msg = "Did not found source entry with DN {!r} (WTF?).".format(src_dn) + LOG.error(msg) + + for key in cur_hash['childs'].keys(): + self._migrate_entries(cur_hash['childs'][key], is_root=False, with_acl=with_acl) + # ------------------------------------------------------------------------- def _run(self): @@ -771,8 +849,7 @@ class LDAPMigrationApplication(BaseApplication): self.check_tmp_dir() self.get_all_dns() self.get_structural_dns() - LOG.info("Sleeping ...") - time.sleep(2) + self.migrate_entries() finally: self.disconnect() @@ -788,4 +865,4 @@ if __name__ == "__main__": pass -# vim: fileencoding=utf-8 filetype=python ts=4 +# vim: fileencoding=utf-8 filetype=python ts=4 list -- 2.39.5