Add non Docker deployment entrypoints

This commit is contained in:
2026-06-09 11:32:34 +08:00
parent fe0133edc4
commit 1fafaa1d03
13 changed files with 299 additions and 90 deletions
+21
View File
@@ -0,0 +1,21 @@
[Unit]
Description=AI Agent Admin FastAPI
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/opt/ai-agent-admin/current/backend-fastapi
Environment=ENV=prod
Environment=PYTHONPATH=/opt/ai-agent-admin/current/backend-fastapi
Environment=APP_HOST=127.0.0.1
Environment=APP_PORT=8001
EnvironmentFile=/opt/ai-agent-admin/current/backend-fastapi/env/prod.env
ExecStart=/opt/ai-agent-admin/current/backend-fastapi/.venv/bin/python -m uvicorn main:app --host 127.0.0.1 --port 8001 --workers 4
Restart=always
RestartSec=5
KillSignal=SIGINT
[Install]
WantedBy=multi-user.target
+106
View File
@@ -0,0 +1,106 @@
#!/usr/bin/env bash
set -euo pipefail
APP_NAME="${APP_NAME:-ai-agent-admin}"
PROJECT_DIR="${PROJECT_DIR:-/opt/ai-agent-admin/current}"
BACKEND_DIR="${BACKEND_DIR:-${PROJECT_DIR}/backend-fastapi}"
WEB_DIST_DIR="${WEB_DIST_DIR:-${PROJECT_DIR}/web/apps/web-ele/dist}"
WEB_ROOT="${WEB_ROOT:-/var/www/ai-agent-admin}"
SERVICE_NAME="${SERVICE_NAME:-ai-agent-admin}"
PYTHON_BIN="${PYTHON_BIN:-python3}"
ENV_FILE="${ENV_FILE:-${BACKEND_DIR}/env/prod.env}"
BACKEND_PORT="${BACKEND_PORT:-8001}"
RUN_MIGRATIONS="${RUN_MIGRATIONS:-1}"
IMPORT_INIT_DATA="${IMPORT_INIT_DATA:-auto}"
BUILD_WEB="${BUILD_WEB:-0}"
log() {
printf '[%s] %s\n' "${APP_NAME}" "$*"
}
require_file() {
if [ ! -f "$1" ]; then
printf 'Missing required file: %s\n' "$1" >&2
exit 1
fi
}
require_dir() {
if [ ! -d "$1" ]; then
printf 'Missing required directory: %s\n' "$1" >&2
exit 1
fi
}
require_dir "${PROJECT_DIR}"
require_dir "${BACKEND_DIR}"
require_file "${ENV_FILE}"
if [ "${BUILD_WEB}" = "1" ]; then
log "building web-ele"
(cd "${PROJECT_DIR}/web" && pnpm --filter @vben/web-ele build)
fi
require_file "${WEB_DIST_DIR}/index.html"
log "publishing web dist to ${WEB_ROOT}"
install -d "${WEB_ROOT}"
rsync -a --delete "${WEB_DIST_DIR}/" "${WEB_ROOT}/"
log "preparing backend venv"
cd "${BACKEND_DIR}"
if [ ! -x ".venv/bin/python" ]; then
"${PYTHON_BIN}" -m venv .venv
fi
# shellcheck source=/dev/null
. .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
if [ "${RUN_MIGRATIONS}" = "1" ]; then
log "running alembic migrations"
ENV=prod python -m alembic upgrade head
fi
if [ "${IMPORT_INIT_DATA}" = "auto" ]; then
USER_COUNT="$(
ENV=prod python - <<'PY'
import asyncio
from sqlalchemy import text
from app.database import AsyncSessionLocal
async def main():
try:
async with AsyncSessionLocal() as db:
result = await db.execute(text("SELECT COUNT(*) FROM core_user"))
print(result.scalar() or 0)
except Exception:
print(0)
asyncio.run(main())
PY
)"
if [ "${USER_COUNT}" = "0" ] && [ -f "db_init.json" ]; then
log "importing initial data"
ENV=prod python scripts/loaddata.py db_init.json
fi
fi
log "restarting ${SERVICE_NAME}"
if command -v systemctl >/dev/null 2>&1; then
systemctl daemon-reload
systemctl restart "${SERVICE_NAME}"
fi
if command -v nginx >/dev/null 2>&1; then
log "reloading nginx"
nginx -t
if command -v systemctl >/dev/null 2>&1; then
systemctl reload nginx
else
nginx -s reload
fi
fi
log "done: backend port ${BACKEND_PORT}, web root ${WEB_ROOT}"
+51
View File
@@ -0,0 +1,51 @@
location = /ai-agent-admin {
return 301 /ai-agent-admin/;
}
location ^~ /ai-agent-admin/basic-api/ {
proxy_pass http://127.0.0.1:8001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
location ^~ /basic-api/ {
proxy_pass http://127.0.0.1:8001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}
location ^~ /ws {
proxy_pass http://127.0.0.1:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
location ^~ /ai-agent-admin/assets/ {
root /var/www;
expires 1y;
add_header Cache-Control "public, immutable";
}
location ^~ /ai-agent-admin/ {
root /var/www;
try_files $uri $uri/ /ai-agent-admin/index.html;
expires -1;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}