#!/usr/bin/env bash # Installs the Cross Code server agent and joins this server to the Hub # (docs/AGENT-1.0-PLAN.md §3). Hub → Servers → Add server shows the command, with a one-time token: # # curl -fsSL https://c-git.crosscode.co.za/joshua/crosscode-platform/raw/branch/main/apps/agent/deploy/install.sh \ # | sudo bash -s -- --hub https://hub.crosscode.co.za --token [--central] # # Church servers also get the crosscode script (/opt/crosscode/tools, linked as # /usr/local/bin/crosscode). The central server (--central) only gets the agent, which is # read-only there. Running it again updates nothing that's there already; use --force to join again. set -Eeuo pipefail REPO="${CROSSCODE_REPO:-https://c-git.crosscode.co.za/joshua/crosscode-platform.git}" ROOT=/opt/crosscode/agent TOOLS=/opt/crosscode/tools HUB="" TOKEN="" MODE=church VERSION="" FORCE="" die() { printf '!!> %s\n' "$*" >&2; exit 1; } step() { printf '==> %s\n' "$*"; } while [ $# -gt 0 ]; do case "$1" in --hub) HUB="$2"; shift 2 ;; --token) TOKEN="$2"; shift 2 ;; --central) MODE=central; shift ;; --version) VERSION="$2"; shift 2 ;; --force) FORCE=--force; shift ;; *) die "Unknown option $1" ;; esac done [ "$(id -u)" = 0 ] || die "Run as root (sudo bash -s -- …)." [[ "$HUB" =~ ^https?:// ]] || die "--hub is required." [[ "$TOKEN" =~ ^[A-Za-z0-9_-]{32,128}$ ]] || die "--token is required (Hub → Servers → Add server)." [ -z "$VERSION" ] || [[ "$VERSION" =~ ^agent@[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "--version: expected agent@x.y.z." step "System packages" export DEBIAN_FRONTEND=noninteractive apt-get update -q apt-get install -y -q git curl ca-certificates if ! command -v node >/dev/null || [ "$(node -p 'process.versions.node.split(".")[0]')" -lt 24 ]; then step "Node.js 24" curl -fsSL https://deb.nodesource.com/setup_24.x | bash - apt-get install -y -q nodejs fi if [ -z "$VERSION" ]; then VERSION="$(git ls-remote --tags --refs "$REPO" 'agent@*' | sed 's#.*refs/tags/##' | sort -t@ -k2 -V | tail -n 1)" [ -n "$VERSION" ] || die "No agent@x.y.z release yet." fi DIR="$ROOT/releases/${VERSION#agent@}" step "Agent $VERSION in $DIR" mkdir -p "$ROOT/releases" chmod 755 /opt/crosscode "$ROOT" 2>/dev/null || true if [ ! -d "$DIR/.git" ]; then git clone --quiet --depth 1 --branch "$VERSION" "$REPO" "$DIR" fi ( cd "$DIR" NODE_ENV=development npm ci --no-audit --no-fund --include=dev --include-workspace-root -w packages/contracts -w apps/agent npm run build -w packages/contracts npm run build -w apps/agent ) [ "$(node "$DIR/apps/agent/dist/cli.js" version)" = "${VERSION#agent@}" ] || die "The build doesn't report $VERSION." ln -sfn "$DIR" "$ROOT/current" install -m 755 "$DIR/apps/agent/deploy/revert.sh" "$ROOT/revert.sh" if [ "$MODE" = church ]; then step "The crosscode script ($TOOLS)" [ -d "$TOOLS/.git" ] || git clone --quiet "$REPO" "$TOOLS" ln -sfn "$TOOLS/scripts/crosscode" /usr/local/bin/crosscode fi step "Joining the Hub ($HUB)" mkdir -p /etc/crosscode /var/lib/crosscode-agent chmod 700 /etc/crosscode /var/lib/crosscode-agent args=(join --hub "$HUB" --token "$TOKEN") [ "$MODE" = central ] && args+=(--central) [ -n "$FORCE" ] && args+=("$FORCE") if [ -f /etc/crosscode/agent.json ] && [ -z "$FORCE" ]; then echo " Already joined (/etc/crosscode/agent.json); keeping it." else node "$ROOT/current/apps/agent/dist/cli.js" "${args[@]}" fi step "systemd" install -m 644 "$DIR/apps/agent/deploy/crosscode-agent.service" /etc/systemd/system/crosscode-agent.service systemctl daemon-reload systemctl enable --now crosscode-agent systemctl restart crosscode-agent step "Done: the server shows on Hub → Servers within a minute (journalctl -u crosscode-agent)."