-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.py
113 lines (97 loc) · 3.49 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
import db as database
import json
import os
from glob import glob
import configparser
import time
import re
import sh
from datetime import datetime
config = configparser.ConfigParser()
config.read('settings.conf')
path_fastd = config['CONFIG']['fastd_path']
location_valiasMap = config['CONFIG']['valiasMap_location']
location_aliasJson = config['CONFIG']['aliasJson_location']
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()
if not os.path.exists(os.path.abspath(path_fastd)):
print("You must first create the in the config specified fastd_path and initiate a git repository!")
if __name__ == "__main__":
exit(2)
else:
return
for node in node_list:
with open(os.path.abspath(os.path.join(path_fastd, node['hostname'].lower() + '.conf')), 'w') as f:
conf = """\
#Hostname: {hostname}
#MAC: {mac}
key "{key}";
""".format(**node)
f.write(conf)
git = sh.git.bake(_cwd=os.path.abspath(path_fastd))
git.add('.')
git.config('user.name', 'FFRN Node Manager')
git.config('user.name', 'ffrn-node-manager@ffrn.de')
git.commit('-m ffrn-node-manager auto commit: ' + datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "--quiet", _ok_code=[0, 1])
git.push("--quiet", _ok_code=[0])
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']
json.update({self.calcMAC(node['mac'].lower()): temp})
return json
def genAliasJson(self):
with open(os.path.abspath(location_aliasJson), '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()
def normalize(self, name):
trans_table = {
0xe4: u'ae',
0xf6: u'oe',
0xfc: u'ue',
0xdf: u'ss',
0xc4: u'Ae',
0xd6: u'Oe',
0xdc: u'Ue',
}
name = re.sub(r"\s+", '-', name)
return name.translate(trans_table)
def genAliasMap(self):
with open(location_valiasMap, 'w') as f:
f.write("# generated by ffrn-node-manager at " + time.ctime() + '\n\n')
for node in self.db.getNodeMailMap():
f.write(self.normalize(node[0].lower()) + "@nodes.ffrn.de " + node[1] + '\n')
# call(['postmap', valiasMap_location]) # update postfix lookup table
if __name__ == "__main__":
ffmap = FFmapConfig()
ffmap.genAliasJson()
fastd = FastdConfig()
fastd.genFastdConf()
alias = aliasMap()
alias.genAliasMap()