# DEVOPS - ERP CONSTRUCCION

**Fecha:** 2025-11-25
**Version:** 1.0.0

---

## Vision General

Este directorio contiene la documentacion y configuracion de DevOps para el ERP de Construccion, incluyendo:

- Contenedores Docker
- CI/CD Pipeline
- Guias de deployment
- Monitoreo y logging

---

## Estructura de Directorio

```
05-devops/
+-- README.md (este archivo)
+-- docker/
|   +-- docker-compose.yml
|   +-- docker-compose.dev.yml
|   +-- docker-compose.prod.yml
|   +-- Dockerfile.backend
|   +-- Dockerfile.frontend
+-- ci-cd/
|   +-- github-actions.yml
|   +-- PIPELINE.md
+-- deployment/
|   +-- DEPLOYMENT-GUIDE.md
|   +-- ROLLBACK-GUIDE.md
+-- monitoring/
|   +-- MONITORING.md
|   +-- ALERTS.md
```

---

## Stack de Infraestructura

### Desarrollo Local

| Servicio | Tecnologia | Puerto |
|----------|------------|--------|
| Backend | Node.js 20 | 3000 |
| Frontend | React/Vite | 5173 |
| Database | PostgreSQL 15 + PostGIS | 5432 |
| Cache | Redis | 6379 |
| Storage | MinIO (S3) | 9000 |

### Produccion

| Servicio | Tecnologia | Proveedor |
|----------|------------|-----------|
| Backend | Node.js | Railway / Render |
| Frontend | React SPA | Vercel / Cloudflare |
| Database | PostgreSQL | Supabase / Neon |
| Cache | Redis | Upstash |
| Storage | S3 | AWS / Cloudflare R2 |

---

## Docker Compose (Desarrollo)

```yaml
# docker-compose.dev.yml
version: '3.8'

services:
  postgres:
    image: postgis/postgis:15-3.3
    environment:
      POSTGRES_USER: erp_user
      POSTGRES_PASSWORD: erp_dev_password
      POSTGRES_DB: erp_construccion_dev
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./database/init:/docker-entrypoint-initdb.d

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

  minio:
    image: minio/minio
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    ports:
      - "9000:9000"
      - "9001:9001"
    command: server /data --console-address ":9001"
    volumes:
      - minio_data:/data

  backend:
    build:
      context: ./apps/backend
      dockerfile: Dockerfile.dev
    environment:
      DATABASE_URL: postgresql://erp_user:erp_dev_password@postgres:5432/erp_construccion_dev
      REDIS_URL: redis://redis:6379
      S3_ENDPOINT: http://minio:9000
    ports:
      - "3000:3000"
    volumes:
      - ./apps/backend:/app
      - /app/node_modules
    depends_on:
      - postgres
      - redis
      - minio

  frontend:
    build:
      context: ./apps/frontend
      dockerfile: Dockerfile.dev
    environment:
      VITE_API_URL: http://localhost:3000
    ports:
      - "5173:5173"
    volumes:
      - ./apps/frontend:/app
      - /app/node_modules

volumes:
  postgres_data:
  minio_data:
```

---

## CI/CD Pipeline

### GitHub Actions Workflow

```yaml
# .github/workflows/ci.yml
name: CI Pipeline

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main, develop]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm ci
      - run: npm run lint

  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgis/postgis:15-3.3
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: erp_test
        ports:
          - 5432:5432
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm ci
      - run: npm run test:ci
        env:
          DATABASE_URL: postgresql://test:test@localhost:5432/erp_test

  build:
    runs-on: ubuntu-latest
    needs: [lint, test]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm ci
      - run: npm run build
```

---

## Deployment

### Ambientes

| Ambiente | Branch | URL | Base de Datos |
|----------|--------|-----|---------------|
| Development | develop | dev.erp.example.com | erp_dev |
| Staging | staging | staging.erp.example.com | erp_staging |
| Production | main | erp.example.com | erp_prod |

### Proceso de Deployment

1. **Merge a main/develop** triggers CI
2. **CI pasa** -> Build y tests
3. **Deploy automatico** a ambiente correspondiente
4. **Health checks** verifican deployment
5. **Rollback automatico** si health check falla

---

## Variables de Entorno

### Backend

```bash
# Database
DATABASE_URL=postgresql://user:password@host:5432/database
DATABASE_POOL_SIZE=10

# Auth
JWT_SECRET=your-secret-key
JWT_EXPIRES_IN=1d
REFRESH_TOKEN_EXPIRES_IN=7d

# Redis
REDIS_URL=redis://localhost:6379

# Storage
S3_ENDPOINT=https://s3.amazonaws.com
S3_BUCKET=erp-construccion
S3_ACCESS_KEY=xxx
S3_SECRET_KEY=xxx

# App
NODE_ENV=production
PORT=3000
LOG_LEVEL=info
```

### Frontend

```bash
VITE_API_URL=https://api.erp.example.com
VITE_WS_URL=wss://api.erp.example.com
VITE_SENTRY_DSN=xxx
```

---

## Monitoreo

### Metricas Clave

| Metrica | Umbral Warning | Umbral Critical |
|---------|----------------|-----------------|
| Response Time (p95) | > 500ms | > 1000ms |
| Error Rate | > 1% | > 5% |
| CPU Usage | > 70% | > 90% |
| Memory Usage | > 70% | > 90% |
| DB Connections | > 80% | > 95% |

### Herramientas

- **APM:** Sentry / New Relic
- **Logs:** Datadog / CloudWatch
- **Uptime:** UptimeRobot / Better Uptime
- **Alertas:** PagerDuty / Slack

---

## Scripts Utiles

```bash
# Iniciar ambiente de desarrollo
./scripts/dev-start.sh

# Reset de base de datos
./scripts/reset-database.sh

# Backup de produccion
./scripts/backup-prod.sh

# Rollback de deployment
./scripts/rollback.sh <version>
```

---

## Proximos Pasos

1. [ ] Crear Dockerfile.backend
2. [ ] Crear Dockerfile.frontend
3. [ ] Configurar GitHub Actions
4. [ ] Configurar Sentry
5. [ ] Crear scripts de deployment

---

## Referencias

- [ADR-001: Stack Tecnologico](../adr/ADR-001-stack-tecnologico.md)
- [ERP Generico - DevOps](/projects/erp-generic/docs/05-devops/)

---

**Ultima actualizacion:** 2025-11-25
**Version:** 1.0.0
