#!/usr/bin/env bash # ============================================================================= # Container entrypoint. # # Modes: # ui (default) - run the Streamlit web app on port 8501 # hunt - run scripts/scheduled_hunt.py once and exit # (used by the Azure Container Apps Job) # # Persistent storage: # If PERSIST_DIR is set (e.g. /persist when the Azure File Share is mounted), # we relocate leads.db, data/, and logs/ there so they survive container # restarts and are shared between the UI app and the scheduled job. # ============================================================================= set -euo pipefail MODE="${1:-ui}" PERSIST_DIR="${PERSIST_DIR:-}" # ----------------------------------------------------------------------------- # Wire up persistent storage (if configured) # ----------------------------------------------------------------------------- if [[ -n "${PERSIST_DIR}" ]]; then mkdir -p "${PERSIST_DIR}/data" "${PERSIST_DIR}/logs" # Seed leads.db on first run if the file share is empty. # leads.db ships with the image (with empty schema). Copy once if missing. if [[ ! -f "${PERSIST_DIR}/leads.db" && -f /app/leads.db ]]; then cp /app/leads.db "${PERSIST_DIR}/leads.db" fi if [[ ! -f "${PERSIST_DIR}/sales_signals.db" && -f /app/sales_signals.db ]]; then cp /app/sales_signals.db "${PERSIST_DIR}/sales_signals.db" fi # Replace in-image copies with symlinks pointing to the file share. rm -f /app/leads.db /app/sales_signals.db ln -sf "${PERSIST_DIR}/leads.db" /app/leads.db ln -sf "${PERSIST_DIR}/sales_signals.db" /app/sales_signals.db # data/ and logs/ likewise rm -rf /app/data /app/logs ln -sf "${PERSIST_DIR}/data" /app/data ln -sf "${PERSIST_DIR}/logs" /app/logs # Audit log target (audit_log.py honours this env var) export AUDIT_LOG_PATH="${PERSIST_DIR}/logs/audit.jsonl" echo "[entrypoint] Persistent storage wired at ${PERSIST_DIR}" else echo "[entrypoint] PERSIST_DIR not set \u2014 using ephemeral container storage." fi # ----------------------------------------------------------------------------- # State sync (free alternative to HF Spaces Pro persistent storage): # pull leads.db from a private HF Dataset repo so the previous session's data # (hunt history, network connections, briefings, source health) is restored # before Streamlit starts. Set HF_TOKEN + HF_STATE_REPO as Space secrets to # enable. No-op if not configured. # ----------------------------------------------------------------------------- if [[ -n "${HF_TOKEN:-}" && -n "${HF_STATE_REPO:-}" ]]; then echo "[entrypoint] Pulling persistent state from HF Dataset ${HF_STATE_REPO}\u2026" cd /app && python -m state_sync pull || echo "[entrypoint] state pull failed (non-fatal)" else echo "[entrypoint] HF_TOKEN / HF_STATE_REPO not set \u2014 skipping state pull. Set both as Space secrets to enable free persistent storage." fi # ----------------------------------------------------------------------------- # Dispatch # ----------------------------------------------------------------------------- case "${MODE}" in ui) echo "[entrypoint] Starting Streamlit UI on :${PORT:-8501}" # XSRF + CORS disabled because we run behind a reverse proxy # (HF Spaces / Azure Container Apps) which causes file_uploader to # fail with AxiosError 403. The app is password-gated via auth.py. # maxUploadSize bumped to 100 MB for LinkedIn connections exports. exec streamlit run /app/app.py \ --server.headless=true \ --server.port="${PORT:-8501}" \ --server.address=0.0.0.0 \ --server.enableXsrfProtection=false \ --server.enableCORS=false \ --server.maxUploadSize=100 \ --server.maxMessageSize=100 \ --browser.gatherUsageStats=false ;; hunt) echo "[entrypoint] Running scheduled hunt" exec python /app/scripts/scheduled_hunt.py ;; shell) echo "[entrypoint] Drop to shell (debug)" exec /bin/bash ;; *) echo "[entrypoint] Unknown mode '${MODE}'. Use: ui | hunt | shell" >&2 exit 2 ;; esac