# Guide de déploiement du Panel Admin en Production

## 🎯 Objectif

Déployer le panel admin sur le serveur OVH avec accès direct à la base PostgreSQL de production.

## 📋 Architecture

```
[Internet]
    ↓
[Traefik - Port 80/443]
    ↓
    ├─→ admin.web-sentinel.taaazzz-prog.fr → [Frontend React (Nginx)]
    ├─→ admin-api.web-sentinel.taaazzz-prog.fr → [Backend FastAPI]
    └─→ [Base PostgreSQL existante: web-sentinel-db]
```

## ✅ Prérequis

1. **Serveur OVH** : Accès SSH à `taaazzz@web-sentinel.taaazzz-prog.fr`
2. **DNS** : Configurer 2 sous-domaines A pointant vers l'IP du serveur :
   - `admin.web-sentinel.taaazzz-prog.fr`
   - `admin-api.web-sentinel.taaazzz-prog.fr`
3. **Base PostgreSQL** : Container `web-sentinel-db` déjà actif
4. **Traefik** : Reverse proxy avec Let's Encrypt (déjà configuré via Fail Daily)

## 🚀 Déploiement automatique

### Méthode 1 : Script bash (recommandé)

```bash
# Depuis Windows avec Git Bash ou WSL
cd deployment
chmod +x deploy_admin.sh
./deploy_admin.sh
```

### Méthode 2 : Déploiement manuel

#### Étape 1 : Copie des fichiers

```powershell
# Depuis Windows PowerShell
scp -r admin/ taaazzz@web-sentinel.taaazzz-prog.fr:~/web-sentinel/
scp deployment/docker-compose.admin.yml taaazzz@web-sentinel.taaazzz-prog.fr:~/web-sentinel/deployment/
scp deployment/.env.admin taaazzz@web-sentinel.taaazzz-prog.fr:~/web-sentinel/deployment/
```

#### Étape 2 : Générer la clé secrète

```bash
ssh taaazzz@web-sentinel.taaazzz-prog.fr
cd ~/web-sentinel/deployment
openssl rand -hex 32  # Copier le résultat
nano .env.admin  # Remplacer CHANGE_ME_RANDOM_STRING_HERE par la clé générée
```

#### Étape 3 : Build et démarrage

```bash
cd ~/web-sentinel/deployment
docker-compose -f docker-compose.admin.yml build
docker-compose -f docker-compose.admin.yml up -d
```

#### Étape 4 : Vérification

```bash
docker ps | grep admin
# Doit afficher :
# - web-sentinel-admin-backend
# - web-sentinel-admin-frontend

docker logs web-sentinel-admin-backend
docker logs web-sentinel-admin-frontend
```

## 🔧 Configuration DNS

Ajouter dans le gestionnaire DNS d'OVH :

```
Type    Nom                                     Valeur
A       admin.web-sentinel.taaazzz-prog.fr     [IP_SERVEUR_OVH]
A       admin-api.web-sentinel.taaazzz-prog.fr [IP_SERVEUR_OVH]
```

**Note** : La propagation DNS peut prendre 1-24h.

## 📝 Modification de account.html

Remplacer le bouton admin dans `web/account.html` (ligne ~250) :

**Avant (local)** :
```javascript
onclick="window.open('http://localhost:5173', '_blank')"
```

**Après (production)** :
```javascript
onclick="window.open('https://admin.web-sentinel.taaazzz-prog.fr', '_blank')"
```

Puis redéployer :
```powershell
scp web/account.html taaazzz@web-sentinel.taaazzz-prog.fr:~/web-sentinel/web/
ssh taaazzz@web-sentinel.taaazzz-prog.fr "docker restart web-sentinel-web"
```

## 🧪 Tests

### 1. Vérifier les certificats SSL

```bash
curl -I https://admin.web-sentinel.taaazzz-prog.fr
curl -I https://admin-api.web-sentinel.taaazzz-prog.fr
```

