]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Importing supermasters and tsigkeys
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 11 Jul 2017 10:17:05 +0000 (12:17 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 11 Jul 2017 10:17:05 +0000 (12:17 +0200)
pp_lib/import_pdnsdata.py

index d646f996ba5044137a6a2a63a21d3161aa77b2dd..9fd81c326f71068db22f2fc27eec9adef5b5a537 100644 (file)
@@ -29,7 +29,7 @@ from .common import pp
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.4.3'
+__version__ = '0.4.4'
 LOG = logging.getLogger(__name__)
 
 # =============================================================================
@@ -106,7 +106,7 @@ class ImportPdnsdataApp(PpConfigApplication):
                 'invalid': 0,
             },
             'tsigkeys': {
-                'has_domain': True,
+                'has_domain': False,
                 'total': 0,
                 'valid': 0,
                 'invalid': 0,
@@ -151,10 +151,12 @@ class ImportPdnsdataApp(PpConfigApplication):
         try:
             self.get_src_info()
             self.clean_tgt_db()
+            self.import_supermasters()
             self.import_domains()
             self.import_cryptokeys()
             self.import_domainmetadata()
             self.import_records()
+            self.import_tsigkeys()
         finally:
             self._close_all()
 
@@ -270,7 +272,7 @@ class ImportPdnsdataApp(PpConfigApplication):
                          %(type)s, %(notified_serial)s, %(account)s)
             ''').strip()
         if self.verbose > 1:
-            LOG.debug("Source SQL:\n{}".format(tgt_sql))
+            LOG.debug("Target SQL:\n{}".format(tgt_sql))
 
 
         with self.tgt_connection.cursor() as tgt_cursor:
@@ -325,7 +327,7 @@ class ImportPdnsdataApp(PpConfigApplication):
                  VALUES (%(id)s, %(domain_id)s, %(flags)s, %(active)s, %(content)s)
             ''').strip()
         if self.verbose > 1:
-            LOG.debug("Source SQL:\n{}".format(tgt_sql))
+            LOG.debug("Target SQL:\n{}".format(tgt_sql))
 
 
         with self.tgt_connection.cursor() as tgt_cursor:
@@ -390,7 +392,7 @@ class ImportPdnsdataApp(PpConfigApplication):
                  VALUES (%(id)s, %(domain_id)s, %(kind)s, %(content)s)
             ''').strip()
         if self.verbose > 1:
-            LOG.debug("Source SQL:\n{}".format(tgt_sql))
+            LOG.debug("Target SQL:\n{}".format(tgt_sql))
 
 
         with self.tgt_connection.cursor() as tgt_cursor:
@@ -457,7 +459,7 @@ class ImportPdnsdataApp(PpConfigApplication):
                          %(ordername)s, %(auth)s)
             ''').strip()
         if self.verbose > 1:
-            LOG.debug("Source SQL:\n{}".format(tgt_sql))
+            LOG.debug("Target SQL:\n{}".format(tgt_sql))
 
 
         with self.tgt_connection.cursor() as tgt_cursor:
@@ -505,6 +507,110 @@ class ImportPdnsdataApp(PpConfigApplication):
         LOG.debug("Commiting changes ...")
         self.tgt_connection.commit()
 
+    # -------------------------------------------------------------------------
+    def import_supermasters(self):
+
+        LOG.info("Importing all supermasters ...")
+
+        src_sql = textwrap.dedent('''\
+            SELECT ip, nameserver, account
+              FROM supermasters
+            ORDER by nameserver
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Source SQL:\n{}".format(src_sql))
+
+        tgt_sql = textwrap.dedent('''\
+            INSERT INTO supermasters (ip, nameserver, account)
+                 VALUES (%(ip)s, %(nameserver)s, %(account)s)
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Target SQL:\n{}".format(tgt_sql))
+
+
+        with self.tgt_connection.cursor() as tgt_cursor:
+            with self.src_connection.cursor() as src_cursor:
+
+                i = 0
+                src_cursor.execute(src_sql)
+                results = src_cursor.fetchall()
+
+                if self.verbose > 2:
+                    LOG.debug("Got supermasters:\n{}".format(pp(results)))
+
+                if not results:
+                    LOG.info("No supermasters in source database.")
+                    LOG.debug("Commiting changes ...")
+                    self.tgt_connection.commit()
+                    return
+
+                for result in results:
+                    i += 1
+                    tgt_cursor.execute(tgt_sql, result)
+                LOG.info("Imported {} supermasters.".format(i))
+
+        LOG.debug("Commiting changes ...")
+        self.tgt_connection.commit()
+
+    # -------------------------------------------------------------------------
+    def import_tsigkeys(self):
+
+        LOG.info("Importing all tsigkeys ...")
+
+        src_sql = textwrap.dedent('''\
+            SELECT id, name, algorithm, secret
+              FROM tsigkeys
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Source SQL:\n{}".format(src_sql))
+
+        tgt_sql = textwrap.dedent('''\
+            INSERT INTO tsigkeys (id, name, algorithm, secret)
+                 VALUES (%(id)s, %(name)s, %(algorithm)s, %(secret)s)
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Target SQL:\n{}".format(tgt_sql))
+
+
+        with self.tgt_connection.cursor() as tgt_cursor:
+            with self.src_connection.cursor() as src_cursor:
+
+                i = 0
+                src_cursor.execute(src_sql)
+                results = src_cursor.fetchall()
+
+                if self.verbose > 2:
+                    LOG.debug("Got tsigkeys:\n{}".format(pp(results)))
+
+                if not results:
+                    LOG.info("No tsigkeys in source database.")
+                    LOG.debug("Commiting changes ...")
+                    self.tgt_connection.commit()
+                    return
+
+                for result in results:
+                    i += 1
+                    tgt_cursor.execute(tgt_sql, result)
+                LOG.info("Imported {} tsigkeys.".format(i))
+
+            LOG.debug("Get max. TsigKey Id ...")
+            sql = "SELECT MAX(id) AS max_id FROM tsigkeys"
+            if self.verbose > 1:
+                LOG.debug("SQL: {}".format(sql))
+            tgt_cursor.execute(sql)
+            result = tgt_cursor.fetchone()
+            if self.verbose > 2:
+                LOG.debug("Got max TsigKey Id:\n{}".format(pp(result)))
+            max_id = int(result[0])
+            sql = "SELECT SETVAL('tsigkeys_id_seq', %s)"
+            LOG.debug("Setting curval of tsigkeys_id_seq to {} ...".format(max_id))
+            if self.verbose > 1:
+                LOG.debug("SQL: {}".format(sql))
+            tgt_cursor.execute(sql, (max_id, ))
+
+        LOG.debug("Commiting changes ...")
+        self.tgt_connection.commit()
+
     # -------------------------------------------------------------------------
     def _close_all(self):