]> Frank Brehm's Git Trees - pixelpark/ldap-migration.git/commitdiff
Connecting and disconnecting from LDA servers
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 12 Nov 2020 16:19:50 +0000 (17:19 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 12 Nov 2020 16:19:50 +0000 (17:19 +0100)
lib/ldap_migration/__init__.py

index d0bd187278d2547d7d733206952c517dd79cbea4..818f54e4a95b1f310d5c0abc8035de6fafa315a2 100644 (file)
@@ -13,9 +13,11 @@ import logging
 import copy
 import sys
 import os
+import time
 
 # 3rd party modules
 
+from ldap3 import Server, Connection, ALL, DSA, IP_V4_PREFERRED, SAFE_SYNC
 from ldap3.core.exceptions import LDAPException
 
 # Own modules
@@ -28,7 +30,7 @@ from fb_tools.errors import FbAppError
 
 from .config import LDAPMigrationConfiguration
 
-__version__ = '0.2.0'
+__version__ = '0.3.0'
 
 LOG = logging.getLogger(__name__)
 CFG_BASENAME = 'ldap-migration.ini'
@@ -240,12 +242,115 @@ class LDAPMigrationApplication(BaseApplication):
 
         self.initialized = True
 
+    # -------------------------------------------------------------------------
+    def connect_source(self):
+
+        server_opts = {}
+        if self.config.src_use_ssl:
+            server_opts['use_ssl'] = True
+            if self.config.src_port != 636:
+                server_opts['port'] = self.config.src_port
+        else:
+            server_opts['use_ssl'] = False
+            if self.config.src_port != 389:
+                server_opts['port'] = self.config.src_port
+        server_opts['get_info'] = DSA
+        server_opts['mode'] = IP_V4_PREFERRED
+        server_opts['connect_timeout'] = self.config.timeout
+        LOG.info("Connecting to source server {!r} ...".format(self.config.src_server))
+        if self.verbose > 1:
+            LOG.debug(
+                "Connect options to source server {!r}: \n".format(self.config.src_server) +
+                pp(server_opts))
+
+        self.src_server = Server(
+            self.config.src_server, **server_opts)
+
+        if self.verbose > 2:
+            LOG.debug("Source server {s}: {re}".format(
+                s=self.src_server, re=repr(self.src_server)))
+
+        self.source = Connection(
+            self.src_server, self.config.src_bind_dn, self.config.src_bind_pw,
+            client_strategy=SAFE_SYNC, auto_bind=True)
+
+        if self.verbose > 1:
+            LOG.debug(
+                "Info about source server {}:\n".format(self.src_server) +
+                repr(self.src_server.info))
+
+    # -------------------------------------------------------------------------
+    def connect_target(self):
+
+        server_opts = {}
+        if self.config.tgt_use_ssl:
+            server_opts['use_ssl'] = True
+            if self.config.tgt_port != 636:
+                server_opts['port'] = self.config.tgt_port
+        else:
+            server_opts['use_ssl'] = False
+            if self.config.tgt_port != 389:
+                server_opts['port'] = self.config.tgt_port
+        server_opts['get_info'] = ALL
+        server_opts['mode'] = IP_V4_PREFERRED
+        server_opts['connect_timeout'] = self.config.timeout
+        LOG.info("Connecting to target server {!r} ...".format(self.config.tgt_server))
+        if self.verbose > 1:
+            LOG.debug(
+                "Connect options to target server {!r}: \n".format(self.config.tgt_server) +
+                pp(server_opts))
+
+        self.tgt_server = Server(
+            self.config.tgt_server, **server_opts)
+
+        if self.verbose > 2:
+            LOG.debug("Target server {s}: {re}".format(
+                s=self.tgt_server, re=repr(self.tgt_server)))
+
+        self.target = Connection(
+            self.tgt_server, self.config.tgt_bind_dn, self.config.tgt_bind_pw,
+            client_strategy=SAFE_SYNC, auto_bind=True)
+
+        if self.verbose > 1:
+            LOG.debug(
+                "Info about target server {}:\n".format(self.tgt_server) +
+                repr(self.tgt_server.info))
+
+    # -------------------------------------------------------------------------
+    def disconnect(self):
+
+        if self.target:
+            LOG.info("Unbinding from target server {} ...".format(self.tgt_server))
+            self.target.unbind()
+            self.target = None
+
+        if self.source:
+            LOG.info("Unbinding from source server {} ...".format(self.src_server))
+            self.source.unbind()
+            self.source = None
+
+        if self.tgt_server:
+            LOG.info("Disconnecting from target server {!r} ...".format(self.config.tgt_server))
+            self.tgt_server = None
+
+        if self.src_server:
+            LOG.info("Disconnecting from source server {!r} ...".format(self.config.src_server))
+            self.src_server = None
+
     # -------------------------------------------------------------------------
     def _run(self):
 
         LOG.info("Starting {a!r}, version {v!r} ...".format(
             a=self.appname, v=self.version))
 
+        try:
+            self.connect_source()
+            self.connect_target()
+            LOG.info("Sleeping ...")
+            time.sleep(2)
+        finally:
+            self.disconnect()
+
         LOG.info("Ending {a!r}.".format(
             a=self.appname, v=self.version))