douyinWEB/scripts/bootstrap_physical.sh
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

468 lines
14 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
# douyinWEB bootstrap — physical / PVE Ubuntu guest (WireGuard primary).
#
# - Binds API/Web/PB/S3 to 0.0.0.0 for WG access
# - Does NOT install local TLS / Nginx / Caddy (TLS stays on edge VPS)
# - Always wipes monitor.db (fresh install: zero Douyin rooms)
# - Writes Caddy site snippet for the edge VPS to copy
#
# Usage:
# bash scripts/install.sh # choose physical
# bash scripts/bootstrap_physical.sh # interactive if vars missing
# DOMAIN=… ADMIN_USER=… ADMIN_PASS=… GIT_URL=… bash scripts/bootstrap_physical.sh
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# shellcheck disable=SC1091
source "$ROOT/scripts/lib/install_common.sh"
require_root
load_install_env_files
log() { printf '\n[%s] %s\n' "$(date '+%F %T')" "$*"; }
die() { printf 'ERROR: %s\n' "$*" >&2; exit 1; }
wizard_physical() {
echo
echo "=== 物理机 / PVE 客户机部署向导 ==="
echo "TLS 在海外 VPS;本机只跑应用,经 WireGuard 被反代。"
ask DOMAIN "对外域名(Caddy 站点名,DNS 指向海外 VPS)"
ask ADMIN_USER "管理员用户名" "admin"
ask_secret ADMIN_PASS "管理员密码"
ask GIT_URL "Git 仓库 URL(本地已有代码填 skip)" "skip"
[[ "$GIT_URL" == "skip" ]] && GIT_URL=""
ask GIT_BRANCH "Git 分支" "main"
ask APP_DIR "安装目录" "/opt/douyinWEB"
ask WG_SELF_IP "本机 WireGuard IP(Caddy 上游)" "10.8.0.2"
ask NEKO_NAT1TO1 "海外 VPS 公网 IP(Neko WebRTC,可稍后填)" ""
ask_yn INSTALL_SHELLCRASH "安装 ShellCrash(中国出海)?" "y"
ask_yn FRESH "清空本机 covers/clips/pb 数据?" "n"
ask BIND_HOST "监听地址" "0.0.0.0"
export DOMAIN ADMIN_USER ADMIN_PASS GIT_URL GIT_BRANCH APP_DIR WG_SELF_IP NEKO_NAT1TO1 INSTALL_SHELLCRASH FRESH BIND_HOST
}
if [[ -z "${DOMAIN:-}" || -z "${ADMIN_USER:-}" || -z "${ADMIN_PASS:-}" ]]; then
wizard_physical
fi
DOMAIN="${DOMAIN:?}"
ADMIN_USER="${ADMIN_USER:?}"
ADMIN_PASS="${ADMIN_PASS:?}"
GIT_URL="${GIT_URL:-}"
GIT_BRANCH="${GIT_BRANCH:-main}"
APP_DIR="${APP_DIR:-/opt/douyinWEB}"
FRESH="${FRESH:-0}"
PUBLIC_BASE="${PUBLIC_BASE:-https://${DOMAIN}}"
BIND_HOST="${BIND_HOST:-0.0.0.0}"
WG_SELF_IP="${WG_SELF_IP:-10.8.0.2}"
NEKO_NAT1TO1="${NEKO_NAT1TO1:-}"
INSTALL_SHELLCRASH="${INSTALL_SHELLCRASH:-0}"
WEB_PORT="${WEB_PORT:-4015}"
API_PORT="${API_PORT:-8014}"
PB_PORT="${PB_PORT:-8092}"
YT_PORT="${YT_PORT:-8020}"
S3_PORT="${S3_PORT:-8333}"
SW_MASTER_PORT="${SW_MASTER_PORT:-19333}"
SW_VOLUME_PORT="${SW_VOLUME_PORT:-18080}"
SW_FILER_PORT="${SW_FILER_PORT:-18888}"
PB_ADMIN_EMAIL="${PB_ADMIN_EMAIL:-${ADMIN_USER}@${DOMAIN}}"
S3_ACCESS="${S3_ACCESS:-douyin}"
S3_BUCKET="${S3_BUCKET:-douyin-covers}"
WEED_VER="${WEED_VER:-3.80}"
PB_VER="${PB_VER:-0.25.8}"
ENV_FILE="/etc/douyinweb.env"
SW_DATA="/data/douyinweb-seaweed"
CADDY_SNIPPET="/root/douyinweb-${DOMAIN}.caddy"
port_in_use() {
local p="$1"
ss -lnt | awk '{print $4}' | grep -E "[:.]${p}$" >/dev/null 2>&1
}
assert_ports_free() {
local p
if systemctl is-active --quiet douyinweb-api 2>/dev/null; then
log "douyinweb already running — skip port check"
return
fi
for p in "$WEB_PORT" "$API_PORT" "$PB_PORT" "$YT_PORT" "$S3_PORT"; do
if port_in_use "$p"; then
die "port ${p} in use — change via env"
fi
done
}
persist_secrets() {
if [[ -f "$ENV_FILE" ]]; then
# shellcheck disable=SC1090
source "$ENV_FILE"
log "loaded existing $ENV_FILE"
return
fi
SESSION_SECRET="$(openssl rand -hex 24)"
S3_SECRET="$(openssl rand -hex 18)"
cat >"$ENV_FILE" <<EOF
# generated $(date -u '+%F %T') UTC — physical deploy
INSTALL_SHAPE=physical
DOMAIN=${DOMAIN}
PUBLIC_BASE=${PUBLIC_BASE}
ADMIN_USER=${ADMIN_USER}
SESSION_SECRET=${SESSION_SECRET}
S3_SECRET=${S3_SECRET}
S3_ACCESS=${S3_ACCESS}
S3_BUCKET=${S3_BUCKET}
BIND_HOST=${BIND_HOST}
WG_SELF_IP=${WG_SELF_IP}
NEKO_NAT1TO1=${NEKO_NAT1TO1}
WEB_PORT=${WEB_PORT}
API_PORT=${API_PORT}
PB_PORT=${PB_PORT}
YT_PORT=${YT_PORT}
S3_PORT=${S3_PORT}
EOF
chmod 600 "$ENV_FILE"
log "wrote $ENV_FILE"
}
install_packages() {
export DEBIAN_FRONTEND=noninteractive
apt-get update -y
apt-get install -y --no-install-recommends \
git curl unzip ca-certificates ffmpeg openssl \
python3 python3-venv python3-pip
command -v node >/dev/null || die "node/npm required — install Node 20+ first"
command -v npm >/dev/null || die "npm required"
}
clone_repo() {
log "app tree $APP_DIR"
if [[ -n "$GIT_URL" ]]; then
if [[ -d "$APP_DIR/.git" ]]; then
git -C "$APP_DIR" remote set-url origin "$GIT_URL"
git -C "$APP_DIR" fetch --depth=1 origin "$GIT_BRANCH"
git -C "$APP_DIR" checkout -B "$GIT_BRANCH" "origin/$GIT_BRANCH"
else
[[ -e "$APP_DIR" && ! -d "$APP_DIR/.git" ]] && die "$APP_DIR exists and is not a git repo"
git clone --depth=1 --branch "$GIT_BRANCH" "$GIT_URL" "$APP_DIR"
fi
else
[[ -d "$APP_DIR" ]] || die "GIT_URL empty and $APP_DIR missing"
fi
# Fresh install: never keep packed monitor.db
rm -f "$APP_DIR/backend/data/monitor.db" \
"$APP_DIR/backend/data/monitor.db-shm" \
"$APP_DIR/backend/data/monitor.db-wal"
if [[ "$FRESH" == "1" ]]; then
rm -rf "$APP_DIR/backend/data/covers" "$APP_DIR/backend/data/clips" "$APP_DIR/pb/pb_data"
mkdir -p "$APP_DIR/backend/data/covers" "$APP_DIR/backend/data/clips"
fi
}
install_pocketbase() {
mkdir -p "$APP_DIR/pb"
if [[ ! -x "$APP_DIR/pb/pocketbase" ]]; then
curl -fsSL -o /tmp/pb.zip \
"https://github.com/pocketbase/pocketbase/releases/download/v${PB_VER}/pocketbase_${PB_VER}_linux_amd64.zip"
unzip -o /tmp/pb.zip pocketbase -d "$APP_DIR/pb"
chmod +x "$APP_DIR/pb/pocketbase"
rm -f /tmp/pb.zip
fi
}
install_seaweed() {
mkdir -p /opt/seaweedfs "$SW_DATA/master" "$SW_DATA/volume" "$SW_DATA/filer" /etc/seaweedfs
if [[ ! -x /opt/seaweedfs/weed ]]; then
curl -fsSL -o /tmp/weed.tgz \
"https://github.com/seaweedfs/seaweedfs/releases/download/${WEED_VER}/linux_amd64.tar.gz"
tar -xzf /tmp/weed.tgz -C /opt/seaweedfs weed
chmod +x /opt/seaweedfs/weed
rm -f /tmp/weed.tgz
fi
cat >/etc/seaweedfs/douyinweb-s3.json <<EOF
{
"identities": [
{
"name": "douyinweb",
"credentials": [{"accessKey": "${S3_ACCESS}", "secretKey": "${S3_SECRET}"}],
"actions": ["Admin", "Read", "Write", "List", "Tagging"]
},
{
"name": "anonymous",
"actions": ["Read:${S3_BUCKET}", "List:${S3_BUCKET}"]
}
]
}
EOF
}
write_units() {
local NODE_BIN NPM_BIN
NODE_BIN="$(command -v node)"
NPM_BIN="$(command -v npm)"
log "writing systemd units (bind ${BIND_HOST})"
cat >/etc/systemd/system/douyinweb-pb.service <<EOF
[Unit]
Description=DouyinWEB PocketBase
After=network.target
[Service]
Type=simple
WorkingDirectory=${APP_DIR}/pb
ExecStart=${APP_DIR}/pb/pocketbase serve --http=${BIND_HOST}:${PB_PORT}
Restart=always
RestartSec=3
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-yt.service <<EOF
[Unit]
Description=DouyinWEB YouTube.js sidecar
After=network.target
[Service]
Type=simple
WorkingDirectory=${APP_DIR}/youtube-svc
Environment=YT_SVC_HOST=127.0.0.1
Environment=YT_SVC_PORT=${YT_PORT}
Environment=NODE_ENV=production
# Physical: Innertube must go through ShellCrash mixed (Douyin stays out of this process).
Environment=HTTP_PROXY=http://127.0.0.1:7890
Environment=HTTPS_PROXY=http://127.0.0.1:7890
Environment=NO_PROXY=127.0.0.1,localhost,10.0.0.0/8,192.168.0.0/16
ExecStart=${NODE_BIN} src/server.mjs
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-api.service <<EOF
[Unit]
Description=DouyinWEB FastAPI (physical)
After=network.target douyinweb-pb.service douyinweb-s3.service
[Service]
Type=simple
WorkingDirectory=${APP_DIR}/backend
TimeoutStopSec=25
KillMode=mixed
Environment=TZ=Asia/Shanghai
Environment=DY_PB_URL=http://127.0.0.1:${PB_PORT}
Environment=DY_PB_ADMIN_EMAIL=${PB_ADMIN_EMAIL}
Environment=DY_PB_ADMIN_PASSWORD=${ADMIN_PASS}
Environment=DY_S3_ENABLED=true
Environment=DY_S3_ENDPOINT=http://127.0.0.1:${S3_PORT}
Environment=DY_S3_PUBLIC_BASE=${PUBLIC_BASE}
Environment=DY_S3_ACCESS_KEY=${S3_ACCESS}
Environment=DY_S3_SECRET_KEY=${S3_SECRET}
Environment=DY_S3_BUCKET=${S3_BUCKET}
Environment=DY_S3_REGION=us-east-1
Environment=DY_YOUTUBE_SVC_URL=http://127.0.0.1:${YT_PORT}
Environment=DY_PUBLIC_BASE_URL=${PUBLIC_BASE}
Environment=DY_INTERNAL_API_URL=http://127.0.0.1:${API_PORT}
Environment=DY_CORS_ORIGINS=${PUBLIC_BASE},http://127.0.0.1:${WEB_PORT}
Environment=DY_AUTH_ENABLED=true
Environment=DY_ADMIN_USERNAME=${ADMIN_USER}
Environment=DY_ADMIN_PASSWORD=${ADMIN_PASS}
Environment=DY_SESSION_SECRET=${SESSION_SECRET}
Environment=DY_NEKO_HTTP_BIND=0.0.0.0
Environment=DY_NEKO_EDGE_UPSTREAM=${WG_SELF_IP}
Environment=DY_NEKO_NAT1TO1=${NEKO_NAT1TO1}
ExecStart=${APP_DIR}/backend/.venv/bin/python -m uvicorn app.main:app --host ${BIND_HOST} --port ${API_PORT}
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-web.service <<EOF
[Unit]
Description=DouyinWEB Next.js (physical)
After=network.target douyinweb-api.service
[Service]
Type=simple
WorkingDirectory=${APP_DIR}/frontend
TimeoutStopSec=20
KillMode=mixed
Environment=TZ=Asia/Shanghai
Environment=NODE_ENV=production
Environment=DY_INTERNAL_API_URL=http://127.0.0.1:${API_PORT}
Environment=DY_PUBLIC_BASE_URL=${PUBLIC_BASE}
Environment=PORT=${WEB_PORT}
Environment=HOSTNAME=${BIND_HOST}
ExecStart=${NPM_BIN} run start -- --hostname ${BIND_HOST} --port ${WEB_PORT}
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-sw-master.service <<EOF
[Unit]
Description=DouyinWEB SeaweedFS master
After=network.target
[Service]
ExecStart=/opt/seaweedfs/weed master -ip=127.0.0.1 -port=${SW_MASTER_PORT} -mdir=${SW_DATA}/master
Restart=always
RestartSec=2
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-sw-volume.service <<EOF
[Unit]
Description=DouyinWEB SeaweedFS volume
After=network.target douyinweb-sw-master.service
Requires=douyinweb-sw-master.service
[Service]
ExecStart=/opt/seaweedfs/weed volume -mserver=127.0.0.1:${SW_MASTER_PORT} -ip=127.0.0.1 -port=${SW_VOLUME_PORT} -dir=${SW_DATA}/volume -max=100
Restart=always
RestartSec=2
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-sw-filer.service <<EOF
[Unit]
Description=DouyinWEB SeaweedFS filer
After=network.target douyinweb-sw-master.service douyinweb-sw-volume.service
[Service]
ExecStart=/opt/seaweedfs/weed filer -master=127.0.0.1:${SW_MASTER_PORT} -ip=127.0.0.1 -port=${SW_FILER_PORT} -defaultReplicaPlacement=000
Restart=always
RestartSec=2
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/douyinweb-s3.service <<EOF
[Unit]
Description=DouyinWEB SeaweedFS S3 API
After=network.target douyinweb-sw-filer.service
Requires=douyinweb-sw-filer.service
[Service]
ExecStart=/opt/seaweedfs/weed s3 -filer=127.0.0.1:${SW_FILER_PORT} -ip.bind=${BIND_HOST} -port=${S3_PORT} -config=/etc/seaweedfs/douyinweb-s3.json
Restart=always
RestartSec=2
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
}
app_python_node() {
log "python venv + pip"
python3 -m venv "$APP_DIR/backend/.venv"
"$APP_DIR/backend/.venv/bin/pip" install -U pip
"$APP_DIR/backend/.venv/bin/pip" install -r "$APP_DIR/backend/requirements.txt"
log "youtube-svc + frontend build"
(cd "$APP_DIR/youtube-svc" && npm install --omit=dev --no-audit --no-fund)
(
cd "$APP_DIR/frontend"
export DY_INTERNAL_API_URL="http://127.0.0.1:${API_PORT}"
export DY_PUBLIC_BASE_URL="${PUBLIC_BASE}"
npm install --no-audit --no-fund
npm run build
)
}
start_stack() {
systemctl enable --now \
douyinweb-sw-master douyinweb-sw-volume douyinweb-sw-filer douyinweb-s3 \
douyinweb-pb douyinweb-yt douyinweb-api douyinweb-web
sleep 4
systemctl is-active \
douyinweb-sw-master douyinweb-sw-volume douyinweb-sw-filer douyinweb-s3 \
douyinweb-pb douyinweb-yt douyinweb-api douyinweb-web
}
ensure_admin() {
mkdir -p "$APP_DIR/pb/pb_data"
"$APP_DIR/pb/pocketbase" superuser upsert "$PB_ADMIN_EMAIL" "$ADMIN_PASS" || log "WARN: pb superuser"
}
write_caddy_snippet() {
cat >"$CADDY_SNIPPET" <<EOF
# Copy to edge VPS: ${CADDY_SITES_DIR:-/etc/caddy/sites}/${DOMAIN}.caddy
# Then: caddy validate && systemctl reload caddy
# Upstream = this guest WireGuard IP ${WG_SELF_IP}
${DOMAIN} {
encode gzip
request_body { max_size 200MB }
handle /api/* {
reverse_proxy ${WG_SELF_IP}:${API_PORT} {
header_up Host {host}
}
}
handle /covers/* {
reverse_proxy ${WG_SELF_IP}:${API_PORT} {
header_up Host {host}
}
}
handle /_/* {
reverse_proxy ${WG_SELF_IP}:${PB_PORT} {
header_up Host {host}
}
}
handle /${S3_BUCKET}/* {
reverse_proxy ${WG_SELF_IP}:${S3_PORT} {
header_up Host {host}
}
}
handle {
reverse_proxy ${WG_SELF_IP}:${WEB_PORT} {
header_up Host {host}
}
}
}
EOF
log "Caddy snippet → $CADDY_SNIPPET"
echo "在海外 VPS 上: INSTALL_SHAPE=edge DOMAIN=${DOMAIN} WG_UPSTREAM_IP=${WG_SELF_IP} bash scripts/bootstrap_physical_edge.sh"
}
maybe_shellcrash() {
if [[ "$INSTALL_SHELLCRASH" != "1" ]]; then
return
fi
if [[ -x "$APP_DIR/scripts/linux/install_shellcrash.sh" ]]; then
log "installing ShellCrash (firewall_area=0)"
# shellcheck disable=SC2086
env ${LIVE_HTTP_PROXY:+LIVE_HTTP_PROXY="$LIVE_HTTP_PROXY"} \
bash "$APP_DIR/scripts/linux/install_shellcrash.sh" || log "WARN: ShellCrash install failed"
else
log "WARN: install_shellcrash.sh missing — skip"
fi
}
verify() {
curl -fsS "http://127.0.0.1:${API_PORT}/api/health" | head -c 400; echo
curl -sS -o /dev/null -w "web:%{http_code}\n" "http://127.0.0.1:${WEB_PORT}/login" || true
echo
echo "PUBLIC ${PUBLIC_BASE}/ (via edge Caddy + WG)"
echo "login ${ADMIN_USER}"
echo "WG IP ${WG_SELF_IP}"
echo "caddy ${CADDY_SNIPPET}"
echo "next: configure wg-quick@wg-dy AllowedIPs=10.8.0.0/24 only; run edge installer on VPS"
}
main() {
log "physical bootstrap DOMAIN=$DOMAIN BIND=$BIND_HOST WG=$WG_SELF_IP"
assert_ports_free
persist_secrets
# shellcheck disable=SC1090
source "$ENV_FILE"
install_packages
clone_repo
install_pocketbase
install_seaweed
write_units
app_python_node
start_stack
ensure_admin
write_caddy_snippet
maybe_shellcrash
verify
log "DONE — no local TLS; finish edge VPS + WireGuard"
}
main "$@"