Skip to content

Commit

Permalink
Add version information to the applications
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Valsecchi committed Mar 30, 2017
1 parent d174b30 commit c371262
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 10 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ else
DOCKER_COMPOSE_VERSION := $(DOCKER_COMPOSE_VERSION_ACTUAL)
endif

GIT_TAG := $(shell git describe --tags --first-parent 2>/dev/null || echo "none")
GIT_HASH := $(shell git rev-parse HEAD)

.PHONY: all
all: acceptance

Expand All @@ -40,8 +43,8 @@ build_acceptance:

.PHONY: build_test_app
build_test_app:
rsync -a c2cwsgiutils c2cwsgiutils_run rel_requirements.txt setup.cfg acceptance_tests/app/
docker build -t $(DOCKER_BASE)_test_app:$(DOCKER_TAG) acceptance_tests/app
rsync -a c2cwsgiutils c2cwsgiutils_run c2cwsgiutils_genversion.py rel_requirements.txt setup.cfg acceptance_tests/app/
docker build -t $(DOCKER_BASE)_test_app:$(DOCKER_TAG) --build-arg "GIT_TAG=$(GIT_TAG)" --build-arg "GIT_HASH=$(GIT_HASH)" acceptance_tests/app

.venv/timestamp: rel_requirements.txt dev_requirements.txt
/usr/bin/virtualenv --python=/usr/bin/python3.5 .venv
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ applications:
* SQL profiler to debug DB performance problems, disabled by default. You must enable it by setting the
SQL_PROFILER_SECRET env var and using the view (/sql_profiler) to switch it ON and OFF. Warning,
it will slow down everything.
* A view to get the version information about the application and the installed packages (/versions.json)
* Error handlers to send JSON messages to the client in case of error
* A cornice service drop in replacement for setting up CORS

Expand Down
4 changes: 4 additions & 0 deletions acceptance_tests/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ ENV SQLALCHEMY_URL_SLAVE postgresql://www-data:www-data@db_slave:5432/test
# Step #2 copy the rest of the files (watch for the .dockerignore)
COPY . /app

ARG GIT_TAG
ARG GIT_HASH

RUN python ./setup.py install && \
./models_graph.py > models.dot && \
./models_graph.py Hello > models_hello.dot && \
./c2cwsgiutils_genversion.py $GIT_TAG $GIT_HASH && \
flake8 c2cwsgiutils c2cwsgiutils_app

CMD ["./c2cwsgiutils_run"]
6 changes: 6 additions & 0 deletions acceptance_tests/tests/tests/test_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test_ok(app_connection):
response = app_connection.get_json('versions.json')
assert 'main' in response
assert 'git_hash' in response['main']
assert 'packages' in response
assert 'pyramid' in response['packages']
3 changes: 2 additions & 1 deletion c2cwsgiutils/pyramid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cornice
import pyramid_tm

from c2cwsgiutils import stats, pyramid_logging, sql_profiler
from c2cwsgiutils import stats, pyramid_logging, sql_profiler, version


def includeme(config):
Expand All @@ -15,5 +15,6 @@ def includeme(config):
stats.init(config)
pyramid_logging.install_subscriber(config)
sql_profiler.init(config)
version.init(config)
config.scan("c2cwsgiutils.services")
config.scan("c2cwsgiutils.errors")
13 changes: 8 additions & 5 deletions c2cwsgiutils/sql_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ def _indent(statement, indent=' '):

def _before_cursor_execute(conn, _cursor, statement, parameters, _context, _executemany):
if statement.startswith("SELECT ") and LOG.isEnabledFor(logging.INFO):
output = "statement:\n%s\nparameters: %s\nplan:\n " % (_indent(_beautify_sql(statement)),
repr(parameters))
output += '\n '.join([row[0] for row in conn.engine.execute("EXPLAIN ANALYZE " + statement,
parameters)])
LOG.info(output)
try:
output = "statement:\n%s\nparameters: %s\nplan:\n " % (_indent(_beautify_sql(statement)),
repr(parameters))
output += '\n '.join([row[0] for row in conn.engine.execute("EXPLAIN ANALYZE " + statement,
parameters)])
LOG.info(output)
except:
pass


def init(config):
Expand Down
15 changes: 15 additions & 0 deletions c2cwsgiutils/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging
import json
import os

VERSIONS_PATH = '/app/versions.json'
LOG = logging.getLogger(__name__)


def init(config):
if os.path.isfile(VERSIONS_PATH):
with open(VERSIONS_PATH) as file:
versions = json.load(file)
config.add_route("c2c_versions", r"/versions.json", request_method="GET")
config.add_view(lambda request: versions, route_name="c2c_versions", renderer="json", http_cache=0)
LOG.info("Installed the /versions.json service")
54 changes: 54 additions & 0 deletions c2cwsgiutils_genversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
import json
import os
import re
import subprocess
import sys

VERSION_RE = re.compile(r'^(.*)==(.*)$')


def _get_git_versions(root):
git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=root).strip()
try:
with open(os.devnull, "w") as devnull:
git_tag = subprocess.check_output(["git", "describe", "--tags", "--first-parent"],
stderr=devnull, cwd=root).strip()
git_tag = re.sub(r"-g[a-f0-9]+$", "", git_tag)
except:
git_tag = None
return {
"git_hash": git_hash,
"git_tag": git_tag
}


def _get_packages_version():
result = {}
with open(os.devnull, "w") as devnull:
for comp in subprocess.check_output(["pip3", "freeze"], stderr=devnull).decode().strip().split('\n'):
matcher = VERSION_RE.match(comp)
if matcher:
name, version = matcher.groups()
result[name] = version
else:
print("Cannot parse pacakge version: " + comp)
return result


def main():
git_tag = sys.argv[1]
git_hash = sys.argv[2]
report = {
'main': {
"git_hash": git_hash,
"git_tag": git_tag
},
'packages': _get_packages_version()
}
with open('versions.json', 'w') as file:
json.dump(report, file, indent=2)


if __name__ == "__main__":
main()
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages


VERSION = '0.7.0'
VERSION = '0.8.0'
HERE = os.path.abspath(os.path.dirname(__file__))
INSTALL_REQUIRES = open(os.path.join(HERE, 'rel_requirements.txt')).read().splitlines()

Expand Down Expand Up @@ -35,6 +35,7 @@
]
},
scripts=[
'c2cwsgiutils_run'
'c2cwsgiutils_run',
'c2cwsgiutils_genversion.py'
]
)

0 comments on commit c371262

Please sign in to comment.