Files
getDomain/domain-api/deploy/multi-region/install_mainland_worker_quick.sh
Your Name e9c0a75b8f debug
2026-04-17 01:04:11 +08:00

141 lines
4.1 KiB
Bash
Executable File
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.
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/install_mainland_worker_quick.conf"
CONFIG_EXAMPLE="${SCRIPT_DIR}/install_mainland_worker_quick.conf.example"
if [[ ! -f "${CONFIG_FILE}" ]]; then
echo "未找到配置文件:${CONFIG_FILE}"
echo "请先执行cp ${CONFIG_EXAMPLE} ${CONFIG_FILE}"
exit 1
fi
# shellcheck disable=SC1090
source "${CONFIG_FILE}"
: "${DB_NAME:=domain}"
: "${DB_USER:=domainuser}"
: "${DB_PASSWORD:=CHANGE_ME_DB_PASSWORD}"
: "${REDIS_PASSWORD:=CHANGE_ME_REDIS_PASSWORD}"
: "${SYNC_SHARED_TOKEN:=CHANGE_ME_SYNC_SHARED_TOKEN}"
: "${MAINLAND_WORKER_NODE_CODE:=mainland-worker-01}"
: "${MAINLAND_CONTROLLER_IP:=127.0.0.1}"
: "${TARGET_API_BASE_URL:=https://api.domain.com/api/v1}"
WORKSPACE_ROOT="/www/wwwroot"
REPO_DIR="${WORKSPACE_ROOT}/getDomain"
BASE_DIR="/opt/domaincheck"
DOMAINCHECK_DIR="${BASE_DIR}/domainCheck"
DOMAIN_API_DIR="${BASE_DIR}/domain-api"
if [[ "${DB_PASSWORD}" == "CHANGE_ME_DB_PASSWORD" || "${REDIS_PASSWORD}" == "CHANGE_ME_REDIS_PASSWORD" || "${SYNC_SHARED_TOKEN}" == "CHANGE_ME_SYNC_SHARED_TOKEN" || "${MAINLAND_CONTROLLER_IP}" == "127.0.0.1" ]]; then
echo "请先修改配置文件中的必填项DB_PASSWORD / REDIS_PASSWORD / SYNC_SHARED_TOKEN / MAINLAND_CONTROLLER_IP"
exit 1
fi
mkdir -p "${WORKSPACE_ROOT}"
if [[ ! -d "${REPO_DIR}/.git" ]]; then
echo "未找到仓库目录:${REPO_DIR}"
echo "全新机器首次部署,必须先手动执行 git clone把仓库拉到 /www/wwwroot/getDomain"
exit 1
fi
echo "已检测到仓库目录:${REPO_DIR}"
echo "当前脚本不会执行任何 Git 命令。"
echo "如需更新代码,请先手动完成 git pull再重新执行本脚本。"
mkdir -p "${BASE_DIR}"
ln -sfn "${REPO_DIR}/domain-api" "${DOMAIN_API_DIR}"
ln -sfn "${REPO_DIR}/domain-web" "${BASE_DIR}/domain-web"
ln -sfn "${REPO_DIR}/domainCheck" "${DOMAINCHECK_DIR}"
cd "${DOMAINCHECK_DIR}"
python3.11 -m venv .venv || true
source .venv/bin/activate
pip install --upgrade pip
python3.11 - <<'PY'
from pathlib import Path
source = Path("requirements.txt")
target = Path("requirements.linux.txt")
raw = source.read_bytes()
for encoding in ("utf-8", "utf-16", "utf-16-le", "utf-16-be", "gbk"):
try:
text = raw.decode(encoding)
break
except UnicodeDecodeError:
continue
else:
raise SystemExit("无法解析 requirements.txt 编码")
skip_prefixes = (
"pywin32==",
"pywin32-ctypes==",
"twisted-iocpsupport==",
"win32-setctime==",
)
lines = []
for line in text.splitlines():
normalized = line.strip().lstrip("\ufeff")
if normalized.lower().startswith(skip_prefixes):
continue
lines.append(normalized)
target.write_text("\n".join(lines) + "\n", encoding="utf-8")
PY
pip install -r requirements.linux.txt
"${DOMAINCHECK_DIR}/.venv/bin/pip" install fastapi uvicorn pydantic-settings psycopg2-binary redis openpyxl python-multipart
cat >"${DOMAINCHECK_DIR}/.env" <<EOF
DB_HOST=${MAINLAND_CONTROLLER_IP}
DB_PORT=5432
DB_DATABASE=${DB_NAME}
DB_USER=${DB_USER}
DB_PASSWORD=${DB_PASSWORD}
REDIS_HOST=${MAINLAND_CONTROLLER_IP}
REDIS_PORT=6379
REDIS_PASSWORD=${REDIS_PASSWORD}
REDIS_DB=0
EOF
cd "${DOMAIN_API_DIR}"
bash deploy/multi-region/bootstrap_mainland.sh /opt/domaincheck worker
cat >/etc/default/domaincheck-worker <<EOF
WORKER_MODE=linux-systemd
QT_QPA_PLATFORM=offscreen
NODE_CODE=${MAINLAND_WORKER_NODE_CODE}
NODE_REGION=mainland
NODE_ROLE=worker
DB_HOST=${MAINLAND_CONTROLLER_IP}
DB_PORT=5432
DB_DATABASE=${DB_NAME}
DB_USER=${DB_USER}
DB_PASSWORD=${DB_PASSWORD}
REDIS_HOST=${MAINLAND_CONTROLLER_IP}
REDIS_PORT=6379
REDIS_PASSWORD=${REDIS_PASSWORD}
REDIS_DB=0
SYNC_PUSH_ENABLED=true
SYNC_SOURCE_REGION=mainland
SYNC_TARGET_REGION=overseas
SYNC_TARGET_API_BASE_URL=${TARGET_API_BASE_URL}
SYNC_SHARED_TOKEN=${SYNC_SHARED_TOKEN}
SYNC_BATCH_SIZE=200
SYNC_POLL_INTERVAL_SECONDS=30
EOF
systemctl daemon-reload
systemctl enable domaincheck-worker
systemctl restart domaincheck-worker
chown -R www:www "${REPO_DIR}"
echo
echo "国内 worker 部署完成,建议继续在国外机查看:"
echo "curl https://api.domain.com/api/v1/runtime/cluster"