summaryrefslogtreecommitdiff
path: root/cronjob.py
blob: 14061b93fa74404820d4791888578df97da6cd66 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/python3

from config import *
from lib_autopeer import *

import os


curs = DB.execute('SELECT name, asn FROM peers WHERE deleted=0')
while row := curs.fetchone():
	name, asn = row
	print(f'Processing {asn}-{name}...')
	birdcfg = open(f'/etc/bird/peers/as{asn}{name}.conf', 'w')
	old_umask = os.umask(0o077)
	wgcfg = open(f'/etc/wireguard/wg{asn%10000:04}{name}.conf', 'w')
	os.umask(old_umask)
	print(_bird_config(name, _get_peer_info(name, asn), _get_my_info(asn)), file=birdcfg)
	print(_wg_config(name, _get_peer_info(name, asn), _get_my_info(asn)), file=wgcfg)
	birdcfg.close()
	wgcfg.close()
	os.system(f'systemctl enable --now wg-quick@wg{asn%10000:04}{name}')

curs = DB.execute('SELECT name, asn FROM peers WHERE deleted=1')
while row := curs.fetchone():
	name, asn = row
	print(f'Deleting {asn}-{name}...')
	try: os.remove(f'/etc/bird/peers/as{asn}{name}.conf')
	except FileNotFoundError: pass
	try: os.remove(f'/etc/wireguard/wg{asn%10000:04}{name}.conf')
	except FileNotFoundError: pass
	os.system(f'systemctl disable --now wg-quick@wg{asn%10000:04}{name}')

os.system('birdc configure')