]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding zone '0.0.7.a.8.7.9.0.1.0.0.2.ip6.arpa' at the end of importing old PDNS data
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 18 Jan 2018 11:35:08 +0000 (12:35 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 18 Jan 2018 11:35:08 +0000 (12:35 +0100)
pp_lib/import_pdnsdata.py

index c517f8dc1ea589aa9306bb762c478725766f3101..e54c34af4969a69ab709f25e9a2f0b8ad6e9a516 100644 (file)
@@ -19,6 +19,8 @@ import textwrap
 import traceback
 import socket
 import ipaddress
+import datetime
+import time
 
 # Third party modules
 import six
@@ -27,11 +29,13 @@ import psycopg2
 import pymysql
 
 # Own modules
-from .common import pp, to_bool
+from .common import pp, to_bool, to_str
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.8.10'
+from .pdns_record import PdnsSoaData
+
+__version__ = '0.9.1'
 LOG = logging.getLogger(__name__)
 
 # =============================================================================
@@ -419,6 +423,7 @@ class ImportPdnsdataApp(PpConfigApplication):
                 self.import_zone_templ()
                 self.import_zone_templ_records()
                 self.import_zones()
+            self.create_ipv6_as_zone()
         finally:
             self._close_all()
 
@@ -657,27 +662,19 @@ class ImportPdnsdataApp(PpConfigApplication):
                             result['account'] = cur_account
                     if self.verbose > 1:
                         LOG.debug("SQL for insert domain:\n{}".format(
-                            tgt_cursor.mogrify(tgt_sql, result)))
+                            to_str(tgt_cursor.mogrify(tgt_sql, result))))
                     if not self.simulate:
                         tgt_cursor.execute(tgt_sql, result)
 
-                    # Inserting domain metadata for SOA-EDIT-DNSUPDATE
+                    # Inserting domain metadata for SOA-EDIT-API
                     params = {
                         'domain_id': dom_id,
-                        'kind': 'SOA-EDIT-DNSUPDATE',
-                        'content': 'DEFAULT',
+                        'kind': 'SOA-EDIT-API',
+                        'content': 'INCEPTION-INCREMENT',
                     }
                     if self.verbose > 1:
-                        LOG.debug("SQL for insert domain metadata 1:\n{}".format(
-                            tgt_cursor.mogrify(tgt_sql_metadata, params)))
-                    if not self.simulate:
-                        tgt_cursor.execute(tgt_sql_metadata, params)
-
-                    # Inserting domain metadata for SOA-EDIT-API
-                    params['kind'] = 'SOA-EDIT-API'
-                    if self.verbose > 1:
-                        LOG.debug("SQL for insert domain metadata 2:\n{}".format(
-                            tgt_cursor.mogrify(tgt_sql_metadata, params)))
+                        LOG.debug("SQL for insert domain metadata:\n{}".format(
+                            to_str(tgt_cursor.mogrify(tgt_sql_metadata, params))))
                     if not self.simulate:
                         tgt_cursor.execute(tgt_sql_metadata, params)
 
@@ -1415,6 +1412,127 @@ class ImportPdnsdataApp(PpConfigApplication):
         LOG.debug("Commiting changes ...")
         self.tgt_connection.commit()
 
