# Wakel AI Instance Deployment - Session Summary

**Date:** 2026-06-27 to 2026-06-28
**Project:** Wakel AI Multi-tenant SaaS Platform
**Location:** /home/ashraffarid2010/wakelai.com

---

## Current Status

### Working Instances (HTTP only)
- ✅ `http://test3-o45wo.wakelai.com` → Hermes Agent (port 9500)
- ✅ `http://test4-o45wo.wakelai.com` → Hermes Agent (port 9501)  
- ✅ `http://test5-o45wo.wakelai.com` → Hermes Agent (port 9502)

### Broken Instances (Need Re-creation)
- ❌ `ee-o45wo.wakelai.com` - Shows landing page (created before fix)
- ❌ `er-o45wo.wakelai.com` - Shows landing page (created before fix)

### HTTPS Issue
- All instances show landing page on HTTPS
- HTTP works correctly
- Need wildcard SSL certificate for `*.wakelai.com`

---

## What Was Fixed

### 1. Docker Port Exposure
**Problem:** Containers created without exposed ports
**Solution:** Updated `src/lib/dokploy.ts` to add port mapping in docker-compose

### 2. Port Allocation System  
**Problem:** No port tracking, conflicts possible
**Solution:** Created `src/lib/port-allocator.ts` with dynamic allocation (9500-9999)

### 3. nginx Configuration
**Problem:** nginx routing to main app instead of instances
**Solution:** 
- Created `src/lib/nginx-manager.ts` for dynamic nginx configs
- Updated to use actual IP (203.161.35.97) instead of localhost
- Instance configs route to specific ports

### 4. Database Schema
**Problem:** No port field in Instance model
**Solution:** Added `port Int? @unique` to Instance model

---

## Files Modified

1. `/home/ashraffarid2010/wakelai.com/src/lib/dokploy.ts`
2. `/home/ashraffarid2010/wakelai.com/src/lib/port-allocator.ts`
3. `/home/ashraffarid2010/wakelai.com/src/lib/nginx-manager.ts`
4. `/home/ashraffarid2010/wakelai.com/src/app/api/instances/route.ts`
5. `/home/ashraffarid2010/wakelai.com/src/app/api/instances/[id]/route.ts`
6. `/home/ashraffarid2010/wakelai.com/prisma/schema.prisma`
7. `/etc/nginx/conf.d/00-wakelai.com.conf`
8. `/etc/nginx/conf.d/99-wakelai-instances-https.conf`

---

## Pending Tasks

### 1. Fix ee-o45wo and er-o45wo Instances
These instances were created before code fixes:
- No port assigned in database
- No nginx config
- Containers running but not accessible

**Fix process:**
```bash
# Delete old instance
DELETE FROM "Instance" WHERE slug = 'ee-o45wo';
docker rm -f genney-ee-o45wo-hermes-ee-o45wo-1;
rm -f /etc/nginx/conf.d/instance-ee-o45wo.conf;

# Re-create with updated code (use create_instance script)
```

### 2. Obtain Wildcard SSL Certificate
**Current blocker:** Need to add DNS TXT record

**DNS Challenge Value:** `nZUjDcUfZtMWV-jQXFhsYK3OVxiBjak9qWxlyIe195g`

**Record to add:**
- Name: `_acme-challenge.wakelai.com`
- Type: `TXT`
- Value: `nZUjDcUfZtMWV-jQXFhsYK3OVxiBjak9qWxlyIe195g`

**DNS Provider:** happy-times.me (external, not on this server)

After adding DNS record, complete certificate request:
```bash
certbot certonly --manual --preferred-challenges dns \
  -d '*.wakelai.com' -d 'wakelai.com' \
  --agree-tos --no-eff-email
```

Then update nginx configs to use wildcard cert.

---

## Environment Variables

```bash
NEXT_PUBLIC_APP_URL=https://wakelai.com
NEXTAUTH_URL=https://wakelai.com
DATABASE_URL=postgresql://wakelai:wakelai_password@localhost:55433/wakelai?schema=public
APP_ROOT_DOMAIN=wakelai.com
HERMES_DOCKER_NETWORK=wakelai-runtime
HERMES_IMAGE=ghcr.io/hostinger/hvps-hermes-agent:latest
HERMES_INTERNAL_PORT=4860
DOKPLOY_API_MODE=local-docker
```

---

## Port Allocation

- Main Next.js App: 9100
- Instance Range: 9500-9999 (50 instances max)
- Docker Network: wakelai-runtime

---

## nginx Configuration Summary

**HTTP (Working):**
- Main domain: wakelai.com → port 9100
- Instance subdomains: *.wakelai.com → specific ports (9500+)

**HTTPS (Partial):**
- Main domain: wakelai.com, www.wakelai.com → port 9100 (SSL cert valid)
- Instance subdomains: Shows landing page (no wildcard SSL cert)

---

## Database Connection

```bash
psql -U wakelai -h localhost -p 55433 -d wakelai
```

**Check instances:**
```sql
SELECT id, name, slug, subdomain, status, "dokployComposeId", port 
FROM "Instance" 
ORDER BY "createdAt" DESC;
```

---

## Instance Creation Script (for reference)

```typescript
import { PrismaClient } from '@prisma/client';
import { provisionHermesOnDokploy } from './src/lib/dokploy';
import { allocatePort } from './src/lib/port-allocator';
import { createInstanceNginxConfig } from './src/lib/nginx-manager';
import { slugify } from './src/lib/utils';

const prisma = new PrismaClient();

async function recreateInstance(name: string, userId: string) {
  const baseSlug = slugify(name);
  const slug = `${baseSlug}-o45wo`;
  const subdomain = `${slug}.wakelai.com`;
  
  const port = await allocatePort();
  
  const instance = await prisma.instance.create({
    data: { name, slug, subdomain, userId, status: 'CREATING', port }
  });
  
  const result = await provisionHermesOnDokploy({
    name: instance.name,
    slug: instance.slug,
    subdomain: instance.subdomain,
    ownerId: userId
  }, port);
  
  await createInstanceNginxConfig(slug, port);
  
  await prisma.instance.update({
    where: { id: instance.id },
    data: { status: 'RUNNING', dokployComposeId: result.composeId }
  });
  
  await prisma.$disconnect();
}
```

---

## Next Steps When Continuing

1. **Fix ee and er instances** - Use re-creation process above
2. **Complete SSL certificate** - Add DNS TXT record and finish certbot request
3. **Update nginx** - Add HTTPS configs for instances after obtaining wildcard cert
4. **Test all instances** - Verify both HTTP and HTTPS work

---

## Important Notes

- Application runs on port 9100 (set with `PORT=9100 npm run start`)
- New instances created via web UI will work correctly if app is running with updated code
- HTTPS for instances requires wildcard SSL certificate (pending DNS update)
- All documentation saved in `DEPLOYMENT_ARCHITECTURE.md` and `IMPLEMENTATION_GUIDE.md`
