Skip to content

Latest commit

 

History

History
190 lines (136 loc) · 5.54 KB

SETUP.md

File metadata and controls

190 lines (136 loc) · 5.54 KB

Codebase production setup

What you need to do by hand before / right after a launch. Most of the platform is automated, but these touch credentials or system services that require root or external dashboards.


One-time host setup

1. Enable systemd user-services to survive logout

Without this, the Convex backup timer (and any other --user service) stops the moment your SSH session closes.

sudo loginctl enable-linger half

Verify:

loginctl show-user half --property=Linger    # → Linger=yes
systemctl --user list-timers codebase-convex-backup.timer

2. Set NEXT_PUBLIC_SENTRY_DSN in .env.production

Sentry is currently disabled because the DSN env var is unset. The platform logs "Sentry disabled" constantly until you fix this.

Get the DSN from sentry.io → your project → Settings → Client Keys (or create a project at https://sentry.io). Then:

# Edit and dotenvx-encrypt the new value
cd web
npx dotenvx set NEXT_PUBLIC_SENTRY_DSN "https://<key>@<host>/<project>" -f .env.production --encrypt

# Verify it decrypts back to the right value when DOTENV_PRIVATE_KEY_PRODUCTION is set
DOTENV_PRIVATE_KEY_PRODUCTION=<your-key> npx dotenvx run -f .env.production -- node -e \
  "console.log(process.env.NEXT_PUBLIC_SENTRY_DSN)"

# Redeploy so the new env is baked into the Next.js bundle
bash ../scripts/deploy-web.sh

After redeploy, hit any unhandled error in prod and confirm it shows up on the Sentry dashboard.

3. Cloudflare SSL → Full (strict) with Origin CA cert

Cloudflare is currently in Flexible mode, meaning the Cloudflare-to-origin hop is plaintext HTTP. Anyone between Cloudflare and the VM (datacenter network, ISP, etc.) sees session cookies and POST bodies in the clear.

Steps:

  1. Cloudflare dashboard → SSL/TLS → Origin Server → Create Certificate. Default options (RSA 2048, 15 years). Copy both the cert and private key.

  2. SSH to the box and place them:

    sudo mkdir -p /etc/ssl/codebase
    sudo nano /etc/ssl/codebase/origin.crt   # paste cert
    sudo nano /etc/ssl/codebase/origin.key   # paste key
    sudo chmod 600 /etc/ssl/codebase/origin.key
  3. Update both nginx server blocks to point at the new files:

    sudo nano /etc/nginx/sites-enabled/codebase-foundation
    # ssl_certificate /etc/ssl/codebase/origin.crt;
    # ssl_certificate_key /etc/ssl/codebase/origin.key;
    sudo nginx -t && sudo systemctl reload nginx
  4. Cloudflare dashboard → SSL/TLS → Overview → set to Full (strict). Tests every connection to origin with cert verification. Watch the site stays up for a minute, then verify with curl --resolve codebase.design:443:<origin-ip> https://codebase.design.

  5. (Optional but recommended) SSL/TLS → Edge Certificates → Always Use HTTPS = ON, Minimum TLS Version = 1.2, HSTS = enable.


Operational runbook

Run a backup right now

bash ~/polyvibe-poc/scripts/backup-convex.sh
# → /home/half/backups/convex/db-<ISO-UTC>.sqlite3.gz

Backups also run automatically every 6 hours via the systemd timer. See scripts/backup-convex.README.md for restore steps.

Off-host backup shipping

Once you have an rclone remote configured:

sudo apt-get install rclone
rclone config            # set up your B2/S3/R2/Wasabi remote
BACKUP_REMOTE=b2:codebase-backups bash scripts/backup-convex-offsite.sh

For automated off-site shipping, add a systemd timer that fires after the local backup cron and exports BACKUP_REMOTE. The script refuses to run without it so a misconfigured cron can't silently no-op.

Reclaim DB space (weekly)

bash scripts/vacuum-convex.sh
# → in-place VACUUM, reclaims fragmented space

systemd timer fires Sunday 04:00 (10-min jitter). Verify with:

systemctl --user list-timers codebase-convex-vacuum.timer

Restart everything

pm2 restart all
docker compose -f web/convex-docker/docker-compose.yml restart convex-backend

Deploy a new build

cd ~/polyvibe-poc
git pull
bash scripts/deploy-web.sh

The sidecar build writes to .next.new/, atomically swaps into .next/ on success, reloads PM2, smoke-tests /, and rolls back if health check fails. Production stays serving the old build during the ~14-minute compile.

Check production health

# Frontend
curl -sI -o /dev/null -w "%{http_code}\n" http://127.0.0.1:3002/

# Backend
curl -sI -o /dev/null -w "%{http_code}\n" http://127.0.0.1:3001/cli/published

# Convex
curl -sI -o /dev/null -w "%{http_code}\n" http://127.0.0.1:3210/version

# PM2 process states
pm2 list

Tail logs

pm2 logs codebase-frontend --lines 50
pm2 logs codebase-backend --lines 50

PM2 logs auto-rotate at 50 MB / 14 days (configured via pm2-logrotate).


Security checklist before any major launch event

  • loginctl show-user half --property=Linger → Linger=yes
  • NEXT_PUBLIC_SENTRY_DSN is set and Sentry events arriving
  • Cloudflare SSL mode = Full (strict), origin cert installed
  • ss -lntp shows Convex ports 3210/3211/6791 + Redis 6379 only on 127.0.0.1
  • pm2 list shows both processes online with restart count < 10 in last hour
  • Recent db-*.sqlite3.gz exists in /home/half/backups/convex/
  • At least one off-host backup destination configured
  • .env.production is dotenvx-encrypted (git show HEAD:web/.env.production | grep encrypted: returns lines)
  • OAuth flow works end-to-end on a fresh browser session (codebase auth login from CLI works)
  • curl https://codebase.design/ returns 200 from outside the LAN