explain why we put subprocess in the dict
[0tDNS.git] / src / add_config.py
blob4fd4feb43c9b2e8516513f25cc3ea605a07d69ff
1 #!/bin/python3
3 from sys import argv
4 import yaml
5 import psycopg2
6 import hashlib
8 db_config_path = '/etc/0tdns/db_connection_config.yml'
10 ovpn_config_path = argv[1]
12 with open(ovpn_config_path) as file:
13 ovpn_config_text = file.read()
15 ovpn_config_raw = bytearray(ovpn_config_text, encoding='utf-8')
17 ovpn_config_hash = hashlib.sha256(ovpn_config_raw).hexdigest()
19 config = yaml.safe_load(open(db_config_path, 'r'))
20 connection = psycopg2.connect(user=config['user'], password=config['password'],
21 host=config['host'], port=config['port'],
22 database=config['database'])
23 cursor = connection.cursor()
25 cursor.execute('''
26 INSERT INTO vpn (location_id, ovpn_config, ovpn_config_sha256)
27 VALUES(%s, %s, %s)''', (11, ovpn_config_text, ovpn_config_hash))
29 connection.commit()
30 cursor.close()
31 connection.close()