Agent 1.0.0: polls the Hub for signed jobs, runs crosscode, sends reports and stats
apps/agent (docs/AGENT-1.0-PLAN.md §3-4, §10): listens on nothing; checks every job (pinned Hub key, server, time window, replays for 90 days, type per mode, strict arguments) before running crosscode with an argument array; opens the sealed config into a root-only file only for the job; redacts log lines; keeps unsent reports in an outbox; encrypts exports before upload; check-ports on the central server (read-only); self-update with automatic revert. One bundled dist/cli.js, a systemd unit and the install script that joins a server with a one-time token. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
# The Cross Code server agent (docs/AGENT-1.0-PLAN.md §3). Root, because the crosscode script it
|
||||
# runs needs root (PostgreSQL, PM2 for each app user, frpc). It listens on nothing.
|
||||
[Unit]
|
||||
Description=Cross Code server agent
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/bin/node /opt/crosscode/agent/current/apps/agent/dist/cli.js run
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
RuntimeDirectory=crosscode-agent
|
||||
RuntimeDirectoryMode=0700
|
||||
StateDirectory=crosscode-agent
|
||||
StateDirectoryMode=0700
|
||||
Environment=NODE_ENV=production
|
||||
# Logs: journalctl -u crosscode-agent
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,91 @@
|
||||
#!/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 <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 <url> 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 -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)."
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
# Started by a one-shot systemd timer after an agent update (src/update.ts). If the new agent
|
||||
# hasn't polled the Hub by now, update-pending.json is still there: switch back to the previous
|
||||
# release and restart. Copied to /opt/crosscode/agent/revert.sh by install.sh.
|
||||
set -euo pipefail
|
||||
ROOT=/opt/crosscode/agent
|
||||
PENDING=/var/lib/crosscode-agent/update-pending.json
|
||||
[ -f "$PENDING" ] || exit 0
|
||||
[ -L "$ROOT/previous" ] || { echo "crosscode-agent revert: no previous release" >&2; exit 1; }
|
||||
ln -sfn "$(readlink "$ROOT/previous")" "$ROOT/current.tmp"
|
||||
mv -T "$ROOT/current.tmp" "$ROOT/current"
|
||||
rm -f "$PENDING"
|
||||
systemctl restart crosscode-agent
|
||||
echo "crosscode-agent revert: the new agent didn't poll in time; back on $(readlink "$ROOT/current")" >&2
|
||||
Reference in New Issue
Block a user