#!/usr/bin/env bash # Install the aligned.click product stack as systemd services on this machine. # # sudo ./deploy/install.sh [APP_DIR] # # APP_DIR defaults to /opt/aligned.click. The unit files are rendered with # that path and the service user substituted in, so the same repo deploys to a # home-dir checkout (SERVICE_USER=atproto-server ./deploy/install.sh # ~/feed.aligned.click) or the /opt layout. Assumes .venv is built there and # the oauth sidecar deps are installed (cd auth/oauth && npm install --omit=dev). # # What it does NOT do, deliberately: # # - **It does not bring your secrets.** .env, ~/.cloudflared/ and the provider # key 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 the service user, and # the only reason this needs sudo at all is to write unit files into /etc. set -euo pipefail SERVICE_USER="${SERVICE_USER:-aligned}" REPO="${1:-/opt/aligned.click}" UNITS=(aligned-oauth aligned-proxy aligned-app aligned-feedgen aligned-admin aligned-ingest aligned-tunnel) 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"; exit 1; } id "$SERVICE_USER" >/dev/null 2>&1 || { if [ "$SERVICE_USER" = aligned ]; then useradd --system --home-dir "$REPO" --shell /usr/sbin/nologin aligned else red "no user '$SERVICE_USER'. Create it, or set SERVICE_USER=… and re-run."; exit 1 fi } 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. Asking `command -v` alone was wrong and # quietly so: run_as is a login shell, and that shell's PATH is the system # default — it carries neither ~/.local/bin nor ~/.opencode/bin, which is where # this script installs all three of these. So nothing it installed was ever # found again, and every run reinstalled everything. Node tolerated that (a # directory swap and a symlink); cloudflared did not, and took the deploy with # it. Look in the place we install to, after the PATH and before giving up. runtime() { # runtime local found found="$(run_as "command -v $1" 2>/dev/null || true)" [ -n "$found" ] || { [ -x "$2" ] && found="$2"; } || true printf '%s' "$found" } # ---------------------------------------------------------------- runtimes # # Everything is installed into the service user's home rather than system-wide. # Nothing else on this machine wants these versions, and an install that lives # in one directory is an install you can delete. step "runtimes" PYTHON="$REPO/.venv/bin/python" [ -x "$PYTHON" ] || { red "$PYTHON does not exist — build it: python3 -m venv $REPO/.venv && $PYTHON -m pip install -r $REPO/requirements.txt"; exit 1; } echo " python $PYTHON" NODE="$(runtime node "$USER_HOME/.local/bin/node")" [ -n "$NODE" ] || { red "node is not installed"; exit 1; } echo " node $NODE" CLOUDFLARED="$(runtime cloudflared "$USER_HOME/.local/bin/cloudflared")" [ -n "$CLOUDFLARED" ] || { red "cloudflared is not installed"; exit 1; } echo " cloudflared $CLOUDFLARED" # ---------------------------------------------------------------- the code step "the code" chown -R "$SERVICE_USER":"$SERVICE_USER" "$REPO" echo " owner $SERVICE_USER" # ---------------------------------------------------------------- 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" # private/ is the one writable subtree. Make sure it exists and is ours. install -d -m 700 -o "$SERVICE_USER" -g "$SERVICE_USER" "$REPO/private" chown -R "$SERVICE_USER:$SERVICE_USER" "$REPO/private" missing=() grep -q '^GREENPT_API_KEY=.\+' "$ENV_FILE" \ || missing+=("GREENPT_API_KEY=… in .env (the model provider's key — every user's turns bill to it)") grep -q '^INDEX_BLUESKY_HANDLE=.\+' "$ENV_FILE" \ || missing+=("INDEX_BLUESKY_HANDLE=… in .env (the crawler's read-only search account)") grep -q '^ADMIN_DID=' "$ENV_FILE" \ || missing+=("ADMIN_DID=… in .env (the domain account that writes records)") [ -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/.json (copy it too)") # ---------------------------------------------------------------- services step "services" for unit in "${UNITS[@]}"; do sed -e "s|/opt/aligned.click|$REPO|g" \ -e "s|User=aligned|User=$SERVICE_USER|g" \ -e "s|Group=aligned|Group=$SERVICE_USER|g" \ -e "s|/usr/bin/node|$NODE|g" \ -e "s|/usr/local/bin/cloudflared|$CLOUDFLARED|g" \ "$REPO/deploy/$unit.service" > "/etc/systemd/system/$unit.service" echo " /etc/systemd/system/$unit.service" done systemctl daemon-reload if [ ${#missing[@]} -gt 0 ]; then # Enabled so they start at boot once the missing pieces arrive, but not # started now — a service that comes up and immediately fails teaches you # nothing except to distrust the status output. 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 1 fi systemctl enable --now "${UNITS[@]}" >/dev/null 2>&1 sleep 4 # ---------------------------------------------------------------- report step "status" for unit in "${UNITS[@]}"; do state=$(systemctl show -p ActiveState "$unit" | cut -d= -f2) printf ' %-24s %s\n' "$unit" "$state" done echo green "installed — open:" echo " https://aligned.click" echo echo "log tail: journalctl -u aligned-app -f"