Files
syops/scripts/hxops_menu.sh
T
2026-03-25 20:27:36 +08:00

440 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
RUN_DIR="${ROOT_DIR}/.run"
BACKEND_DIR="${ROOT_DIR}/backend"
FRONTEND_DIR="${ROOT_DIR}/frontend"
BACKEND_PORT=5002
FRONTEND_PORT=8881
DB_PORT=5432
DB_DATA_DIR="/opt/homebrew/var/postgresql@18"
DB_BIN="/opt/homebrew/opt/postgresql@18/bin/postgres"
mkdir -p "${RUN_DIR}"
is_listening() {
local port="$1"
lsof -nP -iTCP:"${port}" -sTCP:LISTEN >/dev/null 2>&1
}
start_project() {
echo "=== 启动 HXOPS 项目 ==="
if is_listening "${DB_PORT}"; then
echo "[数据库] 端口 ${DB_PORT} 已在监听,跳过启动。"
else
echo "[数据库] 正在启动..."
nohup env LC_ALL="en_US.UTF-8" "${DB_BIN}" -D "${DB_DATA_DIR}" > "${RUN_DIR}/postgres.log" 2>&1 &
echo $! > "${RUN_DIR}/postgres.pid"
sleep 2
if is_listening "${DB_PORT}"; then
echo "[数据库] 启动成功,端口 ${DB_PORT}。"
else
echo "[数据库] 启动失败,请查看日志: ${RUN_DIR}/postgres.log"
return 1
fi
fi
if is_listening "${BACKEND_PORT}"; then
echo "[后端] 端口 ${BACKEND_PORT} 已在监听,跳过启动。"
else
echo "[后端] 正在启动..."
(
cd "${BACKEND_DIR}"
source venv/bin/activate
nohup python3 run.py > "${RUN_DIR}/backend.log" 2>&1 &
echo $! > "${RUN_DIR}/backend.pid"
)
sleep 2
if is_listening "${BACKEND_PORT}"; then
echo "[后端] 启动成功,端口 ${BACKEND_PORT}。"
else
echo "[后端] 启动失败,请查看日志: ${RUN_DIR}/backend.log"
return 1
fi
fi
if is_listening "${FRONTEND_PORT}"; then
echo "[前端] 端口 ${FRONTEND_PORT} 已在监听,跳过启动。"
else
echo "[前端] 正在启动..."
(
cd "${FRONTEND_DIR}"
nohup npm run dev > "${RUN_DIR}/frontend.log" 2>&1 &
echo $! > "${RUN_DIR}/frontend.pid"
)
sleep 3
if is_listening "${FRONTEND_PORT}"; then
echo "[前端] 启动成功,端口 ${FRONTEND_PORT}。"
else
echo "[前端] 启动失败,请查看日志: ${RUN_DIR}/frontend.log"
return 1
fi
fi
echo "后端地址: http://127.0.0.1:${BACKEND_PORT}"
echo "前端地址: http://127.0.0.1:${FRONTEND_PORT}"
echo "日志目录: ${RUN_DIR}"
}
kill_by_pid_file() {
local name="$1"
local pid_file="$2"
if [[ ! -f "${pid_file}" ]]; then
echo "[${name}] 未找到 PID 文件,跳过。"
return
fi
local pid
pid="$(cat "${pid_file}")"
if [[ -z "${pid}" ]] || ! kill -0 "${pid}" >/dev/null 2>&1; then
echo "[${name}] PID ${pid:-<empty>} 不存在,清理 PID 文件。"
rm -f "${pid_file}"
return
fi
echo "[${name}] 正在停止进程 PID=${pid}..."
kill "${pid}" >/dev/null 2>&1 || true
for _ in {1..10}; do
if ! kill -0 "${pid}" >/dev/null 2>&1; then
break
fi
sleep 0.5
done
if kill -0 "${pid}" >/dev/null 2>&1; then
echo "[${name}] 进程未退出,执行强制停止 PID=${pid}。"
kill -9 "${pid}" >/dev/null 2>&1 || true
fi
rm -f "${pid_file}"
echo "[${name}] 已停止。"
}
kill_by_port_if_needed() {
local name="$1"
local port="$2"
local pids
pids="$(lsof -ti tcp:${port} 2>/dev/null || true)"
if [[ -z "${pids}" ]]; then
return
fi
echo "[${name}] 检测到端口 ${port} 仍有进程: ${pids},尝试终止..."
for pid in ${pids}; do
kill "${pid}" >/dev/null 2>&1 || true
done
sleep 1
pids="$(lsof -ti tcp:${port} 2>/dev/null || true)"
if [[ -n "${pids}" ]]; then
for pid in ${pids}; do
kill -9 "${pid}" >/dev/null 2>&1 || true
done
fi
}
stop_project() {
echo "=== 关闭 HXOPS 项目 ==="
kill_by_pid_file "后端" "${RUN_DIR}/backend.pid"
kill_by_pid_file "前端" "${RUN_DIR}/frontend.pid"
kill_by_port_if_needed "后端" "${BACKEND_PORT}"
kill_by_port_if_needed "前端" "${FRONTEND_PORT}"
kill_by_pid_file "数据库" "${RUN_DIR}/postgres.pid"
kill_by_port_if_needed "数据库" "${DB_PORT}"
echo "关闭完成。"
}
push_gitee() {
echo "=== 上传代码到 Gitee ==="
if ! git -C "${ROOT_DIR}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "当前目录不是 Git 仓库,已退出。"
return 1
fi
local remote_url
remote_url="$(git -C "${ROOT_DIR}" remote get-url origin 2>/dev/null || true)"
if [[ -z "${remote_url}" ]]; then
echo "未找到 origin 远程仓库,请先配置后再上传。"
return 1
fi
if [[ "${remote_url}" != *"gitee.com"* ]]; then
echo "警告: 当前 origin 不是 Gitee: ${remote_url}"
read -r -p "仍要继续 push 到 origin 吗? (y/N): " force_push
if [[ ! "${force_push}" =~ ^[Yy]$ ]]; then
echo "已取消。"
return 0
fi
fi
local branch
branch="$(git -C "${ROOT_DIR}" rev-parse --abbrev-ref HEAD)"
if [[ -n "$(git -C "${ROOT_DIR}" status --porcelain)" ]]; then
git -C "${ROOT_DIR}" add -A
read -r -p "请输入提交说明(留空使用默认): " commit_msg
if [[ -z "${commit_msg:-}" ]]; then
commit_msg="chore: update $(date '+%Y-%m-%d %H:%M:%S')"
fi
if [[ -n "$(git -C "${ROOT_DIR}" diff --cached --name-only)" ]]; then
git -C "${ROOT_DIR}" commit -m "${commit_msg}"
fi
else
echo "工作区无变更,将直接 push 当前分支。"
fi
echo "推送分支: ${branch} -> origin/${branch}"
git -C "${ROOT_DIR}" push origin "${branch}"
echo "上传完成。"
}
show_status() {
echo "=== 当前状态 ==="
if is_listening "${DB_PORT}"; then
echo "数据库: 运行中 (端口 ${DB_PORT})"
else
echo "数据库: 未运行"
fi
if is_listening "${BACKEND_PORT}"; then
echo "后端: 运行中 (端口 ${BACKEND_PORT})"
else
echo "后端: 未运行"
fi
if is_listening "${FRONTEND_PORT}"; then
echo "前端: 运行中 (端口 ${FRONTEND_PORT})"
else
echo "前端: 未运行"
fi
}
show_dev_open_ports() {
echo "=== 开发服务端口扫描(排除 macOS 常规系统服务) ==="
echo "说明: 仅显示当前用户 ${USER} 打开的 TCP 监听端口。"
echo ""
printf "%-20s %-8s %-24s\n" "进程名" "PID" "监听地址"
printf "%-20s %-8s %-24s\n" "--------------------" "--------" "------------------------"
local output
output="$(
lsof -nP -iTCP -sTCP:LISTEN 2>/dev/null \
| awk -v user="${USER}" '
function ignored(cmd) {
return (cmd ~ /^(ControlCe|rapportd|cfprefsd|trustd|sharingd|identitys)$/)
}
NR > 1 && $3 == user {
if (ignored($1)) next
endpoint = $(NF-1)
key = $1 "|" $2 "|" endpoint
if (!seen[key]++) {
printf "%-20s %-8s %-24s\n", $1, $2, endpoint
}
}
'
)"
if [[ -z "${output}" ]]; then
echo "未发现当前用户启动的监听端口。"
return
fi
echo "${output}"
}
show_non_system_open_ports() {
echo "=== 非系统端口扫描(全量监听端口 - macOS 系统服务) ==="
echo "说明: 扫描所有 TCP 监听端口,过滤系统进程后按服务分组展示。"
local rows
rows="$(
lsof -nP -iTCP -sTCP:LISTEN 2>/dev/null \
| awk '
NR > 1 {
cmd = $1
pid = $2
user = $3
endpoint = $(NF-1)
key = pid "|" endpoint
if (!seen[key]++) {
print pid "\t" user "\t" cmd "\t" endpoint
}
}
'
)"
if [[ -z "${rows}" ]]; then
echo "未发现监听端口。"
return
fi
local tmp_dir
tmp_dir="$(mktemp -d)"
trap 'rm -rf "${tmp_dir}"' RETURN
local f_openclaw="${tmp_dir}/openclaw.txt"
local f_hxops="${tmp_dir}/hxops.txt"
local f_central="${tmp_dir}/central.txt"
local f_erp="${tmp_dir}/erp.txt"
local f_pg="${tmp_dir}/pg.txt"
local f_other="${tmp_dir}/other.txt"
touch "${f_openclaw}" "${f_hxops}" "${f_central}" "${f_erp}" "${f_pg}" "${f_other}"
local matched=0
while IFS=$'\t' read -r pid user cmd endpoint; do
[[ -z "${pid}" ]] && continue
local full_cmd bin_path
full_cmd="$(ps -p "${pid}" -o command= 2>/dev/null || true)"
bin_path="${full_cmd%% *}"
# 过滤典型系统账号与系统二进制路径
if [[ "${user}" == _* ]]; then
continue
fi
if [[ "${bin_path}" == /System/* || "${bin_path}" == /usr/libexec/* ]]; then
continue
fi
# 过滤常见 macOS 系统守护进程
case "${cmd}" in
launchd|syslogd|mDNSResponder|distnoted|notifyd|powerd|configd|kernelmanagerd|airportd|\
cfprefsd|sharingd|trustd|identityservicesd|ControlCe|rapportd|runningboardd|\
UserEventAgent|logd|securityd|opendirectoryd)
continue
;;
esac
local port group target_file
port="${endpoint##*:}"
if [[ "${port}" == "18789" || "${cmd}" == "DingTalk" ]]; then
group="openclaw"
target_file="${f_openclaw}"
elif [[ "${port}" == "8881" ]]; then
group="hxops"
target_file="${f_hxops}"
elif [[ "${port}" == "8891" ]]; then
group="Central-Auth-Core"
target_file="${f_central}"
elif [[ "${port}" == "8892" ]]; then
group="ERP-Data-Agent"
target_file="${f_erp}"
elif [[ "${port}" == "5432" ]]; then
group="postgresql"
target_file="${f_pg}"
else
group="其它服务"
target_file="${f_other}"
fi
printf "%-20s %-10s %-8s %-24s %-18s\n" "${cmd}" "${user}" "${pid}" "${endpoint}" "${port}" >> "${target_file}"
matched=1
done <<< "${rows}"
if [[ "${matched}" -eq 0 ]]; then
echo "未发现非系统监听端口。"
return
fi
print_group() {
local title="$1"
local file="$2"
[[ ! -s "${file}" ]] && return
echo ""
echo "[$title]"
printf "%-20s %-10s %-8s %-24s %-18s\n" "进程名" "用户" "PID" "监听地址" "端口"
printf "%-20s %-10s %-8s %-24s %-18s\n" "--------------------" "----------" "--------" "------------------------" "------------------"
cat "${file}"
}
print_group "openclaw" "${f_openclaw}"
print_group "hxops" "${f_hxops}"
print_group "Central-Auth-Core" "${f_central}"
print_group "ERP-Data-Agent" "${f_erp}"
print_group "postgresql" "${f_pg}"
print_group "其它服务" "${f_other}"
}
show_help() {
cat <<'HELP'
可输入的指令与结果:
start 或 1 -> 启动项目(数据库+后端+前端)
stop 或 2 -> 关闭项目(后端+前端+数据库)
push 或 3 -> 提交并上传到 Gitee(origin)
status或 4 -> 查看服务运行状态
ports 或 5 -> 扫描当前用户开启的开发服务端口
allports或 6 -> 扫描所有非系统监听端口(应用/自装服务)
help -> 再次显示本帮助
exit 或 0 -> 退出脚本
HELP
}
confirm_and_run() {
local label="$1"
local fn_name="$2"
read -r -p "确认执行【${label}】? (y/N): " confirm
if [[ "${confirm}" =~ ^[Yy]$ ]]; then
"${fn_name}"
else
echo "已取消【${label}】。"
fi
}
main_loop() {
echo "===== HXOPS 单脚本控制台 ====="
show_help
while true; do
echo ""
read -r -p "请输入指令: " cmd
case "${cmd}" in
1|start)
confirm_and_run "启动项目" start_project
;;
2|stop)
confirm_and_run "关闭项目" stop_project
;;
3|push)
confirm_and_run "上传到 Gitee" push_gitee
;;
4|status)
show_status
;;
5|ports|scan)
show_dev_open_ports
;;
6|allports)
show_non_system_open_ports
;;
help)
show_help
;;
0|exit|quit)
echo "已退出。"
exit 0
;;
"")
echo "请输入有效指令,输入 help 可查看可用命令。"
;;
*)
echo "未知指令: ${cmd},输入 help 可查看可用命令。"
;;
esac
done
}
main_loop