Skip to content
Snippets Groups Projects
Commit d8ac96ef authored by Fanna Lautenbach's avatar Fanna Lautenbach
Browse files

add postgres db connector with ssh tunneling and configuration reader

parent 7755f3e2
No related branches found
No related tags found
2 merge requests!4SDC-685: Script which migrates ldvadmin data to ldv-spec-db,!3add postgres db connector with ssh tunneling and configuration reader
from configparser import ConfigParser
def read_config(section, filename='database.cfg'):
parser = ConfigParser()
try:
parser.read(filename)
except FileNotFoundError as exc:
raise FileNotFoundError(
"Configuration file with filename {0} not found".format(filename)
) from exc
db_settings = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db_settings[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db_settings
[postgresql-local]
host=localhost
port=5433
database=ldv-spec-db
user=postgres
password=secret
[postgresql-ldv]
tunnelhost=dop821.astron.nl
tunnelusername=sdco
host=sdc-db.astron.nl
port=5432
database=ldvadmin
user=ldvrbow
password=Wehn5CTYj1RcbstMSGls
\ No newline at end of file
import sys
import psycopg2
from config import read_config
import logging
import argparse
from sshtunnel import SSHTunnelForwarder
import os
def connect_postgresql(section):
""" Connect to the PostgreSQL database server """
conn = None
tunnel = None
try:
# read connection parameters
configuration = read_config(section=section)
logging.info('Connecting PostgreSQL database %s', configuration.get('database', 'no database name given'))
host = configuration.get('host', 'no host given')
if host != 'localhost':
tunnel = open_tunnel(configuration)
conn = psycopg2.connect(host='localhost',
port=tunnel.local_bind_port,
database=configuration.get('database'),
user=configuration.get('user'),
password=configuration.get('password'))
else:
conn = psycopg2.connect(**configuration)
cur = conn.cursor()
cur.execute('SELECT version()')
db_version = cur.fetchone()
logging.info('Database version: ' + db_version[0])
except (Exception, psycopg2.DatabaseError) as error:
logging.error(error)
if tunnel is not None:
tunnel.stop()
return conn, tunnel
def open_tunnel(configuration_params):
tunnel_host = configuration_params.get('tunnelhost', "no tunnel host given")
tunnel_username = configuration_params.get('tunnelusername', "no username for the tunnel given")
host = configuration_params.get('host', "no host given")
port = int(configuration_params.get('port', "no port given"))
try:
ssh_config_file = os.path.expanduser("~/.ssh/config")
except FileNotFoundError as exc:
raise FileNotFoundError(
"Ssh config file not found on standard path '~/.ssh/config'. This is mandatory for opening the ssh tunnel"
) from exc
logging.info("Creating ssh tunnel for %s and port %s with tunnel host %s and username %s", repr(host), port,
repr(tunnel_host), repr(tunnel_username))
ssh_tunnel = SSHTunnelForwarder(
ssh_address_or_host=tunnel_host,
ssh_username=tunnel_username,
ssh_config_file=ssh_config_file,
remote_bind_address=(host, port)
)
ssh_tunnel.start()
return ssh_tunnel
def main():
"""
Opens a database connection from configuration file database.cfg
"""
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG)
# Check the invocation arguments
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--section", help="Add the configuration's section from the database.cfg.")
args = parser.parse_args()
if not args.section:
logging.critical("Error: no configuration section given. Try --help")
sys.exit(-1)
return connect_postgresql(args.section)
if __name__ == '__main__':
connection, server = main()
if connection is not None:
connection.close()
logging.info('Database connection closed.')
if server is not None:
server.stop()
logging.info('Tunneled server stopped.')
......@@ -10,4 +10,5 @@ whitenoise==5.0.1
six==1.15.0
fontawesome-free==5.15.2
pyyaml==6.0
uritemplate==4.1.1
\ No newline at end of file
uritemplate==4.1.1
sshtunnel==0.4.0
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment