2015-01-20 21:53:05 +00:00
|
|
|
import logging
|
2015-07-15 21:25:41 +00:00
|
|
|
from data.model import health
|
2016-08-01 17:02:15 +00:00
|
|
|
from app import build_logs, storage
|
2015-01-20 21:53:05 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
def _check_registry_gunicorn(app):
|
|
|
|
""" Returns the status of the registry gunicorn workers. """
|
|
|
|
# Compute the URL for checking the registry 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]
|
|
|
|
|
|
|
|
registry_url = '%s://localhost%s/v1/_internal_ping' % (app.config['PREFERRED_URL_SCHEME'], port)
|
|
|
|
try:
|
|
|
|
return client.get(registry_url, verify=False, timeout=2).status_code == 200
|
|
|
|
except Exception:
|
|
|
|
logger.exception('Exception when checking registry health: %s', registry_url)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _check_database(app):
|
|
|
|
""" Returns the status of the database, as accessed from this instance. """
|
2015-07-15 21:25:41 +00:00
|
|
|
return health.check_health(app.config)
|
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()
|
|
|
|
|
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'])
|
|
|
|
return True
|
2016-08-03 15:13:27 +00:00
|
|
|
except Exception as ex:
|
|
|
|
logger.exception('Storage check failed with exception %s', ex)
|
2016-08-01 17:02:15 +00:00
|
|
|
return False
|
|
|
|
|
2015-01-20 21:53:05 +00:00
|
|
|
|
|
|
|
_SERVICES = {
|
|
|
|
'registry_gunicorn': _check_registry_gunicorn,
|
|
|
|
'database': _check_database,
|
2016-08-01 17:02:15 +00:00
|
|
|
'redis': _check_redis,
|
|
|
|
'storage': _check_storage,
|
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
|