collect-master.py 1.54 KB
Newer Older
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
1
#!/usr/bin/python2
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
2
3
from twisted.internet import reactor, ssl
from twisted.internet.endpoints import SSL4ServerEndpoint
4
import log_extra
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
5
import logging
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
6
import logging.handlers
7
from client import ClientFactory
8
from plugin import Plugins
9
import master_config
10
import importlib
11

12
13
14
15
16
severity = master_config.get('log_severity')
if severity == 'TRACE':
	severity = log_extra.TRACE_LEVEL
else:
	severity = getattr(logging, severity)
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
17
log_file = master_config.get('log_file')
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
18
19
logging.basicConfig(level=severity, format=master_config.get('log_format'))
if log_file != '-':
20
21
22
	handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=int(master_config.get('log_file_size')), backupCount=int(master_config.get('log_file_count')))
	handler.setFormatter(logging.Formatter(fmt=master_config.get('log_format')))
	logging.getLogger().addHandler(handler)
23
24

loaded_plugins = {}
25
plugins = Plugins()
26
27
28
29
30
31
for (plugin, config) in master_config.plugins().items():
	(modulename, classname) = plugin.rsplit('.', 1)
	module = importlib.import_module(modulename)
	constructor = getattr(module, classname)
	loaded_plugins[plugin] = constructor(plugins, config)
	logging.info('Loaded plugin %s from %s', loaded_plugins[plugin].name(), plugin)
32
# Some configuration, to load the port from?
33
port = master_config.getint('port')
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
34
35
36
with open(master_config.get('cert')) as key:
	cert = ssl.PrivateCertificate.loadPEM(key.read())
endpoint = SSL4ServerEndpoint(reactor, port, cert.options())
37
logging.info('Listening on port %s', port)
38
endpoint.listen(ClientFactory(plugins))
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
39
logging.info('Init done')
40
reactor.run()