From: Frank Brehm Date: Thu, 27 Oct 2022 11:52:34 +0000 (+0200) Subject: Changing logic in mirror-ldap, using a modified pp function. X-Git-Tag: 0.7.0^2~1^2~22 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=83c49c6fe0c068bdf9080e95ed1894bc9af5c4dd;p=pixelpark%2Fpp-admin-tools.git Changing logic in mirror-ldap, using a modified pp function. --- diff --git a/lib/pp_admintools/__init__.py b/lib/pp_admintools/__init__.py index 16797b5..02c4939 100644 --- a/lib/pp_admintools/__init__.py +++ b/lib/pp_admintools/__init__.py @@ -1,9 +1,37 @@ #!/bin/env python3 # -*- coding: utf-8 -*- +from __future__ import absolute_import -__version__ = '0.6.0' +# Standard modules +import shutil + +# Own modules +import fb_tools.common + +__version__ = '0.7.0' MAX_PORT_NUMBER = (2 ** 16) - 1 DEFAULT_CONFIG_DIR = 'pixelpark' +DEFAULT_TERMINAL_WIDTH = 99 +DEFAULT_TERMINAL_HEIGHT = 40 + + +# ============================================================================= +def pp(value, indent=4, width=None, depth=None): + """ + Return a pretty print string of the given value. + + @return: pretty print string + @rtype: str + """ + + if not width: + term_size = shutil.get_terminal_size((DEFAULT_TERMINAL_WIDTH, DEFAULT_TERMINAL_HEIGHT)) + width = term_size.columns + + return fb_tools.common.pp(value, indent=indent, width=width, depth=depth) + + +# ============================================================================= # vim: ts=4 et list diff --git a/lib/pp_admintools/app/__init__.py b/lib/pp_admintools/app/__init__.py index 19da056..cc9e1b3 100644 --- a/lib/pp_admintools/app/__init__.py +++ b/lib/pp_admintools/app/__init__.py @@ -26,7 +26,7 @@ LOG = logging.getLogger(__name__) _ = XLATOR.gettext ngettext = XLATOR.ngettext -__version__ = '0.5.1' +__version__ = '0.6.1' # ============================================================================= @@ -66,6 +66,21 @@ class BaseDPXApplication(FbConfigApplication): env_prefix=env_prefix, config_dir=config_dir ) + # ------------------------------------------------------------------------- + def line(self, width=80, linechar='-', color=None): + """Print out an line on stdout, if not in quiet mode.""" + if self.quiet: + return + + lchar = str(linechar).strip() + if not lchar: + lchar = '-' + + lin = (lchar * width)[0:width] + if color: + lin=self.colored(lin, color) + print(lin) + # ------------------------------------------------------------------------- def empty_line(self): """Print out an empty line on stdout, if not in quiet mode.""" diff --git a/lib/pp_admintools/app/dns_deploy_zones.py b/lib/pp_admintools/app/dns_deploy_zones.py index 0cf7aeb..216a178 100644 --- a/lib/pp_admintools/app/dns_deploy_zones.py +++ b/lib/pp_admintools/app/dns_deploy_zones.py @@ -30,13 +30,14 @@ import six from pytz import timezone, UnknownTimeZoneError # Own modules -from fb_tools.common import pp, to_str +from fb_tools.common import to_str from fb_tools.app import BaseApplication from fb_tools.pidfile import PidFileError, PidFile from .. import __version__ as GLOBAL_VERSION +from .. import pp from .pdns import PpPDNSAppError, PpPDNSApplication @@ -44,7 +45,7 @@ from ..config.dns_deploy_zones import DnsDeployZonesConfig from ..xlate import XLATOR -__version__ = '0.8.5' +__version__ = '0.8.6' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/app/ldap.py b/lib/pp_admintools/app/ldap.py index 3c707d9..9396d00 100644 --- a/lib/pp_admintools/app/ldap.py +++ b/lib/pp_admintools/app/ldap.py @@ -32,13 +32,14 @@ from ldap3 import ALL_ATTRIBUTES from ldap3.core.exceptions import LDAPException # from ldap3.core.exceptions import LDAPException, LDAPBindError -from fb_tools.common import pp, is_sequence, to_bool +from fb_tools.common import is_sequence, to_bool from fb_tools.argparse_actions import TimeoutOptionAction from fb_tools.mailaddress import MailAddress from fb_tools.collections import FrozenCIStringSet, CIStringSet, CIDict # Own modules from .. import __version__ as GLOBAL_VERSION +from .. import pp from ..xlate import XLATOR, format_list @@ -53,7 +54,7 @@ from ..config.ldap import LdapConnectionInfo, LdapConfiguration # rom ..config.ldap import DEFAULT_PORT_LDAP, DEFAULT_PORT_LDAPS from ..config.ldap import DEFAULT_TIMEOUT -__version__ = '0.10.0' +__version__ = '0.10.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -808,6 +809,9 @@ class BaseLdapApplication(BaseDPXApplication): attributes = ['objectClass'] ldap_filter = '(objectClass=*)' + LOG.debug(_("Getting all Entry DNs of LDAP instance {i!r} below {b!r}.").format( + i=inst, b=base_dn)) + req_status, req_result, req_response, req_whatever = ldap.search( search_base=base_dn, search_scope=SUBTREE, search_filter=ldap_filter, get_operational_attributes=False, attributes=attributes, diff --git a/lib/pp_admintools/app/mail.py b/lib/pp_admintools/app/mail.py index 3a63083..8e838c3 100644 --- a/lib/pp_admintools/app/mail.py +++ b/lib/pp_admintools/app/mail.py @@ -21,13 +21,13 @@ from subprocess import Popen, PIPE import smtplib # Third party modules -from fb_tools.common import pp from fb_tools.xlate import format_list from fb_tools import MailAddress # Own modules from .. import __version__ as GLOBAL_VERSION from .. import MAX_PORT_NUMBER, DEFAULT_CONFIG_DIR +from .. import pp from ..xlate import XLATOR @@ -37,7 +37,7 @@ from ..config.mail import MailConfiguration from . import DPXAppError, BaseDPXApplication -__version__ = '0.3.1' +__version__ = '0.3.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/app/mirror_ldap.py b/lib/pp_admintools/app/mirror_ldap.py index ea1d9c5..73a5bc7 100644 --- a/lib/pp_admintools/app/mirror_ldap.py +++ b/lib/pp_admintools/app/mirror_ldap.py @@ -20,11 +20,12 @@ from functools import cmp_to_key # from ldap3 import MODIFY_REPLACE, MODIFY_ADD, MODIFY_DELETE # Own modules -# from fb_tools.common import to_bool, is_sequence, pp -from fb_tools.common import pp +# from fb_tools.common import to_bool, is_sequence # from fb_tools.collections import FrozenCIStringSet, CIStringSet, CIDict from fb_tools.collections import CIDict, CIStringSet +from .. import pp + from ..xlate import XLATOR from ..config.mirror_ldap import MirrorLdapConfiguration @@ -36,7 +37,7 @@ from .ldap import BaseLdapApplication from ..argparse_actions import NonNegativeItegerOptionAction from ..argparse_actions import LimitedFloatOptionAction -__version__ = '0.5.3' +__version__ = '0.6.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -75,7 +76,6 @@ class MirrorLdapApplication(BaseLdapApplication): self.src_dns = [] self.tgt_dns_current = CIDict() - self.registered_tgt_dns = CIDict() self.limit = 0 self.wait_after_write = self.default_wait_after_write @@ -247,10 +247,14 @@ class MirrorLdapApplication(BaseLdapApplication): self.countdown(number=5, delay=1, prompt=msg) self.empty_line() + self.line(linechar='=', color='CYAN') LOG.info("I'm walking, yes indeed I'm walking ...") try: - self.clean_target_instance() + self.get_current_src_entries() + self.get_current_tgt_entries() + self.eval_sync_entries() + # self.clean_target_instance() except KeyboardInterrupt: msg = _("Got a {}:").format('KeyboardInterrupt') + ' ' + _("Interrupted on demand.") @@ -266,24 +270,52 @@ class MirrorLdapApplication(BaseLdapApplication): "Removing all existing entries in target LDAP instance " "(except the base DN entry, of course).")) - self.get_current_tgt_entries() - self.eval_sync_entries() - self.clean_tgt_non_struct_entries() - self.clean_tgt_struct_entries() + # 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.""" + + self.empty_line() + self.line(color='CYAN') + LOG.info(_("Trying to get DNs of all entries in the source LDAP instance.")) + + self.src_dns = self.get_all_entry_dns_hash(self.src_instance) + + sorted_dns = sorted(list(self.src_dns.keys()), key=cmp_to_key(self.compare_ldap_dns)) + + for dn in sorted_dns: + self.register_dn_tokens(dn, self.src_dns[dn], self.src_dns) + + if self.verbose > 2: + if self.verbose > 4: + LOG.debug("Current source entries:\n" + pp(self.src_dns.dict())) + else: + msg = _("Found DNs in instance {!r}:").format(self.src_instance) + LOG.debug(msg + '\n' + pp(sorted_dns)) # ------------------------------------------------------------------------- def get_current_tgt_entries(self): """Get DNs of all entries in the target LDAP instance and sort them.""" - LOG.debug(_("Trying to get DNs of all entries in the target LDAP instance.")) + self.empty_line() + self.line(color='CYAN') + LOG.info(_("Trying to get DNs of all entries in the target LDAP instance.")) self.tgt_dns_current = self.get_all_entry_dns_hash(self.tgt_instance) + sorted_dns = sorted( + list(self.tgt_dns_current.keys()), key=cmp_to_key(self.compare_ldap_dns)) - for dn in sorted(list(self.tgt_dns_current.keys()), key=cmp_to_key(self.compare_ldap_dns)): + for dn in sorted_dns: self.register_dn_tokens(dn, self.tgt_dns_current[dn], self.tgt_dns_current) - if self.verbose > 4: - LOG.debug("Current target entries:\n" + pp(self.tgt_dns_current.dict())) + if self.verbose > 2: + if self.verbose > 4: + LOG.debug("Current target entries:\n" + pp(self.tgt_dns_current.dict())) + else: + msg = _("Found DNs in instance {!r}:").format(self.tgt_instance) + LOG.debug(msg + '\n' + pp(sorted_dns)) # ------------------------------------------------------------------------- def eval_sync_entries(self): diff --git a/lib/pp_admintools/app/pdns.py b/lib/pp_admintools/app/pdns.py index 16bb693..cedfedd 100644 --- a/lib/pp_admintools/app/pdns.py +++ b/lib/pp_admintools/app/pdns.py @@ -21,7 +21,6 @@ import socket import psutil # Own modules -from fb_tools.common import pp from fb_tools.xlate import format_list from fb_pdnstools.zone import PowerDNSZone @@ -30,6 +29,7 @@ from fb_pdnstools.errors import PDNSApiNotFoundError from fb_pdnstools.errors import PDNSApiValidationError from .. import __version__ as GLOBAL_VERSION +from .. import pp from ..argparse_actions import PortOptionAction @@ -40,7 +40,7 @@ from ..config.pdns import PdnsConfiguration from ..xlate import XLATOR -__version__ = '0.9.5' +__version__ = '0.9.6' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/app/remove_ldap_user.py b/lib/pp_admintools/app/remove_ldap_user.py index b0feb4a..13e9311 100644 --- a/lib/pp_admintools/app/remove_ldap_user.py +++ b/lib/pp_admintools/app/remove_ldap_user.py @@ -16,14 +16,16 @@ import crypt from ldap3 import MODIFY_REPLACE, MODIFY_ADD, MODIFY_DELETE # Own modules -from fb_tools.common import to_bool, is_sequence, pp +from fb_tools.common import to_bool, is_sequence + +from .. import pp from ..xlate import XLATOR from .ldap import LdapAppError, FatalLDAPError from .ldap import BaseLdapApplication -__version__ = '0.5.7' +__version__ = '0.5.8' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/app/set_ldap_password.py b/lib/pp_admintools/app/set_ldap_password.py index d7a089d..e40f06c 100644 --- a/lib/pp_admintools/app/set_ldap_password.py +++ b/lib/pp_admintools/app/set_ldap_password.py @@ -28,7 +28,9 @@ except ImportError: # Own modules # from fb_tools.common import to_bool, is_sequence, pp -from fb_tools.common import is_sequence, pp +from fb_tools.common import is_sequence + +from .. import pp from ..xlate import XLATOR @@ -36,7 +38,7 @@ from .ldap import LdapAppError, FatalLDAPError from .ldap import BaseLdapApplication from .ldap import PasswordFileOptionAction -__version__ = '0.6.5' +__version__ = '0.6.6' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/config/dns_deploy_zones.py b/lib/pp_admintools/config/dns_deploy_zones.py index d90c819..038f4b5 100644 --- a/lib/pp_admintools/config/dns_deploy_zones.py +++ b/lib/pp_admintools/config/dns_deploy_zones.py @@ -21,7 +21,7 @@ from pathlib import Path # Own modules -from fb_tools.common import is_sequence, pp, to_bool +from fb_tools.common import is_sequence, to_bool # from .config import ConfigError, BaseConfiguration from fb_tools.multi_config import DEFAULT_ENCODING @@ -29,9 +29,11 @@ from fb_tools.multi_config import DEFAULT_ENCODING from .pdns import PdnsConfigError, PdnsConfiguration from .mail import DEFAULT_CONFIG_DIR +from .. import pp + from ..xlate import XLATOR -__version__ = '0.2.2' +__version__ = '0.2.3' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/config/ldap.py b/lib/pp_admintools/config/ldap.py index 4a27f56..91c1549 100644 --- a/lib/pp_admintools/config/ldap.py +++ b/lib/pp_admintools/config/ldap.py @@ -19,7 +19,6 @@ import re # Own modules from fb_tools import MAX_TIMEOUT -# from fb_tools.common import pp from fb_tools.common import is_sequence, to_bool from fb_tools.multi_config import DEFAULT_ENCODING from fb_tools.obj import FbGenericBaseObject, FbBaseObject @@ -27,12 +26,13 @@ from fb_tools.obj import FbGenericBaseObject, FbBaseObject from . import PpConfigurationError, PpBaseConfiguration from .. import MAX_PORT_NUMBER, DEFAULT_CONFIG_DIR +# from .. import pp from . import VALID_TIERS, DEFAULT_TIER from ..xlate import XLATOR -__version__ = '0.6.1' +__version__ = '0.6.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/config/mail.py b/lib/pp_admintools/config/mail.py index 3f82d3d..92e0c6c 100644 --- a/lib/pp_admintools/config/mail.py +++ b/lib/pp_admintools/config/mail.py @@ -18,7 +18,7 @@ import copy # Own modules -from fb_tools.common import is_sequence, pp, to_bool +from fb_tools.common import is_sequence, to_bool from fb_tools.multi_config import DEFAULT_ENCODING @@ -26,12 +26,13 @@ from fb_tools import MailAddress from .. import __version__ as GLOBAL_VERSION from .. import MAX_PORT_NUMBER, DEFAULT_CONFIG_DIR +from .. import pp from . import PpConfigurationError, PpBaseConfiguration from ..xlate import XLATOR -__version__ = '0.2.1' +__version__ = '0.2.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/config/mirror_ldap.py b/lib/pp_admintools/config/mirror_ldap.py index 6adf088..6766d80 100644 --- a/lib/pp_admintools/config/mirror_ldap.py +++ b/lib/pp_admintools/config/mirror_ldap.py @@ -17,17 +17,18 @@ import re from collections.abc import Mapping # Third party modules -from fb_tools.common import is_sequence, pp +from fb_tools.common import is_sequence from fb_tools.multi_config import DEFAULT_ENCODING # Own modules from .. import DEFAULT_CONFIG_DIR +from .. import pp from .ldap import LdapConfigError, LdapConfiguration from ..xlate import XLATOR -__version__ = '0.2.0' +__version__ = '0.2.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext diff --git a/lib/pp_admintools/config/pdns.py b/lib/pp_admintools/config/pdns.py index b4c0260..3d2587c 100644 --- a/lib/pp_admintools/config/pdns.py +++ b/lib/pp_admintools/config/pdns.py @@ -20,12 +20,13 @@ import copy # Own modules from fb_tools import MAX_TIMEOUT -from fb_tools.common import is_sequence, pp +from fb_tools.common import is_sequence # from .config import ConfigError, BaseConfiguration from fb_tools.multi_config import DEFAULT_ENCODING from .. import __version__ as GLOBAL_VERSION from .. import MAX_PORT_NUMBER +from .. import pp from .mail import MailConfigError, MailConfiguration from .mail import DEFAULT_CONFIG_DIR @@ -34,7 +35,7 @@ from ..xlate import XLATOR LIBRARY_NAME = "pp-pdns-api-client" -__version__ = '0.2.4' +__version__ = '0.2.5' LOG = logging.getLogger(__name__) _ = XLATOR.gettext