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

MR comments; exception handling for unknown files, change ini format to cfg format

parent f7294402
No related branches found
No related tags found
1 merge request!3add postgres db connector with ssh tunneling and configuration reader
Pipeline #33912 passed
from configparser import ConfigParser from configparser import ConfigParser
def read_config(section, filename='database.ini'): def read_config(section, filename='database.cfg'):
parser = ConfigParser() parser = ConfigParser()
parser.read(filename) try:
parser.read(filename)
except FileNotFoundError as exc:
raise FileNotFoundError(
"Configuration file with filename {0} not found".format(filename)
) from exc
db_settings = {} db_settings = {}
if parser.has_section(section): if parser.has_section(section):
......
...@@ -47,12 +47,20 @@ def open_tunnel(configuration_params): ...@@ -47,12 +47,20 @@ def open_tunnel(configuration_params):
tunnel_username = configuration_params.get('tunnelusername', "no username for the tunnel given") tunnel_username = configuration_params.get('tunnelusername', "no username for the tunnel given")
host = configuration_params.get('host', "no host given") host = configuration_params.get('host', "no host given")
port = int(configuration_params.get('port', "no port 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, 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)) repr(tunnel_host), repr(tunnel_username))
ssh_tunnel = SSHTunnelForwarder( ssh_tunnel = SSHTunnelForwarder(
ssh_address_or_host=tunnel_host, ssh_address_or_host=tunnel_host,
ssh_username=tunnel_username, ssh_username=tunnel_username,
ssh_config_file=os.path.expanduser("~/.ssh/config"), ssh_config_file=ssh_config_file,
remote_bind_address=(host, port) remote_bind_address=(host, port)
) )
ssh_tunnel.start() ssh_tunnel.start()
...@@ -61,13 +69,13 @@ def open_tunnel(configuration_params): ...@@ -61,13 +69,13 @@ def open_tunnel(configuration_params):
def main(): def main():
""" """
Opens a database connection from configuration file database.ini Opens a database connection from configuration file database.cfg
""" """
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG) logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG)
# Check the invocation arguments # Check the invocation arguments
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-s", "--section", help="Add the configuration's section from the database.ini.") parser.add_argument("-s", "--section", help="Add the configuration's section from the database.cfg.")
args = parser.parse_args() args = parser.parse_args()
if not args.section: if not args.section:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment