import pymysql
# Own modules
-from .common import pp, to_bool, to_str
+from .common import pp, to_str
from .common import RE_DOT_AT_END
from .cfg_app import PpCfgAppError, PpConfigApplication
from .pdns_record import PdnsSoaData
-__version__ = '0.9.4'
+__version__ = '0.10.1'
LOG = logging.getLogger(__name__)
# =============================================================================
self.tgt_db_user = self.default_tgt_db_user
self.tgt_db_pass = None
- self.tgt_db_is_poweradmin = False
-
self.src_connection = None
self.tgt_connection = None
if 'password' in section:
self.tgt_db_pass = section['password']
- if 'is_poweradmin' in section:
- self.tgt_db_is_poweradmin = to_bool(section['is_poweradmin'])
-
# -------------------------------------------------------------------------
def pre_run(self):
self.import_domainmetadata()
self.import_records()
self.import_tsigkeys()
- if self.tgt_db_is_poweradmin:
- self.import_perm_templ()
- self.import_perm_templ_items()
- self.import_users()
- self.import_zone_templ()
- self.import_zone_templ_records()
- self.import_zones()
- self.create_ipv6_as_zone()
+# self.create_ipv6_as_zone()
finally:
self._close_all()
'comments', 'cryptokeys', 'domainmetadata', 'records',
'supermasters', 'tsigkeys', 'domains',
]
- if self.tgt_db_is_poweradmin:
- tables.insert(0, 'zone_templ')
- tables.insert(0, 'zone_templ_records')
- tables.insert(0, 'zones')
sequences = [
'comments_id_seq', 'cryptokeys_id_seq', 'domainmetadata_id_seq',
'domains_id_seq', 'records_id_seq', 'tsigkeys_id_seq',
]
- if self.tgt_db_is_poweradmin:
- sequences.append('zone_templ_id_seq')
LOG.info("Truncating all tables in target database ...")
if not self.simulate:
tgt_cursor.execute(sql)
- if self.tgt_db_is_poweradmin:
-
- LOG.debug("Truncating table 'perm_templ_items' ...")
- sql = "DELETE FROM perm_templ_items WHERE templ_id != 1"
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql)
-
- LOG.debug("Truncating table 'users' ...")
- sql = 'DELETE FROM users WHERE id > 1'
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql)
-
- LOG.debug("Truncating table 'perm_templ' ...")
- sql = "DELETE FROM perm_templ WHERE id != 1"
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql)
-
if self.tgt_db_type != 'mysql':
for sequence in sequences:
if not self.simulate:
tgt_cursor.execute(sql)
- if self.tgt_db_is_poweradmin:
- LOG.debug("Get max. Zone Template Permission Id ...")
- sql = "SELECT MAX(id) AS max_id FROM perm_templ_items"
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- tgt_cursor.execute(sql)
- result = tgt_cursor.fetchone()
- if self.verbose > 3:
- LOG.debug("Got max Zone Template Permission Id:\n{}".format(pp(result)))
- if result[0] is not None:
- max_id = int(result[0])
- else:
- max_id = 0
- sql = "SELECT SETVAL('perm_templ_items_id_seq', %s)"
- LOG.debug("Setting curval of perm_templ_items_id_seq to {} ...".format(max_id))
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql, (max_id, ))
-
- sql = "SELECT SETVAL('zone_templ_records_id_seq', 0)"
- LOG.debug("Setting curval of zone_templ_records_id_seq to {} ...".format(0))
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql, (max_id, ))
-
- sql = "SELECT SETVAL('zones_id_seq', 0)"
- LOG.debug("Setting curval of zones_id_seq to {} ...".format(0))
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql, (max_id, ))
-
LOG.debug("Commiting changes ...")
self.tgt_connection.commit()
LOG.debug("Commiting changes ...")
self.tgt_connection.commit()
- # -------------------------------------------------------------------------
- def import_perm_templ(self):
-
- LOG.info("Importing all permission templates except 'Administrator' ...")
-
- src_sql = textwrap.dedent('''\
- SELECT id, name, descr
- FROM perm_templ
- WHERE id > 1
- ''').strip()
- if self.verbose > 1:
- LOG.debug("Source SQL:\n{}".format(src_sql))
-
- tgt_sql = textwrap.dedent('''\
- INSERT INTO perm_templ (
- id, name, descr)
- VALUES (
- %(id)s, %(name)s, %(descr)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 > 3:
- LOG.debug("Got users:\n{}".format(pp(results)))
-
- if not results:
- LOG.info("No permission templates in source database.")
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
- return
-
- for result in results:
- i += 1
- if not self.simulate:
- tgt_cursor.execute(tgt_sql, result)
- LOG.info("Imported {} permission templates.".format(i))
-
- if self.tgt_db_type != 'mysql':
- LOG.debug("Get max. Permission Template Id ...")
- sql = "SELECT MAX(id) AS max_id FROM perm_templ"
- 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 permission template Id:\n{}".format(pp(result)))
- max_id = int(result[0])
- sql = "SELECT SETVAL('perm_templ_id_seq', %s)"
- LOG.debug("Setting curval of perm_templ_id_seq to {} ...".format(max_id))
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql, (max_id, ))
-
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
-
- # -------------------------------------------------------------------------
- def import_perm_templ_items(self):
-
- LOG.info("Importing all permission template items ...")
-
- map_sql = textwrap.dedent('SELECT id, name from perm_items')
- if self.verbose > 1:
- LOG.debug("Mapping SQL:\n{}".format(map_sql))
-
- src_sql = textwrap.dedent('''\
- SELECT templ_id, perm_id
- FROM perm_templ_items
- WHERE templ_id != 1
- ''').strip()
- if self.verbose > 1:
- LOG.debug("Source SQL:\n{}".format(src_sql))
-
- tgt_sql = textwrap.dedent('''\
- INSERT INTO perm_templ_items (templ_id, perm_id)
- VALUES (%(templ_id)s, %(perm_id)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:
-
- tplid_map = {}
- tpl_names = {}
-
- LOG.debug("Getting old permission item Ids ...")
- src_cursor.execute(map_sql)
- results = src_cursor.fetchall()
- if self.verbose > 3:
- LOG.debug("Got old permission items:\n{}".format(pp(results)))
- for result in results:
- item_id = int(result['id'])
- item_name = result['name'].lower()
- tpl_names[item_name] = item_id
-
- LOG.debug("Getting new permission item Ids ...")
- tgt_cursor.execute(map_sql)
- results = tgt_cursor.fetchall()
- if self.verbose > 3:
- LOG.debug("Got new permission items:\n{}".format(pp(results)))
- for result in results:
- if self.tgt_db_type != 'mysql':
- item_id = int(result[0])
- item_name = result[1].lower()
- else:
- item_id = int(result['id'])
- item_name = result['name'].lower()
- if item_name in tpl_names:
- old_item_id = tpl_names[item_name]
- tplid_map[old_item_id] = item_id
- del tpl_names
- if self.verbose > 2:
- LOG.debug("Mapping old -> new template item Ids:\n{}".format(pp(tplid_map)))
-
- i = 0
- src_cursor.execute(src_sql)
- results = src_cursor.fetchall()
-
- if self.verbose > 3:
- LOG.debug("Got permission template items:\n{}".format(pp(results)))
-
- if not results:
- LOG.info("No permission template items in source database.")
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
- return
-
- for result in results:
- i += 1
- perm_id = int(result['perm_id'])
- if perm_id in tplid_map:
- result['perm_id'] = tplid_map[perm_id]
- if not self.simulate:
- tgt_cursor.execute(tgt_sql, result)
- LOG.info("Imported {} permission template items.".format(i))
-
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
-
- # -------------------------------------------------------------------------
- def import_zone_templ(self):
-
- LOG.info("Importing all zone templates ...")
-
- src_sql = textwrap.dedent('''\
- SELECT id, name, descr, owner
- FROM zone_templ
- ''').strip()
- if self.verbose > 1:
- LOG.debug("Source SQL:\n{}".format(src_sql))
-
- tgt_sql = textwrap.dedent('''\
- INSERT INTO zone_templ (
- id, name, descr, owner)
- VALUES (
- %(id)s, %(name)s, %(descr)s, %(owner)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 > 3:
- LOG.debug("Got zone templates:\n{}".format(pp(results)))
-
- if not results:
- LOG.info("No zone templates in source database.")
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
- return
-
- for result in results:
- i += 1
- if not self.simulate:
- tgt_cursor.execute(tgt_sql, result)
- LOG.info("Imported {} zone templates.".format(i))
-
- if self.tgt_db_type != 'mysql':
- LOG.debug("Get max. Zone Template Id ...")
- sql = "SELECT MAX(id) AS max_id FROM zone_templ"
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- tgt_cursor.execute(sql)
- result = tgt_cursor.fetchone()
- if self.verbose > 3:
- LOG.debug("Got max user Id:\n{}".format(pp(result)))
- if result[0] is None:
- max_id = 1
- else:
- max_id = int(result[0])
- sql = "SELECT SETVAL('zone_templ_id_seq', %s)"
- LOG.debug("Setting curval of zone_templ_id_seq to {} ...".format(max_id))
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql, (max_id, ))
-
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
-
- # -------------------------------------------------------------------------
- def import_users(self):
-
- LOG.info("Importing all users except 'admin' ...")
-
- src_sql = textwrap.dedent('''\
- SELECT id, username, password, fullname, email, description, perm_templ, active
- FROM users
- WHERE id > 1
- ''').strip()
- if self.verbose > 1:
- LOG.debug("Source SQL:\n{}".format(src_sql))
-
- tgt_sql = textwrap.dedent('''\
- INSERT INTO users (
- id, username, password, fullname, email,
- description, perm_templ, active, use_ldap)
- VALUES (
- %(id)s, %(username)s, %(password)s, %(fullname)s, %(email)s,
- %(description)s, %(perm_templ)s, %(active)s, 0)
- ''').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 > 3:
- LOG.debug("Got users:\n{}".format(pp(results)))
-
- if not results:
- LOG.info("No users in source database.")
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
- return
-
- for result in results:
- i += 1
- if not self.simulate:
- tgt_cursor.execute(tgt_sql, result)
- LOG.info("Imported {} users.".format(i))
-
- if self.tgt_db_type != 'mysql':
- LOG.debug("Get max. User Id ...")
- sql = "SELECT MAX(id) AS max_id FROM users"
- 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 user Id:\n{}".format(pp(result)))
- max_id = int(result[0])
- sql = "SELECT SETVAL('users_id_seq', %s)"
- LOG.debug("Setting curval of users_id_seq to {} ...".format(max_id))
- if self.verbose > 1:
- LOG.debug("SQL: {}".format(sql))
- if not self.simulate:
- tgt_cursor.execute(sql, (max_id, ))
-
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
-
- # -------------------------------------------------------------------------
- def import_zone_templ_records(self):
-
- LOG.info("Importing all zone template records ...")
-
- src_sql = textwrap.dedent('''\
- SELECT zone_templ_id, name, type, content, ttl, prio
- FROM zone_templ_records
- ORDER BY id
- ''').strip()
- if self.verbose > 1:
- LOG.debug("Source SQL:\n{}".format(src_sql))
-
- tgt_sql = textwrap.dedent('''\
- INSERT INTO zone_templ_records (
- zone_templ_id, name, type, content, ttl, prio)
- VALUES (
- %(zone_templ_id)s, %(name)s, %(type)s, %(content)s, %(ttl)s, %(prio)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 > 3:
- LOG.debug("Got zone templates:\n{}".format(pp(results)))
-
- if not results:
- LOG.info("No zone templates in source database.")
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
- return
-
- for result in results:
- i += 1
- if not self.simulate:
- tgt_cursor.execute(tgt_sql, result)
- LOG.info("Imported {} zone templates.".format(i))
-
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
-
- # -------------------------------------------------------------------------
- def import_zones(self):
-
- LOG.info("Importing all zones ...")
-
- src_sql = textwrap.dedent('''\
- SELECT domain_id, owner, comment, zone_templ_id
- FROM zones
- WHERE domain_id IN (
- SELECT id FROM domains)
- ORDER BY id
- ''').strip()
- if self.verbose > 1:
- LOG.debug("Source SQL:\n{}".format(src_sql))
-
- tgt_sql = textwrap.dedent('''\
- INSERT INTO zones (
- domain_id, owner, comment, zone_templ_id)
- VALUES (
- %(domain_id)s, %(owner)s, %(comment)s, %(zone_templ_id)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 > 3:
- LOG.debug("Got zones:\n{}".format(pp(results)))
-
- if not results:
- LOG.info("No zones in source database.")
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
- return
-
- for result in results:
- i += 1
-
- comment = result['comment']
- if comment is not None:
- comment = comment.strip()
- if comment == '':
- comment = None
- result['comment'] = comment
-
- zone_templ_id = result['zone_templ_id']
- if zone_templ_id is not None and zone_templ_id == 0:
- result['zone_templ_id'] = None
-
- if not self.simulate:
- tgt_cursor.execute(tgt_sql, result)
-
- LOG.info("Imported {} zones.".format(i))
-
- LOG.debug("Commiting changes ...")
- self.tgt_connection.commit()
-
# -------------------------------------------------------------------------
def create_ipv6_as_zone(self):