summaryrefslogtreecommitdiff
path: root/cronjob.py
blob: abe5b1ecbb352321ed93611d3ed514a773d4b637 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python3

from config import *
from lib_autopeer import *

import os

sp_conf_str = ''

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)
	peer_info = _get_peer_info(name, asn)
	my_info = _get_my_info(asn)
	print(_bird_config(name, peer_info, my_info), file=birdcfg)
	print(_wg_config(name, peer_info, my_info), file=wgcfg)
	sp_conf_str += f'''
+++ {asn%10000:04}{name}
menu = {asn%10000:04}{name}
title = {asn}-{name}
remark = ICMP echo measured to WireGuard endpoint
host = {peer_info.endpoint}
'''
	birdcfg.close()
	wgcfg.close()
	os.system(f'systemctl enable --now wg-quick@wg{asn%10000:04}{name}')
curs.close()

spcfg = open('/opt/autopeer/smokeping-peer.conf', 'w')
print(sp_conf_str, file=spcfg)
spcfg.close()
os.system('systemctl reload smokeping')

curs = DB.execute('SELECT name, asn FROM peers WHERE deleted=1')
deletions = []
while row := curs.fetchone():
	name, asn = row
	print(f'Deleting {asn}-{name}...')
	os.system(f'systemctl disable --now wg-quick@wg{asn%10000:04}{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
	deletions.append((name, asn))
curs.close()

for name, asn in deletions:
	DB.execute('DELETE FROM peers WHERE name = :name AND asn = :asn', dict(name=name, asn=asn))

os.system('/usr/sbin/birdc configure')