From 6e0dc1df085866bea891e1a94623798a57063bb1 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 15 Jun 2015 15:52:08 -0400 Subject: [PATCH 1/2] Add health check endpoint to verify that the locally running DB revision matches that of the database Fixes #132 --- Dockerfile | 2 ++ endpoints/web.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/Dockerfile b/Dockerfile index d1307b102..0011a98f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -55,6 +55,8 @@ RUN mkdir /usr/local/nginx/logs/ RUN TEST=true venv/bin/python -m unittest discover -f RUN TEST=true venv/bin/python -m test.registry_tests -f +RUN PYTHONPATH=. venv/bin/alembic heads > ALEMBIC_HEAD + VOLUME ["/conf/stack", "/var/log", "/datastorage", "/tmp", "/conf/etcd"] EXPOSE 443 8443 80 diff --git a/endpoints/web.py b/endpoints/web.py index 8a3500790..110e145a2 100644 --- a/endpoints/web.py +++ b/endpoints/web.py @@ -8,6 +8,7 @@ from urlparse import urlparse from health.healthcheck import get_healthchecker from data import model +from data.database import db from data.model.oauth import DatabaseAuthorizationProvider from app import app, billing as stripe, build_logs, avatar, signer, log_archive from auth.auth import require_session_login, process_oauth @@ -227,6 +228,30 @@ def endtoend_health(): return response +@web.route('/health/dbrevision', methods=['GET']) +@route_show_if(features.BILLING) +@no_cache +def dbrevision_health(): + # Find the revision from the database. + result = db.execute_sql('select * from alembic_version limit 1').fetchone() + db_revision = result[0] + + # Find the local revision from the file system. + with open('ALEMBIC_HEAD', 'r') as f: + local_revision = f.readline().split(' ')[0] + + data = { + 'db_revision': db_revision, + 'local_revision': local_revision, + } + + status_code = 200 if db_revision == local_revision else 400 + + response = jsonify(dict(data=data, status_code=status_code)) + response.status_code = status_code + return response + + @web.route('/tos', methods=['GET']) @no_cache def tos(): From 7b94e37c9535f1a00659cfc173dd64b1a6671be0 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 16 Jun 2015 17:43:02 -0400 Subject: [PATCH 2/2] Clarify why we use features.BILLING as the feature flag on the route --- endpoints/web.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoints/web.py b/endpoints/web.py index 110e145a2..34300bce7 100644 --- a/endpoints/web.py +++ b/endpoints/web.py @@ -229,7 +229,7 @@ def endtoend_health(): @web.route('/health/dbrevision', methods=['GET']) -@route_show_if(features.BILLING) +@route_show_if(features.BILLING) # Since this is only used in production. @no_cache def dbrevision_health(): # Find the revision from the database.