# Strapi Installation in cms Jail

## Prerequisites

- `db` jail running at 192.168.100.2 with PostgreSQL 18
- `cms` jail provisioned at 192.168.100.3 (see warden-bootstrap)
- Node.js 24 installed inside cms jail
- Network connectivity between cms and db jails (warden0 bridge)

## Step 1: ZFS snapshot

Always snapshot before changes:

```sh
# on host — dry-run first
sudo zfs snapshot -nv zroot/clawdie-runtime/jails/db@pre-strapi-DD-mon-YYYY
sudo zfs snapshot -nv zroot/clawdie-runtime/jails/cms@pre-strapi-DD-mon-YYYY

# then execute
sudo zfs snapshot zroot/clawdie-runtime/jails/db@pre-strapi-DD-mon-YYYY
sudo zfs snapshot zroot/clawdie-runtime/jails/cms@pre-strapi-DD-mon-YYYY
```

## Step 2: Provision cms jail

Use warden-bootstrap pattern to create the cms jail:

```sh
# on host
sudo bastille create -T cms 15.0-RELEASE 192.168.100.3 warden0 -g 192.168.100.1
sudo bastille config cms set host.hostname cms.clawdie.si
sudo bastille restart cms
```

Verify:

```sh
jls
sudo jexec cms hostname    # should show cms.clawdie.si
sudo jexec cms ping -c 1 192.168.100.2  # should reach db jail
```

## Step 3: Install Node.js in cms jail

```sh
sudo jexec cms pkg install -y node24 npm git
sudo jexec cms node --version
```

## Step 4: Create PostgreSQL database for Strapi (in db jail)

```sh
sudo jexec db su - postgres -c "createdb strapi_cms"
sudo jexec db su - postgres -c "createuser strapi_user"
sudo jexec db su - postgres -c "psql -c \"ALTER USER strapi_user WITH PASSWORD 'changeme';\""
sudo jexec db su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE strapi_cms TO strapi_user;\""
```

Change the password to something secure. Store it in the Strapi `.env`.

### Allow remote connections from cms jail

Edit `pg_hba.conf` inside db jail to allow connections from 192.168.100.3:

```sh
sudo jexec db su - postgres -c "echo 'host strapi_cms strapi_user 192.168.100.3/32 scram-sha-256' >> /var/db/postgres/data18/pg_hba.conf"
```

Edit `postgresql.conf` to listen on all interfaces:

```sh
sudo jexec db su - postgres -c "sed -i '' \"s/#listen_addresses = 'localhost'/listen_addresses = '*'/\" /var/db/postgres/data18/postgresql.conf"
```

Reload PostgreSQL:

```sh
sudo jexec db service postgresql reload
```

Verify from cms jail:

```sh
sudo jexec cms pkg install -y postgresql16-client
sudo jexec cms psql -h 192.168.100.2 -U strapi_user -d strapi_cms -c "SELECT 1;"
```

## Step 5: Create Strapi project

```sh
sudo jexec cms su - clawdie -c "cd /home/clawdie && npx create-strapi-app@latest strapi --no-run"
```

Select PostgreSQL when prompted, or configure after creation.

## Step 6: Configure PostgreSQL connection

Create `/home/clawdie/strapi/.env` inside cms jail:

```
DATABASE_HOST=192.168.100.2
DATABASE_PORT=5432
DATABASE_NAME=strapi_cms
DATABASE_USERNAME=strapi_user
DATABASE_PASSWORD=changeme
NODE_OPTIONS=--max-old-space-size=512
```

Edit `config/database.js`:

```js
module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: env('DATABASE_HOST', '192.168.100.2'),
      port: env.int('DATABASE_PORT', 5432),
      database: env('DATABASE_NAME', 'strapi_cms'),
      user: env('DATABASE_USERNAME', 'strapi_user'),
      password: env('DATABASE_PASSWORD', ''),
      ssl: false,
    },
  },
});
```

## Step 7: Install PostgreSQL client dependency

```sh
sudo jexec cms su - clawdie -c "cd /home/clawdie/strapi && npm install pg"
```

## Step 8: Build and start

```sh
# build admin panel
sudo jexec cms su - clawdie -c "cd /home/clawdie/strapi && npm run build"

# start in development mode first to create admin user
sudo jexec cms su - clawdie -c "cd /home/clawdie/strapi && npm run develop"
```

## Step 9: Create admin user

Open `http://192.168.100.3:1337/admin` from a Tailscale-connected browser.
Create the first admin user through the registration form.

## Step 10: Verify API

```sh
curl -s http://192.168.100.3:1337/api | python3 -m json.tool
```

## Step 11: Post-install snapshot

```sh
sudo zfs snapshot -nv zroot/clawdie-runtime/jails/cms@strapi-installed-DD-mon-YYYY
sudo zfs snapshot zroot/clawdie-runtime/jails/cms@strapi-installed-DD-mon-YYYY
```
