Files
IPAM/docker/entrypoint.sh
Mathieu 6c020e7ee3 feat(docker): finalise le déploiement compose (sqlite + postgres)
- Compose nettoyé en deux profils isolés (sqlite, postgres) avec
  healthcheck HTTP et network_mode host pour la découverte LAN.
- Override docker-compose.bridge.yml pour les environnements où
  host mode n'est pas disponible (macOS/Windows).
- Entrypoint tolérant : fallback prisma db push quand aucune
  migration n'existe encore.
- Dockerfile robuste sans package-lock.json (npm install fallback).
- .env.docker.example et docker/README.md pour un démarrage en
  une commande.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-15 14:03:46 +02:00

66 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
# ---------------------------------------------------------------
# Entrypoint IPAM
# ---------------------------------------------------------------
# 1. Attend la base de données si PostgreSQL
# 2. Applique les migrations Prisma (prisma migrate deploy)
# 3. Optionnel : seed initial si RUN_SEED=true
# 4. Démarre le serveur Next.js (mode standalone)
# ---------------------------------------------------------------
set -eu
log() { printf '\033[36m[entrypoint]\033[0m %s\n' "$*"; }
err() { printf '\033[31m[entrypoint]\033[0m %s\n' "$*" >&2; }
# --- 1. Attente DB (PostgreSQL uniquement) ------------------------
if [ "${DATABASE_PROVIDER:-sqlite}" = "postgresql" ]; then
if [ -z "${DATABASE_URL:-}" ]; then
err "DATABASE_URL requis pour PostgreSQL"
exit 1
fi
log "Attente de PostgreSQL…"
# Extraction host:port de l'URL (format postgresql://user:pass@host:port/db?…)
HOST_PORT=$(printf '%s' "$DATABASE_URL" | sed -E 's#.*@([^/?]+).*#\1#')
HOST=$(printf '%s' "$HOST_PORT" | cut -d: -f1)
PORT=$(printf '%s' "$HOST_PORT" | cut -d: -f2)
[ "$PORT" = "$HOST" ] && PORT=5432
RETRIES=30
while ! nc -z "$HOST" "$PORT" 2>/dev/null; do
RETRIES=$((RETRIES - 1))
if [ "$RETRIES" -le 0 ]; then
err "PostgreSQL injoignable après 30 tentatives ($HOST:$PORT)"
exit 1
fi
sleep 1
done
log "PostgreSQL disponible ($HOST:$PORT)"
fi
# --- 2. Crée le dossier data pour SQLite --------------------------
if [ "${DATABASE_PROVIDER:-sqlite}" = "sqlite" ]; then
mkdir -p /app/data
fi
# --- 3. Migrations Prisma -----------------------------------------
# Si des migrations existent → migrate deploy (production-safe)
# Sinon (premier démarrage sans baseline) → db push pour initialiser le schéma
if [ -d /app/prisma/migrations ] && [ "$(find /app/prisma/migrations -mindepth 1 -maxdepth 1 -type d 2>/dev/null | head -n 1)" != "" ]; then
log "Application des migrations Prisma…"
npx prisma migrate deploy
else
log "Aucune migration trouvée — initialisation du schéma via 'prisma db push'…"
npx prisma db push --skip-generate --accept-data-loss
fi
# --- 4. Seed optionnel --------------------------------------------
if [ "${RUN_SEED:-false}" = "true" ]; then
log "Exécution du seed initial…"
npx tsx prisma/seed.ts || err "Seed échoué (non bloquant)"
fi
# --- 5. Lancement du serveur --------------------------------------
log "Démarrage du serveur Next.js sur :${PORT:-3000}"
exec node server.js