Files
getDomain/domain-api/deploy/multi-region/bootstrap_mainland.sh
2026-04-18 23:52:51 +08:00

89 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BASE_DIR="${1:-/opt/domaincheck}"
ROLE="${2:-controller}"
API_DIR="$BASE_DIR/domain-api"
DOMAIN_DIR="$BASE_DIR/domainCheck"
SERVICE_NAME="domaincheck-worker"
SYNC_AGENT_SERVICE_NAME="domaincheck-sync-agent"
ENV_FILE="/etc/default/domaincheck-worker"
TEMPLATE_DIR="$API_DIR/deploy/multi-region/templates"
if [ "$ROLE" != "controller" ] && [ "$ROLE" != "worker" ]; then
echo "invalid role: $ROLE"
echo "usage: bash domain-api/deploy/multi-region/bootstrap_mainland.sh /opt/domaincheck [controller|worker]"
exit 1
fi
echo "[1/7] preparing directories"
mkdir -p "$BASE_DIR" "$DOMAIN_DIR" "$API_DIR/runtime"
if [ "$ROLE" = "controller" ]; then
mkdir -p /var/lib/domaincheck-runtime-db /var/lib/domaincheck-redis
fi
echo "[2/7] checking python environment"
if [ ! -x "$DOMAIN_DIR/.venv/bin/python" ]; then
echo "missing python venv: $DOMAIN_DIR/.venv/bin/python"
echo "please deploy domainCheck first and create .venv there"
exit 1
fi
echo "[2.5/7] installing mainland runtime dependencies"
"$DOMAIN_DIR/.venv/bin/pip" install -q fastapi uvicorn pydantic-settings psycopg2-binary redis openpyxl python-multipart
echo "[3/7] fixing runtime permissions"
touch "$DOMAIN_DIR/detect_worker.log"
chmod 664 "$DOMAIN_DIR/detect_worker.log"
echo "[4/7] installing worker service"
install -m 0644 "$API_DIR/deploy/systemd/domain-worker.service" "/etc/systemd/system/${SERVICE_NAME}.service"
if [ "$ROLE" = "controller" ]; then
install -m 0644 "$API_DIR/deploy/systemd/domain-sync-agent.service" "/etc/systemd/system/${SYNC_AGENT_SERVICE_NAME}.service"
fi
echo "[4.5/7] preparing environment file"
if [ ! -f "$ENV_FILE" ]; then
if [ "$ROLE" = "controller" ]; then
TEMPLATE_FILE="$TEMPLATE_DIR/domaincheck-worker.controller.env.example"
else
TEMPLATE_FILE="$TEMPLATE_DIR/domaincheck-worker.worker.env.example"
fi
install -m 0644 "$TEMPLATE_FILE" "$ENV_FILE"
sed -i "s#__BASE_DIR__#${BASE_DIR}#g" "$ENV_FILE"
chmod 0644 "$ENV_FILE"
echo "created env template: $ENV_FILE"
else
echo "env file already exists: $ENV_FILE"
fi
echo "[5/7] reloading systemd"
systemctl daemon-reload
if [ "$ROLE" = "controller" ]; then
echo "[6/7] controller notes"
echo "controller mode selected"
echo "please ensure local redis and postgresql_runtime are installed and listening on localhost"
echo "sync agent service: ${SYNC_AGENT_SERVICE_NAME}"
else
echo "[6/7] worker node notes"
echo "worker mode selected"
echo "this node is expected to connect to mainland redis and runtime-db"
fi
echo "[7/7] summary"
echo "mainland bootstrap completed"
echo "role: ${ROLE}"
echo "service: ${SERVICE_NAME}"
echo "next commands:"
echo " systemctl enable ${SERVICE_NAME}"
echo " systemctl restart ${SERVICE_NAME}"
echo " systemctl status ${SERVICE_NAME} --no-pager -l"
if [ "$ROLE" = "controller" ]; then
echo " systemctl enable ${SYNC_AGENT_SERVICE_NAME}"
echo " systemctl restart ${SYNC_AGENT_SERVICE_NAME}"
echo " systemctl status ${SYNC_AGENT_SERVICE_NAME} --no-pager -l"
fi
echo " vi ${ENV_FILE}"