2015-01-20 21:53:05 +00:00
|
|
|
import logging
|
2017-05-11 03:55:19 +00:00
|
|
|
from app import build_logs, storage, authentication
|
2017-07-11 10:48:25 +00:00
|
|
|
from health.models_pre_oci import pre_oci_model as model
|
2015-01-20 21:53:05 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2018-01-12 21:07:10 +00:00
|
|
|
def _check_gunicorn(endpoint):
|
|
|
|
def fn(app):
|
|
|
|
""" Returns the status of the gunicorn workers. """
|
|
|
|
# Compute the URL for checking the endpoint. We append a port if and only if the
|
|
|
|
# hostname contains one.
|
|
|
|
client = app.config['HTTPCLIENT']
|
|
|
|
hostname_parts = app.config['SERVER_HOSTNAME'].split(':')
|
|
|
|
port = ''
|
|
|
|
if len(hostname_parts) == 2:
|
|
|
|
port = ':' + hostname_parts[1]
|
|
|
|
|
|
|
|
scheme = app.config['PREFERRED_URL_SCHEME']
|
|
|
|
if app.config.get('EXTERNAL_TLS_TERMINATION', False):
|
|
|
|
scheme = 'http'
|
|
|
|
|
|
|
|
registry_url = '%s://localhost%s/%s' % (scheme, port, endpoint)
|
|
|
|
try:
|
|
|
|
status_code = client.get(registry_url, verify=False, timeout=2).status_code
|
|
|
|
return (status_code == 200, 'Got non-200 response for worker: %s' % status_code)
|
|
|
|
except Exception as ex:
|
|
|
|
logger.exception('Exception when checking worker health: %s', registry_url)
|
|
|
|
return (False, 'Exception when checking worker health: %s' % registry_url)
|
|
|
|
|
|
|
|
return fn
|
2015-01-20 21:53:05 +00:00
|
|
|
|
2017-07-11 10:48:55 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
def _check_database(app):
|
|
|
|
""" Returns the status of the database, as accessed from this instance. """
|
2017-07-11 10:48:25 +00:00
|
|
|
return model.check_health(app.config)
|
2015-01-20 21:53:05 +00:00
|
|
|
|
2017-07-11 10:48:55 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
def _check_redis(app):
|
|
|
|
""" Returns the status of Redis, as accessed from this instance. """
|
|
|
|
return build_logs.check_health()
|
|
|
|
|
2017-07-11 10:48:55 +00:00
|
|
|
|
2016-08-01 17:02:15 +00:00
|
|
|
def _check_storage(app):
|
|
|
|
""" Returns the status of storage, as accessed from this instance. """
|
|
|
|
try:
|
|
|
|
storage.validate(storage.preferred_locations, app.config['HTTPCLIENT'])
|
2017-05-11 04:05:14 +00:00
|
|
|
return (True, None)
|
2016-08-03 15:13:27 +00:00
|
|
|
except Exception as ex:
|
|
|
|
logger.exception('Storage check failed with exception %s', ex)
|
2017-05-11 04:05:14 +00:00
|
|
|
return (False, 'Storage check failed with exception %s' % ex.message)
|
2016-08-01 17:02:15 +00:00
|
|
|
|
2017-05-11 03:55:19 +00:00
|
|
|
def _check_auth(app):
|
|
|
|
""" Returns the status of the auth engine, as accessed from this instance. """
|
2017-05-11 04:05:14 +00:00
|
|
|
return authentication.ping()
|
2017-05-11 03:55:19 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
|
|
|
|
_SERVICES = {
|
2018-01-12 21:07:10 +00:00
|
|
|
'registry_gunicorn': _check_gunicorn('v1/_internal_ping'),
|
|
|
|
'web_gunicorn': _check_gunicorn('_internal_ping'),
|
|
|
|
'verbs_gunicorn': _check_gunicorn('c1/_internal_ping'),
|
2015-01-20 21:53:05 +00:00
|
|
|
'database': _check_database,
|
2016-08-01 17:02:15 +00:00
|
|
|
'redis': _check_redis,
|
|
|
|
'storage': _check_storage,
|
2017-05-11 03:55:19 +00:00
|
|
|
'auth': _check_auth,
|
2015-01-20 21:53:05 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 17:24:56 +00:00
|
|
|
def check_all_services(app, skip):
|
2015-01-20 21:53:05 +00:00
|
|
|
""" Returns a dictionary containing the status of all the services defined. """
|
|
|
|
status = {}
|
|
|
|
for name in _SERVICES:
|
2015-10-22 17:24:56 +00:00
|
|
|
if name in skip:
|
|
|
|
continue
|
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
status[name] = _SERVICES[name](app)
|
|
|
|
|
2015-10-22 17:24:56 +00:00
|
|
|
return status
|