+    # -------------------------------------------------------------------------
+    def create_ipv6_as_zone(self):
+
+        zone_name = '0.0.7.a.8.7.9.0.1.0.0.2.ip6.arpa'
+        net_addr = '2001:978:a700::'
+        nameservers = (
+            'ns1.pp-dns.com.',
+            'ns2.pp-dns.com.',
+            'ns3.pp-dns.com.',
+            'ns4.pp-dns.com.',
+        )
+        mail_addr = 'hostmaster.pixelpark.net.'
+
+        LOG.info("Creating zone {z!r} for AS network {n!r} ...".format(
+            z=zone_name, n=net_addr))
+
+        today = datetime.date.today()
+        serial = 1000000 * today.year + 10000 * today.month + 100 * today.day + 1
+        domain_id = 9999
+        refresh = 10800
+        retry = 3600
+        expire = 604800
+        ttl = 3600
+        change_date = int(time.time())
+
+        with self.tgt_connection.cursor() as tgt_cursor:
+
+            LOG.debug("Inserting domain ...")
+            sql = textwrap.dedent('''\
+                INSERT INTO domains (name, master, type, notified_serial, account)
+                  VALUES (%(zone_name)s, '', 'MASTER', %(serial)s, 'public')
+                ''').strip()
+            data = {'zone_name': zone_name, 'serial': serial}
+            if self.verbose > 1:
+                LOG.debug("SQL for insert domain:\n{}".format(
+                    to_str(tgt_cursor.mogrify(sql, data))))
+            if not self.simulate:
+                tgt_cursor.execute(sql, data)
+
+            LOG.debug("Retrieving domain_id from DB ...")
+            sql = 'SELECT id FROM domains WHERE name = %s'
+            if self.verbose > 1:
+                LOG.debug("SQL for retrieving domain_id:\n{}".format(
+                    to_str(tgt_cursor.mogrify(sql, [zone_name]))))
+            if not self.simulate:
+                domain_id = None
+                tgt_cursor.execute(sql, [zone_name])
+                results = tgt_cursor.fetchall()
+                if self.verbose > 2:
+                    LOG.debug("Got results:\n{}".format(pp(results)))
+                for result in results:
+                    domain_id = result[0]
+                if domain_id is None:
+                    raise ImportPdnsdataError(
+                        "Did not found Domain Id of zone {!r}.".format(zone_name))
+            LOG.info("Using Id of zone {z!r}: {i}.".format(z=zone_name, i=domain_id))
+
+            soa = PdnsSoaData(
+                primary=nameservers[0], email=mail_addr, serial=serial,
+                refresh=refresh, retry=retry, expire=expire, ttl=ttl,
+                appname=self.appname, verbose=self.verbose,  base_dir=self.base_dir,
+            )
+
+            LOG.debug("Inserting SOA {!r} ...".format(soa.data))
+            sql = textwrap.dedent('''\
+                INSERT INTO records (
+                        domain_id, name, type, content, ttl, prio,
+                        change_date, disabled, ordername, auth)
+                    VALUES (
+                        %(domain_id)s, %(name)s, 'SOA', %(content)s, %(ttl)s, 0,
+                        %(change_date)s, %(disabled)s, '', %(auth)s)
+                ''').strip()
+            data = {
+                'domain_id': domain_id, 'name': zone_name, 'content': soa.data,
+                'ttl': ttl, 'change_date': change_date, 'disabled': False, 'auth': True,
+            }
+            if self.verbose > 1:
+                LOG.debug("SQL for insert SOA:\n{}".format(
+                    to_str(tgt_cursor.mogrify(sql, data))))
+            if not self.simulate:
+                tgt_cursor.execute(sql, data)
+
+            LOG.debug("Inserting nameservers ...")
+            sql = textwrap.dedent('''\
+                INSERT INTO records (
+                        domain_id, name, type, content, ttl, prio,
+                        change_date, disabled, ordername, auth)
+                    VALUES (
+                        %(domain_id)s, %(name)s, 'NS', %(content)s, %(ttl)s, 0,
+                        %(change_date)s, %(disabled)s, '', %(auth)s)
+                ''').strip()
+            for ns in nameservers:
+                data = {
+                    'domain_id': domain_id, 'name': zone_name, 'content': ns,
+                    'ttl': ttl, 'change_date': change_date, 'disabled': False, 'auth': True,
+                }
+                if self.verbose > 1:
+                    LOG.debug("SQL for insert nameserver:\n{}".format(
+                        to_str(tgt_cursor.mogrify(sql, data))))
+                if not self.simulate:
+                    tgt_cursor.execute(sql, data)
+
+            LOG.debug("Inserting domain metadata ...")
+            sql = textwrap.dedent('''\
+                INSERT INTO domainmetadata (domain_id, kind, content)
+                    VALUES (%(domain_id)s, %(kind)s, %(content)s)
+                ''').strip()
+            data = {
+                'domain_id': domain_id,
+                'kind': 'SOA-EDIT-API',
+                'content': 'INCEPTION-INCREMENT',
+            }
+            if self.verbose > 1:
+                LOG.debug("SQL for insert domain metadata:\n{}".format(
+                    to_str(tgt_cursor.mogrify(sql, data))))
+            if not self.simulate:
+                tgt_cursor.execute(sql, data)
+
+        LOG.debug("Commiting changes ...")
+        self.tgt_connection.commit()
+
     # -------------------------------------------------------------------------
     def _close_all(self):