]> Frank Brehm's Git Trees - pixelpark/ldap-migration.git/commitdiff
Adding target LDAP environment as a parameter
authorFrank Brehm <frank@brehm-online.com>
Wed, 9 Feb 2022 09:31:39 +0000 (10:31 +0100)
committerFrank Brehm <frank@brehm-online.com>
Wed, 9 Feb 2022 09:31:39 +0000 (10:31 +0100)
etc/ldap-migration.ini.default
lib/ldap_migration/__init__.py
lib/ldap_migration/config.py

index baea3c75239f68e98141e687a5f1996a9bc98692..8259af999e40cce16e0a664e533ccacbf1218fdb 100644 (file)
@@ -1,22 +1,38 @@
 [source]
 
-host = ldap.pixelpark.com
-ssl = False
-port = 389
-bind_dn = cn=admin
+host = ldap.pixelpark.com
+ssl = False
+port = 389
+bind_dn = cn=admin
 ; bind_pw = ?????
 
-[target]
+[target.dev]
 
-; host = ldap2.pixelpark.com
-; ssl = True
-; port = 636
-; bind_dn = cn=admin,o=isp
+host = dev-ldap2.pixelpark.com
+ssl = True
+port = 636
+bind_dn = cn=admin
+; bind_pw = ?????
+
+[target.test]
+
+host = test-ds.pixelpark.com
+ssl = True
+port = 636
+bind_dn = cn=admin
+; bind_pw = ?????
+
+[target.prd]
+
+host = prd-ds.pixelpark.com
+ssl = True
+port = 636
+bind_dn = cn=admin
 ; bind_pw = ?????
 
 [common]
 
-suffix = o=isp
-timeout = 30
+suffix = o=isp
+timeout = 30
 
 ; vim: filetype=dosini
index 442a0c65ea4e9e9949d114edb6e55c483877f5c4..ce6a7a6711b618494c2101746a60725e343f7ea9 100644 (file)
@@ -45,10 +45,11 @@ from fb_tools.app import BaseApplication, DirectoryOptionAction
 from fb_tools.config import CfgFileOptionAction
 from fb_tools.errors import FbAppError
 from fb_tools.collections import FrozenCIStringSet, CIStringSet, CIDict
+from fb_tools.xlate import format_list
 
-from .config import LDAPMigrationConfiguration
+from .config import LDAPMigrationConfiguration, DEFAULT_ENV
 
-__version__ = '0.12.0'
+__version__ = '0.13.0'
 
 LOG = logging.getLogger(__name__)
 CFG_BASENAME = 'ldap-migration.ini'
@@ -298,6 +299,7 @@ class LDAPMigrationApplication(BaseApplication):
         self.lap = 0
         self.total_count = 0
         self.do_aci = False
+        self.init_only = False
 
         self.struct_entries = []
         self.all_entries = []
@@ -393,9 +395,17 @@ class LDAPMigrationApplication(BaseApplication):
         self._cfg_dir = self.base_dir.joinpath('etc')
         self._cfg_file = self.cfg_dir.joinpath(CFG_BASENAME)
         default_cfg_file = copy.copy(self.cfg_file)
+        envs = format_list(LDAPMigrationConfiguration.environments, do_repr=True, style='or')
 
         app_group = self.arg_parser.add_argument_group('Migration options')
 
+        app_group.add_argument(
+            '-E', '--env', '--environment', metavar='ENVIRONMENT', dest='env',
+            required=True, choices=LDAPMigrationConfiguration.environments, default=DEFAULT_ENV,
+            help=("The target environment. Valid values are {envs}. "
+                "Default: {env!r}.").format(envs=envs, env=DEFAULT_ENV),
+        )
+
         app_group.add_argument(
             '-T', '--timeout', dest='timeout', type=int, metavar='SECONDS',
             action=LimitedIntegerOptionAction,
@@ -437,6 +447,12 @@ class LDAPMigrationApplication(BaseApplication):
             help="Configuration file (default: {!r})".format(str(default_cfg_file))
         )
 
