RFP-API / entrypoint.sh
chouchouvs's picture
Update entrypoint.sh
d4a0bf7 verified
#!/usr/bin/env bash
set -euo pipefail
# --- Réglages (surchageables en Variables/Secrets HF) ---
: "${GITHUB_REPO_API_URL:=https://github.com/chourmovs/RFPmasterApi.git}"
: "${GITHUB_REPO_CORE_URL:=https://github.com/chourmovs/RFPmaster.git}"
: "${GITHUB_TOKEN:=}" # si besoin pour repos privés
: "${API_MODULE:=rfp_api_app}" # module exposant 'app'
: "${API_APP_ATTR:=app}"
: "${BRANCH_API:=}" # ex: main ; vide = HEAD
: "${BRANCH_CORE:=}" # ex: main
: "${WORKSPACE:=/home/user/workspace}"
# Assure que ~/.local/bin (pip --user) est dans le PATH
export PATH="/home/user/.local/bin:/usr/local/bin:/usr/bin:/bin:${PATH}"
API_DIR="${WORKSPACE}/RFPmasterApi"
CORE_DIR="${WORKSPACE}/RFPmaster" # contiendra rfp_parser/
echo "===== Application Startup at $(date '+%F %T') ====="
echo "[startup] WORKSPACE=${WORKSPACE}"
mkdir -p "${WORKSPACE}"
clone_or_update () {
local repo_url="$1" target="$2" branch="$3"
if [ ! -d "${target}/.git" ]; then
echo "[git] cloning ${repo_url} -> ${target}"
if [ -n "${GITHUB_TOKEN}" ]; then
git -c http.extraHeader="Authorization: Basic $(printf 'oauth2:%s' "${GITHUB_TOKEN}" | base64 -w0)" \
clone --depth=1 ${branch:+-b "${branch}"} "${repo_url}" "${target}"
else
git clone --depth=1 ${branch:+-b "${branch}"} "${repo_url}" "${target}"
fi
else
echo "[git] updating ${target}"
git -C "${target}" fetch --depth=1 origin
if [ -n "${branch}" ]; then
git -C "${target}" checkout -q "${branch}" || true
fi
git -C "${target}" reset --hard ${branch:+origin/"${branch}"} ${branch:+"$(true)"} || git -C "${target}" reset --hard origin/HEAD
fi
}
# --- Clone/update des deux repos ---
clone_or_update "${GITHUB_REPO_API_URL}" "${API_DIR}" "${BRANCH_API}"
clone_or_update "${GITHUB_REPO_CORE_URL}" "${CORE_DIR}" "${BRANCH_CORE}"
# Safe dir pour Git (HF)
git config --global --add safe.directory "${API_DIR}" || true
git config --global --add safe.directory "${CORE_DIR}" || true
# --- Installation des dépendances Python (au run) ---
echo "[pip] Checking uvicorn presence..."
if ! command -v uvicorn >/dev/null 2>&1; then
echo "[pip] uvicorn not found → installing requirements"
if [ -f "${API_DIR}/requirements.txt" ]; then
# --user pour installer dans ~/.local et éviter les droits root
python -m pip install --user --no-cache-dir -r "${API_DIR}/requirements.txt"
else
# garde-fou minimal
python -m pip install --user --no-cache-dir fastapi "uvicorn[standard]" requests pandas openpyxl
fi
else
echo "[pip] uvicorn OK"
fi
# (Optionnel) si ton repo CORE a aussi un requirements.txt :
if [ -f "${CORE_DIR}/requirements.txt" ]; then
echo "[pip] Installing CORE requirements"
python -m pip install --user --no-cache-dir -r "${CORE_DIR}/requirements.txt" || true
fi
# --- PYTHONPATH : on expose les deux répertoires (API + CORE/rfp_parser) ---
export PYTHONPATH="${API_DIR}:${CORE_DIR}:${PYTHONPATH:-}"
# --- Sanity checks imports ---
echo "[check] importing ${API_MODULE}"
python - <<'PY' "${API_MODULE}"
import importlib, sys
m = sys.argv[1]
importlib.import_module(m)
print(f"[check] import {m}: OK")
PY
echo "[check] importing rfp_parser"
python - <<'PY'
import importlib
importlib.import_module("rfp_parser")
print("[check] import rfp_parser: OK")
PY
# --- Lancement Uvicorn ---
cd "${API_DIR}"
echo "[uvicorn] launching ${API_MODULE}:${API_APP_ATTR} on 0.0.0.0:7860"
exec uvicorn "${API_MODULE}:${API_APP_ATTR}" --host 0.0.0.0 --port 7860