-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolserver.py
More file actions
427 lines (346 loc) · 13.6 KB
/
solserver.py
File metadata and controls
427 lines (346 loc) · 13.6 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/python
# =========================== adjust path =====================================
import sys
import os
if __name__ == "__main__":
here = sys.path[0]
sys.path.insert(0, os.path.join(here, '..', 'smartmeshsdk', 'libs'))
# =========================== imports =========================================
# from default Python
import time
import json
import subprocess
import threading
import logging.config
# third-party packages
import bottle
import influxdb
# project-specific
import solserver_version
from dustCli import DustCli
from sensorobjectlibrary import Sol as sol, \
SolExceptions,\
SolUtils
#============================ logging =========================================
logging.config.fileConfig('logging.conf')
log = logging.getLogger('solserver')
log.setLevel(logging.DEBUG)
#============================ defines =========================================
CONFIGFILE = 'solserver.config'
STATSFILE = 'solserver.stats'
ALLSTATS = [
#== admin
'ADM_NUM_CRASHES',
#== connection to server
'JSON_NUM_REQ',
'JSON_NUM_UNAUTHORIZED',
#== DB
'DB_NUM_WRITES_OK',
'DB_NUM_WRITES_FAIL',
'NUM_SET_ACTION_REQ',
'NUM_OBJECTS_DB_FAIL',
'NUM_OBJECTS_DB_OK',
'NUM_JSON_REQ',
]
#============================ helpers =========================================
#============================ classes =========================================
#======== JSON API to receive notifications from SolManager
class JsonApiThread(threading.Thread):
class HTTPSServer(bottle.ServerAdapter):
def run(self, handler):
from cheroot.wsgi import Server as WSGIServer
from cheroot.ssl.pyopenssl import pyOpenSSLAdapter
server = WSGIServer((self.host, self.port), handler)
server.ssl_adapter = pyOpenSSLAdapter(
certificate = SolUtils.AppConfig().get('solserver_certificate'),
private_key = SolUtils.AppConfig().get('solserver_private_key'),
)
try:
server.start()
log.info("Server started")
finally:
server.stop()
def __init__(self):
# local variables
self.sites = []
self.influxClient = influxdb.client.InfluxDBClient(
host = SolUtils.AppConfig().get('influxdb_host'),
port = SolUtils.AppConfig().get('influxdb_port'),
database = SolUtils.AppConfig().get('influxdb_database'),
)
self.actions = []
# initialize web server
self.web = bottle.Bottle()
# interaction with SolManager
self.web.route(
path = '/api/v1/o.json',
method = 'PUT',
callback = self._webhandle_o_PUT,
)
self.web.route(
path = '/api/v1/getactions/',
method = 'GET',
callback = self._webhandle_getactions_GET,
)
# interaction with administrator
self.web.route(
path = '/api/v1/echo.json',
method = 'POST',
callback = self._webhandle_echo_POST,
)
self.web.route(
path = '/api/v1/status.json',
method = 'GET',
callback = self._webhandle_status_GET,
)
self.web.route(
path = '/api/v1/setaction/',
method = 'POST',
callback = self._webhandle_setactions_POST,
)
# start the thread
threading.Thread.__init__(self)
self.name = 'JsonApiThread'
self.daemon = True
self.start()
def run(self):
try:
self.web.run(
host = '0.0.0.0',
port = SolUtils.AppConfig().get('solserver_tcpport'),
server = self.HTTPSServer,
quiet = True,
debug = False,
)
except bottle.BottleException:
raise
except Exception as err:
SolUtils.logCrash(err, SolUtils.AppStats(), threadName=self.name)
log.info("JsonApiThread started")
#======================== public ==========================================
def close(self):
# bottle thread is daemon, it will close when main thread closes
pass
def set_action(self, action):
action_exists = False
for item in self.actions:
if cmp(action, item) == 0:
action_exists = True
if not action_exists:
self.actions.append(action)
def get_actions(self, site):
actions = []
for item in self.actions:
if item["site"] == site:
actions.append(item)
self.actions = [] # removing previous actions
return actions
#======================== private =========================================
#=== webhandlers
# decorator
def _authorized_webhandler(func):
def hidden_decorator(self):
try:
# update stats
SolUtils.AppStats().increment('JSON_NUM_REQ')
# authorize the client
siteName = self._authorizeClient(
token = bottle.request.headers.get('X-REALMS-Token'),
)
# abort if not authorized
if not siteName:
return bottle.HTTPResponse(
body = json.dumps({'error': 'Unauthorized'}),
status = 401,
headers= {'Content-Type': 'application/json'},
)
# abort if not valid JSON payload
if bottle.request.json is not None:
try:
json.dumps(bottle.request.json)
except ValueError:
return bottle.HTTPResponse(
body = json.dumps(
{'error': 'Malformed JSON body'}
),
status = 400,
headers= {'Content-Type': 'application/json'},
)
# retrieve the return value
returnVal = func(self, siteName=siteName)
# send back answer
return bottle.HTTPResponse(
status = 200,
headers = {'Content-Type': 'application/json'},
body = json.dumps(returnVal),
)
except SolExceptions.UnauthorizedError:
return bottle.HTTPResponse(
status = 401,
headers = {'Content-Type': 'application/json'},
body = json.dumps({'error': 'Unauthorized'}),
)
except Exception as err:
crashMsg = SolUtils.logCrash(err, SolUtils.AppStats())
return bottle.HTTPResponse(
status = 500,
headers = {'Content-Type': 'application/json'},
body = json.dumps(crashMsg),
)
return hidden_decorator
# interaction with SolManager
@_authorized_webhandler
def _webhandle_o_PUT(self, siteName=None):
# abort if not JSON payload
if bottle.request.json is None:
return bottle.HTTPResponse(
body=json.dumps(
{'error': 'JSON body is required'}
),
status=400,
headers={'Content-Type': 'application/json'},
)
# http->bin
try:
sol_binl = sol.http_to_bin(bottle.request.json)
except:
return bottle.HTTPResponse(
body = json.dumps(
{'error': 'Malformed JSON body contents'}
),
status = 400,
headers= {'Content-Type': 'application/json'},
)
# bin->json->influxdb format, then write to put database
sol_influxdbl = []
for sol_bin in sol_binl:
# convert bin->json
sol_json = sol.bin_to_json(sol_bin)
# convert json->influxdb
tags = self._get_tags(siteName, sol_json["mac"])
sol_influxdbl += [sol.json_to_influxdb(sol_json, tags)]
# write to database
try:
self.influxClient.write_points(sol_influxdbl)
except:
SolUtils.AppStats().increment('NUM_OBJECTS_DB_FAIL')
raise
else:
SolUtils.AppStats().increment('NUM_OBJECTS_DB_OK')
@_authorized_webhandler
def _webhandle_getactions_GET(self, siteName=None):
"""
Triggered when the solmanager requests the solserver for actions
Ex:
1. solmanager asks solserver for actions
2. solserver tells solmanager to update its SOL library
"""
return self.get_actions(siteName)
# interaction with administrator
@_authorized_webhandler
def _webhandle_echo_POST(self, siteName=None):
return bottle.request.body.read()
@_authorized_webhandler
def _webhandle_status_GET(self, siteName=None):
return {
'version solserver': solserver_version.VERSION,
'version Sol': list(sol.version()),
'uptime computer': self._exec_cmd('uptime'),
'utc': int(time.time()),
'date': time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()),
#'last reboot': self._exec_cmd('last reboot'), # TODO not working anymore
'stats': SolUtils.AppStats().get()
}
@_authorized_webhandler
def _webhandle_setactions_POST(self, siteName=None):
"""
Add an action to passively give order to the solmanager.
When the solmanager can't be reached by the solserver, the solmanager
periodically ask the server for actions.
"""
# format action
action_json = {
"action": bottle.request.json["action"],
"site": bottle.request.json["site"],
}
# add action to list if action is available
available_actions = ["update"]
if bottle.request.json["action"] in available_actions:
self.set_action(action_json)
return "Action OK"
#=== misc
def _get_tags(self, site_name, mac):
"""
:param str mac : a dash-separated mac address
:return: the tags associated to the given mac address
:rtype: dict
Notes: tags are read from 'site.py' file
"""
return_tags = {"mac": mac} # default tag is only mac
for site in self.sites:
if site["name"] == site_name:
for key, tags in site["motes"].iteritems():
if mac == key:
return_tags.update(tags)
return_tags["site"] = site["name"]
return return_tags
def _authorizeClient(self, token=None):
assert token
siteName = None
searchAfterReload = False
while True:
for site in self.sites:
if site['token'] == token:
siteName = site["name"]
break
if (not siteName) and (searchAfterReload is False):
with open('solserver.sites', 'r') as f:
self.sites = json.load(f)["sites"]
searchAfterReload = True
else:
break
if not siteName:
SolUtils.AppStats().increment('JSON_NUM_UNAUTHORIZED')
return siteName
@staticmethod
def _exec_cmd(cmd):
try:
returnVal = subprocess.check_output(cmd, shell=False)
except subprocess.CalledProcessError:
returnVal = "ERROR"
return returnVal
#======== main application thread
class SolServer(object):
def __init__(self):
# init Singletons -- must be first init
SolUtils.AppConfig(config_file=CONFIGFILE)
SolUtils.AppStats(stats_file=STATSFILE, stats_list=ALLSTATS)
# API thread
self.jsonApiThread = JsonApiThread()
# CLI interface
self.cli = DustCli.DustCli("SolServer", self._clihandle_quit)
self.cli.registerCommand(
name = 'stats',
alias = 's',
description = 'print the stats',
params = [],
callback = self._clihandle_stats,
)
@staticmethod
def _clihandle_quit():
time.sleep(.3)
print "bye bye."
# all threads as daemonic, will close automatically
@staticmethod
def _clihandle_stats(params):
stats = SolUtils.AppStats().get()
output = []
for k in sorted(stats.keys()):
output += ['{0:<30}: {1}'.format(k, stats[k])]
output = '\n'.join(output)
print output
# =========================== main ============================================
def main():
SolServer()
if __name__ == '__main__':
main()