]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding impot of domains.
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 10 Jul 2017 16:14:30 +0000 (18:14 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 10 Jul 2017 16:14:30 +0000 (18:14 +0200)
pp_lib/import_pdnsdata.py

index d685bb4d83964594aac068815819ecacc05913a7..5c02979006d68bad3e0d69a2abe9b326ca92d0ba 100644 (file)
@@ -29,7 +29,7 @@ from .common import pp
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.3.0'
+__version__ = '0.4.0'
 LOG = logging.getLogger(__name__)
 
 # =============================================================================
@@ -112,6 +112,7 @@ class ImportPdnsdataApp(PpConfigApplication):
         try:
             self.get_src_info()
             self.clean_tgt_db()
+            self.import_domains()
         finally:
             self._close_all()
 
@@ -181,8 +182,6 @@ class ImportPdnsdataApp(PpConfigApplication):
     # -------------------------------------------------------------------------
     def clean_tgt_db(self):
 
-        result = None
-
         tables = [
             'comments', 'cryptokeys', 'domainmetadata', 'records',
             'supermasters', 'tsigkeys', 'domains',
@@ -190,9 +189,7 @@ class ImportPdnsdataApp(PpConfigApplication):
 
         LOG.info("Truncating all tables in target database ...")
 
-        cur = self.tgt_connection.cursor()
-
-        try:
+        with self.tgt_connection.cursor() as tgt_cursor:
 
             for table in tables:
 
@@ -200,13 +197,62 @@ class ImportPdnsdataApp(PpConfigApplication):
                 sql = 'TRUNCATE TABLE {} RESTART IDENTITY CASCADE'.format(table)
                 if self.verbose > 1:
                     LOG.debug("SQL: {}".format(sql))
-                cur.execute(sql)
+                tgt_cursor.execute(sql)
 
-            LOG.debug("Commiting changes ...")
-            self.tgt_connection.commit()
+        LOG.debug("Commiting changes ...")
+        self.tgt_connection.commit()
 
-        finally:
-            cur.close()
+    # -------------------------------------------------------------------------
+    def import_domains(self):
+
+        LOG.info("Importing all domains ...")
+
+        src_sql = textwrap.dedent('''\
+            SELECT id, name, master, last_check, type, notified_serial, account
+              FROM domains
+            ORDER by name
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Source SQL: {}".format(src_sql))
+
+        tgt_sql = textwrap.dedent('''\
+            INSERT INTO domains (id, name, master, last_check, type, notified_serial, account)
+                 VALUES (%(id)s, %(name)s, %(master)s, %(last_check)s,
+                         %(type)s, %(notified_serial)s, %(account)s)
+            ''').strip()
+        if self.verbose > 1:
+            LOG.debug("Source SQL: {}".format(tgt_sql))
+
+
+        with self.tgt_connection.cursor() as tgt_cursor:
+            with self.src_connection.cursor() as src_cursor:
+
+                src_cursor.execute(src_sql)
+                results = src_cursor.fetchall()
+
+                if self.verbose > 2:
+                    LOG.debug("Got domains:\n{}".format(pp(results)))
+
+                for result in results:
+                    tgt_cursor.execute(tgt_sql, result)
+
+            LOG.debug("Get max. Domain Id ...")
+            sql = "SELECT MAX(id) AS max_id from domains"
+            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 domain Id:\n{}".format(pp(result)))
+            max_id = int(result[0])
+            sql = "select setval('domains_id_seq', %s)"
+            LOG.debug("Setting curval of domains_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()
 
 
     # -------------------------------------------------------------------------