#!/usr/bin/env bash # /workspace/hyperstitionstudio-core/deploy/sync-vps-repos.sh # Hydrates per-repository install directories from REPOS_ROOT into LIVE_REPOS (tmpfs RAM mount), # supplies untracked haiperdohp-jemaytreeah/.gitignore files (config.js, db.json, db_lite.json), # creates top-level route symlinks, pre-compresses (.br / .gz) static assets, and idempotently # syncs/validates/reloads /etc/caddy/Caddyfile and systemd units by default (negated via --no-caddy). # # Usage: # ./sync-vps-repos.sh [all|] [--no-pull] [--no-caddy|--no-services] set -euo pipefail on_error() { local exit_code=$? local line_no="${1:-unknown}" echo "[ERROR] sync-vps-repos.sh failed at line ${line_no} with exit code ${exit_code} while running: ${BASH_COMMAND}" >&2 exit "${exit_code}" } trap 'on_error ${LINENO}' ERR run_checked() { local label="$1" shift local out="" local rc=0 out="$("$@" 2>&1)" || rc=$? if [[ "${rc}" -ne 0 ]]; then echo "[ERROR] ${label} failed (exit code ${rc}): $*" >&2 if [[ -n "${out}" ]]; then echo "${out}" >&2 fi return "${rc}" fi } has_cmd() { command -v "$1" >&2 } SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" DEFAULT_REPOS_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" export REPOS_ROOT="${REPOS_ROOT:-${DEFAULT_REPOS_ROOT}}" export LIVE_REPOS="${LIVE_REPOS:-/var/www/hyperstitionstud.io/repos}" TARGET_REPO="all" DO_PULL=1 DO_CADDY=1 for arg in "$@"; do case "${arg}" in --no-pull) DO_PULL=0 ;; --no-caddy|--no-services) DO_CADDY=0 ;; *) TARGET_REPO="${arg}" ;; esac done SUDO="" if [[ "${EUID:-$(id -u)}" -ne 0 ]] && type -P sudo >&2; then SUDO="sudo" fi PYTHON_BIN="python3" if [[ -x "${REPOS_ROOT}/.venv/bin/python3" ]]; then PYTHON_BIN="${REPOS_ROOT}/.venv/bin/python3" fi export PYTHONPATH="${REPOS_ROOT}/hyperstitionstudio-core/src${PYTHONPATH:+:${PYTHONPATH}}" if [[ -f "${REPOS_ROOT}/workspace.toml" ]]; then export HYPERSTITIONSTUDIO_CONFIG="${REPOS_ROOT}/workspace.toml" fi # Detect whether the CI artifact repository is cloned as ingglish-hyperdope-ci or hyperdope-ci-build CI_REPO_DIR="ingglish-hyperdope-ci" if [[ ! -d "${REPOS_ROOT}/${CI_REPO_DIR}" ]] && [[ -d "${REPOS_ROOT}/hyperdope-ci-build" ]]; then CI_REPO_DIR="hyperdope-ci-build" fi REPOS=( "hyperstitionstudio-core:main" "ingglish-hyperdope-gematria:main" "technocapital-singularity-zoology:main" "encyclopedia-knowledge-graph:main" "haiperdohp-jemaytreeah:main" "${CI_REPO_DIR}:feat/ingglish-aq-corpus" ) if [[ "${DO_PULL}" -eq 1 ]]; then for entry in "${REPOS[@]}"; do repo="${entry%%:*}" branch="${entry##*:}" if [[ "${TARGET_REPO}" != "all" && "${TARGET_REPO}" != "${repo}" ]]; then continue fi if [[ -d "${REPOS_ROOT}/${repo}/.git" ]]; then remote_name="origin" if [[ "${repo}" == "haiperdohp-jemaytreeah" ]] && git -C "${REPOS_ROOT}/${repo}" remote | grep -qx "downstream"; then remote_name="downstream" fi echo "[1/5] Updating ${REPOS_ROOT}/${repo} (${remote_name}/${branch})..." prev_head="$(git -C "${REPOS_ROOT}/${repo}" rev-parse HEAD)" run_checked "git fetch ${repo}" git -C "${REPOS_ROOT}/${repo}" fetch --force "${remote_name}" "${branch}" --tags run_checked "git merge ${repo}" git -C "${REPOS_ROOT}/${repo}" merge --ff-only "${remote_name}/${branch}" new_head="$(git -C "${REPOS_ROOT}/${repo}" rev-parse HEAD)" if [[ "${repo}" == "hyperstitionstudio-core" && "${prev_head}" != "${new_head}" && -z "${HYPERSTITION_SYNC_REEXEC:-}" ]]; then echo "[1/5] hyperstitionstudio-core updated (${prev_head:0:7} -> ${new_head:0:7}); re-executing updated sync-vps-repos.sh..." export HYPERSTITION_SYNC_REEXEC=1 exec "${REPOS_ROOT}/hyperstitionstudio-core/deploy/sync-vps-repos.sh" "$@" fi fi done fi # Supply untracked files listed in haiperdohp-jemaytreeah/.gitignore (config.js, db.json, db_lite.json) if [[ -d "${REPOS_ROOT}/haiperdohp-jemaytreeah" ]]; then if [[ ! -f "${REPOS_ROOT}/haiperdohp-jemaytreeah/config.js" ]] && [[ -f "${REPOS_ROOT}/haiperdohp-jemaytreeah/config.default.js" ]]; then echo "[build] Copying haiperdohp-jemaytreeah/config.default.js -> config.js..." run_checked "copy haiperdohp-jemaytreeah config.js" cp "${REPOS_ROOT}/haiperdohp-jemaytreeah/config.default.js" "${REPOS_ROOT}/haiperdohp-jemaytreeah/config.js" fi if [[ ! -f "${REPOS_ROOT}/haiperdohp-jemaytreeah/db_lite.json" || ! -f "${REPOS_ROOT}/haiperdohp-jemaytreeah/db.json" ]] && [[ -f "${REPOS_ROOT}/haiperdohp-jemaytreeah/tools/generate_db_json.py" ]]; then echo "[build] Generating haiperdohp-jemaytreeah/db.json and db_lite.json from db.txt..." (cd "${REPOS_ROOT}/haiperdohp-jemaytreeah" && run_checked "generate_db_json.py" "${PYTHON_BIN}" tools/generate_db_json.py db.txt db.json) fi fi echo "[2/5] Synchronizing local workspace.toml version and running hs-doctor, version_sync --check, and 6-Gate Knowledge Graph Verification..." if [[ -f "${REPOS_ROOT}/workspace.toml" ]] && [[ -f "${REPOS_ROOT}/hyperstitionstudio-core/workspace.example.toml" ]]; then # Sync schema_version in untracked parent workspace.toml with hyperstitionstud_io.__version__ run_checked "version_sync (sync untracked workspace.toml)" "${PYTHON_BIN}" -m hyperstitionstud_io.version_sync fi run_checked "version_sync --check" "${PYTHON_BIN}" -m hyperstitionstud_io.version_sync --check run_checked "hs-doctor" "${PYTHON_BIN}" -m hyperstitionstud_io.ci.doctor run_checked "build_encyclopedia_knowledge_graph.py --verify-only" "${PYTHON_BIN}" "${REPOS_ROOT}/encyclopedia-knowledge-graph/scripts/build_encyclopedia_knowledge_graph.py" --verify-only echo "[3/5] Syncing per-repository install directories into ${LIVE_REPOS}..." LIVE_SUDO="" if [[ ! -w "$(dirname "${LIVE_REPOS}")" ]] && [[ -n "${SUDO}" ]]; then LIVE_SUDO="${SUDO}" fi ${LIVE_SUDO} mkdir -p "${LIVE_REPOS}" for entry in "${REPOS[@]}"; do repo="${entry%%:*}" if [[ "${TARGET_REPO}" != "all" && "${TARGET_REPO}" != "${repo}" ]]; then continue fi if [[ -d "${REPOS_ROOT}/${repo}" ]]; then ${LIVE_SUDO} mkdir -p "${LIVE_REPOS}/${repo}" run_checked "rsync ${repo}" ${LIVE_SUDO} rsync -a --delete --exclude='.git' "${REPOS_ROOT}/${repo}/" "${LIVE_REPOS}/${repo}/" fi done if [[ "${CI_REPO_DIR}" == "hyperdope-ci-build" ]] && [[ -d "${LIVE_REPOS}/hyperdope-ci-build" ]] && [[ ! -e "${LIVE_REPOS}/ingglish-hyperdope-ci" ]]; then ${LIVE_SUDO} ln -sfn "hyperdope-ci-build" "${LIVE_REPOS}/ingglish-hyperdope-ci" fi # Create relative route symlinks inside LIVE_REPOS so both Caddy and standalone HTTP servers resolve all active routes ${LIVE_SUDO} ln -sfn "hyperstitionstudio-core/site/index.html" "${LIVE_REPOS}/index.html" ${LIVE_SUDO} ln -sfn "hyperstitionstudio-core/site/learn" "${LIVE_REPOS}/learn" ${LIVE_SUDO} ln -sfn "hyperstitionstudio-core/site/quant" "${LIVE_REPOS}/quant" # Note: /gallery is disabled for now (tracked as a long-term plan in docs/PLAN-MEMETIC-GALLERY-SUBPAGE.pdf) ${LIVE_SUDO} rm -rf "${LIVE_REPOS}/gallery" "${LIVE_REPOS}/hyperstitionstudio-core/site/gallery" ${LIVE_SUDO} ln -sfn "encyclopedia-knowledge-graph" "${LIVE_REPOS}/graph" ${LIVE_SUDO} ln -sfn "technocapital-singularity-zoology/encyclopedia" "${LIVE_REPOS}/encyclopedia" ${LIVE_SUDO} ln -sfn "technocapital-singularity-zoology" "${LIVE_REPOS}/zoology" ${LIVE_SUDO} ln -sfn "ingglish-hyperdope-gematria/ingglish-aq-archive" "${LIVE_REPOS}/archive" ${LIVE_SUDO} ln -sfn "haiperdohp-jemaytreeah" "${LIVE_REPOS}/calc" ${LIVE_SUDO} ln -sfn "ingglish-hyperdope-ci" "${LIVE_REPOS}/ci" ${LIVE_SUDO} ln -sfn "hyperstitionstudio-core/schemas" "${LIVE_REPOS}/schemas" echo "[4/5] Pre-compressing static HTML, JS, JSON, XML, Canvas, MD & CSS assets (< 5 MB) (.br / .gz)..." run_checked "gzip pre-compression" ${LIVE_SUDO} find "${LIVE_REPOS}" -type f -size -5M \ \( -name "*.html" -o -name "*.js" -o -name "*.json" -o -name "*.graphml" -o -name "*.gexf" -o -name "*.canvas" -o -name "*.css" -o -name "*.md" \) \ -exec gzip -9 -k -f {} \; if [[ -n "$(type -P brotli || true)" ]]; then run_checked "brotli pre-compression" ${LIVE_SUDO} find "${LIVE_REPOS}" -type f -size -5M \ \( -name "*.html" -o -name "*.js" -o -name "*.json" -o -name "*.graphml" -o -name "*.gexf" -o -name "*.canvas" -o -name "*.css" -o -name "*.md" \) \ -exec brotli -q 11 -k -f {} \; fi # Step 5: Idempotent systemd unit & /etc/caddy/Caddyfile synchronization (enabled by default; skip with --no-caddy) if [[ "${DO_CADDY}" -eq 1 ]]; then DEPLOY_DIR="${REPOS_ROOT}/hyperstitionstudio-core/deploy" if [[ -n "$(type -P systemctl || true)" ]] && [[ -d "/run/systemd/system" ]]; then UNITS_CHANGED=0 for unit_name in "hyperstition-tmpfs-sync.service" "hyperstition-mcp.service"; do src_u="${DEPLOY_DIR}/${unit_name}" dst_u="/etc/systemd/system/${unit_name}" if [[ -f "${src_u}" ]]; then if [[ ! -f "${dst_u}" ]] || ! cmp -s "${src_u}" "${dst_u}"; then echo "[5/5] Updating systemd unit ${dst_u}..." run_checked "copy ${unit_name}" ${SUDO} cp "${src_u}" "${dst_u}" UNITS_CHANGED=1 fi fi done if [[ "${UNITS_CHANGED}" -eq 1 ]]; then run_checked "systemctl daemon-reload" ${SUDO} systemctl daemon-reload run_checked "systemctl enable hyperstition-tmpfs-sync.service" ${SUDO} systemctl enable hyperstition-tmpfs-sync.service run_checked "systemctl enable --now hyperstition-mcp.service" ${SUDO} systemctl enable --now hyperstition-mcp.service run_checked "systemctl restart hyperstition-mcp.service" ${SUDO} systemctl restart hyperstition-mcp.service elif [[ -f "/etc/systemd/system/hyperstition-mcp.service" ]] && ! systemctl is-active --quiet hyperstition-mcp.service; then run_checked "systemctl enable --now hyperstition-mcp.service" ${SUDO} systemctl enable --now hyperstition-mcp.service fi fi SRC_CADDY="${DEPLOY_DIR}/Caddyfile" DST_CADDY="/etc/caddy/Caddyfile" if [[ -n "$(type -P caddy || true)" ]] && [[ -d "/etc/caddy" ]] && [[ -f "${SRC_CADDY}" ]]; then if [[ -f "${DST_CADDY}" ]] && cmp -s "${SRC_CADDY}" "${DST_CADDY}"; then echo "[5/5] /etc/caddy/Caddyfile is already identical to deploy/Caddyfile (no reload needed)." else echo "[5/5] Validating and updating ${DST_CADDY} from ${SRC_CADDY}..." run_checked "caddy validate ${SRC_CADDY}" ${SUDO} caddy validate --config "${SRC_CADDY}" --adapter caddyfile run_checked "copy Caddyfile" ${SUDO} cp "${SRC_CADDY}" "${DST_CADDY}" if [[ -n "$(type -P systemctl || true)" ]] && [[ -d "/run/systemd/system" ]]; then if systemctl is-active --quiet caddy; then run_checked "systemctl reload caddy" ${SUDO} systemctl reload caddy echo "[5/5] Reloaded Caddy with updated /etc/caddy/Caddyfile." else run_checked "systemctl enable --now caddy" ${SUDO} systemctl enable --now caddy echo "[5/5] Started Caddy with updated /etc/caddy/Caddyfile." fi fi fi else echo "[5/5] Caddy or /etc/caddy not present on this host; skipping Caddyfile reload." fi else echo "[5/5] Skipping Caddyfile & systemd service sync (--no-caddy)." fi echo "[OK] Per-repository install directories hydrated and pre-compressed at ${LIVE_REPOS}"