From e06d5f7e01b681bb6fe59430d544344c4c20f363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9A=E9=80=9A?= Date: Wed, 25 Mar 2026 20:27:36 +0800 Subject: [PATCH] =?UTF-8?q?032=E8=84=9A=E6=9C=AC=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .run/backend.log | 174 +++++++++++++++++ .run/frontend.log | 9 + scripts/hxops_menu.sh | 439 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 622 insertions(+) create mode 100644 .run/backend.log create mode 100644 .run/frontend.log create mode 100755 scripts/hxops_menu.sh diff --git a/.run/backend.log b/.run/backend.log new file mode 100644 index 00000000..34b1ec2f --- /dev/null +++ b/.run/backend.log @@ -0,0 +1,174 @@ + * Serving Flask app 'app' + * Debug mode: on +WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. + * Running on all addresses (0.0.0.0) + * Running on http://127.0.0.1:5002 + * Running on http://192.168.205.95:5002 +Press CTRL+C to quit + * Restarting with stat + * Debugger is active! + * Debugger PIN: 679-745-431 +127.0.0.1 - - [25/Mar/2026 13:35:38] "GET /api/user/announcements HTTP/1.1" 500 - +127.0.0.1 - - [25/Mar/2026 13:35:38] "GET /api/agents/public HTTP/1.1" 500 - +Traceback (most recent call last): + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 143, in __init__ + self._dbapi_connection = engine.raw_connection() + ^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3317, in raw_connection + return self.pool.connect() + ^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 448, in connect + return _ConnectionFairy._checkout(self) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 1272, in _checkout + fairy = _ConnectionRecord.checkout(pool) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 712, in checkout + rec = pool._do_get() + ^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 177, in _do_get + with util.safe_reraise(): + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 121, in __exit__ + raise exc_value.with_traceback(exc_tb) + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 175, in _do_get + return self._create_connection() + ^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 389, in _create_connection + return _ConnectionRecord(self) + ^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 674, in __init__ + self.__connect() + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 900, in __connect + with util.safe_reraise(): + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 121, in __exit__ + raise exc_value.with_traceback(exc_tb) + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 896, in __connect + self.dbapi_connection = connection = pool._invoke_creator(self) + ^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 667, in connect + return dialect.connect(*cargs_tup, **cparams) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 630, in connect + return self.loaded_dbapi.connect(*cargs, **cparams) # type: ignore[no-any-return] # NOQA: E501 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/psycopg2/__init__.py", line 122, in connect + conn = _connect(dsn, connection_factory=connection_factory, **kwasync) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused + Is the server running on that host and accepting TCP/IP connections? +connection to server at "localhost" (::1), port 5432 failed: Connection refused + Is the server running on that host and accepting TCP/IP connections? + + +The above exception was the direct cause of the following exception: + +Traceback (most recent call last): + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/flask/app.py", line 1536, in __call__ + return self.wsgi_app(environ, start_response) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/flask/app.py", line 1514, in wsgi_app + response = self.handle_exception(e) + ^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/flask_cors/extension.py", line 176, in wrapped_function + return cors_after_request(app.make_response(f(*args, **kwargs))) + ^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/flask/app.py", line 1511, in wsgi_app + response = self.full_dispatch_request() + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/flask/app.py", line 919, in full_dispatch_request + rv = self.handle_user_exception(e) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/flask_cors/extension.py", line 176, in wrapped_function + return cors_after_request(app.make_response(f(*args, **kwargs))) + ^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/flask/app.py", line 917, in full_dispatch_request + rv = self.dispatch_request() + ^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/flask/app.py", line 902, in dispatch_request + return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/app/api/agents.py", line 40, in list_public_agents + agents = Agent.query.filter_by(is_active=True).order_by(Agent.id.asc()).all() + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/orm/query.py", line 2704, in all + return self._iter().all() # type: ignore + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/orm/query.py", line 2857, in _iter + result: Union[ScalarResult[_T], Result[_T]] = self.session.execute( + ^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2351, in execute + return self._execute_internal( + ^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2239, in _execute_internal + conn = self._connection_for_bind(bind) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2108, in _connection_for_bind + return trans._connection_for_bind(engine, execution_options) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "", line 2, in _connection_for_bind + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/orm/state_changes.py", line 137, in _go + ret_value = fn(self, *arg, **kw) + ^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 1187, in _connection_for_bind + conn = bind.connect() + ^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3293, in connect + return self._connection_cls(self) + ^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 145, in __init__ + Connection._handle_dbapi_exception_noconnection( + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2448, in _handle_dbapi_exception_noconnection + raise sqlalchemy_exception.with_traceback(exc_info[2]) from e + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 143, in __init__ + self._dbapi_connection = engine.raw_connection() + ^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 3317, in raw_connection + return self.pool.connect() + ^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 448, in connect + return _ConnectionFairy._checkout(self) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 1272, in _checkout + fairy = _ConnectionRecord.checkout(pool) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 712, in checkout + rec = pool._do_get() + ^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 177, in _do_get + with util.safe_reraise(): + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 121, in __exit__ + raise exc_value.with_traceback(exc_tb) + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 175, in _do_get + return self._create_connection() + ^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 389, in _create_connection + return _ConnectionRecord(self) + ^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 674, in __init__ + self.__connect() + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 900, in __connect + with util.safe_reraise(): + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 121, in __exit__ + raise exc_value.with_traceback(exc_tb) + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/pool/base.py", line 896, in __connect + self.dbapi_connection = connection = pool._invoke_creator(self) + ^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 667, in connect + return dialect.connect(*cargs_tup, **cparams) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 630, in connect + return self.loaded_dbapi.connect(*cargs, **cparams) # type: ignore[no-any-return] # NOQA: E501 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/Users/teemo/Documents/dev/AI/hxops/backend/venv/lib/python3.12/site-packages/psycopg2/__init__.py", line 122, in connect + conn = _connect(dsn, connection_factory=connection_factory, **kwasync) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused + Is the server running on that host and accepting TCP/IP connections? +connection to server at "localhost" (::1), port 5432 failed: Connection refused + Is the server running on that host and accepting TCP/IP connections? + +(Background on this error at: https://sqlalche.me/e/20/e3q8) +127.0.0.1 - - [25/Mar/2026 13:36:00] "POST /api/user/login HTTP/1.1" 503 - +127.0.0.1 - - [25/Mar/2026 13:36:08] "POST /api/user/login HTTP/1.1" 503 - +/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/multiprocessing/resource_tracker.py:254: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown + warnings.warn('resource_tracker: There appear to be %d ' diff --git a/.run/frontend.log b/.run/frontend.log new file mode 100644 index 00000000..cc800d05 --- /dev/null +++ b/.run/frontend.log @@ -0,0 +1,9 @@ + +> frontend@0.0.0 dev +> vite + + + VITE v8.0.0-beta.15 ready in 191 ms + + ➜ Local: http://localhost:8881/ + ➜ Network: use --host to expose diff --git a/scripts/hxops_menu.sh b/scripts/hxops_menu.sh new file mode 100755 index 00000000..9735232e --- /dev/null +++ b/scripts/hxops_menu.sh @@ -0,0 +1,439 @@ +#!/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:-} 不存在,清理 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