Watch
1
0
Fork
You've already forked Seyyed_arc
0
forked from hesabix/arc
Seyyed_arc/hesabixAPI/deployment/systemd/hesabix-api-prestart.sh
Babak Alizadeh 121e2df12d fix(migrations): auto-merge Alembic heads before API upgrade
Multiple heads were crashing hesabix-api on start. Merge them automatically during prestart, update, and deploy.
2026-11-22 00:20:51 +03:30

152 lines
5.4 KiB
Shell
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# اجرا با دسترسی root (ExecStartPre=! در hesabix-api.service)
# ۱) نصب postgresql-client نسخه‌دار اگر pg_dump فقط wrapper باشد (نصب‌های قبلی)
# ۲) systemctl daemon-reload — بعد از تغییر یونیت‌ها یا اسکریپت‌هایی مثل build_web
# ۳) در صورت نبودن/خرابی وابستگی‌های Python، نصب از آینه‌ها مطابق deploy.sh
# ۴) اگر Alembic چند head داشته باشد، قبل از upgrade آن‌ها را merge می‌کند
set -euo pipefail
# systemd ممکن است PATH کوتاه بدهد؛ runuser در /usr/sbin است
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH:-}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
API_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
VENV_PY="${API_DIR}/.venv/bin/python"
PIP="${API_DIR}/.venv/bin/pip"
log() { echo "hesabix-api-prestart: $*" >&2; }
APP_ROOT="$(cd "${API_DIR}/../.." && pwd)"
ENSURE_SECRETS="${APP_ROOT}/app/scripts/ensure_api_production_secrets.sh"
if [[ -f "${ENSURE_SECRETS}" ]]; then
chmod +x "${ENSURE_SECRETS}" 2>/dev/null || true
if ! APP_ROOT="${APP_ROOT}" bash "${ENSURE_SECRETS}"; then
log "ensure_api_production_secrets failed"
exit 1
fi
fi
# نصب postgresql-client نسخه‌دار روی نصب‌های قبلی (قبل از daemon-reload تا drop-in اعمال شود).
# شکست apt API را پایین نمی‌آورد — بکاپ تا نصب موفق بسته کار نمی‌کند.
ENSURE_PG_CLIENT=""
for _pg_client_script in \
"${API_DIR}/../scripts/ensure_postgresql_client.sh" \
"${APP_ROOT}/app/scripts/ensure_postgresql_client.sh"; do
if [[ -f "${_pg_client_script}" ]]; then
ENSURE_PG_CLIENT="${_pg_client_script}"
break
fi
done
if [[ -n "${ENSURE_PG_CLIENT}" ]]; then
chmod +x "${ENSURE_PG_CLIENT}" 2>/dev/null || true
log "ensuring postgresql-client tools for pg_dump"
bash "${ENSURE_PG_CLIENT}" --nonfatal || log "ensure_postgresql_client skipped (non-fatal)"
fi
if ! command -v systemctl >/dev/null 2>&1; then
log "systemctl not found; skipping daemon-reload"
else
systemctl daemon-reload
fi
if [[ ! -x "$VENV_PY" || ! -x "$PIP" ]]; then
log "venv not ready (missing $VENV_PY or $PIP)"
exit 1
fi
run_as_www() {
if id -u www-data >/dev/null 2>&1; then
if command -v runuser >/dev/null 2>&1; then
runuser -u www-data -- "$@"
elif command -v sudo >/dev/null 2>&1; then
sudo -u www-data -- "$@"
else
log "نه runuser و نه sudo؛ اجرا با کاربر فعلی (root)"
"$@"
fi
else
log "www-data user missing; running as current user"
"$@"
fi
}
set_pip_env_for_url() {
local index_url="$1"
export PIP_INDEX_URL="${index_url}"
if [[ "${index_url}" != *"pypi.org"* ]]; then
local host
host=$(echo "${index_url}" | sed -n 's|https\?://\([^/]*\).*|\1|p')
export PIP_TRUSTED_HOST="${host}"
else
unset PIP_TRUSTED_HOST || true
fi
}
# همان مسیر import واقعی workerها
set +e
import_err="$(run_as_www env PYTHONPATH="${API_DIR}" "$VENV_PY" -c "import app.main" 2>&1)"
import_rc=$?
set -e
if [[ "${import_rc}" -ne 0 ]]; then
if grep -q "Production security validation failed" <<<"${import_err}"; then
log "app.main import failed due to production settings, not missing packages:"
log "${import_err}"
exit 1
fi
log "import app.main failed; attempting pip install -e . from Hesabix PyPI mirror..."
log "${import_err}"
# Sequential indexes only (no extra-index). A dead extra-index stalls every pip lookup.
MIRRORS=()
if [[ -n "${PIP_INDEX_URL:-}" ]]; then
MIRRORS+=("${PIP_INDEX_URL}")
fi
MIRRORS+=(
"https://pypi.org/simple"
"https://pypi.tuna.tsinghua.edu.cn/simple"
"https://mirrors.cloud.tencent.com/pypi/simple"
"https://p.mirror.hesabix.ir/simple"
"https://mirrors.aliyun.com/pypi/simple"
)
seen_mirrors=" "
for url in "${MIRRORS[@]}"; do
[[ -z "$url" ]] && continue
case "${seen_mirrors}" in *" ${url} "*) continue ;; esac
seen_mirrors+="${url} "
log "trying PyPI: $url"
set_pip_env_for_url "$url"
pip_env=(env -u PIP_EXTRA_INDEX_URL PIP_INDEX_URL="${PIP_INDEX_URL}" PIP_DEFAULT_TIMEOUT=15)
pip_args=("$PIP" --isolated --disable-pip-version-check --timeout 15 --retries 2 install --index-url "$url")
if [[ -n "${PIP_TRUSTED_HOST:-}" ]]; then
pip_env+=(PIP_TRUSTED_HOST="${PIP_TRUSTED_HOST}")
pip_args+=(--trusted-host "${PIP_TRUSTED_HOST}")
fi
if ! run_as_www "${pip_env[@]}" "${pip_args[@]}" --upgrade pip setuptools wheel \
|| ! run_as_www "${pip_env[@]}" "${pip_args[@]}" -e "${API_DIR}"; then
continue
fi
if run_as_www env PYTHONPATH="${API_DIR}" "$VENV_PY" -c "import app.main"; then
log "OK after install from $url"
import_rc=0
break
fi
done
if [[ "${import_rc}" -ne 0 ]]; then
log "could not fix Python dependencies or app.main still does not import"
exit 1
fi
fi
# Root can write merge revisions; the next ExecStartPre (`upgrade head`) runs as www-data.
log "ensuring a single Alembic head"
if ! env PYTHONPATH="${API_DIR}" "$VENV_PY" "${API_DIR}/scripts/check_migration_branches.py"; then
log "failed to auto-merge Alembic heads"
exit 1
fi
if id -u www-data >/dev/null 2>&1; then
chown -R www-data:www-data "${API_DIR}/migrations/versions" 2>/dev/null || true
fi
exit 0