2014-02-04 00:08:37 +00:00
|
|
|
import redis
|
|
|
|
import json
|
2015-09-30 19:23:19 +00:00
|
|
|
import time
|
2014-02-04 00:08:37 +00:00
|
|
|
|
2014-05-09 22:45:11 +00:00
|
|
|
from util.dynamic import import_class
|
2014-09-12 17:13:14 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
|
|
|
|
ONE_DAY = timedelta(days=1)
|
|
|
|
|
2014-05-09 22:45:11 +00:00
|
|
|
|
2014-03-28 18:20:06 +00:00
|
|
|
class BuildStatusRetrievalError(Exception):
|
|
|
|
pass
|
2014-02-04 00:08:37 +00:00
|
|
|
|
2014-05-09 22:45:11 +00:00
|
|
|
class RedisBuildLogs(object):
|
2014-02-12 23:58:40 +00:00
|
|
|
ERROR = 'error'
|
|
|
|
COMMAND = 'command'
|
|
|
|
PHASE = 'phase'
|
|
|
|
|
2014-10-01 18:16:42 +00:00
|
|
|
def __init__(self, redis_config):
|
2015-10-22 17:24:56 +00:00
|
|
|
args = dict(redis_config)
|
|
|
|
args.update({'socket_connect_timeout': 5})
|
|
|
|
|
|
|
|
self._redis_config = redis_config
|
|
|
|
self._redis = redis.StrictRedis(**args)
|
2014-02-04 00:08:37 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _logs_key(build_id):
|
|
|
|
return 'builds/%s/logs' % build_id
|
|
|
|
|
|
|
|
def append_log_entry(self, build_id, log_obj):
|
|
|
|
"""
|
|
|
|
Appends the serialized form of log_obj to the end of the log entry list
|
|
|
|
and returns the new length of the list.
|
|
|
|
"""
|
|
|
|
return self._redis.rpush(self._logs_key(build_id), json.dumps(log_obj))
|
|
|
|
|
2014-08-19 00:34:39 +00:00
|
|
|
def append_log_message(self, build_id, log_message, log_type=None, log_data=None):
|
2014-02-04 00:08:37 +00:00
|
|
|
"""
|
|
|
|
Wraps the message in an envelope and push it to the end of the log entry
|
2014-02-11 00:12:43 +00:00
|
|
|
list and returns the index at which it was inserted.
|
2014-02-04 00:08:37 +00:00
|
|
|
"""
|
|
|
|
log_obj = {
|
|
|
|
'message': log_message
|
|
|
|
}
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
if log_type:
|
|
|
|
log_obj['type'] = log_type
|
2014-02-11 00:12:43 +00:00
|
|
|
|
2014-08-19 00:34:39 +00:00
|
|
|
if log_data:
|
|
|
|
log_obj['data'] = log_data
|
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
return self._redis.rpush(self._logs_key(build_id), json.dumps(log_obj)) - 1
|
2014-02-04 00:08:37 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
def get_log_entries(self, build_id, start_index):
|
2014-11-24 21:07:38 +00:00
|
|
|
"""
|
2014-02-04 00:08:37 +00:00
|
|
|
Returns a tuple of the current length of the list and an iterable of the
|
2014-02-12 23:58:40 +00:00
|
|
|
requested log entries.
|
2014-02-04 00:08:37 +00:00
|
|
|
"""
|
2014-03-25 17:29:06 +00:00
|
|
|
try:
|
|
|
|
llen = self._redis.llen(self._logs_key(build_id))
|
|
|
|
log_entries = self._redis.lrange(self._logs_key(build_id), start_index, -1)
|
|
|
|
return (llen, (json.loads(entry) for entry in log_entries))
|
|
|
|
except redis.ConnectionError:
|
2014-03-28 18:20:06 +00:00
|
|
|
raise BuildStatusRetrievalError('Cannot retrieve build logs')
|
2014-02-04 00:08:37 +00:00
|
|
|
|
2014-09-12 17:13:14 +00:00
|
|
|
def expire_log_entries(self, build_id):
|
2014-09-11 19:33:10 +00:00
|
|
|
"""
|
2014-09-12 17:13:14 +00:00
|
|
|
Sets the log entry to expire in 1 day.
|
2014-09-11 19:33:10 +00:00
|
|
|
"""
|
2014-09-12 17:13:14 +00:00
|
|
|
self._redis.expire(self._logs_key(build_id), ONE_DAY)
|
2014-09-11 19:33:10 +00:00
|
|
|
|
2014-02-04 00:08:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def _status_key(build_id):
|
|
|
|
return 'builds/%s/status' % build_id
|
|
|
|
|
|
|
|
def set_status(self, build_id, status_obj):
|
|
|
|
"""
|
|
|
|
Sets the status key for this build to json serialized form of the supplied
|
|
|
|
obj.
|
|
|
|
"""
|
|
|
|
self._redis.set(self._status_key(build_id), json.dumps(status_obj))
|
|
|
|
|
|
|
|
def get_status(self, build_id):
|
|
|
|
"""
|
|
|
|
Loads the status information for the specified build id.
|
|
|
|
"""
|
2014-03-25 17:29:06 +00:00
|
|
|
try:
|
|
|
|
fetched = self._redis.get(self._status_key(build_id))
|
|
|
|
except redis.ConnectionError:
|
2014-03-28 18:20:06 +00:00
|
|
|
raise BuildStatusRetrievalError('Cannot retrieve build status')
|
2014-03-25 17:29:06 +00:00
|
|
|
|
2014-02-04 00:08:37 +00:00
|
|
|
return json.loads(fetched) if fetched else None
|
2014-05-09 22:45:11 +00:00
|
|
|
|
2015-09-30 19:23:19 +00:00
|
|
|
@staticmethod
|
|
|
|
def _health_key():
|
|
|
|
return '_health'
|
|
|
|
|
2014-05-13 22:53:42 +00:00
|
|
|
def check_health(self):
|
|
|
|
try:
|
2015-10-22 17:24:56 +00:00
|
|
|
args = dict(self._redis_config)
|
|
|
|
args.update({'socket_connect_timeout': 1, 'socket_timeout': 1})
|
|
|
|
|
|
|
|
connection = redis.StrictRedis(**args)
|
|
|
|
if not connection.ping() == True:
|
2015-09-30 19:23:19 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Ensure we can write and read a key.
|
2015-10-22 17:24:56 +00:00
|
|
|
connection.set(self._health_key(), time.time())
|
|
|
|
connection.get(self._health_key())
|
2015-09-30 19:23:19 +00:00
|
|
|
|
|
|
|
return True
|
2014-05-13 22:53:42 +00:00
|
|
|
except redis.ConnectionError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-05-09 22:45:11 +00:00
|
|
|
class BuildLogs(object):
|
|
|
|
def __init__(self, app=None):
|
|
|
|
self.app = app
|
|
|
|
if app is not None:
|
|
|
|
self.state = self.init_app(app)
|
|
|
|
else:
|
|
|
|
self.state = None
|
|
|
|
|
|
|
|
def init_app(self, app):
|
2014-10-01 18:16:42 +00:00
|
|
|
buildlogs_config = app.config.get('BUILDLOGS_REDIS')
|
|
|
|
if not buildlogs_config:
|
|
|
|
# This is the old key name.
|
|
|
|
buildlogs_config = {
|
|
|
|
'host': app.config.get('BUILDLOGS_REDIS_HOSTNAME')
|
|
|
|
}
|
|
|
|
|
2014-05-09 22:45:11 +00:00
|
|
|
buildlogs_options = app.config.get('BUILDLOGS_OPTIONS', [])
|
|
|
|
buildlogs_import = app.config.get('BUILDLOGS_MODULE_AND_CLASS', None)
|
|
|
|
|
|
|
|
if buildlogs_import is None:
|
|
|
|
klass = RedisBuildLogs
|
|
|
|
else:
|
|
|
|
klass = import_class(buildlogs_import[0], buildlogs_import[1])
|
|
|
|
|
2014-10-01 18:16:42 +00:00
|
|
|
buildlogs = klass(buildlogs_config, *buildlogs_options)
|
2014-05-09 22:45:11 +00:00
|
|
|
|
|
|
|
# register extension with app
|
|
|
|
app.extensions = getattr(app, 'extensions', {})
|
|
|
|
app.extensions['buildlogs'] = buildlogs
|
|
|
|
return buildlogs
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2014-08-19 00:34:39 +00:00
|
|
|
return getattr(self.state, name, None)
|