Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173#!/usr/bin/env bash# Install the pi-collab (terminal) stack as systemd services on this machine.## sudo deploy/install.sh## Run it from the checkout, on the machine that will run the server (the# mini-PC). It is idempotent: run it again after pulling new code and it will# reinstall the units and restart the services.## What it does NOT do, deliberately:## - **It does not bring your secrets.** `.env` and `~/.cloudflared/` are not in# the repo and are not invented here. The script tells you which are missing# and refuses to pretend the service is working without them.# - **It does not open a port.** Nothing here listens outside loopback except# through the Cloudflare tunnel, which is an outbound connection.# - **It does not run the services as root.** They run as SERVICE_USER (and the# docker containers as the docker group). The only reason this needs sudo is# to write unit files into /etc and to install docker.set -euo pipefail
SERVICE_USER="${SERVICE_USER:-atproto-server}"REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"UNITS=(pi-tunnel pi-sandbox pi-oauth pi-gate pi-server pi-watch)
red() { printf '\033[31m%s\033[0m\n' "$*"; }green(){ printf '\033[32m%s\033[0m\n' "$*"; }warn() { printf '\033[33m%s\033[0m\n' "$*"; }step() { printf '\n\033[1m%s\033[0m\n' "$*"; }
[ "$(id -u)" -eq 0 ] || { red "run this with sudo — it writes unit files to /etc/systemd/system and may install docker"; exit 1; }id "$SERVICE_USER" >/dev/null 2>&1 || { red "no user '$SERVICE_USER'. Create it, or set SERVICE_USER=… and re-run."; exit 1; }
USER_HOME="$(getent passwd "$SERVICE_USER" | cut -d: -f6)"run_as() { sudo -u "$SERVICE_USER" -H bash -lc "$*"; }
# Where a runtime is, if it is anywhere. run_as is a login shell; its PATH is# the system default and carries neither ~/.local/bin nor ~/.opencode/bin.runtime() { # runtime <command> <path we would have installed it to> local found found="$(run_as "command -v $1" 2>/dev/null || true)" [ -n "$found" ] || { [ -x "$2" ] && found="$2"; } || true printf '%s' "$found"}
# ---------------------------------------------------------------- runtimes
step "runtimes"
NODE="$(runtime node "$USER_HOME/.local/bin/node")"if [ -z "$NODE" ]; then red "node is not installed. Install it (or place it at $USER_HOME/.local/bin/node) and re-run." exit 1fiecho " node $NODE ($($NODE --version))"
CLOUDFLARED="$(runtime cloudflared "$USER_HOME/.local/bin/cloudflared")"if [ -z "$CLOUDFLARED" ]; then red "cloudflared is not installed. Install it (or place it at $USER_HOME/.local/bin/cloudflared) and re-run." exit 1fiecho " cloudflared $CLOUDFLARED"
if ! command -v docker >/dev/null 2>&1; then echo " docker not installed — installing docker.io (this needs the network)" apt-get update -qq apt-get install -y -qq docker.io >/dev/null systemctl enable --now docker >/dev/null 2>&1 || truefiDOCKER="$(command -v docker || echo /usr/bin/docker)"echo " docker $DOCKER"
# ---------------------------------------------------------------- the code
step "the code"
chown -R "$SERVICE_USER":"$SERVICE_USER" "$REPO"echo " owner $SERVICE_USER"
# deps: root (ws, pi-tui) and the oauth sidecar (@atproto/oauth-client-node).run_as "cd '$REPO' && PATH=\"$USER_HOME/.local/bin:\$PATH\" npm install --omit=dev --silent"echo " root deps installed"run_as "cd '$REPO/oauth' && PATH=\"$USER_HOME/.local/bin:\$PATH\" npm install --omit=dev --silent"echo " oauth deps installed"
mkdir -p "$REPO/data" "$REPO/data/pi/sessions" "$REPO/data/chat" "$REPO/data/ssh-hostkeys" "$REPO/private/oauth"chown -R "$SERVICE_USER":"$SERVICE_USER" "$REPO/data" "$REPO/private"echo " data/ ready (data/pi is the committed record)"
# Sandbox ssh keypair, generated on first run like dev.sh does.if [ ! -f "$REPO/sandbox/.club_ed25519.pub" ] && [ ! -n "${CLUB_SSH_PUBKEY:-}" ]; then run_as "ssh-keygen -q -t ed25519 -f '$REPO/sandbox/.club_ed25519' -N '' -C pi-collab" echo " generated sandbox ssh key into sandbox/.club_ed25519"fi
# ---------------------------------------------------------------- config
step "config"
ENV_FILE="$REPO/.env"[ -f "$ENV_FILE" ] || { install -m 600 -o "$SERVICE_USER" -g "$SERVICE_USER" /dev/null "$ENV_FILE"; }chmod 600 "$ENV_FILE"; chown "$SERVICE_USER":"$SERVICE_USER" "$ENV_FILE"
append_env() { # append_env KEY=VALUE local k="${1%%=*}" grep -q "^$k=" "$ENV_FILE" || printf '\n%s\n' "$1" >> "$ENV_FILE"}
append_env "PUBLIC_URL=https://coding.prosocial.social"append_env "OAUTH_PORT=4100" # aligned+agent oauth own 4098/4099 on this boxappend_env "OAUTH_URL=http://127.0.0.1:4100"append_env "PROXY_URL=http://127.0.0.1:8443"append_env "PI_SANDBOX=1"append_env "PI_SANDBOX_CONTAINER=pi-sandbox"append_env "SSH_PORT=2223"if ! grep -q '^CLUB_SSH_PUBKEY=' "$ENV_FILE" && [ -f "$REPO/sandbox/.club_ed25519.pub" ]; then printf 'CLUB_SSH_PUBKEY=%s\n' "$(cat "$REPO/sandbox/.club_ed25519.pub")" >> "$ENV_FILE"fi
missing=()grep -q '^GREENPT_API_KEY=.\+' "$ENV_FILE" \ || missing+=("GREENPT_API_KEY=… in .env (the model provider key — every user's turns bill to it; aligned's .env already has one to copy)")grep -q '^CLUB_SSH_PUBKEY=.\+' "$ENV_FILE" \ || missing+=("CLUB_SSH_PUBKEY=… in .env (the sandbox ssh key for Zed; generated into sandbox/.club_ed25519.pub — set it here)")[ -f "$USER_HOME/.cloudflared/cert.pem" ] || missing+=("$USER_HOME/.cloudflared/cert.pem (copy from the machine that ran 'cloudflared tunnel login')")ls "$USER_HOME"/.cloudflared/*.json >/dev/null 2>&1 || missing+=("$USER_HOME/.cloudflared/<tunnel-id>.json (the 'coding' tunnel creds — copy it too)")
# ---------------------------------------------------------------- services
step "services"
for unit in "${UNITS[@]}"; do sed -e "s|%REPO%|$REPO|g" \ -e "s|%SERVICE_USER%|$SERVICE_USER|g" \ -e "s|%NODE%|$NODE|g" \ -e "s|%CLOUDFLARED%|$CLOUDFLARED|g" \ -e "s|%SSH_PORT%|${SSH_PORT:-2223}|g" \ "$REPO/deploy/systemd/$unit.service" > "/etc/systemd/system/$unit.service" echo " /etc/systemd/system/$unit.service"done
systemctl daemon-reload
if [ ${#missing[@]} -gt 0 ]; then systemctl enable "${UNITS[@]}" >/dev/null 2>&1 step "not started — these are missing:" for m in "${missing[@]}"; do red " $m"; done echo echo "Add them, then: sudo systemctl start ${UNITS[*]}" exit 1fi
systemctl enable --now "${UNITS[@]}" >/dev/null 2>&1sleep 4
step "status"ok=0for unit in "${UNITS[@]}"; do s="$(systemctl is-active "$unit" || true)" if [ "$s" = active ]; then green " $s $unit"; else red " $s $unit"; ok=1; fidone
echoif [ "$ok" = 0 ]; then green "all six running, and they start at boot." echo " logs: journalctl -u pi-server -f" echo " check: curl -s https://coding.prosocial.social/health"else red "something did not start." echo " journalctl -u <unit> -n 40 --no-pager" exit 1fi