website-scripts/sites-manage-cloud.sh

244 lines
6.8 KiB
Bash
Raw Permalink 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.

#!/bin/bash
# 站点管理脚本(自动适配本地/云端)
#
# 本地 Mac 运行 → 通过 SSH 操作 txjp 服务器
# 云服务器运行 → 直接操作本地 Docker
#
# 用法bash sites-manage-cloud.sh [命令] [站点名...]
# 不加命令默认 status
# 站点名ldap www cloud token gitea issue assets oa nginx sso monitor
# ai上述全部、all同 ai
set -e
# ============================================================
# 自动检测运行环境
#
# 优先级:
# 1. OA_REMOTE 环境变量 — "local" 强制本地 / "ssh" 强制远程
# 2. hostname 匹配 — 包含 "Tencent" 则判为云服务器本地
# 3. 以上都不满足 → SSH 远程模式
# ============================================================
THIS_HOST=$(hostname 2>/dev/null || uname -n 2>/dev/null || echo "")
if [ "$OA_REMOTE" = "local" ]; then
IS_LOCAL_SERVER=true
LABEL="txjp本地运行 · 手动指定)"
elif [ "$OA_REMOTE" = "ssh" ]; then
IS_LOCAL_SERVER=false
SERVER="txjp"
LABEL="txjpSSH 远程 · 手动指定)"
elif echo "$THIS_HOST" | grep -qi "Tencent"; then
IS_LOCAL_SERVER=true
LABEL="txjp本地运行 · 自动检测)"
else
IS_LOCAL_SERVER=false
SERVER="txjp"
LABEL="txjpSSH 远程 · 自动检测)"
fi
# 执行命令:服务器本地直接跑,否则 SSH
run() {
if $IS_LOCAL_SERVER; then
bash -c "$*"
else
ssh $SERVER "$*"
fi
}
BASE="/root/docker"
# 站点 → 目录
site_dir() {
case $1 in
ldap) echo "ldap-ai" ;;
www) echo "www-ai" ;;
cloud) echo "cloud-ai" ;;
token) echo "token-ai" ;;
gitea) echo "gitea-ai" ;;
issue) echo "issue-ai" ;;
assets) echo "assets-ai" ;;
oa) echo "oa-ai" ;;
nginx) echo "nginx-proxy-ai" ;;
sso) echo "sso-ai" ;;
monitor) echo "monitor-ai" ;;
esac
}
# 站点 → 容器名
site_container() {
case $1 in
ldap) echo "lldap" ;;
www) echo "www-ai" ;;
cloud) echo "cloud-ai" ;;
token) echo "token-ai" ;;
gitea) echo "gitea-ai" ;;
issue) echo "issue-ai" ;;
assets) echo "assets-ai" ;;
oa) echo "oa-ai" ;;
nginx) echo "nginx-ai" ;;
sso) echo "authelia" ;;
monitor) echo "monitor-ai" ;;
esac
}
AI_SITES="ldap www cloud token gitea issue assets oa nginx sso monitor"
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; RED='\033[0;31m'; NC='\033[0m'
log() { printf "${GREEN}[✓]${NC} %s\n" "$1"; }
warn() { printf "${YELLOW}[!]${NC} %s\n" "$1"; }
err() { printf "${RED}[✗]${NC} %s\n" "$1"; }
# 检查 Docker 服务是否运行
check_docker() {
if $IS_LOCAL_SERVER; then
# 本地模式:检查本地 Docker
if ! docker info >/dev/null 2>&1; then
err "Docker 服务未启动,请先启动 Docker"
exit 1
fi
else
# SSH 模式:检查远程 Docker
if ! ssh $SERVER "docker info" >/dev/null 2>&1; then
err "远程服务器 Docker 服务未运行"
exit 1
fi
fi
}
expand_sites() {
local result=()
for s in "$@"; do
case $s in
ai|all) for x in $AI_SITES; do result+=("$x"); done ;;
*) result+=("$s") ;;
esac
done
echo "${result[@]}"
}
# ============================================================
# 操作
# ============================================================
remote_start() {
local name=$1 dir="${BASE}/$(site_dir $name)"
local container="$(site_container $name)"
if run "docker ps --format '{{.Names}}' | grep -q '^${container}$'" 2>/dev/null; then
warn "$name 已在运行"
return
fi
log "启动 $name ..."
run "cd $dir && docker compose up -d" 2>/dev/null && log "$name 已启动" || err "$name 启动失败"
}
remote_stop() {
local name=$1 dir="${BASE}/$(site_dir $name)"
local container="$(site_container $name)"
if ! run "docker ps --format '{{.Names}}' | grep -q '^${container}$'" 2>/dev/null; then
warn "$name 未在运行"
return
fi
log "停止 $name ..."
run "cd $dir && docker compose down" 2>/dev/null && log "$name 已停止" || err "$name 停止失败"
}
remote_status() {
local targets=("$@")
echo ""
printf "${CYAN}=========================================${NC}\n"
printf "${CYAN} 站点状态 — ${LABEL}${NC}\n"
printf "${CYAN}=========================================${NC}\n"
echo ""
for name in "${targets[@]}"; do
local container="$(site_container $name)"
local status
status=$(run "docker ps --format '{{.Status}}' -f name=^${container}$" 2>/dev/null || echo "")
if [ -n "$status" ]; then
printf " ${GREEN}${NC} %-8s 运行中 (%s)\n" "$name" "$status"
else
printf " ${RED}${NC} %-8s 未运行\n" "$name"
fi
done
echo ""
}
show_help() {
echo ""
printf "${CYAN}用法:${NC} bash sites-manage-cloud.sh [命令] [站点名...]\n"
echo ""
echo " 当前环境: ${LABEL}"
echo ""
echo " 命令:"
echo " start 启动站点"
echo " stop 停止站点"
echo " restart 重启站点"
echo " status 查看状态"
echo ""
echo " 站点名(可指定多个,空格分隔):"
echo " ldap LLDAP 用户目录"
echo " www tlyq.ai 官网"
echo " cloud 云平台登录页"
echo " token Token 工厂登录页"
echo " gitea Gitea 代码托管"
echo " issue 工单系统"
echo " assets 设备资产管理"
echo " oa OA 统一门户"
echo " nginx 反向代理"
echo " sso Authelia OIDC 认证"
echo " monitor 告警监控中心"
echo " ai 全部 tlyq.ai 站点"
echo ""
echo "示例:"
echo " bash sites-manage-cloud.sh start ai # 启动全部"
echo " bash sites-manage-cloud.sh restart nginx # 重启 nginx"
echo " bash sites-manage-cloud.sh stop oa issue # 停 OA + 工单"
echo " bash sites-manage-cloud.sh status # 查看所有状态"
echo ""
}
# ============================================================
# 入口
# ============================================================
ACTION=${1:-status}
shift || true
TARGETS=($(expand_sites "$@"))
if [ ${#TARGETS[@]} -eq 0 ]; then
TARGETS=($(expand_sites ai))
fi
# 检查 Docker 服务help 命令除外)
if [ "$ACTION" != "help" ] && [ "$ACTION" != "--help" ] && [ "$ACTION" != "-h" ]; then
check_docker
fi
case $ACTION in
start)
for s in "${TARGETS[@]}"; do remote_start "$s"; done
;;
stop)
for s in "${TARGETS[@]}"; do remote_stop "$s"; done
;;
restart)
for s in "${TARGETS[@]}"; do remote_stop "$s"; done
echo ""
log "等待 2 秒后启动..."
sleep 2
for s in "${TARGETS[@]}"; do remote_start "$s"; done
;;
status)
remote_status "${TARGETS[@]}"
;;
help|--help|-h)
show_help
;;
*)
err "未知命令: $ACTION"
show_help
exit 1
;;
esac