Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/hesabixAPI/deployment/systemd/hesabix-api-prestart.sh
Babak Alizadeh 29be99daf3 Stop deploy pip from stalling on dead extra-index mirrors.
Probe indexes first, fall back sequentially, and persist a reachable PyPI source so hesabix-update does not hang on Aliyun SSL timeouts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-28 17:45:51 +03:30

120 lines
3.8 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)
# ۱) systemctl daemon-reload — بعد از تغییر یونیت‌ها یا اسکریپت‌هایی مثل build_web
# ۲) در صورت نبودن/خرابی وابستگی‌های Python، نصب از آینه‌ها مطابق deploy.sh
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
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
}
# همان مسیر 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}" -eq 0 ]]; then
exit 0
fi
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}"
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
}
# 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"
exit 0
fi
done
log "could not fix Python dependencies or app.main still does not import"
exit 1