]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Printing dots on long lasting actions.
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 1 Feb 2024 17:26:30 +0000 (18:26 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 1 Feb 2024 17:26:30 +0000 (18:26 +0100)
lib/pp_admintools/app/mirror_ldap.py

index 1bf50a502d738758003e1a6f9f593d45057e8023..423230dc3087b113203ddf3adc3f15b439ad93af 100644 (file)
@@ -36,7 +36,7 @@ from ..errors import DpxLdapReadError
 from ..errors import DpxLdapSessionError
 from ..xlate import XLATOR
 
-__version__ = '1.2.1'
+__version__ = '1.2.2'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -523,6 +523,8 @@ class MirrorLdapApplication(BaseLdapApplication):
 
         for dn in sorted(self.tgt_dns_current.keys(), key=cmp_to_key(self.compare_ldap_dns)):
 
+            if self.verbose == 1:
+                self.print_dot()
             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)))
@@ -585,6 +587,8 @@ class MirrorLdapApplication(BaseLdapApplication):
 
         for dn in list(reversed(dns)):
 
+            if self.verbose == 1:
+                self.print_dot()
             entry = self.tgt_dns_current[dn]
             if not entry['childs']:
                 continue
@@ -637,6 +641,8 @@ class MirrorLdapApplication(BaseLdapApplication):
 
         for dn in dns:
 
+            if self.verbose == 1:
+                self.print_dot()
             if self.mirror_entry(dn):
                 count += 1
 
@@ -924,6 +930,8 @@ class MirrorLdapApplication(BaseLdapApplication):
 
         for dn in dns:
 
+            if self.verbose == 1:
+                self.print_dot()
             if dn in self.src_struct_dns:
                 continue
 
@@ -960,63 +968,8 @@ class MirrorLdapApplication(BaseLdapApplication):
         attributes = ['member', 'uniqueMember']
 
         for dn in dns:
-
-            if dn in self.keep_entry_dns:
-                if self.verbose > 1:
-                    LOG.debug(_('Entry {!r} is set to be kept.').format(dn))
-                continue
-
-            if self.verbose > 1:
-                self.empty_line()
-            if self.verbose > 1:
-                LOG.debug(_('Mirroring entry {!r} ...').format(dn))
-
-            try:
-                src_entry = self.get_entry(dn, self.src_instance, attributes)
-            except LDAPSocketReceiveError as e:
-                msg = _('Error on reading entry {!r} from source:').format(dn) + ' ' + str(e)
-                raise DpxLdapReadError(msg)
-            if not src_entry:
-                msg = _('Did not found {!r} in the source LDAP.').format(dn)
-                LOG.warn(msg)
-                continue
-            src_attribs = self.normalized_attributes(src_entry, omit_memberof=True)
-            src_attribs_dict = src_attribs.dict()
-            if self.verbose > 2:
-                LOG.debug('Got source entry:\n' + pp(src_attribs_dict))
-
-            try:
-                tgt_entry = self.get_entry(dn, self.tgt_instance, attributes, tries=1)
-            except LDAPSocketReceiveError as e:
-                msg = _('Error on reading entry {!r} from target:').format(dn) + ' ' + str(e)
-                raise DpxLdapReadError(msg)
-            if not tgt_entry:
-                LOG.warn(_('Target entry {!r} not found.').format(dn))
-                continue
-
-            tgt_attribs = self.normalized_attributes(tgt_entry, omit_memberof=True)
-            tgt_attribs_dict = tgt_attribs.dict()
-
-            if self.verbose > 2:
-                LOG.debug('Got target entry:\n' + pp(tgt_attribs_dict))
-
-            changes = self.generate_modify_data(dn, src_attribs, tgt_attribs)
-            if changes:
-                self.empty_line()
-                LOG.info(_('Modifying entry {!r} ...').format(dn))
-                msg = _('Got modify data for DN {!r}:').format(dn)
-                LOG.debug(msg + '\n' + pp(changes))
-                self.modify_entry(self.tgt_instance, dn, changes)
-                self.mirrored_entries += 1
+            if self.update_membership(dn, attributes):
                 count += 1
-                if dn not in self.mirrored_dns:
-                    self.total_updated += 1
-                if self.wait_after_write and not self.simulate:
-                    time.sleep(self.wait_after_write * 2)
-            else:
-                if self.verbose > 1:
-                    LOG.debug(_('No changes necessary on DN {!r}.').format(dn))
-                continue
 
         if count:
             self.empty_line()
@@ -1028,6 +981,69 @@ class MirrorLdapApplication(BaseLdapApplication):
             msg = _('Mirrored no group entries to target LDAP instance.')
         LOG.info(msg)
 
+    # -------------------------------------------------------------------------
+    def update_membership(self, dn, attributes):
+        """Update all 'member' and 'uniqueMember' attributes of given dn."""
+        if dn in self.keep_entry_dns:
+            if self.verbose > 1:
+                LOG.debug(_('Entry {!r} is set to be kept.').format(dn))
+            return False
+
+        if self.verbose == 1:
+            self.print_dot()
+
+        if self.verbose > 1:
+            LOG.debug(_('Mirroring entry {!r} ...').format(dn))
+
+        try:
+            src_entry = self.get_entry(dn, self.src_instance, attributes)
+        except LDAPSocketReceiveError as e:
+            msg = _('Error on reading entry {!r} from source:').format(dn) + ' ' + str(e)
+            raise DpxLdapReadError(msg)
+        if not src_entry:
+            msg = _('Did not found {!r} in the source LDAP.').format(dn)
+            LOG.warn(msg)
+            return False
+        src_attribs = self.normalized_attributes(src_entry, omit_memberof=True)
+        src_attribs_dict = src_attribs.dict()
+        if self.verbose > 2:
+            LOG.debug('Got source entry:\n' + pp(src_attribs_dict))
+
+        try:
+            tgt_entry = self.get_entry(dn, self.tgt_instance, attributes, tries=1)
+        except LDAPSocketReceiveError as e:
+            msg = _('Error on reading entry {!r} from target:').format(dn) + ' ' + str(e)
+            raise DpxLdapReadError(msg)
+        if not tgt_entry:
+            LOG.warn(_('Target entry {!r} not found.').format(dn))
+            return False
+
+        tgt_attribs = self.normalized_attributes(tgt_entry, omit_memberof=True)
+        tgt_attribs_dict = tgt_attribs.dict()
+
+        if self.verbose > 2:
+            LOG.debug('Got target entry:\n' + pp(tgt_attribs_dict))
+
+        changes = self.generate_modify_data(dn, src_attribs, tgt_attribs)
+        changed = False
+        if changes:
+            self.empty_line()
+            LOG.info(_('Modifying entry {!r} ...').format(dn))
+            msg = _('Got modify data for DN {!r}:').format(dn)
+            LOG.debug(msg + '\n' + pp(changes))
+            self.modify_entry(self.tgt_instance, dn, changes)
+            self.mirrored_entries += 1
+            changed = True
+            if dn not in self.mirrored_dns:
+                self.total_updated += 1
+            if self.wait_after_write and not self.simulate:
+                time.sleep(self.wait_after_write * 2)
+        else:
+            if self.verbose > 1:
+                LOG.debug(_('No changes necessary on DN {!r}.').format(dn))
+
+        return changed
+
 
 # =============================================================================
 if __name__ == '__main__':