From 3767074570e46d7f1f9a60ae4bfb8f35b51e1f0c Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 20 Oct 2022 11:44:37 +0200 Subject: [PATCH] Checking LDAP instances for mirroring --- lib/pp_admintools/app/mirror_ldap.py | 106 ++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/lib/pp_admintools/app/mirror_ldap.py b/lib/pp_admintools/app/mirror_ldap.py index 86d2d02..5f0708c 100644 --- a/lib/pp_admintools/app/mirror_ldap.py +++ b/lib/pp_admintools/app/mirror_ldap.py @@ -9,6 +9,7 @@ from __future__ import absolute_import # Standard modules +import sys import logging # Third party modules @@ -16,6 +17,7 @@ import logging # Own modules # from fb_tools.common import to_bool, is_sequence, pp +# from fb_tools.common import pp from ..xlate import XLATOR @@ -26,7 +28,7 @@ from .ldap import BaseLdapApplication from ..argparse_actions import NonNegativeItegerOptionAction from ..argparse_actions import LimitedFloatOptionAction -__version__ = '0.1.0' +__version__ = '0.2.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -50,15 +52,19 @@ class MirrorLdapApplication(BaseLdapApplication): use_multiple_ldap_connections = False show_cmdline_ldap_timeout = True apply_default_ldap_instance_if_not_given = False + show_force_option = True default_wait_after_write = 0.05 # ------------------------------------------------------------------------- def __init__(self, appname=None, base_dir=None): + self.src_instance = None self.src = None + self.tgt_instance = None self.tgt = None + self.limit = 0 self.wait_after_write = self.default_wait_after_write self.only_struct = False @@ -75,6 +81,8 @@ class MirrorLdapApplication(BaseLdapApplication): "substituting the content of some entry attributes by another values." ) + self._force_desc_msg = _("Do not execute a countdown before starting the application.") + super(MirrorLdapApplication, self).__init__( appname=appname, description=desc, base_dir=base_dir, initialized=False) @@ -115,9 +123,105 @@ class MirrorLdapApplication(BaseLdapApplication): super(MirrorLdapApplication, self)._verify_instances( is_admin=True, readonly=False, has_sync_source=True) + # ------------------------------------------------------------------------- + def post_init(self): + """Execute some actions after initialising.""" + + super(MirrorLdapApplication, self).post_init() + + limit = getattr(self.args, 'limit', 0) + if limit: + print() + if self.simulate: + self.limit = limit + LOG.warn(_( + "Limiting the number of entries for mirroring to {} " + "entries.").format(limit)) + else: + LOG.error(_( + "Limition the number of entries for mirroring may " + "only be done in simulation mode.")) + print() + self.arg_parser.print_usage(sys.stdout) + self.exit(1) + + self.wait = getattr(self.args, 'wait', self.default_wait_after_write) + + self._check_source_instance() + + # ------------------------------------------------------------------------- + def _check_source_instance(self): + + tgt_name = self.ldap_instances[0] + LOG.debug(_( + "Checking mirror source instance for target instance {!r} ...").format(tgt_name)) + + src_name = self.cfg.ldap_connection[tgt_name].sync_source + LOG.debug(_("Got {!r} as the mirror source instance.").format(src_name)) + + if src_name not in self.cfg.ldap_connection: + msg = _("Error in configuration:") + msg += ' ' + _( + "the source LDAP instance {src!r} for mirroring to {tgt!r} " + "does not exists.").format(src=src_name, tgt=tgt_name) + LOG.error(msg) + self.exit(3) + + if tgt_name.lower() == src_name.lower(): + msg = _("Error in configuration:") + msg += ' ' + _( + "The source LDAP instance {src!r} must not be the same like the " + "target instance.").format(src=src_name) + LOG.error(msg) + self.exit(3) + + src_base_dn = self.cfg.ldap_connection[src_name].base_dn + tgt_base_dn = self.cfg.ldap_connection[tgt_name].base_dn + + if src_base_dn != tgt_base_dn: + msg = _("Error in configuration:") + msg += ' ' + _( + "the base DN {src_dn!r} of the source instance {src!r} does not match the " + "base DN {tgt_dn!r} of the target instance {tgt!r}.").format( + src_dn=src_base_dn, src=src_name, tgt_dn=tgt_base_dn, tgt=tgt_name) + LOG.error(msg) + self.exit(3) + + self.src_instance = src_name + self.tgt_instance = tgt_name + self.ldap_instances.append(src_name) + + # ------------------------------------------------------------------------- + def pre_run(self): + + super(MirrorLdapApplication, self).pre_run() + + self.src = self.ldap_connection[self.src_instance] + self.tgt = self.ldap_connection[self.tgt_instance] + + # ------------------------------------------------------------------------- + def disconnect_all(self): + + self.src = None + self.tgt = None + + super(MirrorLdapApplication, self).disconnect_all() + # ------------------------------------------------------------------------- def _run(self): + if not self.quiet and not self.force: + print() + src_url = self.cfg.ldap_connection[self.src_instance].url + tgt_url = self.cfg.ldap_connection[self.tgt_instance].url + msg = _( + "Start mirroring LDAP instance {src!r} ({src_url}) to instance {tgt!r} " + "({tgt_url}) ...").format( + src=self.src_instance, src_url=src_url, tgt=self.tgt_instance, tgt_url=tgt_url) + self.countdown(number=5, delay=1, prompt=msg) + + if not self.quiet: + print() LOG.info("I'm walking, yes indeed I'm walking ...") -- 2.39.5