From afcc73eb215e8f873ed982a869e515e0450f6a56 Mon Sep 17 00:00:00 2001 From: Wojciech Kosior Date: Fri, 12 Jun 2020 16:31:38 +0200 Subject: add small fixes and pseudo-logging to hourly script --- src/hourly.py | 60 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/hourly.py b/src/hourly.py index 4dd8f73..da6cf3b 100755 --- a/src/hourly.py +++ b/src/hourly.py @@ -39,36 +39,42 @@ def get_vpn_connections(cursor, hour): ''') return cursor.fetchall() -ztdns_config = get_ztdns_config() -if ztdns_config['enabled'] != 'yes': - exit() - -connection = start_db_connection(ztdns_config) -cursor = connection.cursor() +with open("/var/log/0tdns.log", "w") as logfile: + # round down to an hour - this datetime format is one + # of the formats accepted by postgres + hour = strftime('%Y-%m-%d %H:00', gmtime()) + logfile.write("Running for {}\n".format(hour)) + + ztdns_config = get_ztdns_config() + if ztdns_config['enabled'] != 'yes': + logfile.write("0tdns not enabled in the config - exiting\n") + exit() -# round down to an hour - this datetime format is one -# of the formats accepted by postgres -hour = strftime('%Y-%m-%d %H:00', gmtime()) + connection = start_db_connection(ztdns_config) + cursor = connection.cursor() -vpns = get_vpn_connections(cursor, hour) + vpns = get_vpn_connections(cursor, hour) -handled_vpns = ztdns_config['handled_vpns'] -if handled_vpns: - vpns = [vpn for vpn in vpns if vpn[0] in handled_vpns] + handled_vpns = ztdns_config.get('handled_vpns') + if handled_vpns: + logfile.write("Only handling vpns of ids {}\n".format(handled_vpns)) + vpns = [vpn for vpn in vpns if vpn[0] in handled_vpns] -for vpn_id, config_hash in vpns: - config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash) - if not path.isfile(config_path): - sync_ovpn_config(cursor, vpn_id, config_path, config_hash) + for vpn_id, config_hash in vpns: + config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash) + if not path.isfile(config_path): + logfile.write("Syncing config for vpn {} with hash {}\n"\ + .format(vpn_id, config_hash)) + sync_ovpn_config(cursor, vpn_id, config_path, config_hash) -cursor.close() -connection.close() + cursor.close() + connection.close() -for vpn_id, config_hash in vpns: - config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash) - physical_ip = get_default_host_address(ztdns_config['host']) - route_through_veth = ztdns_config['host'] + "/32" - command_in_namespace = [perform_queries, hour, str(vpn_id)] - print('RUNNING OPENVPN FOR {}'.format(vpn_id)) - subprocess.run([wrapper, config_path, physical_ip, - route_through_veth] + command_in_namespace) + for vpn_id, config_hash in vpns: + config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash) + physical_ip = get_default_host_address(ztdns_config['host']) + route_through_veth = ztdns_config['host'] + "/32" + command_in_namespace = [perform_queries, hour, str(vpn_id)] + logfile.write("Running connection for vpn {}\n".format(vpn_id)) + subprocess.run([wrapper, config_path, physical_ip, + route_through_veth] + command_in_namespace) -- cgit v1.2.3