142 lines
4.0 KiB
Bash
Executable File
142 lines
4.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
CONFIG_FILE="${SCRIPT_DIR}/install_mainland_controller_quick.conf"
|
||
CONFIG_EXAMPLE="${SCRIPT_DIR}/install_mainland_controller_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_CONTROLLER_NODE_CODE:=mainland-controller-01}"
|
||
: "${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" ]]; then
|
||
echo "请先修改配置文件中的必填项:DB_PASSWORD / REDIS_PASSWORD / SYNC_SHARED_TOKEN"
|
||
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=127.0.0.1
|
||
DB_PORT=5432
|
||
DB_DATABASE=${DB_NAME}
|
||
DB_USER=${DB_USER}
|
||
DB_PASSWORD=${DB_PASSWORD}
|
||
|
||
REDIS_HOST=127.0.0.1
|
||
REDIS_PORT=6379
|
||
REDIS_PASSWORD=${REDIS_PASSWORD}
|
||
REDIS_DB=0
|
||
EOF
|
||
|
||
cd "${DOMAIN_API_DIR}"
|
||
bash deploy/multi-region/bootstrap_mainland.sh /opt/domaincheck controller
|
||
|
||
cat >/etc/default/domaincheck-worker <<EOF
|
||
WORKER_MODE=linux-systemd
|
||
QT_QPA_PLATFORM=offscreen
|
||
NODE_CODE=${MAINLAND_CONTROLLER_NODE_CODE}
|
||
NODE_REGION=mainland
|
||
NODE_ROLE=control
|
||
|
||
DB_HOST=127.0.0.1
|
||
DB_PORT=5432
|
||
DB_DATABASE=${DB_NAME}
|
||
DB_USER=${DB_USER}
|
||
DB_PASSWORD=${DB_PASSWORD}
|
||
|
||
REDIS_HOST=127.0.0.1
|
||
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 enable domaincheck-sync-agent
|
||
systemctl restart domaincheck-worker
|
||
systemctl restart domaincheck-sync-agent
|
||
chown -R www:www "${REPO_DIR}"
|
||
|
||
echo
|
||
echo "国内 controller 部署完成,建议继续执行:"
|
||
echo "cd ${DOMAIN_API_DIR} && bash deploy/multi-region/check_mainland_controller.sh"
|