From dc19e0be138be375b5b2a8d2c5db549823e578c5 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 11 Jul 2017 12:17:05 +0200 Subject: [PATCH] Importing supermasters and tsigkeys --- pp_lib/import_pdnsdata.py | 118 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 6 deletions(-) diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index d646f99..9fd81c3 100644 --- a/pp_lib/import_pdnsdata.py +++ b/pp_lib/import_pdnsdata.py @@ -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): -- 2.39.5