from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.7.1'
+__version__ = '0.7.2'
LOG = logging.getLogger(__name__)
# =============================================================================
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()
finally:
self._close_all()
if not self.simulate:
tgt_cursor.execute(sql)
+ 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)))
+ max_id = int(result[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, ))
LOG.debug("Commiting changes ...")
self.tgt_connection.commit()
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):
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: