Testing: - Add pytest configuration (pytest.ini) - Add test fixtures (tests/conftest.py) - Add ContentGenerator tests (13 tests) - Add ContentScheduler tests (16 tests) - Add PublisherManager tests (16 tests) - All 45 tests passing Production Docker: - Add docker-compose.prod.yml with healthchecks, resource limits - Add Dockerfile.prod with multi-stage build, non-root user - Add nginx.prod.conf with SSL, rate limiting, security headers - Add .env.prod.example template Maintenance Scripts: - Add backup.sh for database and media backups - Add restore.sh for database restoration - Add cleanup.sh for log rotation and Docker cleanup - Add healthcheck.sh with Telegram alerts Documentation: - Add DEPLOY.md with complete deployment guide Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
359 lines
7.2 KiB
Markdown
359 lines
7.2 KiB
Markdown
# Deployment Guide
|
|
|
|
## Social Media Automation System
|
|
|
|
Guía completa para desplegar el sistema en producción.
|
|
|
|
---
|
|
|
|
## Requisitos del Sistema
|
|
|
|
### Hardware Mínimo
|
|
- **CPU**: 2 cores
|
|
- **RAM**: 4GB
|
|
- **Disco**: 20GB SSD
|
|
- **Red**: Conexión estable a internet
|
|
|
|
### Software
|
|
- Docker 24.0+
|
|
- Docker Compose 2.20+
|
|
- Git
|
|
|
|
### Puertos Requeridos
|
|
- **80**: HTTP (redirección a HTTPS)
|
|
- **443**: HTTPS (aplicación principal)
|
|
|
|
---
|
|
|
|
## Instalación Rápida
|
|
|
|
```bash
|
|
# 1. Clonar repositorio
|
|
git clone https://git.consultoria-as.com/consultoria-as/social-media-automation.git
|
|
cd social-media-automation
|
|
|
|
# 2. Crear archivo de configuración
|
|
cp .env.prod.example .env.prod
|
|
|
|
# 3. Editar configuración
|
|
nano .env.prod
|
|
|
|
# 4. Generar certificados SSL (ver sección SSL)
|
|
|
|
# 5. Iniciar servicios
|
|
docker-compose -f docker-compose.prod.yml up -d
|
|
|
|
# 6. Verificar estado
|
|
docker-compose -f docker-compose.prod.yml ps
|
|
```
|
|
|
|
---
|
|
|
|
## Configuración Detallada
|
|
|
|
### Variables de Entorno (.env.prod)
|
|
|
|
#### Base de Datos
|
|
```bash
|
|
POSTGRES_USER=social_automation
|
|
POSTGRES_PASSWORD=<contraseña_segura>
|
|
POSTGRES_DB=social_automation
|
|
```
|
|
|
|
#### Aplicación
|
|
```bash
|
|
SECRET_KEY=<genera_con: openssl rand -hex 32>
|
|
ENVIRONMENT=production
|
|
```
|
|
|
|
#### APIs de Redes Sociales
|
|
|
|
**X (Twitter)**
|
|
1. Ve a https://developer.twitter.com
|
|
2. Crea una app con permisos de lectura/escritura
|
|
3. Genera tokens de acceso
|
|
|
|
```bash
|
|
X_API_KEY=<api_key>
|
|
X_API_SECRET=<api_secret>
|
|
X_ACCESS_TOKEN=<access_token>
|
|
X_ACCESS_SECRET=<access_secret>
|
|
X_BEARER_TOKEN=<bearer_token>
|
|
```
|
|
|
|
**Meta (Facebook, Instagram, Threads)**
|
|
1. Ve a https://developers.facebook.com
|
|
2. Crea una app tipo "Business"
|
|
3. Agrega productos: Facebook Login, Instagram Graph API
|
|
4. Genera token de página con permisos:
|
|
- `pages_manage_posts`
|
|
- `pages_read_engagement`
|
|
- `instagram_basic`
|
|
- `instagram_content_publish`
|
|
|
|
```bash
|
|
META_ACCESS_TOKEN=<page_access_token>
|
|
FACEBOOK_PAGE_ID=<page_id>
|
|
INSTAGRAM_ACCOUNT_ID=<instagram_business_id>
|
|
THREADS_USER_ID=<threads_user_id>
|
|
```
|
|
|
|
**DeepSeek API**
|
|
1. Ve a https://platform.deepseek.com
|
|
2. Genera una API key
|
|
|
|
```bash
|
|
DEEPSEEK_API_KEY=<api_key>
|
|
DEEPSEEK_BASE_URL=https://api.deepseek.com
|
|
```
|
|
|
|
**ImgBB (subida de imágenes)**
|
|
1. Ve a https://api.imgbb.com
|
|
2. Genera una API key
|
|
|
|
```bash
|
|
IMGBB_API_KEY=<api_key>
|
|
```
|
|
|
|
**Telegram (notificaciones)**
|
|
1. Habla con @BotFather en Telegram
|
|
2. Crea un bot con `/newbot`
|
|
3. Obtén el chat_id con @userinfobot
|
|
|
|
```bash
|
|
TELEGRAM_BOT_TOKEN=<bot_token>
|
|
TELEGRAM_CHAT_ID=<chat_id>
|
|
```
|
|
|
|
---
|
|
|
|
## Certificados SSL
|
|
|
|
### Opción 1: Let's Encrypt (Recomendado)
|
|
|
|
```bash
|
|
# Instalar certbot
|
|
apt install certbot
|
|
|
|
# Generar certificados
|
|
certbot certonly --standalone -d tu-dominio.com
|
|
|
|
# Copiar a nginx
|
|
cp /etc/letsencrypt/live/tu-dominio.com/fullchain.pem nginx/ssl/
|
|
cp /etc/letsencrypt/live/tu-dominio.com/privkey.pem nginx/ssl/
|
|
|
|
# Renovación automática (agregar a crontab)
|
|
0 0 1 * * certbot renew --quiet && docker-compose -f docker-compose.prod.yml restart nginx
|
|
```
|
|
|
|
### Opción 2: Certificado Autofirmado (Solo desarrollo)
|
|
|
|
```bash
|
|
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
|
|
-keyout nginx/ssl/privkey.pem \
|
|
-out nginx/ssl/fullchain.pem \
|
|
-subj "/CN=localhost"
|
|
```
|
|
|
|
---
|
|
|
|
## Comandos Útiles
|
|
|
|
### Gestión de Servicios
|
|
|
|
```bash
|
|
# Iniciar todos los servicios
|
|
docker-compose -f docker-compose.prod.yml up -d
|
|
|
|
# Detener todos los servicios
|
|
docker-compose -f docker-compose.prod.yml down
|
|
|
|
# Ver logs en tiempo real
|
|
docker-compose -f docker-compose.prod.yml logs -f
|
|
|
|
# Ver logs de un servicio específico
|
|
docker-compose -f docker-compose.prod.yml logs -f app
|
|
|
|
# Reiniciar un servicio
|
|
docker-compose -f docker-compose.prod.yml restart app
|
|
|
|
# Ver estado de los contenedores
|
|
docker-compose -f docker-compose.prod.yml ps
|
|
```
|
|
|
|
### Base de Datos
|
|
|
|
```bash
|
|
# Ejecutar migraciones
|
|
docker-compose -f docker-compose.prod.yml exec app alembic upgrade head
|
|
|
|
# Acceder a PostgreSQL
|
|
docker exec -it social-automation-db psql -U social_automation
|
|
|
|
# Backup manual
|
|
./scripts/maintenance/backup.sh
|
|
|
|
# Restaurar backup
|
|
./scripts/maintenance/restore.sh backups/database/db_backup_YYYYMMDD.sql.gz
|
|
```
|
|
|
|
### Celery
|
|
|
|
```bash
|
|
# Ver tareas activas
|
|
docker exec social-automation-flower celery -A app.worker.celery_app inspect active
|
|
|
|
# Ver tareas programadas
|
|
docker exec social-automation-flower celery -A app.worker.celery_app inspect scheduled
|
|
|
|
# Purgar cola
|
|
docker exec social-automation-worker celery -A app.worker.celery_app purge -f
|
|
```
|
|
|
|
---
|
|
|
|
## Mantenimiento
|
|
|
|
### Tareas Programadas (Crontab)
|
|
|
|
```bash
|
|
# Editar crontab
|
|
crontab -e
|
|
|
|
# Agregar las siguientes líneas:
|
|
|
|
# Backup diario a las 2 AM
|
|
0 2 * * * /ruta/al/proyecto/scripts/maintenance/backup.sh >> /var/log/backup.log 2>&1
|
|
|
|
# Limpieza semanal los domingos a las 3 AM
|
|
0 3 * * 0 /ruta/al/proyecto/scripts/maintenance/cleanup.sh >> /var/log/cleanup.log 2>&1
|
|
|
|
# Health check cada 5 minutos
|
|
*/5 * * * * /ruta/al/proyecto/scripts/maintenance/healthcheck.sh > /dev/null 2>&1
|
|
```
|
|
|
|
### Actualizaciones
|
|
|
|
```bash
|
|
# 1. Hacer backup
|
|
./scripts/maintenance/backup.sh
|
|
|
|
# 2. Obtener cambios
|
|
git pull origin main
|
|
|
|
# 3. Reconstruir imágenes
|
|
docker-compose -f docker-compose.prod.yml build
|
|
|
|
# 4. Aplicar migraciones
|
|
docker-compose -f docker-compose.prod.yml exec app alembic upgrade head
|
|
|
|
# 5. Reiniciar servicios
|
|
docker-compose -f docker-compose.prod.yml up -d
|
|
|
|
# 6. Verificar
|
|
./scripts/maintenance/healthcheck.sh
|
|
```
|
|
|
|
---
|
|
|
|
## Troubleshooting
|
|
|
|
### La aplicación no inicia
|
|
|
|
```bash
|
|
# Ver logs detallados
|
|
docker-compose -f docker-compose.prod.yml logs app
|
|
|
|
# Verificar variables de entorno
|
|
docker-compose -f docker-compose.prod.yml exec app env | grep -E "(DATABASE|REDIS|SECRET)"
|
|
|
|
# Probar conexión a DB
|
|
docker exec social-automation-db pg_isready -U social_automation
|
|
```
|
|
|
|
### Error de conexión a la base de datos
|
|
|
|
```bash
|
|
# Verificar que DB esté corriendo
|
|
docker ps | grep db
|
|
|
|
# Verificar health check
|
|
docker inspect social-automation-db | grep -A 10 Health
|
|
|
|
# Reiniciar DB
|
|
docker-compose -f docker-compose.prod.yml restart db
|
|
```
|
|
|
|
### Worker no procesa tareas
|
|
|
|
```bash
|
|
# Ver estado del worker
|
|
docker-compose -f docker-compose.prod.yml logs worker
|
|
|
|
# Verificar Redis
|
|
docker exec social-automation-redis redis-cli ping
|
|
|
|
# Reiniciar worker y beat
|
|
docker-compose -f docker-compose.prod.yml restart worker beat
|
|
```
|
|
|
|
### Error 502 Bad Gateway
|
|
|
|
```bash
|
|
# Verificar que app esté respondiendo
|
|
curl http://localhost:8000/api/health
|
|
|
|
# Ver logs de nginx
|
|
docker-compose -f docker-compose.prod.yml logs nginx
|
|
|
|
# Reiniciar nginx
|
|
docker-compose -f docker-compose.prod.yml restart nginx
|
|
```
|
|
|
|
### Alto uso de disco
|
|
|
|
```bash
|
|
# Ejecutar limpieza
|
|
./scripts/maintenance/cleanup.sh
|
|
|
|
# Limpiar Docker
|
|
docker system prune -a --volumes
|
|
|
|
# Verificar tamaño de backups
|
|
du -sh backups/
|
|
```
|
|
|
|
---
|
|
|
|
## Seguridad
|
|
|
|
### Checklist de Producción
|
|
|
|
- [ ] Cambiar todas las contraseñas por defecto
|
|
- [ ] Generar SECRET_KEY único
|
|
- [ ] Configurar certificados SSL válidos
|
|
- [ ] Configurar firewall (solo puertos 80, 443)
|
|
- [ ] Configurar backups automáticos
|
|
- [ ] Configurar monitoreo y alertas
|
|
- [ ] Habilitar autenticación en Flower
|
|
|
|
### Firewall (UFW)
|
|
|
|
```bash
|
|
# Configurar firewall básico
|
|
ufw default deny incoming
|
|
ufw default allow outgoing
|
|
ufw allow ssh
|
|
ufw allow 80
|
|
ufw allow 443
|
|
ufw enable
|
|
```
|
|
|
|
---
|
|
|
|
## Soporte
|
|
|
|
- **Repositorio**: https://git.consultoria-as.com/consultoria-as/social-media-automation
|
|
- **Documentación API**: https://tu-dominio.com/docs
|
|
- **Monitor Celery**: https://tu-dominio.com/flower/
|