+        app_group.add_argument(
+            '-I', '--init-only', action="store_true", dest="init_only",
+            help=("Only perform initialization steps, like connecting to the LDAP "
+                "servers and discovering target schema, don't execute the migration."),
+        )
+
     # -------------------------------------------------------------------------
     def _get_log_formatter(self, is_term=True):
 
@@ -567,9 +583,12 @@ class LDAPMigrationApplication(BaseApplication):
             self._cfg_file = self.args.cfg_file
             self._cfg_dir = self.cfg_file.parent
 
+        if self.args.init_only:
+            self.init_only = True
+
         self.config = LDAPMigrationConfiguration(
             appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
-            config_file=self.cfg_file)
+            config_file=self.cfg_file, environment=self.args.env)
 
         self.config.read()
         if self.config.verbose > self.verbose:
@@ -2520,14 +2539,16 @@ class LDAPMigrationApplication(BaseApplication):
             self.connect_source()
             self.connect_target()
             self.discover_target_schema()
-            self.get_all_dns()
-            self.get_structural_dns()
-            self.migrate_entries()
-            self.detailled_summary()
+            if not self.init_only:
+                 self.get_all_dns()
+                 self.get_structural_dns()
+                 self.migrate_entries()
+                 self.detailled_summary()
 
         finally:
             self.disconnect()
-            self.summary()
+            if not self.init_only:
+                 self.summary()
 
         LOG.info("Ending {a!r}.".format(
             a=self.appname, v=self.version))
index 2014ef719099ed10520b25a3d19e49a2b9f08fa0..d11b6547d694f08ca34834a786cc5bac772fb8da 100644 (file)
@@ -11,6 +11,7 @@ from __future__ import absolute_import
 # Standard module
 import logging
 import re
+
 from pathlib import Path
 
 # Third party modules
@@ -20,11 +21,12 @@ import yaml
 from fb_tools.common import to_bool
 
 from fb_tools.config import ConfigError, BaseConfiguration
+from fb_tools.xlate import format_list
 
 __version__ = '0.3.1'
 
 LOG = logging.getLogger(__name__)
-
+DEFAULT_ENV = 'prd'
 
 # =============================================================================
 class LDAPMigrationConfigError(ConfigError):
@@ -49,7 +51,7 @@ class LDAPMigrationConfiguration(BaseConfiguration):
     default_tgt_server = 'ldap2.pixelpark.com'
     default_tgt_use_ssl = True
     default_tgt_port = 636
-    default_tgt_bind_dn = 'cn=admin,o=isp'
+    default_tgt_bind_dn = 'cn=admin'
 
     default_suffix = 'o=isp'
     default_timeout = 20
@@ -58,15 +60,26 @@ class LDAPMigrationConfiguration(BaseConfiguration):
     max_timeout = 3600
     default_wait_after_write = 0.1
 
+    environments = ('dev', 'test', 'prd')
+
     re_bind_dn = re.compile(r'^\s*bind[_-]?dn\s*$', re.IGNORECASE)
     re_bind_pw = re.compile(r'^\s*bind[_-]?(?:pw|passwd|passsword)\s*$', re.IGNORECASE)
 
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
-            xlations_definitions_base=None,
+            xlations_definitions_base=None, environment=DEFAULT_ENV,
             encoding=None, config_dir=None, config_file=None, initialized=False):
 
+        if environment not in self.environments:
+            envs = format_list(self.environments, do_repr=True, style='or')
+            msg = ("Given environment {env!r} is invalid. A valid environment "
+                "must be one of {envs}.").format(
+                env=environment, envs=envs)
+            raise ValueError(msg)
+
+        self.environment = environment
+
         self.src_server = self.default_src_server
         self.src_use_ssl = self.default_src_use_ssl
         self._src_port = self.default_src_port
@@ -229,6 +242,12 @@ class LDAPMigrationConfiguration(BaseConfiguration):
             self._eval_config_source(config, section_name)
             return
 
+        tgt_section = 'target.' + self.environment
+        if section_name.lower() == tgt_section:
+            self._eval_config_target(config, section_name)
+            return
+
+        # Overruling target definitions
         if section_name.lower() == 'target' or section_name.lower() == 'tgt':
             self._eval_config_target(config, section_name)
             return