diff --git a/Dockerfile b/Dockerfile index 56e63fd..b83f846 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,6 +46,13 @@ ENV PROJ_DATA=/opt/conda/envs/loko-env/share/proj # Argument de build pour activer l'installation des librairies IA (ultralytics, OpenCV...) # Passer INSTALL_AI_LIBS=true dans le docker-compose pour les inclure ARG INSTALL_AI_LIBS=false +ARG GIT_COMMIT=unknown +ARG BUILD_DATE=unknown +ARG LOKO_VERSION=1.0.0 + +ENV GIT_COMMIT_SHA=${GIT_COMMIT} +ENV BUILD_DATE=${BUILD_DATE} +ENV LOKO_VERSION=${LOKO_VERSION} # Copie et installation des dépendances Python COPY requirements/ /app/requirements/ diff --git a/loko/api/urls.py b/loko/api/urls.py index 36f9a80..a66d479 100644 --- a/loko/api/urls.py +++ b/loko/api/urls.py @@ -1,4 +1,5 @@ from django.urls import path, re_path, include +from .views_version import VersionInfoView from .views import ( GeoProxyView, InterventionListCreateView, @@ -10,6 +11,7 @@ from .views import ( app_name = "zapi" urlpatterns = [ + path("version/", VersionInfoView.as_view(), name="system-version"), # API root (optionnel: une vue d'info/health) # path("", APIRootView.as_view(), name="root"), diff --git a/loko/api/views_version.py b/loko/api/views_version.py new file mode 100644 index 0000000..8e0039c --- /dev/null +++ b/loko/api/views_version.py @@ -0,0 +1,59 @@ +import os +import json +import subprocess +from django.http import JsonResponse +from django.utils import timezone +from rest_framework.views import APIView +from rest_framework.permissions import AllowAny + + +def get_version_info(): + version = os.environ.get('LOKO_VERSION', '1.0.0') + commit_sha = os.environ.get('GIT_COMMIT_SHA') or os.environ.get('GIT_COMMIT') + branch = os.environ.get('GIT_BRANCH', 'main') + build_date = os.environ.get('BUILD_DATE') + + if not commit_sha: + for possible_path in ['/app/build_info.json', 'build_info.json']: + if os.path.exists(possible_path): + try: + with open(possible_path, 'r', encoding='utf-8') as f: + data = json.load(f) + commit_sha = data.get('git_commit') or data.get('commit_sha') + version = data.get('version', version) + branch = data.get('branch', branch) + build_date = data.get('build_date', build_date) + break + except Exception: + pass + + if not commit_sha: + try: + commit_sha = subprocess.check_output( + ['git', 'rev-parse', 'HEAD'], + stderr=subprocess.DEVNULL, + timeout=1 + ).decode('utf-8').strip() + except Exception: + pass + + commit_short = commit_sha[:7] if commit_sha else None + + return { + 'status': 'online', + 'app': 'loko', + 'version': version, + 'git_commit': commit_sha, + 'git_commit_short': commit_short, + 'branch': branch, + 'build_date': build_date, + 'timestamp': timezone.now().isoformat() + } + + +class VersionInfoView(APIView): + permission_classes = [AllowAny] + authentication_classes = [] + + def get(self, request, *args, **kwargs): + return JsonResponse(get_version_info())