268 lines
7.5 KiB
Bash
Executable File
268 lines
7.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||
cd "${SCRIPT_DIR}"
|
||
|
||
PROJECT_SERVICES=("hxops-backend" "hxops-frontend")
|
||
PUBLIC_SERVICES=("shared-postgres" "shared-redis")
|
||
|
||
resolve_compose_cmd() {
|
||
if command -v docker-compose >/dev/null 2>&1; then
|
||
COMPOSE_CMD="docker-compose"
|
||
elif docker compose version >/dev/null 2>&1; then
|
||
COMPOSE_CMD="docker compose"
|
||
else
|
||
echo "❌ 未找到 docker-compose 或 docker compose 命令"
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
container_exists() {
|
||
docker ps -a --format '{{.Names}}' | grep -Fxq "$1"
|
||
}
|
||
|
||
container_running() {
|
||
docker ps --format '{{.Names}}' | grep -Fxq "$1"
|
||
}
|
||
|
||
get_host_ip() {
|
||
local ip=""
|
||
ip="$( (ip route get 1.1.1.1 2>/dev/null || true) | awk '{for (i = 1; i <= NF; i++) if ($i == "src") {print $(i+1); exit}}' )"
|
||
if [[ -z "${ip}" ]]; then
|
||
ip="$( (hostname -I 2>/dev/null || true) | awk '{print $1}' )"
|
||
fi
|
||
if [[ -z "${ip}" ]]; then
|
||
ip="127.0.0.1"
|
||
fi
|
||
echo "${ip}"
|
||
}
|
||
|
||
get_mapped_port() {
|
||
local container="$1"
|
||
local cport="$2"
|
||
local raw
|
||
raw="$(docker port "${container}" "${cport}" 2>/dev/null || true)"
|
||
echo "${raw}" | head -n 1 | awk -F: '{print $NF}'
|
||
}
|
||
|
||
tcp_port_open() {
|
||
local host="$1"
|
||
local port="$2"
|
||
|
||
if command -v timeout >/dev/null 2>&1; then
|
||
timeout 1 bash -c ">/dev/tcp/${host}/${port}" >/dev/null 2>&1
|
||
else
|
||
bash -c ">/dev/tcp/${host}/${port}" >/dev/null 2>&1
|
||
fi
|
||
}
|
||
|
||
http_probe_code() {
|
||
local url="$1"
|
||
if ! command -v curl >/dev/null 2>&1; then
|
||
echo "curl-missing"
|
||
return 0
|
||
fi
|
||
|
||
local code
|
||
code="$(curl -sS -m 3 -o /dev/null -w '%{http_code}' "${url}" 2>/dev/null || true)"
|
||
if [[ -z "${code}" ]]; then
|
||
code="failed"
|
||
fi
|
||
echo "${code}"
|
||
}
|
||
|
||
start_public_services() {
|
||
echo "== 启动公共服务(数据库/Redis)=="
|
||
for name in "${PUBLIC_SERVICES[@]}"; do
|
||
if container_exists "${name}"; then
|
||
if container_running "${name}"; then
|
||
echo " ${name}: 已运行"
|
||
else
|
||
echo " ${name}: 启动中"
|
||
docker start "${name}" >/dev/null
|
||
echo " ${name}: 已启动"
|
||
fi
|
||
else
|
||
echo " ${name}: 未找到容器(跳过)"
|
||
fi
|
||
done
|
||
}
|
||
|
||
start_all_services() {
|
||
resolve_compose_cmd
|
||
start_public_services
|
||
echo "== 启动项目服务(前后端)=="
|
||
${COMPOSE_CMD} up -d
|
||
echo "✅ 项目服务已启动"
|
||
}
|
||
|
||
stop_project_services() {
|
||
resolve_compose_cmd
|
||
echo "== 停止项目前后端服务(不影响数据库/Redis)=="
|
||
${COMPOSE_CMD} stop "${PROJECT_SERVICES[@]}"
|
||
echo "✅ 项目前后端已停止"
|
||
}
|
||
|
||
run_deploy() {
|
||
if [[ -x "./deploy.sh" ]]; then
|
||
./deploy.sh
|
||
elif [[ -f "./deploy.sh" ]]; then
|
||
bash ./deploy.sh
|
||
else
|
||
echo "❌ 未找到 deploy.sh"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
show_public_service_status() {
|
||
echo "== 公共服务状态 =="
|
||
for name in "${PUBLIC_SERVICES[@]}"; do
|
||
if container_exists "${name}"; then
|
||
status="$(docker inspect -f '{{.State.Status}}' "${name}")"
|
||
health="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "${name}")"
|
||
echo " ${name}: status=${status}, health=${health}"
|
||
else
|
||
echo " ${name}: not-found"
|
||
fi
|
||
done
|
||
}
|
||
|
||
check_dependency_service() {
|
||
local label="$1"
|
||
local name="$2"
|
||
local cport="$3"
|
||
|
||
if ! container_exists "${name}"; then
|
||
echo " ${label}(${name}): not-found"
|
||
return 0
|
||
fi
|
||
|
||
local status health mapped_port tcp_state
|
||
status="$(docker inspect -f '{{.State.Status}}' "${name}")"
|
||
health="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "${name}")"
|
||
mapped_port="$(get_mapped_port "${name}" "${cport}")"
|
||
|
||
if [[ -n "${mapped_port}" ]]; then
|
||
if tcp_port_open "127.0.0.1" "${mapped_port}"; then
|
||
tcp_state="ok"
|
||
else
|
||
tcp_state="failed"
|
||
fi
|
||
echo " ${label}(${name}): status=${status}, health=${health}, host_port=${mapped_port}, tcp=${tcp_state}"
|
||
else
|
||
echo " ${label}(${name}): status=${status}, health=${health}, host_port=none(仅容器网络)"
|
||
fi
|
||
}
|
||
|
||
show_dependency_checks() {
|
||
echo "== 依赖服务检查 =="
|
||
check_dependency_service "数据库" "shared-postgres" "5432/tcp"
|
||
check_dependency_service "Redis" "shared-redis" "6379/tcp"
|
||
}
|
||
|
||
check_nginx_mapping() {
|
||
local label="$1"
|
||
local name="$2"
|
||
local cport="$3"
|
||
|
||
if ! container_exists "${name}"; then
|
||
echo " ${label}(${name}:${cport}): not-found"
|
||
return 0
|
||
fi
|
||
|
||
local mapped_port
|
||
mapped_port="$(get_mapped_port "${name}" "${cport}")"
|
||
if [[ -z "${mapped_port}" ]]; then
|
||
echo " ${label}(${name}:${cport}): no-host-mapping"
|
||
return 0
|
||
fi
|
||
|
||
if tcp_port_open "127.0.0.1" "${mapped_port}"; then
|
||
echo " ${label}(${name}:${cport}): host_port=${mapped_port}, tcp=ok"
|
||
else
|
||
echo " ${label}(${name}:${cport}): host_port=${mapped_port}, tcp=failed"
|
||
fi
|
||
}
|
||
|
||
show_nginx_port_checks() {
|
||
echo "== Nginx 对外端口检查 =="
|
||
check_nginx_mapping "网关入口" "nginx-gateway" "8881/tcp"
|
||
check_nginx_mapping "前端直连" "hxops-frontend" "80/tcp"
|
||
}
|
||
|
||
show_entry_urls() {
|
||
local host_ip gateway_port frontend_port backend_port
|
||
host_ip="$(get_host_ip)"
|
||
gateway_port="$(get_mapped_port "nginx-gateway" "8881/tcp")"
|
||
frontend_port="$(get_mapped_port "hxops-frontend" "80/tcp")"
|
||
backend_port="$(get_mapped_port "hxops-backend" "5052/tcp")"
|
||
|
||
echo "== 网站入口地址 =="
|
||
|
||
if [[ -n "${gateway_port}" ]]; then
|
||
local gateway_url
|
||
gateway_url="http://${host_ip}:${gateway_port}/"
|
||
echo " 主入口(网关): ${gateway_url} [HTTP $(http_probe_code "${gateway_url}")]"
|
||
else
|
||
echo " 主入口(网关): nginx-gateway:8881 未映射到宿主机"
|
||
fi
|
||
|
||
if [[ -n "${frontend_port}" ]]; then
|
||
local frontend_url
|
||
frontend_url="http://${host_ip}:${frontend_port}/"
|
||
echo " 前端直连: ${frontend_url} [HTTP $(http_probe_code "${frontend_url}")]"
|
||
else
|
||
echo " 前端直连: hxops-frontend:80 未映射到宿主机"
|
||
fi
|
||
|
||
if [[ -n "${backend_port}" ]]; then
|
||
local backend_url
|
||
backend_url="http://${host_ip}:${backend_port}/api/agents"
|
||
echo " 后端API示例: ${backend_url} [HTTP $(http_probe_code "${backend_url}")]"
|
||
else
|
||
echo " 后端API示例: hxops-backend:5052 未映射到宿主机"
|
||
fi
|
||
}
|
||
|
||
check_service_status() {
|
||
resolve_compose_cmd
|
||
echo "== 项目服务状态 =="
|
||
${COMPOSE_CMD} ps
|
||
show_public_service_status
|
||
show_dependency_checks
|
||
show_nginx_port_checks
|
||
show_entry_urls
|
||
}
|
||
|
||
show_help() {
|
||
cat <<'EOF'
|
||
可用命令:
|
||
1 启动项目所有服务(含公共服务:数据库/Redis)
|
||
2 停止项目前后端服务(不含数据库与Redis)
|
||
3 执行部署脚本 deploy.sh
|
||
4 检查项目/依赖服务状态、Nginx端口与网站入口
|
||
9 显示命令帮助
|
||
0 退出脚本
|
||
EOF
|
||
}
|
||
|
||
main_loop() {
|
||
show_help
|
||
while true; do
|
||
echo
|
||
read -r -p "请输入选项 [1/2/3/4/9/0]: " choice
|
||
case "${choice}" in
|
||
1) start_all_services ;;
|
||
2) stop_project_services ;;
|
||
3) run_deploy ;;
|
||
4) check_service_status ;;
|
||
9) show_help ;;
|
||
0) echo "退出"; exit 0 ;;
|
||
*) echo "无效选项:${choice}" ;;
|
||
esac
|
||
done
|
||
}
|
||
|
||
main_loop
|