From: Frank Brehm Date: Mon, 10 Jul 2017 14:44:35 +0000 (+0200) Subject: Adding target database connection X-Git-Tag: 0.1.2~170 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=c594db497202cb8703cacf3c2075c6a2e527258e;p=pixelpark%2Fadmin-tools.git Adding target database connection --- diff --git a/pp_lib/import_pdnsdata.py b/pp_lib/import_pdnsdata.py index 9e27c3a..5643886 100644 --- a/pp_lib/import_pdnsdata.py +++ b/pp_lib/import_pdnsdata.py @@ -16,10 +16,12 @@ import logging.config import re import pwd import textwrap +import traceback # Third party modules import six +import psycopg2 import pymysql # Own modules @@ -47,6 +49,13 @@ class ImportPdnsdataApp(PpConfigApplication): src_db_user = 'pdns' src_db_pass = 'ohtior4KaFei' + # Target DB data + tgt_db_host = 'systemshare.pixelpark.com' + tgt_db_port = 5432 + tgt_db_schema = 'pdns' + tgt_db_user = 'pdns' + tgt_db_pass = 'oo?fah7gai7X' + # ------------------------------------------------------------------------- def __init__(self, appname=None, version=__version__): @@ -58,6 +67,7 @@ class ImportPdnsdataApp(PpConfigApplication): self.default_mail_recipients = ['frank.brehm@pixelpark.com'] self.src_connection = None + self.tgt_connection = None super(ImportPdnsdataApp, self).__init__( appname=appname, version=version, description=description, @@ -77,8 +87,24 @@ class ImportPdnsdataApp(PpConfigApplication): cursorclass=pymysql.cursors.DictCursor ) except ValueError as e: - LOG.error("Could not connect to source databasei ({}): {}".format( + LOG.error("Could not connect to source database ({}): {}".format( e.__class__.__name__, e)) + traceback.print_exc() + self.exit(6) + + try: + self.tgt_connection = psycopg2.connect( + host=self.tgt_db_host, + port=self.tgt_db_port, + dbname=self.tgt_db_schema, + user=self.tgt_db_user, + password=self.tgt_db_pass, + ) + except ValueError as e: + LOG.error("Could not connect to target database ({}): {}".format( + e.__class__.__name__, e)) + traceback.print_exc() + self.exit(7) # ------------------------------------------------------------------------- def _run(self): @@ -156,9 +182,24 @@ class ImportPdnsdataApp(PpConfigApplication): if self.src_connection: LOG.debug("Closing source database connection.") - self.src_connection.close() + try: + self.src_connection.close() + except Exception as e: + LOG.error("Could not close source database connection ({}): {}".format( + e.__class__.__name__, e)) + traceback.print_exc() self.src_connection = None + if self.tgt_connection: + LOG.debug("Closing target database connection.") + try: + self.tgt_connection.close() + except Exception as e: + LOG.error("Could not close target database connection ({}): {}".format( + e.__class__.__name__, e)) + traceback.print_exc() + self.tgt_connection = None + # ------------------------------------------------------------------------- def post_run(self):