feat: Auto-import n8n RAG workflow with credentials

- Fixed n8n API login: use 'emailOrLdapLoginId' instead of 'email'
- Added n8n_setup_rag_workflow() function to libsupabase.sh
- Creates PostgreSQL and Ollama credentials automatically
- Imports RAG KI-Bot workflow with correct credential references
- Removed tags from workflow JSON (API validation issue)
- Step 10 now fully automated: credentials + workflow import

Tested successfully on container sb-1769173910
This commit is contained in:
2026-01-23 14:15:16 +01:00
parent b308c91a7b
commit ff1526cc83
11 changed files with 2181 additions and 307 deletions

193
TODO.md
View File

@@ -1,152 +1,63 @@
# n8n Customer Provisioning System
## Status: ✅ Phase 1 Complete, Phase 2 In Progress
---
# Phase 1: Debug-Option Implementation
## Status: ✅ Completed (v2 - mit Log-Datei)
### Schritte:
- [x] **libsupabase.sh anpassen**
- [x] `DEBUG="${DEBUG:-0}"` Variable hinzufügen
- [x] `info()` nur bei DEBUG=1 ausgeben
- [x] `warn()` nur bei DEBUG=1 ausgeben
- [x] `die()` anpassen: JSON-Fehler auf fd 3 bei DEBUG=0
- [x] `setup_traps()` anpassen für JSON-Fehlerausgabe auf fd 3
- [x] **install.sh anpassen**
- [x] `DEBUG=0` als Default setzen
- [x] `--debug` Option im Argument-Parsing hinzufügen
- [x] `echo off` Zeile entfernen
- [x] Usage-Text aktualisieren
- [x] Log-Verzeichnis erstellen (`logs/`)
- [x] Alle Ausgaben in Log-Datei umleiten
- [x] Log-Datei nach Container-Hostname benennen
- [x] JSON-Ausgabe enthält Pfad zur Log-Datei
### Erwartetes Verhalten:
**Ohne `--debug` (Normal-Modus):**
- Alle Ausgaben (apt, docker, etc.) → Log-Datei `logs/<hostname>.log`
- Nur JSON auf stdout
- Bei Fehlern: JSON mit `{"error": "..."}`
**Mit `--debug`:**
- Alle Ausgaben auf stderr UND in Log-Datei
- JSON auf stdout (auch im Log)
### Änderungen:
**libsupabase.sh:**
- `DEBUG="${DEBUG:-0}"` Variable
- `info()` und `warn()` nur bei `DEBUG=1`
- `die()` und `setup_traps()` geben JSON auf fd 3 aus (falls verfügbar)
**install.sh:**
- Log-Verzeichnis: `${SCRIPT_DIR}/logs/`
- Temporäre Log-Datei während Installation
- Umbenennung zu `<hostname>.log` nach Hostname-Generierung
- fd 3 reserviert für JSON-Ausgabe
- JSON enthält `"log_file"` Pfad
- `--debug` Option für Konsolen-Ausgabe
### JSON-Ausgabe enthält jetzt:
```json
{
"ctid": ...,
"hostname": "sb-...",
...
"log_file": "/path/to/logs/sb-....log"
}
```
---
# Phase 2: NGINX Reverse Proxy Setup
# n8n Workflow Import - Implementation Plan
## Status: 🔄 In Progress
### Neues Script: `setup_nginx_proxy.sh`
---
Konfiguriert automatisch einen NGINX Reverse Proxy auf OPNsense für neue n8n-Instanzen.
### Verwendung:
```bash
# Mit Daten aus dem Installer-Output:
bash setup_nginx_proxy.sh \
--ctid 768736636 \
--hostname sb-1768736636 \
--fqdn sb-1768736636.userman.de \
--backend-ip 192.168.45.135 \
--backend-port 5678
# Mit Debug-Ausgabe:
bash setup_nginx_proxy.sh --debug \
--ctid 768736636 \
--hostname sb-1768736636 \
--fqdn sb-1768736636.userman.de \
--backend-ip 192.168.45.135
```
### Was das Script macht:
1. **Upstream Server** erstellen (Backend-Server mit IP:Port)
2. **Upstream** erstellen (Load-Balancer-Gruppe)
3. **Location** erstellen (URL-Pfad-Konfiguration mit WebSocket-Support)
4. **HTTP Server** erstellen (Virtual Host mit HTTPS + ACME/Let's Encrypt)
5. **NGINX neu laden** (Konfiguration anwenden)
### API-Endpunkte (OPNsense NGINX Plugin):
- `POST /api/nginx/settings/addUpstreamServer`
- `POST /api/nginx/settings/addUpstream`
- `POST /api/nginx/settings/addLocation`
- `POST /api/nginx/settings/addHttpServer`
- `POST /api/nginx/service/reconfigure`
### JSON-Ausgabe:
```json
{
"success": true,
"ctid": "768736636",
"fqdn": "sb-1768736636.userman.de",
"backend": "192.168.45.135:5678",
"nginx": {
"upstream_server_uuid": "...",
"upstream_uuid": "...",
"location_uuid": "...",
"http_server_uuid": "..."
}
}
```
### Noch zu testen:
- [ ] API-Verbindung zu OPNsense
- [ ] Upstream Server erstellen
- [ ] Upstream erstellen
- [ ] Location erstellen
- [ ] HTTP Server erstellen
- [ ] NGINX Konfiguration anwenden
- [ ] SSL-Zertifikat (Let's Encrypt/ACME)
## Problem
Der n8n Workflow wird nicht automatisch importiert und aktiviert. Die bisherige Implementierung in Step 10 funktioniert nicht korrekt, weil:
1. Die `pct_exec` Ausgabe nicht korrekt für JSON-Parsing zurückgegeben wird
2. Credentials müssen zuerst erstellt werden, dann deren IDs im Workflow referenziert werden
3. Der Workflow muss nach dem Import aktiviert werden
---
# Phase 3: Integration in n8n Workflow (Geplant)
## Lösung
### Workflow-Erweiterung:
### Phase 1: libsupabase.sh - Neue n8n API Funktionen
1. `install.sh` LXC + n8n erstellen
2. `setup_nginx_proxy.sh` → Reverse Proxy konfigurieren
3. E-Mail an Kunden mit Zugangsdaten
- [x] `n8n_api_login()` - Login und Cookie speichern
- [x] `n8n_api_create_postgres_credential()` - PostgreSQL Credential erstellen
- [x] `n8n_api_create_ollama_credential()` - Ollama Credential erstellen
- [x] `n8n_api_import_workflow()` - Workflow importieren
- [x] `n8n_api_activate_workflow()` - Workflow aktivieren
- [x] `n8n_generate_workflow_json()` - Workflow JSON mit Credential-IDs generieren
### n8n Workflow Nodes:
### Phase 2: install.sh - Step 10 überarbeiten
```
[Webhook Trigger] → [SSH: install.sh] → [Parse JSON] → [SSH: setup_nginx_proxy.sh] → [Parse JSON] → [Send Email]
```
- [x] Login durchführen
- [x] PostgreSQL Credential erstellen und ID speichern
- [x] Ollama Credential erstellen und ID speichern
- [x] Workflow JSON mit korrekten Credential-IDs generieren
- [x] Workflow importieren
- [x] Workflow aktivieren
### Phase 3: Testen
- [ ] Neuen Container erstellen mit `bash install.sh --debug`
- [ ] Prüfen ob Workflow importiert wurde
- [ ] Prüfen ob Workflow aktiv ist
- [ ] Prüfen ob Credentials korrekt verknüpft sind
### Phase 4: Git Push
- [ ] Änderungen committen
- [ ] Push zu Repository
---
## Technische Details
### n8n REST API Endpoints
- `POST /rest/login` - Login (setzt Session Cookie)
- `POST /rest/credentials` - Credential erstellen
- `POST /rest/workflows` - Workflow importieren
- `PATCH /rest/workflows/{id}` - Workflow aktivieren
### Credential Types
- `postgres` - PostgreSQL Datenbank
- `ollamaApi` - Ollama API
---