Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Determine git revision hash once at startup #113

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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