]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding import-pdnsdata and pp_lib/import_pdnsdata.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Jul 2017 14:52:08 +0000 (16:52 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 7 Jul 2017 14:52:08 +0000 (16:52 +0200)
import-pdnsdata [new file with mode: 0755]
pp_lib/import_pdnsdata.py [new file with mode: 0644]

diff --git a/import-pdnsdata b/import-pdnsdata
new file mode 100755 (executable)
index 0000000..22a8ce7
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+# Standard modules
+import sys
+import os
+import logging
+import locale
+
+# own modules:
+cur_dir = os.getcwd()
+base_dir = cur_dir
+
+if sys.argv[0] != '' and sys.argv[0] != '-c':
+    cur_dir = os.path.dirname(sys.argv[0])
+if os.path.exists(os.path.join(cur_dir, 'pp_lib')):
+    sys.path.insert(0, os.path.abspath(cur_dir))
+
+from pp_lib.import_pdnsdata import ImportPdnsdataApp
+
+log = logging.getLogger(__name__)
+
+__author__ = 'Frank Brehm <frank.brehm@pixelpark.com>'
+__copyright__ = '(C) 2017 by Frank Brehm, Pixelpark GmbH, Berlin'
+
+appname = os.path.basename(sys.argv[0])
+
+locale.setlocale(locale.LC_ALL, '')
+
+app = ImportPdnsdataApp(appname=appname)
+app.initialized = True
+
+if app.verbose > 2:
+    print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app))
+
+app()
+
+sys.exit(0)
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py
new file mode 100644 (file)
index 0000000..9e27c3a
--- /dev/null
@@ -0,0 +1,178 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+@author: Frank Brehm
+@contact: frank.brehm@pixelpark.com
+@copyright: © 2017 by Frank Brehm, Berlin
+@summary: The module for the 'import-pdnsdata' application
+          to import all data from the current PowerDNS database
+"""
+from __future__ import absolute_import
+
+# Standard modules
+import os
+import logging
+import logging.config
+import re
+import pwd
+import textwrap
+
+# Third party modules
+import six
+
+import pymysql
+
+# Own modules
+from .common import pp
+
+from .cfg_app import PpCfgAppError, PpConfigApplication
+
+__version__ = '0.1.0'
+LOG = logging.getLogger(__name__)
+
+# =============================================================================
+class ImportPdnsdataError(PpCfgAppError):
+    pass;
+
+# =============================================================================
+class ImportPdnsdataApp(PpConfigApplication):
+    """
+    Application class for the 'import-pdnsdata'-Application.
+    """
+
+    # Source DB data
+    src_db_host = 'mysql-pp06.pixelpark.com'
+    src_db_port = 3306
+    src_db_schema = 'pdns'
+    src_db_user = 'pdns'
+    src_db_pass = 'ohtior4KaFei'
+
+    # -------------------------------------------------------------------------
+    def __init__(self, appname=None, version=__version__):
+
+
+        description = textwrap.dedent('''\
+        Importing complete Database for PowerDNS from old DB into the new one.
+        ''').strip()
+
+        self.default_mail_recipients = ['frank.brehm@pixelpark.com']
+
+        self.src_connection = None
+
+        super(ImportPdnsdataApp, self).__init__(
+            appname=appname, version=version, description=description,
+            cfg_stems='import-pdnsdata'
+        )
+
+    # -------------------------------------------------------------------------
+    def pre_run(self):
+
+        try:
+            self.src_connection = pymysql.connect(
+                host=self.src_db_host,
+                db=self.src_db_schema,
+                user=self.src_db_user,
+                password=self.src_db_pass,
+                charset='utf8',
+                cursorclass=pymysql.cursors.DictCursor
+            )
+        except ValueError as e:
+            LOG.error("Could not connect to source databasei ({}): {}".format(
+                e.__class__.__name__, e))
+
+    # -------------------------------------------------------------------------
+    def _run(self):
+
+        try:
+            self.get_src_info()
+        finally:
+            self._close_all()
+
+    # -------------------------------------------------------------------------
+    def get_src_info(self):
+
+        result = None
+
+        nr_cryptokeys = 0
+        nr_domainmetadata = 0
+        nr_domains = 0
+        nr_records = 0
+        nr_supermasters = 0
+        nr_tsigkeys = 0
+
+        with self.src_connection.cursor() as cursor:
+
+            # Retrieve number of domains
+            sql = "SELECT COUNT(*) AS count_domains FROM domains"
+            cursor.execute(sql)
+            result = cursor.fetchone()
+            nr_domains = int(result['count_domains'])
+
+            # Retrieve number of cryptokeys
+            sql = "SELECT COUNT(*) AS count_cryptokeys FROM cryptokeys"
+            cursor.execute(sql)
+            result = cursor.fetchone()
+            nr_cryptokeys = int(result['count_cryptokeys'])
+
+            # Retrieve number of domainmetadata
+            sql = "SELECT COUNT(*) AS count_domainmetadata FROM domainmetadata"
+            cursor.execute(sql)
+            result = cursor.fetchone()
+            nr_domainmetadata = int(result['count_domainmetadata'])
+
+            # Retrieve number of records
+            sql = "SELECT COUNT(*) AS count_records FROM records"
+            cursor.execute(sql)
+            result = cursor.fetchone()
+            nr_records = int(result['count_records'])
+
+            # Retrieve number of supermasters
+            sql = "SELECT COUNT(*) AS count_supermasters FROM supermasters"
+            cursor.execute(sql)
+            result = cursor.fetchone()
+            nr_supermasters = int(result['count_supermasters'])
+
+            # Retrieve number of tsigkeys
+            sql = "SELECT COUNT(*) AS count_tsigkeys FROM tsigkeys"
+            cursor.execute(sql)
+            result = cursor.fetchone()
+            nr_tsigkeys = int(result['count_tsigkeys'])
+
+        title = "Number of rows in current PowerDNS database"
+
+        print()
+        print(title)
+        print(('=' * len(title)))
+        print("Found cryptokeys:     {:>8}".format(nr_cryptokeys))
+        print("Found domainmetadata: {:>8}".format(nr_domainmetadata))
+        print("Found domains:        {:>8}".format(nr_domains))
+        print("Found records:        {:>8}".format(nr_records))
+        print("Found supermasters:   {:>8}".format(nr_supermasters))
+        print("Found tsigkeys:       {:>8}".format(nr_tsigkeys))
+        print()
+
+    # -------------------------------------------------------------------------
+    def _close_all(self):
+
+        if self.src_connection:
+            LOG.debug("Closing source database connection.")
+            self.src_connection.close()
+            self.src_connection = None
+
+    # -------------------------------------------------------------------------
+    def post_run(self):
+
+        if self.verbose > 1:
+            LOG.info("executing post_run() ...")
+        self._close_all()
+
+
+# =============================================================================
+
+if __name__ == "__main__":
+
+    pass
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list