From dcb0d3c365b210e5b8d92780363a1f68031e868f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Sat, 25 Apr 2026 15:06:11 +0800 Subject: [PATCH] chore(moicen): add env/bootstrap scripts for split AuthCore layout Make moicen env copying compatible with sibling AuthCore repo and add start/stop/build helper scripts for huiwings stack bring-up on moicen hosts. Made-with: Cursor --- cp_envs_moicen.sh | 5 +- cp_envs_moicen_huiwings_databases.sh | 36 +++++++++++ ...n_cargo_build_release_via_alchemy_socks.sh | 42 +++++++++++++ scripts/moicen_start_huiwings_stack.sh | 61 +++++++++++++++++++ scripts/moicen_stop_huiwings_stack.sh | 7 +++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100755 cp_envs_moicen_huiwings_databases.sh create mode 100755 scripts/moicen_cargo_build_release_via_alchemy_socks.sh create mode 100755 scripts/moicen_start_huiwings_stack.sh create mode 100755 scripts/moicen_stop_huiwings_stack.sh diff --git a/cp_envs_moicen.sh b/cp_envs_moicen.sh index 7a28e12..bb272e9 100755 --- a/cp_envs_moicen.sh +++ b/cp_envs_moicen.sh @@ -1,8 +1,9 @@ #!/bin/sh set -x -cp envs/moicen/htyuc.env htyuc/.env -cp envs/moicen/htyuc.env htyuc_models/.env +# htyuc 在 AuthCore 时 huike 根下可无此目录 +[ -d htyuc ] && cp envs/moicen/htyuc.env htyuc/.env +[ -d htyuc_models ] && cp envs/moicen/htyuc.env htyuc_models/.env cp envs/moicen/htyws.env htyws/.env cp envs/moicen/htyws.env htyws_models/.env cp envs/moicen/htykc.env htykc/.env diff --git a/cp_envs_moicen_huiwings_databases.sh b/cp_envs_moicen_huiwings_databases.sh new file mode 100755 index 0000000..57938ed --- /dev/null +++ b/cp_envs_moicen_huiwings_databases.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env sh +# 在 **moicen 本机** 使用:连接 localhost 的 *_huiwings(与 alchemy 上库名、与你导入的数据一致), +# 并沿用 moicen 的对外域名(*.moicen.com),代替 cp_envs_moicen.sh 里默认的 *_moicen 库名。 +# 在 monorepo 的 **huike-back** 目录下执行;若同级的 ../AuthCore 存在,则一并写 UC。 +set -e +HUIHUI="envs/huiwings" +# 与 envs/moicen 一致,仅数据库部分改为 huiwings +_transform() { + sed -e 's/\.huiwings\.cn/.moicen.com/g' "$1" +} +HIKE="." +[ -d "$HUIHUI" ] || { echo "在 huike-back 根目录执行"; exit 1; } + +if [ -d "$HIKE/htyws" ]; then + _transform "$HUIHUI/htyws.env" > "$HIKE/htyws/.env" + _transform "$HUIHUI/htyws.env" > "$HIKE/htyws_models/.env" +fi +if [ -d "$HIKE/htykc" ]; then + _transform "$HUIHUI/htykc.env" > "$HIKE/htykc/.env" + _transform "$HUIHUI/htykc.env" > "$HIKE/htykc_models/.env" +fi +if [ -d "$HIKE/htyts" ]; then + _transform "$HUIHUI/htyts.env" > "$HIKE/htyts/.env" + _transform "$HUIHUI/htyts.env" > "$HIKE/htyts_models/.env" +fi +if [ -d "$HIKE/htyproc" ] && [ -f "$HUIHUI/htyproc.env" ]; then + _transform "$HUIHUI/htyproc.env" > "$HIKE/htyproc/.env" +fi +AUTH=../AuthCore +if [ -d "$AUTH/htyuc" ]; then + _transform "$HUIHUI/htyuc.env" > "$AUTH/htyuc/.env" + _transform "$HUIHUI/htyuc.env" > "$AUTH/htyuc_models/.env" + # 若从 AuthCore 根 `cargo run -p htyuc`(cwd=AuthCore),dotenv 读此文件 + _transform "$HUIHUI/htyuc.env" > "$AUTH/.env" +fi +echo "OK. Wrote .env (postgres *_huiwings + *.moicen.com) under huike-back and ../AuthCore if present." diff --git a/scripts/moicen_cargo_build_release_via_alchemy_socks.sh b/scripts/moicen_cargo_build_release_via_alchemy_socks.sh new file mode 100755 index 0000000..99617c7 --- /dev/null +++ b/scripts/moicen_cargo_build_release_via_alchemy_socks.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# 在 **moicen 本机**:临时建立到 alchemy 的 SSH 本地转发,经 socks5h://127.0.0.1:1080 出网,完成 +# AuthCore/htyuc 与 huike-back 中 htyws、htykc 的 `cargo build --release`;结束后关闭隧道,不污染当前 shell 的代理变量。 +# 与 `plan_skills/moicen/moicen-tunnel-alchemy-v2ray-1080-proxy.md` §4 同思路。 +# 用法:在 huike-back 根 +# MOICEN_REPO_ROOT=~/works/huiwing-migration ./scripts/moicen_cargo_build_release_via_alchemy_socks.sh +# 可改 ALCH_SSH=alchemy-studio.cn +set -euo pipefail + +ALCH_SSH="${ALCH_SSH:-alchemy-studio.cn}" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +HUIKE_DEFAULT="$(cd "$SCRIPT_DIR/.." && pwd)" +REPO_ROOT_DEFAULT="$(cd "$SCRIPT_DIR/../.." && pwd)" +MOICEN_REPO_ROOT="${MOICEN_REPO_ROOT:-$REPO_ROOT_DEFAULT}" +AUTH="${MOICEN_AUTH_ROOT:-$MOICEN_REPO_ROOT/AuthCore}" +HUIKE="${MOICEN_HUIKE_ROOT:-$HUIKE_DEFAULT}" + +[ -f "$AUTH/Cargo.toml" ] && [ -f "$HUIKE/Cargo.toml" ] || { echo "检查 MOICEN_REPO_ROOT / 路径: AUTH=$AUTH HUIKE=$HUIKE" >&2; exit 1; } +command -v ssh >/dev/null + +cleanup() { kill "$TUNPID" 2>/dev/null || true; } +# shellcheck disable=SC2016 +if ss -lntp 2>/dev/null | grep -q ':1080'; then + echo "本机 1080 已被占用;请先关闭已有 SSH 转发或改用他口(并同步 ALL_PROXY 文档)" >&2 + exit 1 +fi +ssh -N -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -o ConnectTimeout=15 \ + -L 127.0.0.1:1080:127.0.0.1:1080 "weli@$ALCH_SSH" & +TUNPID=$! +sleep 3 +trap cleanup EXIT INT TERM + +export ALL_PROXY=socks5h://127.0.0.1:1080 +export https_proxy=$ALL_PROXY +export http_proxy=$ALL_PROXY +# shellcheck disable=SC1090 +. "$HOME/.cargo/env" +export CARGO_HTTP_MULTIPLEXING=true + +( cd "$AUTH" && cargo build --release -p htyuc ) +( cd "$HUIKE" && cargo build --release -p htyws -p htykc ) +echo "OK. release 构建完成。隧道本脚本退出时关闭。启动 HTTP 服务时请使用 scripts/moicen_start_huiwings_stack.sh(已去代理)。" diff --git a/scripts/moicen_start_huiwings_stack.sh b/scripts/moicen_start_huiwings_stack.sh new file mode 100755 index 0000000..fafeb8a --- /dev/null +++ b/scripts/moicen_start_huiwings_stack.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# 在**新** moicen 裸机:与旧机 alchemy 相同布局(/mnt/.../AuthCore + huike-back), +# 在已「cp_envs + pg *_huiwings 数据 + redis 已起 + 已 cargo build --release 可选」后,nohup 起 UC/WS/KC(与现网进程方式一致:在各 crate 子目录 cargo run 以读 .env)。 +# 在 huike-back 根以: ./scripts/moicen_start_huiwings_stack.sh +# 可设 MOICEN_HUIKE_ROOT / MOICEN_AUTH_ROOT 为绝对路径(默认=本脚本的 ../ 与 ../AuthCore)。 +set -euo pipefail +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +HUIKE_DEFAULT="$(cd "$SCRIPT_DIR/.." && pwd)" +# scripts/.. = huike-back, ../.. = monorepo 根 +REPO_ROOT_DEFAULT="$(cd "$SCRIPT_DIR/../.." && pwd)" +AUTH_DEFAULT="$REPO_ROOT_DEFAULT/AuthCore" + +HUIKE="${MOICEN_HUIKE_ROOT:-$HUIKE_DEFAULT}" +if [ -d "$AUTH_DEFAULT/htyuc" ]; then + AUTH="${MOICEN_AUTH_ROOT:-$AUTH_DEFAULT}" +else + AUTH="${MOICEN_AUTH_ROOT:-$(cd "$HUIKE/../AuthCore" 2>/dev/null && pwd || true)}" +fi + +[ -d "$HUIKE/htyws" ] && [ -d "$AUTH/htyuc" ] || { echo "需要 monorepo:\$HUIKE= $HUIKE 、\$AUTH= $AUTH"; exit 1; } +# 默认不覆盖 .env;要铺 envs 时显式设其一:MOICEN_ENV_COPY=huiwings | moicen +if [ "${MOICEN_ENV_COPY:-}" = huiwings ] && [ -x "$HUIKE/cp_envs_moicen_huiwings_databases.sh" ]; then + "$HUIKE/cp_envs_moicen_huiwings_databases.sh" +elif [ "${MOICEN_ENV_COPY:-}" = moicen ] && [ -x "$HUIKE/cp_envs_moicen.sh" ]; then + ( cd "$HUIKE" && sh ./cp_envs_moicen.sh ) + for _f in htyuc htyuc_models; do + if [ -d "$AUTH/$_f" ] && [ -f "$HUIKE/envs/moicen/htyuc.env" ]; then + cp "$HUIKE/envs/moicen/htyuc.env" "$AUTH/$_f/.env" + fi + done +fi + +if ! command -v redis-cli >/dev/null 2>&1 || ! redis-cli -h 127.0.0.1 -p 6379 ping 2>/dev/null | grep -q PONG; then + echo "Redis 未响应 127.0.0.1:6379,请先启动 redis。" >&2 + exit 1 +fi + +REL="" +# 有任一 release 可执行则全程加 --release(与旧机用 debug 时可改 REL 或先不 build --release) +if [ -f "$AUTH/target/release/htyuc" ] 2>/dev/null || [ -f "$HUIKE/target/release/htyws" ] 2>/dev/null; then + REL=--release +fi + +# 出网时若设过 ALL_PROXY 用于 cargo 下载,**启动 HTTP 时勿继承**(本服务连本机 PG/Redis,走 SOCKS 会异常或慢) +CARGO_NO_PROXY=(env -u ALL_PROXY -u all_proxy -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY) +# shellcheck disable=SC1090 +_SRC_ENV='[ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"' + +# AuthCore: CWD=htyuc 与现网 htyuc 进程一致 +( cd "$AUTH/htyuc" && nohup "${CARGO_NO_PROXY[@]}" bash -c "$_SRC_ENV; exec cargo run $REL" >>"$AUTH/htyuc/htyuc.nohup.log" 2>&1 ) & + +( cd "$HUIKE/htyws" && nohup "${CARGO_NO_PROXY[@]}" bash -c "$_SRC_ENV; exec cargo run $REL" >>"$HUIKE/htyws/htyws.nohup.log" 2>&1 ) & + +( cd "$HUIKE/htykc" && nohup "${CARGO_NO_PROXY[@]}" bash -c "$_SRC_ENV; exec cargo run $REL" >>"$HUIKE/htykc/htykc.nohup.log" 2>&1 ) & + +if [ -d "$HUIKE/htyts" ] && { [ "${MOICEN_START_HTYTS:-0}" = 1 ] || [ "${MOICEN_START_HTYTS:-0}" = yes ]; }; then + ( cd "$HUIKE/htyts" && nohup "${CARGO_NO_PROXY[@]}" bash -c "$_SRC_ENV; exec cargo run $REL" >>"$HUIKE/htyts/htyts.nohup.log" 2>&1 ) & +fi + +echo "已后台启动。日志: $AUTH/htyuc/htyuc.nohup.log, $HUIKE/htyws/htyws.nohup.log, $HUIKE/htykc/htykc.nohup.log" +echo "htyts 需另设 export MOICEN_START_HTYTS=1 再执行本脚本。" diff --git a/scripts/moicen_stop_huiwings_stack.sh b/scripts/moicen_stop_huiwings_stack.sh new file mode 100755 index 0000000..282de2e --- /dev/null +++ b/scripts/moicen_stop_huiwings_stack.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# 在**即将下线的** moicen 或切流量前,结束 UC/WS/KC/htyts 的 cargo 可执行体(与 ps 里 /target/.../htyuc|htyws|htykc|htyts 匹配)。勿在有其它同名进程的主机上盲目使用。 +set -euo pipefail +for p in htyuc htyws htykc htyts; do + pkill -f "/target/.*/$p" 2>/dev/null || true +done +echo "已发送 pkill;请 ps 确认无残留;admin/ts 前端的反代在其余入口自行维护。"