From 99b849171f87affa3fdba25c118aec8809d8b61a Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Fri, 29 Jan 2016 12:51:33 +0000 Subject: [PATCH] Status page updated with api version and db version. --- app/__init__.py | 21 +++++++++++++++ app/status/healthcheck.py | 7 ++--- app/version.py | 1 + application.py | 18 ++++++++++++- scripts/push-tag.sh | 55 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 app/version.py create mode 100644 scripts/push-tag.sh diff --git a/app/__init__.py b/app/__init__.py index e0a0b23f4..7b38a376e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,4 +1,6 @@ import os +import re +import ast from flask import request, url_for from flask._compat import string_types @@ -116,3 +118,22 @@ def convert_to_number(value): return float(value) if "." in value else int(value) except (TypeError, ValueError): return value + + +def get_api_version(): + _version_re = re.compile(r'__version__\s+=\s+(.*)') + version = 'n/a' + dir_path = os.path.dirname(os.path.abspath(__file__)) + with open(os.path.join(dir_path, 'version.py'), 'rb') as f: + version = str(ast.literal_eval(_version_re.search( + f.read().decode('utf-8')).group(1))) + return version + + +def get_db_version(): + try: + query = 'SELECT version_num FROM alembic_version' + full_name = db.session.execute(query).fetchone()[0] + return full_name.split('_')[0] + except: + return 'n/a' diff --git a/app/status/healthcheck.py b/app/status/healthcheck.py index 0407cabc4..b513bc600 100644 --- a/app/status/healthcheck.py +++ b/app/status/healthcheck.py @@ -6,6 +6,7 @@ status = Blueprint('status', __name__) @status.route('/_status', methods=['GET', 'POST']) def show_status(): - return jsonify( - status="ok", - ), 200 + from app import (get_api_version, get_db_version) + return jsonify(status="ok", + api_version=get_api_version(), + db_version=get_db_version()), 200 diff --git a/app/version.py b/app/version.py new file mode 100644 index 000000000..df9144c54 --- /dev/null +++ b/app/version.py @@ -0,0 +1 @@ +__version__ = '0.1.1' diff --git a/application.py b/application.py index 741edd91e..fe0e868f9 100644 --- a/application.py +++ b/application.py @@ -4,7 +4,7 @@ from __future__ import print_function import os from flask.ext.script import Manager, Server from flask.ext.migrate import Migrate, MigrateCommand -from app import create_app, db +from app import (create_app, db, get_api_version, get_db_version) application = create_app(os.getenv('NOTIFY_API_ENVIRONMENT') or 'development') manager = Manager(application) @@ -22,5 +22,21 @@ def list_routes(): print("{:10} {}".format(", ".join(rule.methods - set(['OPTIONS', 'HEAD'])), rule.rule)) +@manager.command +def api_version(): + """ + Retrieve the version of the api. + """ + return get_api_version() + + +@manager.command +def db_version(): + """ + Retrieve the db version. + """ + return get_db_version() + + if __name__ == '__main__': manager.run() diff --git a/scripts/push-tag.sh b/scripts/push-tag.sh new file mode 100644 index 000000000..5a74e9a05 --- /dev/null +++ b/scripts/push-tag.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +function pretty() { + local blue="\033[34m" + local reset="\033[0m" + while read line; do + echo -e "${blue}[publishing]${reset} ${line}" + done +} + +function warn() { + local orange="\033[33m" + local reset="\033[0m" + while read line; do + echo -e "${orange}[warning]${reset} ${line}" + done +} + +function get_sha(){ + REF=$(git log -n 1 -- VERSION --name-only) + SHA=$(echo $REF | awk '{ print $2 }') + + echo "checking out ${SHA}" | pretty + + git checkout $SHA +} + +function get_version(){ + VERSION=$(python setup.py --version) + echo "latest version is ${VERSION}" | pretty +} + +function push_tag_or_die(){ + TAG_EXISTS=$(git tag | grep -G "^${VERSION}$") + if [ "$TAG_EXISTS" ]; then + echo "Tag already exists, exiting" | warn + exit 0 + else + push_tag $VERSION + fi +} + +function push_tag(){ + git tag -a $VERSION -m "Version tag for ${VERSION}" + echo "Pushing tags to github ${VERSION} to Github" | pretty + git push origin --tags +} + +function main(){ + get_sha + get_version + push_tag_or_die +} + +main \ No newline at end of file