#!/usr/bin/env bash set -euo pipefail ROLE="${1:-}" BASE_DIR="${2:-/opt/domaincheck}" WWW_USER="${WWW_USER:-www}" WWW_GROUP="${WWW_GROUP:-www}" usage() { cat <<'EOF' usage: bash domain-api/deploy/multi-region/fix_mainland_release_base.sh [base_dir] examples: bash domain-api/deploy/multi-region/fix_mainland_release_base.sh worker bash domain-api/deploy/multi-region/fix_mainland_release_base.sh control EOF } if [[ "${ROLE}" != "worker" && "${ROLE}" != "control" ]]; then usage >&2 exit 1 fi if [[ "$(id -u)" != "0" ]]; then echo "please run as root" >&2 exit 1 fi require_path() { local path="$1" if [[ ! -e "${path}" ]]; then echo "required path missing: ${path}" >&2 exit 1 fi } install_dropin() { local service_name="$1" local content="$2" local dropin_dir="/etc/systemd/system/${service_name}.d" local dropin_file="${dropin_dir}/current-path.conf" mkdir -p "${dropin_dir}" printf '%s\n' "${content}" > "${dropin_file}" echo "installed ${dropin_file}" } echo "[1/6] validate existing runtime" require_path "${BASE_DIR}" require_path "${BASE_DIR}/domainCheck" require_path "${BASE_DIR}/domainCheck/.venv/bin/python" require_path "${BASE_DIR}/domainCheck/detect_worker.py" require_path "${BASE_DIR}/domain-api" echo "[2/6] make release root writable for ${WWW_USER}:${WWW_GROUP}" chgrp "${WWW_GROUP}" "${BASE_DIR}" chmod 2775 "${BASE_DIR}" mkdir -p "${BASE_DIR}/downloads" "${BASE_DIR}/releases" chown -R "${WWW_USER}:${WWW_GROUP}" "${BASE_DIR}/downloads" "${BASE_DIR}/releases" echo "[3/6] ensure current link exists" if [[ -L "${BASE_DIR}/current" ]]; then echo "keep existing symlink: ${BASE_DIR}/current -> $(readlink -f "${BASE_DIR}/current" || true)" elif [[ -e "${BASE_DIR}/current" ]]; then echo "path exists but is not a symlink: ${BASE_DIR}/current" >&2 exit 1 else ln -s "${BASE_DIR}" "${BASE_DIR}/current" echo "created symlink: ${BASE_DIR}/current -> ${BASE_DIR}" fi echo "[4/6] install systemd drop-ins" install_dropin "domaincheck-worker" "[Service] WorkingDirectory=${BASE_DIR}/current/domainCheck ExecStart= ExecStart=${BASE_DIR}/domainCheck/.venv/bin/python ${BASE_DIR}/current/domainCheck/detect_worker.py" install_dropin "domaincheck-node-agent" "[Service] User=root Group=root WorkingDirectory=${BASE_DIR}/current/domain-api ExecStart= ExecStart=${BASE_DIR}/domainCheck/.venv/bin/python -m app.node_agent" if [[ "${ROLE}" == "control" ]]; then install_dropin "domaincheck-api" "[Service] WorkingDirectory=${BASE_DIR}/current/domain-api ExecStart= ExecStart=${BASE_DIR}/domainCheck/.venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8100" install_dropin "domaincheck-sync-agent" "[Service] WorkingDirectory=${BASE_DIR}/current/domain-api ExecStart= ExecStart=${BASE_DIR}/domainCheck/.venv/bin/python -m app.sync_agent" fi echo "[5/6] reload systemd and restart node agent" systemctl daemon-reload systemctl restart domaincheck-node-agent echo "[6/6] summary" echo "role=${ROLE}" echo "base_dir=${BASE_DIR}" echo "current_target=$(readlink -f "${BASE_DIR}/current" || true)" echo echo "recommended next checks:" echo " systemctl status domaincheck-node-agent --no-pager -l" echo " systemctl cat domaincheck-worker" if [[ "${ROLE}" == "control" ]]; then echo " systemctl cat domaincheck-api" echo " systemctl cat domaincheck-sync-agent" fi