← back to Small Business Builder

DEPLOY.md

77 lines

# smb-builder → Kamatera deploy

Target: `45.61.58.125:9760` (IP:port, no public domain yet).

## Why a script and not "just go"

Standing rule (`feedback_pm2_deploy_explicit_auth.md`): pm2 actions on cncp-adjacent / production hosts need explicit deploy-intent. "go" / "y" is not enough. So the deploy is in your hands; this prep is everything-but-the-button.

## Pre-reqs

- New ADMIN_TOKEN was generated to `/tmp/.smb_admin_token_<pid>` (chmod 600, owner-only). The path is in your shell history from this session — find it with `ls /tmp/.smb_admin_token_*`.
- SSH access to `root@45.61.58.125` is configured.
- Local PG `small_business_directory` already exists; remote DB will be created fresh.

## Running the deploy

```bash
# 1. expose the new token in your shell WITHOUT putting it in argv:
set -a
source /tmp/.smb_admin_token_<pid>   # token file is just the value, not KEY=val — see step 1a
set +a

# 1a. ALTERNATIVE if the file is a bare token (no KEY=val), inject as env directly:
export ADMIN_TOKEN="$(cat /tmp/.smb_admin_token_<pid>)"

# 2. run the deploy script:
bash ~/Projects/small-business-builder/scripts/deploy-kamatera.sh

# 3. clean up:
unset ADMIN_TOKEN
rm /tmp/.smb_admin_token_*
```

## What the script does (6 steps)

1. **rsync code** → `kamatera:/root/Projects/small-business-builder` (excludes node_modules / .git / .env / logs)
2. **Ensure DB** exists (idempotent — checks first, only creates if missing)
3. **Run migrations** (`migrations/001_init.sql`, `migrations/002_website_analysis.sql`)
4. **Write .env** via stdin pipe (token never enters argv / ps / shell history)
5. **npm install --omit=dev** + **pm2 start + save**
6. **Smoke test** `http://localhost:9760/` from inside Kamatera

## Verify after

```bash
# from your laptop (or this Mac):
curl -i http://45.61.58.125:9760/healthz
curl -i -H "x-admin-token: $ADMIN_TOKEN" http://45.61.58.125:9760/admin
```

## Rollback

If something breaks:
```bash
ssh root@45.61.58.125 "pm2 stop smb-builder && pm2 delete smb-builder"
ssh root@45.61.58.125 "psql -U postgres -c 'DROP DATABASE small_business_directory'"
```

## Next steps after IP:port works

- Pick a public domain (smbbuilder.com? small-business-builder.com? CNCP entry says nothing yet)
- Add nginx config + Let's Encrypt SSL on Kamatera
- Update DNS via `domain-name-agent`
- Route ADMIN_TOKEN into `secrets-manager` for future rotation:
  ```bash
  echo "SMB_ADMIN_TOKEN=$ADMIN_TOKEN" | node ~/Projects/secrets-manager/cli.js import-paste
  ```

## Rule check

- ✅ explicit auth: you trigger the deploy, not me
- ✅ no secret in bash argv: stdin pipe + heredoc only
- ✅ no DNS changes (no public domain yet)
- ✅ no Mac2 services touched
- ✅ idempotent — re-running is safe
- ⚠ remote prod side effects: pm2 process created, new PG database. Both reversible (see Rollback).