### 2. Tester l'API backend

```bash
curl https://admin-api.web-sentinel.taaazzz-prog.fr/api/health
# Devrait retourner : {"status": "ok"}
```

### 3. Accéder au panel

1. Se connecter sur https://web-sentinel.taaazzz-prog.fr/account.html avec un compte SYSOP
2. Cliquer sur "🛠️ Panel Admin"
3. Le panel doit s'ouvrir sur https://admin.web-sentinel.taaazzz-prog.fr

## 🐛 Dépannage

### Problème : "502 Bad Gateway" sur admin.web-sentinel.taaazzz-prog.fr

**Solution** :
```bash
docker logs web-sentinel-admin-frontend
docker logs web-sentinel-admin-backend
docker logs faildaily-traefik-ssl  # Vérifier les routes Traefik
```

### Problème : "CORS error" dans la console du navigateur

**Solution** : Vérifier le fichier `.env.admin` :
```bash
ssh taaazzz@web-sentinel.taaazzz-prog.fr
cat ~/web-sentinel/deployment/.env.admin
# Vérifier ALLOWED_ORIGINS et CORS_ORIGINS
```

### Problème : Erreur de connexion à la base de données

**Solution** :
```bash
# Vérifier que le réseau Docker existe
docker network ls | grep web-sentinel-network

# Si absent, le créer :
docker network create web-sentinel-network

# Reconnecter la base PostgreSQL au réseau
docker network connect web-sentinel-network web-sentinel-db

# Redémarrer les conteneurs admin
docker-compose -f ~/web-sentinel/deployment/docker-compose.admin.yml restart
```

### Problème : Le panel affiche "Failed to fetch"

**Solution** : Vérifier que le backend est accessible :
```bash
curl https://admin-api.web-sentinel.taaazzz-prog.fr/api/licenses
# Si erreur 404 ou 502, vérifier les logs backend
docker logs web-sentinel-admin-backend -f
```

## 📊 Monitoring

### Vérifier les conteneurs actifs

```bash
docker ps --filter "name=admin"
```

### Logs en temps réel

```bash
# Backend
docker logs -f web-sentinel-admin-backend

# Frontend
docker logs -f web-sentinel-admin-frontend
```

### Utilisation des ressources

```bash
docker stats web-sentinel-admin-backend web-sentinel-admin-frontend
```

## 🔐 Sécurité

### Changement de la clé secrète

```bash
ssh taaazzz@web-sentinel.taaazzz-prog.fr
cd ~/web-sentinel/deployment
openssl rand -hex 32 > new_secret.key
nano .env.admin  # Remplacer ADMIN_SECRET_KEY
docker-compose -f docker-compose.admin.yml restart admin-backend
```

### Sauvegarde de la configuration

```bash
ssh taaazzz@web-sentinel.taaazzz-prog.fr
tar -czf ~/backup_admin_$(date +%Y%m%d).tar.gz ~/web-sentinel/admin ~/web-sentinel/deployment/.env.admin
```

## 🔄 Mise à jour du panel

```bash
# Depuis le PC local
git pull origin main
cd deployment
./deploy_admin.sh  # Redéploie automatiquement
```

Ou manuellement :
```bash
scp -r admin/ taaazzz@web-sentinel.taaazzz-prog.fr:~/web-sentinel/
ssh taaazzz@web-sentinel.taaazzz-prog.fr "cd ~/web-sentinel/deployment && docker-compose -f docker-compose.admin.yml up -d --build"
```

## 📞 Support

En cas de problème, vérifier dans l'ordre :
1. Les logs Docker (`docker logs`)
2. Les routes Traefik (`docker logs faildaily-traefik-ssl`)
3. La connectivité réseau (`docker network inspect web-sentinel-network`)
4. La configuration DNS (propagation complète ?)
5. Les certificats SSL (Traefik génère automatiquement via Let's Encrypt)
