From e76982544fd401fa025375900d0b4ca8eae591a2 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 27 Oct 2022 14:45:00 +0200 Subject: [PATCH] Limiting deletion of entries in taget instance --- lib/pp_admintools/app/mirror_ldap.py | 80 +++++++++++++++++++--------- 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/lib/pp_admintools/app/mirror_ldap.py b/lib/pp_admintools/app/mirror_ldap.py index e66056d..be4cade 100644 --- a/lib/pp_admintools/app/mirror_ldap.py +++ b/lib/pp_admintools/app/mirror_ldap.py @@ -257,25 +257,14 @@ class MirrorLdapApplication(BaseLdapApplication): self.get_current_src_entries() self.get_current_tgt_entries() self.eval_sync_entries() - # self.clean_target_instance() + self.clean_tgt_non_struct_entries() + self.clean_tgt_struct_entries() except KeyboardInterrupt: msg = _("Got a {}:").format('KeyboardInterrupt') + ' ' + _("Interrupted on demand.") LOG.error(msg) self.exit(10) - # ------------------------------------------------------------------------- - def clean_target_instance(self): - """Cleaning the target instance.""" - - self.empty_line() - LOG.info(_( - "Removing all existing entries in target LDAP instance " - "(except the base DN entry, of course).")) - - # self.clean_tgt_non_struct_entries() - # self.clean_tgt_struct_entries() - # ------------------------------------------------------------------------- def get_current_src_entries(self): """Get DNs of all entries in the source LDAP instance and sort them.""" @@ -397,56 +386,79 @@ class MirrorLdapApplication(BaseLdapApplication): # ------------------------------------------------------------------------- def clean_tgt_non_struct_entries(self): - """Removing all non structural entries in target instance. + """Removing non structural entries in target instance. + + Only those entries are removed, which are no more existing in the source instance. Structural entries are entries without any childs. """ self.empty_line() - LOG.info(_("Removing all non structural entries from target LDAP instance.")) + self.line(color='CYAN') + LOG.info(_("Removing non structural entries from target LDAP instance.")) if not self.quiet: time.sleep(2) self.empty_line() + count = 0 + for dn in sorted(list(self.tgt_dns_current.keys()), key=cmp_to_key(self.compare_ldap_dns)): entry = self.tgt_dns_current[dn] if 'childs' not in entry: LOG.error("Found entry {dn!r}:\n{e}".format(dn=dn, e=pp(entry))) self.exit(5) + + if dn in self.src_dns: + if self.verbose > 4: + msg = _("Entry {!r} exists on the source instance, will not be removed.") + LOG.debug(msg.format(dn)) + continue + if entry['childs']: - if self.verbose > 1: - LOG.debug(_( - "Entry {!r} is a structural entry, will not be removed " - "at this point.").format(dn)) + if self.verbose > 4: + msg = _( + "Entry {!r} is a structural entry, will not be removed at this point.") + LOG.debug(msg.format(dn)) continue if dn in self.keep_entry_dns: LOG.debug(_("Entry {!r} is set to be kept.").format(dn)) continue - if dn in self.sync_entry_dns: - LOG.debug(_("Entry {!r} is set to be synchronized.").format(dn)) - continue - self.delete_entry(self.tgt_instance, dn) + count += 1 if self.wait_after_write and not self.simulate: time.sleep(self.wait_after_write) + if count: + msg = ngettext( + "Removed one not structural entry in target LDAP instance.", + "Removed {no} not structural entries in target LDAP instance.", + count).format(no=count) + else: + msg = _("None not structural entries in target LDAP instance removed.") + LOG.info(msg) + # ------------------------------------------------------------------------- def clean_tgt_struct_entries(self): - """Removing all structural entries in target instance. + """Removing structural entries in target instance. + + Only those entries are removed, which are no more existing in the source instance. Structural entries are entries without any childs. """ self.empty_line() - LOG.info(_("Removing all structural entries from target LDAP instance.")) + self.line(color='CYAN') + LOG.info(_("Removing structural entries from target LDAP instance.")) if not self.quiet: time.sleep(2) self.empty_line() - dns = sorted(list(self.tgt_dns_current.keys()), key=cmp_to_key(self.compare_ldap_dns)) + dns = sorted(self.tgt_struct_dns_current.as_list(), key=cmp_to_key(self.compare_ldap_dns)) + + count = 0 for dn in list(reversed(dns)): @@ -454,6 +466,12 @@ class MirrorLdapApplication(BaseLdapApplication): if not entry['childs']: continue + if dn in self.src_dns: + if self.verbose > 4: + msg = _("Entry {!r} exists on the source instance, will not be removed.") + LOG.debug(msg.format(dn)) + continue + if dn in self.keep_entry_dns: LOG.debug(_("Entry {!r} is set to be kept.").format(dn)) continue @@ -463,9 +481,19 @@ class MirrorLdapApplication(BaseLdapApplication): continue self.delete_entry(self.tgt_instance, dn) + count += 1 if self.wait_after_write and not self.simulate: time.sleep(self.wait_after_write) + if count: + msg = ngettext( + "Removed one structural entry in target LDAP instance.", + "Removed {no} structural entries in target LDAP instance.", + count).format(no=count) + else: + msg = _("None structural entries in target LDAP instance removed.") + LOG.info(msg) + # ============================================================================= if __name__ == "__main__": -- 2.39.5