-
Notifications
You must be signed in to change notification settings - Fork 8
/
octohook.py
executable file
·132 lines (103 loc) · 4.08 KB
/
octohook.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from __future__ import print_function
import threading
import bottle
import sys
import yaml
try:
from Queue import Queue
except ImportError:
from queue import Queue
from kweb.webservice import SSLWSGIRefServer, WService
from kweb.webrouter import WRouter
from kcmd.cmdservice import ConCommander2
from klib.configuration import Configurator
import atexit
import logging
def quit_gracefully():
logging.info('Bye')
def cmdservice_worker(data_q, config):
cc2 = ConCommander2(config)
cc2.setup(data_q)
cc2.do_loop()
return
def web_server_worker(data_q, config):
wsapp = WService(queue=data_q, config=config)
wrouter = WRouter(wsapp)
wrouter.route_server(config.server())
# Start SSL-wrapped bottle
logging.info('Starting Webservice server (daemon) ')
sslsrv = SSLWSGIRefServer(host=config.server()['web']['host'],
port=config.server()['web']['port'])
bottle.run(server=sslsrv, debug=config.server()['web']['debug'],
quiet=config.server()['web']['quiet'])
return
def web_client_worker(data_q, config):
wsapp = WService(queue=data_q, config=config)
wrouter = WRouter(wsapp)
wrouter.route_client(config.client())
# Start SSL-wrapped bottle
sslsrv = SSLWSGIRefServer(host=config.client()['web']['host'],
port=config.client()['web']['port'])
logging.info('Starting Webservice client (daemon) ')
bottle.run(server=sslsrv, debug=config.client()['web']['debug'],
quiet=config.client()['web']['quiet'])
return
def threader(config):
data_queue = Queue(config.boot()['queue_watermark'])
config.thread_queue = Queue(3)
if config.roles()['web']['client'] == True:
wct = threading.Thread(target=web_client_worker,
args=(data_queue, config))
wct.daemon = True
wct.start()
if config.roles()['web']['server'] == True:
wst = threading.Thread(target=web_server_worker,
args=(data_queue, config))
wst.daemon = True
wst.start()
logging.info('Starting Command server, use <Ctrl-D> , `q`, `quit` to quit')
cst = threading.Thread(target=cmdservice_worker, args=(data_queue, config))
cst.start()
return
def bootstrap(startargs):
if len(sys.argv) != 2:
logging.critical("Need config File")
return
cfgfile = startargs[1]
logo="""
_________________________________________________________________
|____|____|____|____|____|____|____|____|____|____|____|____|____
____|____|____|____|____|____|____|____|____|____|____|____|____|
____ __ __ __ __
/ __ \ _____ / /_ ____ / / / /____ ____ / /__
/ / / // ___// __// __ \ / /_/ // __ \ / __ \ / //_/
/ /_/ // /__ / /_ / /_/ // __ // /_/ // /_/ // ,<
\____/ \___/ \__/ \____//_/ /_/ \____/ \____//_/|_|
When Cats Have 9 lives....
_________________________________________________________________
|____|____|____|____|____|____|____|____|____|____|____|____|___|_
___|____|____|____|____|____|____|____|____|____|____|____|____|_
"""
print(logo)
print("Loading from {}".format(cfgfile))
try:
with open(cfgfile, 'r') as ymlfile:
config = Configurator(yaml.load(ymlfile))
loglevel=logging.INFO
logfile="logs/octolog.log"
bootargs=config.boot()
if 'loglevel' in bootargs:
loglevel = getattr(logging, bootargs['loglevel'].upper(), None)
if not isinstance(loglevel, int):
print("Invalid log level:{} Using defaults".format(loglevel))
if 'logfile' in bootargs:
logfile=bootargs['logfile']
logging.basicConfig(filename=logfile,
format='%(asctime)s:%(levelname)s:%(message)s',
level=loglevel)
threader(config)
except IOError as e:
print("Unable to open config file {}".format(e))
if __name__ == '__main__':
atexit.register(quit_gracefully)
bootstrap(sys.argv)