2014-09-11 19:33:10 +00:00
|
|
|
import logging
|
|
|
|
|
2015-08-28 18:08:13 +00:00
|
|
|
from util.registry.gzipinputstream import GzipInputStream
|
2014-09-11 19:33:10 +00:00
|
|
|
from flask import send_file, abort
|
|
|
|
|
2014-09-11 15:18:28 +00:00
|
|
|
from data.userfiles import DelegateUserfiles, UserfilesHandlers
|
2014-09-08 20:43:17 +00:00
|
|
|
|
2014-09-11 19:33:10 +00:00
|
|
|
|
|
|
|
JSON_MIMETYPE = 'application/json'
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class LogArchiveHandlers(UserfilesHandlers):
|
2015-11-16 20:01:12 +00:00
|
|
|
methods = ['GET']
|
|
|
|
|
2014-09-11 19:33:10 +00:00
|
|
|
def get(self, file_id):
|
|
|
|
path = self._files.get_file_id_path(file_id)
|
|
|
|
try:
|
2015-08-28 18:08:13 +00:00
|
|
|
data_stream = self._storage.stream_read_file(self._locations, path)
|
|
|
|
return send_file(GzipInputStream(data_stream), mimetype=JSON_MIMETYPE)
|
2014-09-11 19:33:10 +00:00
|
|
|
except IOError:
|
2017-04-21 18:40:09 +00:00
|
|
|
logger.exception('Could not read archived logs')
|
2014-09-11 19:33:10 +00:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
2014-09-08 20:43:17 +00:00
|
|
|
class LogArchive(object):
|
2014-09-11 15:18:28 +00:00
|
|
|
def __init__(self, app=None, distributed_storage=None):
|
2014-09-08 20:43:17 +00:00
|
|
|
self.app = app
|
|
|
|
if app is not None:
|
2014-09-11 15:18:28 +00:00
|
|
|
self.state = self.init_app(app, distributed_storage)
|
2014-09-08 20:43:17 +00:00
|
|
|
else:
|
|
|
|
self.state = None
|
|
|
|
|
2014-09-11 15:18:28 +00:00
|
|
|
def init_app(self, app, distributed_storage):
|
|
|
|
location = app.config.get('LOG_ARCHIVE_LOCATION')
|
|
|
|
path = app.config.get('LOG_ARCHIVE_PATH', None)
|
2014-09-08 20:43:17 +00:00
|
|
|
|
2014-09-11 15:18:28 +00:00
|
|
|
handler_name = 'logarchive_handlers'
|
2014-09-08 20:43:17 +00:00
|
|
|
|
2016-04-15 17:51:54 +00:00
|
|
|
log_archive = DelegateUserfiles(app, distributed_storage, location, path,
|
|
|
|
handler_name=handler_name)
|
2014-09-08 20:43:17 +00:00
|
|
|
|
2014-09-11 15:18:28 +00:00
|
|
|
app.add_url_rule('/logarchive/<file_id>',
|
2014-09-11 19:33:10 +00:00
|
|
|
view_func=LogArchiveHandlers.as_view(handler_name,
|
|
|
|
distributed_storage=distributed_storage,
|
|
|
|
location=location,
|
|
|
|
files=log_archive))
|
2014-09-08 20:43:17 +00:00
|
|
|
|
|
|
|
# register extension with app
|
|
|
|
app.extensions = getattr(app, 'extensions', {})
|
2014-09-11 15:18:28 +00:00
|
|
|
app.extensions['log_archive'] = log_archive
|
|
|
|
return log_archive
|
2014-09-08 20:43:17 +00:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.state, name, None)
|