-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
88 lines (77 loc) · 2.64 KB
/
generator.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
import db as database
import json
import os
from glob import glob
import configparser
from subprocess import call
import time
config = configparser.ConfigParser()
config.read('settings.conf')
path_fastd = config['CONFIG']['fastd_path']
path_ffmap = config['CONFIG']['ffmap_path']
class FastdConfig(object):
def __init__(self):
self.db = database.DB()
def removeOldFiles(self):
data = self.db.getNodeList()
nodes = [e['hostname'] for e in data]
current_files = glob(os.path.join(path_fastd, '*.conf'))
new_files = [ e + '.config' for e in nodes]
for e in current_files:
if e not in new_files:
os.remove(e)
def genFastdConf(self):
node_list = self.db.getNodeList()
self.removeOldFiles()
for node in node_list:
with open(os.path.abspath(os.path.join(path_fastd, node['hostname'] + '.conf')), 'w') as f:
conf = """\
#Hostname: {hostname}
#MAC: {mac}
#Koordinaten: {coords}
#Nick: {nickname}
#Mail: {email}
#Token: {token}
key "{key}";
""".format(**node)
f.write(conf)
class FFmapConfig(object):
def __init__(self):
self.db = database.DB()
def calcMAC(self, mac):
mlist = mac.split(':')
mlist = [int(x, 16) for x in mlist]
mlist[0] += 0x02
mlist[3] = ((mlist[3] + 1) % 0x100)
return ':'.join(['{:02x}'.format(x) for x in mlist])
def genJson(self):
node_list = self.db.getNodeList()
json = {}
for node in node_list:
temp = {}
temp['name'] = node['hostname']
if node['coords']:
temp['gps'] = node['coords']
json.update({self.calcMAC(node['mac'].lower()): temp})
return json
def genAliasJson(self):
with open(os.path.abspath(os.path.join(path_ffmap, 'aliases.json')), 'w') as f:
f.write(json.dumps(self.genJson(), sort_keys=True, indent=4, separators=(',', ': ')))
class aliasMap(object):
def __init__(self):
self.db = database.DB()
self.config_path = '/etc/postfix/virtual_alias.map'
def genAliasMap(self):
with open(self.config_path, 'w') as f:
f.write("# generated by ffrn-node-manager at " + time.ctime() + '\n\n')
for node in self.db.getNodeMailMap():
f.write(node[0].lower() + "@nodes.ffrn.de " + node[1] + '\n')
call(['postmap', self.config_path]) # update postfix lookup table
if __name__ == "__main__":
ffmap = FFmapConfig()
ffmap.genAliasJson()
fastd = FastdConfig()
fastd.genFastdConf()
alias = aliasMap()
alias.genAliasMap()