Files
CyberGril/deploy/hel913-unified/api_ctl.sh
T

147 lines
5.5 KiB
Bash
Raw 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
# HEL-913 统一体验运行包 · 统一实例 API 启停(单进程 HTTP+HTTPS 双入口)
#
# 启动器 deploy/voice/serve_dual_8360_8361.py 保证 HTTP 与 HTTPS 共用同一
# API 进程/同一份状态(lifespan 只进一次)。本脚本只做本实例常规启停安全:
# - 子命令只认 start|stop|status,其他一律拒绝退出 2(不会静默当 start);
# - start 前核对 pid 句柄:本实例已在跑 → 拒绝重复 start(不覆盖句柄);
# 句柄 pid 活着但不属于本实例(PID 复用)→ 拒绝动手,提示人工处理;
# - stop/start 失败收尾只杀经身份核验的进程(/proc/<pid> 的 cmdline 含
# 本启动器且 cwd 是本实例代码目录),身份不符一律不杀;
# - start 健康检查不过 → 清掉自己刚起的进程、保留日志、退出 1。
#
# 用法: [ENV=..] bash deploy/hel913-unified/api_ctl.sh start|stop|status
# 环境变量(默认值按部署说明的实例布局):
# HEL913_BASE 实例根目录(含 current/ etc/ data/ logs/)
# HEL913_HTTP HTTP 端口(默认 8360)
# HEL913_HTTPS HTTPS 端口(默认 8361;语音页需要 secure context)
# HEL913_VENV 产品 3.11 venv python(默认 /opt/cybergril-hel879/current/.venv311/bin/python)
# HEL913_CERT / HEL913_KEY 证书路径(默认 /opt/cybergril-voice-certs/ 下既有证书)
set -u
BASE=${HEL913_BASE:-/opt/cybergril-hel913}
CODE=${HEL913_CODE:-$BASE/current}
HTTP=${HEL913_HTTP:-8360}
HTTPS=${HEL913_HTTPS:-8361}
VENV=${HEL913_VENV:-/opt/cybergril-hel879/current/.venv311/bin/python}
CERT=${HEL913_CERT:-/opt/cybergril-voice-certs/cybergril-voice.pem}
KEY=${HEL913_KEY:-/opt/cybergril-voice-certs/cybergril-voice.key}
LOG=$BASE/logs/api.log
PIDF=$BASE/logs/api.pid
MARKER="serve_dual_8360_8361.py"
usage() { echo "usage: [ENV=..] bash $0 start|stop|status" >&2; exit 2; }
# 进程身份核验:pid 存活、cmdline 含本启动器、cwd 是本实例代码目录。
# 任一不满足即视为“不是我们的进程”(含 PID 被系统复用),一律不动它。
ours() {
local p=$1 cmd cw
case "$p" in ''|*[!0-9]*) return 1 ;; esac
[ "$p" != "$$" ] || return 1
kill -0 "$p" 2>/dev/null || return 1
cmd=$(tr '\0' ' ' < "/proc/$p/cmdline" 2>/dev/null) || return 1
cw=$(readlink "/proc/$p/cwd" 2>/dev/null) || return 1
case "$cmd" in *"$MARKER"*) ;; *) return 1 ;; esac
[ "$cw" = "$CODE" ] || return 1
return 0
}
pidfile_pid() { [ -f "$PIDF" ] && cat "$PIDF" 2>/dev/null; }
stop() {
local p
p=$(pidfile_pid)
if [ -z "$p" ]; then
echo "api not running (no pid file)"
return 0
fi
if kill -0 "$p" 2>/dev/null && ! ours "$p"; then
echo "REFUSED: pid $p is alive but is NOT this instance's api (pid reuse?) — not killing; resolve $PIDF manually" >&2
return 1
fi
if ours "$p"; then
kill "$p"
sleep 3
if ours "$p"; then kill -9 "$p"; sleep 1; fi
fi
rm -f "$PIDF"
echo "api stopped"
return 0
}
start() {
local p
p=$(pidfile_pid)
if [ -n "$p" ] && kill -0 "$p" 2>/dev/null; then
if ours "$p"; then
echo "REFUSED: api already running (pid $p) — stop first" >&2
else
echo "REFUSED: pid file has live pid $p that is not this instance's api (pid reuse?) — resolve $PIDF manually" >&2
fi
exit 1
fi
rm -f "$PIDF" # 死句柄:清掉再起
[ -d "$CODE" ] || { echo "REFUSED: code dir missing: $CODE" >&2; exit 1; }
[ -x "$VENV" ] || { echo "REFUSED: venv python not executable: $VENV" >&2; exit 1; }
[ -r "$CERT" ] && [ -r "$KEY" ] || { echo "REFUSED: cert/key not readable ($CERT / $KEY)" >&2; exit 1; }
mkdir -p "$BASE/logs" "$BASE/data"
cd "$CODE" || exit 1
set -a
export CYBERGRIL_DB=$BASE/data/companion.db
export CYBERGRIL_SEMANTIC=${HEL913_SEMANTIC:-stub}
export CYBERGRIL_R2_ENABLED=${HEL913_R2:-1}
export TZ=Asia/Shanghai
export CYBERGRIL_SEMANTIC_CONFIG=$BASE/etc/semantic.yaml
export MEM0_TELEMETRY=False
export FASTEMBED_CACHE_PATH=$BASE/data/sem/fastembed-cache
export HF_HUB_OFFLINE=1
export TRANSFORMERS_OFFLINE=1
export CYBERGRIL_MODELS=$BASE/etc/models.yaml
export CYBERGRIL_VOICE_CONFIG=$BASE/etc/voice.yaml
export CYBERGRIL_WEB=$CODE/web/dist
export PYTHONPATH=$CODE/src
set +a
nohup "$VENV" deploy/voice/serve_dual_8360_8361.py \
--http-port "$HTTP" --https-port "$HTTPS" --cert "$CERT" --key "$KEY" \
> "$LOG" 2>&1 &
p=$!
echo "$p" > "$PIDF"
echo "api starting (pid $p) http=$HTTP https=$HTTPS"
sleep 6
if curl -s --max-time 3 "http://127.0.0.1:$HTTP/api/health" >/dev/null \
&& curl -sk --max-time 3 "https://127.0.0.1:$HTTPS/api/health" >/dev/null; then
echo "OK: $HTTP + $HTTPS healthy"
return 0
fi
echo "FAIL: health check did not pass — full log kept at $LOG; tail:" >&2
tail -20 "$LOG" >&2
# 失败收尾:只清自己刚起的这个进程(身份核验),日志保留
if ours "$p"; then
kill "$p"; sleep 2
ours "$p" && kill -9 "$p"
fi
rm -f "$PIDF"
echo "cleaned up failed start (log kept: $LOG)" >&2
exit 1
}
status() {
local p
p=$(pidfile_pid)
if [ -n "$p" ]; then
if ours "$p"; then echo "running pid $p (identity verified)"
elif kill -0 "$p" 2>/dev/null; then echo "pid $p alive but NOT ours (stale pid file?)"
else echo "pid $p not alive (stale pid file)"; fi
else
echo "no pid file"
fi
curl -s --max-time 3 "http://127.0.0.1:$HTTP/api/health" && echo " <- health" \
|| echo "health not answering"
}
case "${1:-}" in
start) start ;;
stop) stop ;;
status) status ;;
*) usage ;;
esac