gmao/docker/entrypoint.sh

92 lines
2.9 KiB
Bash
Raw Normal View History

2026-08-14 18:02:25 +02:00
#!/bin/bash
set -e
# Forcer l'utilisation des packages Python installes pour gmao
export PATH=/home/gmao/.local/bin:$PATH
export PYTHONPATH=/home/gmao/.local/lib/python3.13/site-packages:$PYTHONPATH
# Si on est root, corriger les permissions des volumes montes
if [ "$(id -u)" = "0" ]; then
echo "Mise a jour des permissions des volumes..."
chown -R 1000:1000 /app/app_new /app/data /app/migrations /app/gmao_watchdog.py /app/ent_watchdog.py /app/pronote_watchdog.py /app/run_app_new.py /app/watchdog_dnd.py /var/log/supervisor /run/supervisor 2>/dev/null || true
chmod -R u+rX /app 2>/dev/null || true
fi
# Attendre MariaDB
echo "Attente de MariaDB sur ${DB_HOST:-mariadb}:${DB_PORT:-3306}..."
python3 - <<PYEOF "${DB_HOST:-mariadb}" "${DB_PORT:-3306}"
import socket, time, sys
host, port = sys.argv[1], int(sys.argv[2])
while True:
try:
s = socket.socket()
s.connect((host, port))
s.close()
break
except Exception:
time.sleep(1)
PYEOF
echo "MariaDB OK"
# S'assurer que les dossiers de donnees existent
mkdir -p /app/data/cache /app/app_new/uploads /app/app_new/instance
chown -R 1000:1000 /app/data /app/app_new/uploads /app/app_new/instance 2>/dev/null || true
# Alembic : une base neuve est créée depuis les modèles, une base importée du
# dump est rattachée au bon point historique, puis toutes les évolutions
# suivantes passent obligatoirement par les migrations.
echo "Application des migrations Alembic..."
ALEMBIC_STATE="$(python3 - <<'PYEOF'
2026-08-14 18:02:25 +02:00
from sqlalchemy import inspect
from app_new import create_app
app = create_app()
with app.app_context():
engine = app.extensions['migrate'].db.engine
tables = set(inspect(engine).get_table_names())
if 'alembic_version' in tables:
print('tracked')
elif 'users' not in tables:
print('empty')
elif 'closure_work_days' in tables:
print('imported_f1')
2026-08-14 18:02:25 +02:00
else:
print('imported_e0')
2026-08-14 18:02:25 +02:00
PYEOF
)"
case "$ALEMBIC_STATE" in
empty)
echo "Base vide : création du schéma courant."
python3 - <<'PYEOF'
from app_new import create_app, db
app = create_app()
with app.app_context():
db.create_all()
PYEOF
flask --app wsgi:app db stamp head
;;
imported_e0)
echo "Dump historique détecté : rattachement à e0f4a5b6c7d8."
flask --app wsgi:app db stamp e0f4a5b6c7d8
flask --app wsgi:app db upgrade
;;
imported_f1)
echo "Base importée déjà enrichie : rattachement à f1a5b6c7d8e9."
flask --app wsgi:app db stamp f1a5b6c7d8e9
flask --app wsgi:app db upgrade
;;
tracked)
flask --app wsgi:app db upgrade
;;
*)
echo "Etat Alembic inattendu: $ALEMBIC_STATE" >&2
exit 1
;;
esac
2026-08-14 18:02:25 +02:00
echo "Lancement de l'application..."
# Lancer supervisord en root, il demarrera les programmes avec l'utilisateur specifie
exec /home/gmao/.local/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf