compute_os/deploy/install.sh

159 lines
6.8 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# Guided, per-user deployment for the Compute Service Manager on Linux.
set -Eeuo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
CURRENT_USER="$(id -un)"
DEFAULT_INSTALL_DIR="$SCRIPT_DIR"
color() {
local code="$1"
shift
printf '\033[%sm%s\033[0m\n' "$code" "$*"
}
info() { color '36' "$*"; }
success() { color '32' "$*"; }
warn() { color '33' "$*"; }
fail() { color '31' "$*" >&2; exit 1; }
ask() {
local prompt="$1" default="$2" answer
read -r -p "$prompt [$default]: " answer
printf '%s' "${answer:-$default}"
}
confirm() {
local prompt="$1" answer
read -r -p "$prompt [Y/n]: " answer
[[ -z "$answer" || "$answer" =~ ^[Yy]$ ]]
}
version_at_least() {
local actual="$1" required="$2"
[[ "$(printf '%s\n%s\n' "$required" "$actual" | sort -V | head -n1)" == "$required" ]]
}
[[ "$(uname -s)" == "Linux" ]] || fail '该脚本仅支持 Linux。'
[[ "$EUID" -ne 0 ]] || fail '请使用普通 Linux 账号运行;不要以 root 运行。'
[[ -t 0 ]] || fail '该脚本需要交互式终端,请通过 SSH 登录后直接执行。'
[[ -f "$SCRIPT_DIR/backend/app/main.py" && -f "$SCRIPT_DIR/frontend/package.json" ]] || fail '请从项目 deploy/install.sh 执行,或确保脚本位于项目 deploy 目录。'
command -v systemctl >/dev/null || fail '未找到 systemctl;当前系统需要 systemd。'
command -v python3 >/dev/null || fail '未找到 python3,请先安装 Python 3.10 或更高版本。'
command -v node >/dev/null || fail '未找到 node,请先安装 Node.js 18 或更高版本。'
command -v npm >/dev/null || fail '未找到 npm,请先安装 Node.js 18 或更高版本。'
systemctl --user show-environment >/dev/null 2>&1 || fail '当前账号的 systemd user manager 不可用。请通过正常 SSH 登录后重试,并确认 PAM/systemd-logind 已启动用户会话。'
PYTHON_VERSION="$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])')"
NODE_VERSION="$(node --version | sed 's/^v//')"
version_at_least "$PYTHON_VERSION" '3.10' || fail "Python 版本过低:$PYTHON_VERSION(需要 3.10+)。"
version_at_least "$NODE_VERSION" '18.0' || fail "Node.js 版本过低:$NODE_VERSION(需要 18+)。"
printf '\n'
color '32' 'Compute Service Manager · Linux 引导式部署'
printf '运行账号:%s\n项目来源:%s\n\n' "$CURRENT_USER" "$SCRIPT_DIR"
INSTALL_DIR="$(ask '部署目录' "$DEFAULT_INSTALL_DIR")"
INSTALL_DIR="${INSTALL_DIR/#\~/$HOME}"
INSTALL_DIR="$(mkdir -p -- "$INSTALL_DIR" && cd -- "$INSTALL_DIR" && pwd)"
[[ "$INSTALL_DIR" != *[[:space:]%\\]* ]] || fail '部署目录不能包含空格、百分号或反斜杠(systemd unit 路径限制)。'
[[ "$INSTALL_DIR" != "$SCRIPT_DIR/"* ]] || fail '部署目录不能位于项目源码目录内部。'
if [[ "$INSTALL_DIR" != "$SCRIPT_DIR" ]]; then
if [[ -n "$(find "$INSTALL_DIR" -mindepth 1 -maxdepth 1 -print -quit)" ]] && ! confirm "部署目录已有文件,是否更新覆盖同名项目文件?"; then
fail '用户取消部署。'
fi
mkdir -p "$INSTALL_DIR/backend" "$INSTALL_DIR/frontend" "$INSTALL_DIR/deploy"
# Exclude machine-specific build/runtime files while copying the project source.
tar -C "$SCRIPT_DIR" \
--exclude='./backend/.venv' --exclude='./backend/__pycache__' \
--exclude='./frontend/node_modules' --exclude='./frontend/dist' \
-cf - backend frontend deploy README.md .gitignore | tar -C "$INSTALL_DIR" -xf -
fi
DEFAULT_PORT='8199'
PORT="$(ask '面板端口' "$DEFAULT_PORT")"
[[ "$PORT" =~ ^[0-9]{1,5}$ ]] && (( PORT >= 1 && PORT <= 65535 )) || fail '端口必须是 1 到 65535 之间的数字。'
BIND_HOST="$(ask '监听地址(0.0.0.0 可供内网团队访问)' '0.0.0.0')"
[[ "$BIND_HOST" =~ ^[A-Za-z0-9.:_-]+$ ]] || fail '监听地址格式无效。'
ENV_DIR="$HOME/.config/compute-service-manager"
ENV_FILE="$ENV_DIR/manager.env"
UNIT_DIR="$HOME/.config/systemd/user"
UNIT_FILE="$UNIT_DIR/compute-service-manager.service"
mkdir -p "$ENV_DIR" "$UNIT_DIR"
TOKEN=""
read -r -s -p '管理密钥(留空则自动生成):' TOKEN
printf '\n'
if [[ -z "$TOKEN" ]]; then
if command -v openssl >/dev/null; then
TOKEN="$(openssl rand -hex 32)"
else
TOKEN="$(python3 -c 'import secrets; print(secrets.token_urlsafe(48))')"
fi
fi
[[ "$TOKEN" =~ ^[A-Za-z0-9._~+/=-]+$ ]] || fail '管理密钥仅可包含字母、数字及 . _ ~ + / = - 字符。'
info '安装后端 Python 依赖…'
python3 -m venv "$INSTALL_DIR/backend/.venv"
"$INSTALL_DIR/backend/.venv/bin/python" -m pip install --upgrade pip
"$INSTALL_DIR/backend/.venv/bin/pip" install -r "$INSTALL_DIR/backend/requirements.txt"
info '安装前端依赖并构建生产页面…'
if [[ -f "$INSTALL_DIR/frontend/package-lock.json" ]]; then
(cd "$INSTALL_DIR/frontend" && npm ci)
else
(cd "$INSTALL_DIR/frontend" && npm install)
fi
(cd "$INSTALL_DIR/frontend" && npm run build)
# Keep the shared API secret out of the unit file and restrict it to this account.
umask 077
cat > "$ENV_FILE" <<EOF
PANEL_DATABASE_PATH=$HOME/.local/share/compute-service-manager/panel.db
PANEL_SERVICE_HOME=$HOME/.local/share/compute-service-manager
PANEL_UNIT_DIR=$HOME/.config/systemd/user
EOF
printf 'PANEL_API_TOKEN=%s\n' "$TOKEN" >> "$ENV_FILE"
chmod 600 "$ENV_FILE"
cat > "$UNIT_FILE" <<EOF
[Unit]
Description=Compute Service Manager
After=network.target
[Service]
Type=simple
WorkingDirectory=$INSTALL_DIR/backend
EnvironmentFile=$ENV_FILE
ExecStart=$INSTALL_DIR/backend/.venv/bin/uvicorn app.main:app --host $BIND_HOST --port $PORT
Restart=on-failure
RestartSec=3
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable --now compute-service-manager.service
if command -v loginctl >/dev/null && loginctl show-user "$CURRENT_USER" -p Linger --value 2>/dev/null | grep -qx 'no'; then
if command -v sudo >/dev/null && confirm '启用 linger,使面板在退出 SSH 后仍持续运行?(需要 sudo 权限)'; then
sudo loginctl enable-linger "$CURRENT_USER" || warn 'linger 启用失败;面板可能会在退出登录后停止。'
else
warn '尚未启用 linger;面板可能会在退出登录后停止。稍后可执行:sudo loginctl enable-linger '"$CURRENT_USER"
fi
fi
HOST_IP="$(hostname -I 2>/dev/null | awk '{print $1}')"
printf '\n'
success '部署完成。'
printf '本机访问: http://127.0.0.1:%s\n' "$PORT"
if [[ -n "$HOST_IP" ]]; then
printf '内网访问: http://%s:%s\n' "$HOST_IP" "$PORT"
fi
printf '管理密钥: %s\n' "$TOKEN"
printf '密钥文件: %s(权限 600)\n' "$ENV_FILE"
printf '\n服务管理命令:\n systemctl --user status compute-service-manager\n systemctl --user restart compute-service-manager\n journalctl --user -u compute-service-manager -f\n'
warn '请通过防火墙限制访问来源;直接 HTTP 部署适用于可信内网,公网访问应配置 HTTPS 反向代理。'