Ajout du support pour Docker

This commit is contained in:
Lyanis Souidi 2024-06-03 15:17:38 +02:00
parent 99942f40ea
commit e159ca23ad
Signed by: lyanis
GPG Key ID: 202150AA0DAB9FAC
4 changed files with 385 additions and 0 deletions

184
.dockerignore Normal file
View File

@ -0,0 +1,184 @@
# ---> Emacs
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
# cask packages
.cask/
dist/
# Flycheck
flycheck_*.el
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
envsco8/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# Mac OSX OS generated files
.DS_Store?
Thumbs.db
*.DS_Store
# Subversion (protects when importing)
.svn
# VS Code
.vscode/
*.code-workspace
# PyCharm
.idea/
copy
# Symlinks static ScoDoc
app/static/links/[0-9]*.*[0-9]
# Essais locaux
xp/
/.git
/.gitea

34
Dockerfile Normal file
View File

@ -0,0 +1,34 @@
FROM debian:12
ARG SCODOC_VERSION=dev \
SCODOC_COMMIT=dev \
IMAGE_DATE=dev
LABEL org.opencontainers.image.created=$IMAGE_DATE \
org.opencontainers.image.authors="ScoDoc" \
org.opencontainers.image.url="https://scodoc.org" \
org.opencontainers.image.documentation="https://scodoc.org" \
org.opencontainers.image.source="https://git.scodoc.org/ScoDoc/ScoDoc.git" \
org.opencontainers.image.version=$SCODOC_VERSION \
org.opencontainers.image.vendor="ScoDoc" \
org.opencontainers.image.licenses="GPL-2.0-only" \
org.opencontainers.image.ref.name=$SCODOC_COMMIT \
org.opencontainers.image.title="ScoDoc" \
org.opencontainers.image.description="ScoDoc: un logiciel libre pour le suivi de la scolarité"
COPY . /opt/scodoc
RUN apt-get update \
&& apt-get upgrade --yes \
&& useradd --shell /bin/bash --create-home --comment "ScoDoc service" scodoc \
&& mkdir -p /opt/scodoc-data \
&& chown -R scodoc:scodoc /opt/scodoc /opt/scodoc-data \
&& apt-get install --yes curl gcc graphviz graphviz-dev libpq-dev cracklib-runtime libcrack2-dev libpango-1.0-0 pango1.0-tools python3-dev python3-venv python3-pip python3-wheel postgresql-client \
&& apt-get clean \
&& su -c "(cd /opt/scodoc && python3 -m venv venv && source venv/bin/activate && pip install wheel && pip install -r requirements-3.11.txt)" scodoc
EXPOSE 8000
HEALTHCHECK CMD curl --fail http://127.0.0.1:8000 || exit 1
ENTRYPOINT [ "/opt/scodoc/tools/docker-entrypoint.sh" ]

65
docker-compose.yml Normal file
View File

@ -0,0 +1,65 @@
services:
scodoc:
image: scodoc/scodoc
container_name: scodoc
restart: unless-stopped
environment:
SCODOC_DATABASE_URI: postgresql://scodb:scodb@db/SCODOC
CACHE_REDIS_HOST: cache
SCODOC_ADMIN_MAIL: admin@scodoc.local
SCODOC_ADMIN_PASSWORD: p@ssword
#SCODOC_MAIL_FROM: no-reply@scodoc.local
#MAIL_SERVER: smtp.scodoc.local
#MAIL_PORT: 587
#MAIL_USE_TLS: True
#MAIL_USERNAME: scodoc
#MAIL_PASSWORD: scodoc
ports:
- "127.0.0.1:8000:8000"
volumes:
- scodata:/opt/scodoc-data
depends_on:
db:
condition: service_started
cache:
condition: service_healthy
networks:
- db
- cache
db:
image: postgres:15
container_name: scodoc_db
restart: unless-stopped
environment:
POSTGRES_USER: scodb
POSTGRES_PASSWORD: scodb
volumes:
- scodb:/var/lib/postgresql/data
networks:
- db
cache:
image: valkey/valkey
container_name: scodoc_cache
restart: unless-stopped
networks:
- cache
healthcheck:
test: ["CMD", "valkey-cli", "ping"]
interval: 5s
timeout: 5s
retries: 5
watchtower:
image: containrrr/watchtower
container_name: watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /etc/localtime:/etc/localtime:ro
command: scodoc scodoc_db scodoc_cache --schedule "0 0 2 * * *"
volumes:
scodata:
scodb:
networks:
db:
cache:

102
tools/docker-entrypoint.sh Executable file
View File

