from .pdns_record import PdnsSoaData
-__version__ = '0.10.2'
+__version__ = '0.10.3'
LOG = logging.getLogger(__name__)
# =============================================================================
VALUES (%(domain_id)s, %(kind)s, %(content)s)
''').strip()
+ sql_insert_record = textwrap.dedent('''\
+ INSERT INTO records (id, domain_id, name, type, content,
+ ttl, prio, change_date, disabled,
+ ordername, auth)
+ VALUES (%(id)s, %(domain_id)s, %(name)s, %(type)s, %(content)s,
+ %(ttl)s, %(prio)s, %(change_date)s, %(disabled)s,
+ %(ordername)s, %(auth)s)
+ ''').strip()
+
# -------------------------------------------------------------------------
def __init__(self, appname=None, version=__version__):
LOG.debug("Commiting changes ...")
self.tgt_connection.commit()
+ # -------------------------------------------------------------------------
+ def _import_record(self, record, tgt_cursor):
+
+ if self.tgt_db_type == 'mysql':
+ record['disabled'] = 0
+ else:
+ record['disabled'] = False
+ if record['auth'] is None:
+ record['auth'] = True
+ else:
+ if record['auth']:
+ record['auth'] = True
+ else:
+ record['auth'] = False
+ if record['ordername'] is None:
+ dom_id = record['domain_id']
+ if dom_id in self.domain_ids:
+ dom_name = self.domain_ids[dom_id]
+ if record['name'] == dom_name:
+ record['ordername'] = ''
+ else:
+ idx = record['name'].rfind('.' + dom_name)
+ if idx >= 0:
+ record['ordername'] = record['name'][:idx]
+ else:
+ record['ordername'] = ''
+ else:
+ record['ordername'] = ''
+ if record['type'] in ('NS', 'MX'):
+ record['content'] = RE_DOT_AT_END.sub('', record['content'])
+ elif record['type'] == 'SOA':
+ soa = PdnsSoaData.init_from_data(
+ record['content'], appname=self.appname,
+ verbose=self.verbose, base_dir=self.base_dir)
+ soa.primary = RE_DOT_AT_END.sub('', soa.primary)
+ soa.email = RE_DOT_AT_END.sub('', soa.email)
+ record['content'] = soa.data
+ if self.verbose > 3:
+ LOG.debug("SQL for insert record:\n{}".format(
+ to_str(tgt_cursor.mogrify(self.sql_insert_record, record))))
+ if not self.simulate:
+ tgt_cursor.execute(self.sql_insert_record, record)
+
# -------------------------------------------------------------------------
def import_records(self):
if self.verbose > 1:
LOG.debug("Source SQL:\n{}".format(src_sql))
- tgt_sql = textwrap.dedent('''\
- INSERT INTO records (id, domain_id, name, type, content,
- ttl, prio, change_date, disabled,
- ordername, auth)
- VALUES (%(id)s, %(domain_id)s, %(name)s, %(type)s, %(content)s,
- %(ttl)s, %(prio)s, %(change_date)s, %(disabled)s,
- %(ordername)s, %(auth)s)
- ''').strip()
if self.verbose > 1:
- LOG.debug("Target SQL:\n{}".format(tgt_sql))
+ LOG.debug("Target SQL:\n{}".format(self.sql_insert_record))
with self.tgt_connection.cursor() as tgt_cursor:
with self.src_connection.cursor() as src_cursor:
for result in results:
i += 1
- if self.tgt_db_type == 'mysql':
- result['disabled'] = 0
- else:
- result['disabled'] = False
- if result['auth'] is None:
- result['auth'] = True
- else:
- if result['auth']:
- result['auth'] = True
- else:
- result['auth'] = False
- if result['ordername'] is None:
- dom_id = result['domain_id']
- if dom_id in self.domain_ids:
- dom_name = self.domain_ids[dom_id]
- if result['name'] == dom_name:
- result['ordername'] = ''
- else:
- idx = result['name'].rfind('.' + dom_name)
- if idx >= 0:
- result['ordername'] = result['name'][:idx]
- else:
- result['ordername'] = ''
- else:
- result['ordername'] = ''
- if result['type'] in ('NS', 'MX'):
- result['content'] = RE_DOT_AT_END.sub('', result['content'])
- elif result['type'] == 'SOA':
- soa = PdnsSoaData.init_from_data(
- result['content'], appname=self.appname,
- verbose=self.verbose, base_dir=self.base_dir)
- soa.primary = RE_DOT_AT_END.sub('', soa.primary)
- soa.email = RE_DOT_AT_END.sub('', soa.email)
- result['content'] = soa.data
- if not self.simulate:
- tgt_cursor.execute(tgt_sql, result)
+ self._import_record(result, tgt_cursor)
+
LOG.info("Imported {} records.".format(i))
if self.tgt_db_type != 'mysql':
sql = "SELECT SETVAL('records_id_seq', %s)"
LOG.debug("Setting curval of records_id_seq to {} ...".format(max_id))
if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
+ LOG.debug("SQL: {}".format(to_str(tgt_cursor.mogrify(sql, (max_id, )))))
if not self.simulate:
tgt_cursor.execute(sql, (max_id, ))