Files
web/scripts/lib-yh-compose-deploy.sh
2026-03-27 11:55:00 +08:00

65 lines
2.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.
# shellcheck shell=bash
# 由 pull-and-restart.sh / restart.sh 在定义好 ROOT、compose_cmd、run_sudo 之后 source。
# 依赖:项目根存在 docker-compose.host-nginx.yml与主 compose 合并使用)。
YH_COMPOSE_FILES="-f docker-compose.yml -f docker-compose.host-nginx.yml"
# 宿主机 Nginxsystemd是否在运行不检测 443 归属,以免误判。
host_nginx_online() {
command -v systemctl >/dev/null 2>&1 || return 1
systemctl is-active nginx >/dev/null 2>&1 && return 0
systemctl is-active nginx.service >/dev/null 2>&1 && return 0
return 1
}
# 停止本项目的 Compose 栈(含 yh_nginx 等),不操作宿主机 Nginx 服务。
yh_compose_down() {
if [ ! -f "$ROOT/docker-compose.host-nginx.yml" ]; then
compose_cmd down --remove-orphans 2>/dev/null || true
return 0
fi
compose_cmd $YH_COMPOSE_FILES down --remove-orphans 2>/dev/null || true
}
# 按宿主机 Nginx 是否在线,选择仅起业务容器或起完整栈(含 yh_nginx
yh_compose_up() {
if [ ! -f "$ROOT/docker-compose.host-nginx.yml" ]; then
echo "未找到 docker-compose.host-nginx.yml使用默认 compose up."
compose_cmd up -d --force-recreate
return 0
fi
if host_nginx_online; then
echo "检测到宿主机 Nginx 已运行:不启动容器 yh_nginxmongo/api/web/admin 绑定本机回环(见 nginx/yuheng.host.conf。"
compose_cmd $YH_COMPOSE_FILES up -d --force-recreate mongo api web admin
else
echo "未检测到宿主机 Nginx 运行:启动完整栈(含容器 yh_nginx 终止 TLS。"
compose_cmd $YH_COMPOSE_FILES --profile compose-internal-nginx up -d --force-recreate
fi
}
# 仅当宿主机 Nginx 在线时:从模板写入站点配置并重载(不执行 systemctl start/stop nginx
yh_install_host_nginx_site_conf() {
local domain="${NGINX_DOMAIN:-yuheng.yuxindazhineng.com}"
local tpl="$ROOT/nginx/yuheng.host.conf"
local out="/etc/nginx/conf.d/${domain}.conf"
host_nginx_online || {
echo "宿主机 Nginx 未运行,跳过写入站点配置(由容器 yh_nginx 对外)。"
return 0
}
if [ ! -f "$tpl" ]; then
echo "未找到 $tpl,跳过宿主机站点配置生成。"
return 0
fi
mkdir -p "$ROOT/verify-root"
sed "s|__VERIFY_ROOT__|$ROOT/verify-root|g" "$tpl" | run_sudo tee "$out" >/dev/null
if run_sudo nginx -t 2>/dev/null; then
run_sudo systemctl reload nginx 2>/dev/null && echo "宿主机 Nginx 已重载($out)。" || true
else
echo "警告: 宿主机 nginx -t 失败,请检查 $out" >&2
fi
}