Skip to content

Commit

Permalink
fix: Determine git revision hash once at startup (#113)
Browse files Browse the repository at this point in the history
This way, the server continues to supply the correct git revision hash
  even if the code changes after startup. Unless overridden by an
  environment variable, simply uses `git rev-parse --short HEAD`. In
  production, uses the environment override because the eventual flask
  process doesn't have the correct permissions to perform the git
  command.

  Resolves #108.
  • Loading branch information
gwhitney authored Apr 9, 2024
1 parent d4875ab commit 2cb6471
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
21 changes: 16 additions & 5 deletions flaskr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
Init file (creates app and database)
"""

import os
from flask import Flask, current_app
import click
from dotenv import load_dotenv
from flask import Flask, current_app
from flask.cli import with_appcontext
from flask_cors import CORS
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
import logging
from logging.handlers import RotatingFileHandler
import os
import subprocess # for calling git
import sys

from dotenv import load_dotenv

from .config import config

Expand Down Expand Up @@ -57,7 +58,17 @@ def create_app(environment=None, oeis_scheme='https', oeis_hostport='oeis.org'):
app.config.from_object(config[environment])
app.config['oeis_scheme'] = oeis_scheme
app.config['oeis_hostport'] = oeis_hostport


# Remember the git hash of the code we are running:
if 'GIT_REVISION_HASH' in os.environ:
app.config['git_revision_hash'] = os.getenv('GIT_REVISION_HASH')
else:
# hash can be provided by the command
# git rev-parse --short HEAD
# thanks to: https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script/
app.config['git_revision_hash'] = subprocess.check_output(
['git', 'rev-parse', '--short', 'HEAD'], encoding='utf8').strip()

# Logging
file_handler = RotatingFileHandler('api.log', maxBytes=10000, backupCount=1)
file_handler.setLevel(logging.INFO)
Expand Down Expand Up @@ -101,4 +112,4 @@ def clear_database_command():
click.echo("No action taken")
return
clear_database()
click.echo("Database cleared")
click.echo("Database cleared")
10 changes: 2 additions & 8 deletions flaskr/nscope/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from cypari2.convert import gen_to_python
import re
import requests
import subprocess # for calling git


executor = Executor()
Expand Down Expand Up @@ -327,13 +326,8 @@ def get_oeis_factors(oeis_id, num_elements):
@bp.route("/api/get_commit", methods=["GET"])
def get_git_commit():
""" Returns the short git hash for the current build of backscope
as provided by the command
git rev-parse --short HEAD
thanks to: https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script/
(as determined at startup in flaskr/__init__.py)
"""
short_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], encoding='utf8')
return jsonify({
'short_commit_hash': short_hash.strip()
'short_commit_hash': current_app.config['git_revision_hash']
})


1 change: 1 addition & 0 deletions server/production.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
cd /home/scope/repos/backscope &&
source .venv/bin/activate &&
sh tools/install-requirements.sh &&
export GIT_REVISION_HASH=$(git rev-parse --short HEAD) &&
gunicorn --workers 3 --bind unix:/home/scope/repos/backscope/backscope.sock -m 777 wsgi:app

0 comments on commit 2cb6471

Please sign in to comment.