@ -0,0 +1,102 @@
#!/bin/bash
# Script à lancer en tant que root au démarrage du container Docker
echo "Initialisation de ScoDoc..."
# Le répertoire de ce script:
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
source "$SCRIPT_DIR/config.sh"
source "$SCRIPT_DIR/utils.sh"
if [ -z "${SCODOC_DATABASE_URI}" ]; then
echo "La variable d'environnement SCODOC_DATABASE_URI doit être définie pour poursuivre le démarrage."
exit 1
fi
db_name=$(echo $SCODOC_DATABASE_URI | sed -E 's/.*\/([^\/]+)$/\1/')
if [[ "$db_name" == "$SCODOC_DATABASE_URI" ]]; then
# nom de la base de données par défaut si non spécifié dans l'URI
db_name="SCODOC"
export SCODOC_DATABASE_URI="$SCODOC_DATABASE_URI/$db_name"
fi
# URI de la base 'postgres', nécessaire pour les commandes de création de base
PG_DATABASE_URI=$(echo "$SCODOC_DATABASE_URI" | sed -E 's|/[^/]+$|/postgres|')
if [ -z "${CACHE_REDIS_HOST}" ]; then
echo "La variable d'environnement CACHE_REDIS_HOST doit être définie pour poursuivre le démarrage."
exit 1
fi
cd /opt/scodoc || die "Error: chdir to /opt/scodoc"
mkdir -p /opt/scodoc-data || die "Error: mkdir /opt/scodoc-data"
# ------------ CREATION ENVIRONNEMENT
# Création du fichier .env si absent
if ! [ -f /opt/scodoc-data/.env ]; then
if [ -z "${SCODOC_ADMIN_MAIL}" ]; then
echo "La variable d'environnement SCODOC_ADMIN_MAIL doit être définie pour poursuivre l'installation (nécessaire uniquement pour le premier démarrage)."
exit 1
fi
SECRET_KEY=$(python3 -c "import uuid; print(uuid.uuid4().hex)")
cat > /opt/scodoc-data/.env <<EOF
# .env for ScoDoc (production)
FLASK_APP=scodoc.py
FLASK_ENV=production
SCODOC_ADMIN_MAIL="$SCODOC_ADMIN_MAIL" # important: le mail de admin
SECRET_KEY="$SECRET_KEY" # une chaine aléatoire"
EOF
echo "Fichier /opt/scodoc-data/.env créé avec:"
cat /opt/scodoc-data/.env
echo
echo "Vous pouvez le modifier si besoin."
echo
fi
# ------------ VERIFICATIONS DES REPERTOIRES ET DROITS
# déjà fait par le Dockerfile, mais certaines fausses manips de nos utilisateurs
# ont pu changer ça:
set_scodoc_var_dir
change_scodoc_file_ownership
# ------------ CREATION BASE DE DONNEES
# Création de la base de données si elle n'existe pas
echo "Connexion à la base de données.."
if ! pg_isready -d $PG_DATABASE_URI --timeout=30; then
echo "Erreur: impossible de se connecter à la base de données."
exit 1
fi
if psql $PG_DATABASE_URI -lqt | cut -d \| -f 1 | grep -iqw "$db_name"; then
echo "Connexion réussie, la base $db_name existe."
else
echo "Connexion réussie, la base $db_name n'existe pas. Création..."
su -c "/opt/scodoc/tools/create_database.sh $db_name" scodoc || die "Erreur: create_database.sh $db_name"
echo "Base $db_name créée."
# ------------ INITIALISATION BASE DE DONNEES
echo
echo "Création des tables et du compte admin"
echo
if [ -z "${SCODOC_ADMIN_PASSWORD}" ]; then
echo "La variable d'environnement SCODOC_ADMIN_PASSWORD doit être définie pour poursuivre l'installation (nécessaire uniquement pour le premier démarrage)."
exit 1
fi
su -c "cd /opt/scodoc; source venv/bin/activate; flask db upgrade; flask sco-db-init; flask user-password admin --password $SCODOC_ADMIN_PASSWORD" scodoc || die "Erreur: sco-db-init"
echo
echo "Base initialisée et admin créé."
echo
fi
# ------------ LANCEMENT DES SERVICES
echo
echo "ScoDoc configuré et démarré."
echo "Vous pouvez vous connecter en web et vous identifier comme \"admin\"."
echo
su -c "cd /opt/scodoc; /opt/scodoc/venv/bin/gunicorn -b 0.0.0.0:8000 -w 4 --timeout 600 scodoc:app" scodoc