]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Limiting deletion of entries in taget instance
authorFrank Brehm <frank@brehm-online.com>
Thu, 27 Oct 2022 12:45:00 +0000 (14:45 +0200)
committerFrank Brehm <frank@brehm-online.com>
Thu, 27 Oct 2022 12:45:00 +0000 (14:45 +0200)
lib/pp_admintools/app/mirror_ldap.py

index e66056d8c6e21880e4f9faffc1f6bb3a3cabf9d5..be4cade22ee1502ace60a2374bdb26e25ef3e78f 100644 (file)
@@ -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__":