diff options
author | Wojciech Kosior <kwojtus@protonmail.com> | 2020-06-04 13:01:22 +0200 |
---|---|---|
committer | Wojciech Kosior <kwojtus@protonmail.com> | 2020-06-04 13:05:35 +0200 |
commit | bad64ce7e041fba0fc48e4b2a6f82509090cd456 (patch) | |
tree | 6da7990f9387d2e8d02ea7471dde02070074d57e | |
parent | 5b42795d2537a6bc1735018eea40d087c8ca40f2 (diff) | |
download | 0tdns-bad64ce7e041fba0fc48e4b2a6f82509090cd456.tar.gz 0tdns-bad64ce7e041fba0fc48e4b2a6f82509090cd456.zip |
put connecting to db in separate source file
-rwxr-xr-x | install.sh | 3 | ||||
-rwxr-xr-x | src/hourly.py | 15 | ||||
-rw-r--r-- | src/ztdns_db_connectivity.py | 14 | ||||
-rwxr-xr-x | uninstall.sh | 1 |
4 files changed, 24 insertions, 9 deletions
@@ -22,6 +22,9 @@ install -D -m644 db_connection_config.yml "$INSTALL_ROOT"/etc/0tdns/db_connectio # This one would make sense to be executed directly, so it'll go to sbin install -D -m744 src/hourly.py "$INSTALL_ROOT"/usr/sbin/hourly.py +# This one shall be imported from other scripts +install -D -m644 src/ztdns_db_connectivity.py "$INSTALL_ROOT"/usr/lib/python3/dist-packages/ztdns_db_connectivity.py + # This is the script, that will get called by cron install -D -m744 src/hourly.sh "$INSTALL_ROOT"/usr/sbin/hourly.sh diff --git a/src/hourly.py b/src/hourly.py index c1c598b..34097a3 100755 --- a/src/hourly.py +++ b/src/hourly.py @@ -2,13 +2,13 @@ from sys import argv import subprocess -import os.path -import yaml -import psycopg2 +from os import path + +# our own module used by several scripts in the project +from ztdns_db_connectivity import start_db_connection wrapper = '/var/lib/0tdns/vpn_wrapper.sh' perform_queries = '/var/lib/0tdns/perform_queries.py' -db_config_path = '/etc/0tdns/db_connection_config.yml' def sync_ovpn_config(cursor, vpn_id, config_path, config_hash): cursor.execute(''' @@ -35,10 +35,7 @@ def get_vpn_connections(cursor, hour): ''') return cursor.fetchall() -config = yaml.safe_load(open(db_config_path, 'r')) -connection = psycopg2.connect(user=config['user'], password=config['password'], - host=config['host'], port=config['port'], - database=config['database']) +connection = start_db_connection() cursor = connection.cursor() hour = argv[1] @@ -46,7 +43,7 @@ vpns = get_vpn_connections(cursor, hour) for vpn_id, config_hash in vpns: config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash) - if not os.path.isfile(config_path): + if not path.isfile(config_path): sync_ovpn_config(cursor, vpn_id, config_path, config_hash) cursor.close() diff --git a/src/ztdns_db_connectivity.py b/src/ztdns_db_connectivity.py new file mode 100644 index 0000000..b754daa --- /dev/null +++ b/src/ztdns_db_connectivity.py @@ -0,0 +1,14 @@ +import yaml +import psycopg2 + +db_config_path = '/etc/0tdns/db_connection_config.yml' + +def start_db_connection(): + config = yaml.safe_load(open(db_config_path, 'r')) + connection = psycopg2.connect(user=config['user'], password=config['password'], + host=config['host'], port=config['port'], + database=config['database']) + # we might later decide that each user of start_db_connection() + # should set it themselves - but for now, set it here + connection.autocommit = True + return connection diff --git a/uninstall.sh b/uninstall.sh index 55e1f3e..7c14cba 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -11,4 +11,5 @@ if [ "x$1" = "x--delete-files" ]; then rm -r /etc/netns/0tdns* rm /usr/sbin/hourly.sh /usr/sbin/hourly.py rm -r /etc/0tdns + rm /usr/lib/python3/dist-packages/ztdns_db_connectivity.py fi |