]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Starting with rewrite for MySQL target DB
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 21 Jul 2017 10:38:28 +0000 (12:38 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 21 Jul 2017 10:38:28 +0000 (12:38 +0200)
pp_lib/import_pdnsdata.py

index cfe942b72ef380caa4e827fb408a626f93e1ad5c..f11eb4c55c005c9c92a12edbf7580a2963fc9112 100644 (file)
@@ -30,7 +30,7 @@ from .common import pp
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.6.1'
+__version__ = '0.6.2'
 LOG = logging.getLogger(__name__)
 
 # =============================================================================
@@ -282,6 +282,12 @@ class ImportPdnsdataApp(PpConfigApplication):
     # -------------------------------------------------------------------------
     def pre_run(self):
 
+        self.connect_src_db()
+        self.connect_tgt_db()
+
+    # -------------------------------------------------------------------------
+    def connect_src_db(self):
+
         result = None
 
         LOG.debug("Connecting to source MySQL database on {}@{}:{}/{} ...".format(
@@ -312,6 +318,52 @@ class ImportPdnsdataApp(PpConfigApplication):
                 e.__class__.__name__, e))
             self.exit(6)
 
+    # -------------------------------------------------------------------------
+    def connect_tgt_db(self):
+
+        if self.tgt_db_type == 'mysql':
+            self.connect_tgt_db_mysql()
+        else:
+            self.connect_tgt_db_psql()
+
+     # -------------------------------------------------------------------------
+    def connect_tgt_db_mysql(self):
+
+        result = None
+
+        LOG.debug("Connecting to target MySQL database on {}@{}:{}/{} ...".format(
+            self.tgt_db_user, self.tgt_db_host, self.tgt_db_port, self.tgt_db_schema))
+        try:
+            self.tgt_connection = pymysql.connect(
+                host=self.tgt_db_host,
+                port=self.tgt_db_port,
+                db=self.tgt_db_schema,
+                user=self.tgt_db_user,
+                password=self.tgt_db_pass,
+                charset='utf8',
+                cursorclass=pymysql.cursors.DictCursor
+            )
+
+            sql = 'SHOW VARIABLES LIKE "version"'
+            if self.verbose > 1:
+                LOG.debug("SQL: {}".format(sql))
+            with self.tgt_connection.cursor() as cursor:
+                cursor.execute(sql)
+                result = cursor.fetchone()
+            if self.verbose > 2:
+                LOG.debug("Got version info:\n{}".format(pp(result)))
+            LOG.info("Target database is MySQL version {!r}.".format(result['Value']))
+
+        except (pymysql.err.OperationalError) as e:
+            LOG.error("Could not connect to target database ({}): {}".format(
+                e.__class__.__name__, e))
+            self.exit(6)
+
+     # -------------------------------------------------------------------------
+    def connect_tgt_db_psql(sellf):
+
+        result = None
+
         LOG.debug("Connecting to target PostgreSQL database on {}@{}:{}/{} ...".format(
             self.tgt_db_user, self.tgt_db_host, self.tgt_db_port, self.tgt_db_schema))
         try: