# Deploying the Defentra Agent on cybershaan.com

This hosts the agent's **dashboard** at `https://agent.cybershaan.com`. It's a
prepare-only tool (you publish to LinkedIn yourself), so no inbound LinkedIn
setup is needed. Works with **no Anthropic key** (template drafts); add one later
to upgrade to AI-written posts.

Pick **one** of the three methods below. PM2 is the simplest.

---

## 0. Prerequisites (on the server, once)

- A Linux server (Ubuntu/Debian assumed) that you control.
- **Node.js ≥ 20** and npm:
  ```bash
  curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
  sudo apt-get install -y nodejs
  ```
- **Chromium** (for poster rendering) + Nginx + certbot:
  ```bash
  sudo apt-get install -y chromium nginx certbot python3-certbot-nginx
  ```
  (On some distros the package is `chromium-browser`.)
- **DNS:** add an `A` record `agent.cybershaan.com` → your server's IP.
- **Firewall:** allow 80/443 (and your SSH port):
  ```bash
  sudo ufw allow 'Nginx Full' && sudo ufw allow OpenSSH && sudo ufw enable
  ```

---

## 1. Get the code onto the server

From your Mac (where the project lives), copy the archive up and unpack it:

```bash
# on your Mac
scp defentra-linkedin-agent.tar.gz you@cybershaan.com:/tmp/

# on the server
sudo mkdir -p /opt/defentra-linkedin-agent
sudo tar -xzf /tmp/defentra-linkedin-agent.tar.gz -C /opt/defentra-linkedin-agent --strip-components=1
sudo chown -R $USER:$USER /opt/defentra-linkedin-agent
cd /opt/defentra-linkedin-agent
```

Install dependencies:

```bash
npm install            # downloads deps (+ Chromium unless one is installed)
# If Chromium is already installed and you want to skip the download:
# PUPPETEER_SKIP_DOWNLOAD=1 npm install
```

Create and edit `.env`:

```bash
cp .env.example .env
nano .env
```

**Must set:**
- `DASHBOARD_PASSWORD` — **change it from the default** to something strong.
- `PUBLIC_BASE_URL=https://agent.cybershaan.com`

**Optional:**
- `ANTHROPIC_API_KEY` — only if you want AI-written captions.
- `NVD_API_KEY` — free, speeds CVE checks (https://nvd.nist.gov/developers/request-an-api-key).
- `SCHEDULER_ENABLED=true` — to auto-prepare a batch morning & evening.
- `PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium` — if Puppeteer can't find a browser.

Quick smoke test (Ctrl+C to stop):
```bash
npm run server      # should log: listening on :8099
```

---

## 2A. Run it with PM2  (recommended)

```bash
sudo npm install -g pm2
pm2 start ecosystem.config.cjs
pm2 save
pm2 startup           # run the command it prints, to start on boot
pm2 logs defentra-agent   # watch logs
```

## 2B. Run it with systemd

```bash
sudo cp deploy/defentra-agent.service /etc/systemd/system/
# edit User/WorkingDirectory in that file if needed
sudo systemctl daemon-reload
sudo systemctl enable --now defentra-agent
sudo systemctl status defentra-agent
journalctl -u defentra-agent -f
```

## 2C. Run it with Docker

```bash
# .env must exist first (step 1)
docker compose up -d --build
docker compose logs -f
```

---

## 3. Put Nginx + HTTPS in front

```bash
sudo cp deploy/nginx-agent.conf /etc/nginx/sites-available/agent.cybershaan.com
sudo ln -s /etc/nginx/sites-available/agent.cybershaan.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

# issue the TLS cert (certbot edits the Nginx config for you)
sudo certbot --nginx -d agent.cybershaan.com
```

Now open **https://agent.cybershaan.com** and log in with `DASHBOARD_USER` /
`DASHBOARD_PASSWORD`. Click **Fetch latest posts**.

---

## 4. Updating later

```bash
cd /opt/defentra-linkedin-agent
# upload a new tarball and unpack over the top (keeps your .env and data/)
sudo tar -xzf /tmp/defentra-linkedin-agent.tar.gz -C /opt/defentra-linkedin-agent --strip-components=1
npm install
pm2 restart defentra-agent        # or: sudo systemctl restart defentra-agent
```

Your `.env` and `data/` (posts + posters) are never in the archive, so updates
don't touch them.

---

## Security checklist (important for a public host)

- [ ] **Changed `DASHBOARD_PASSWORD`** from the default to a strong value.
- [ ] Dashboard reachable **only over HTTPS** (certbot done).
- [ ] App listens on `127.0.0.1:8099` / behind Nginx, **not** exposed directly.
      (With Docker it's bound to `127.0.0.1`; for PM2/systemd keep `PORT=8099`
      and don't open 8099 in the firewall.)
- [ ] `.env` and `data/db.json` kept private (not world-readable):
      `chmod 600 .env`.
- [ ] Only ports 80/443/SSH open in the firewall.

The dashboard already has HTTP Basic auth, a brute-force lockout (8 fails →
15-min block), and hardened headers — but a strong password + HTTPS are on you.

---

## Troubleshooting

- **Poster render fails / Chromium error** → `sudo apt-get install -y chromium`
  and set `PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium` in `.env`.
- **502 from Nginx** → the app isn't running; check `pm2 logs` /
  `journalctl -u defentra-agent`.
- **Dashboard loads but "Failed to load"** → wrong `PUBLIC_BASE_URL` or you
  opened `http://` instead of `https://`.
- **Batch is slow** → add a free `NVD_API_KEY`; without a key NVD lookups are
  heavily rate-limited.
