API for the RAVEN platform developed with FastAPI.
This project implements a RESTful API for the RAVEN platform using FastAPI, SQLAlchemy, and Pydantic. The API is structured following best practices to facilitate its maintenance and scalability.
- PostgreSQL Support: The API now uses PostgreSQL instead of SQLite for better performance and scalability
- Enhanced Workspace Management: Workspaces now support multiple teams (team_ids array)
- Advanced Filtering: Filter workspaces by user_id (creator or team member)
- Complete CRUD Operations: Full permit operations including create, read, update, delete
- User Information Endpoint: Get current user information for login operations
- Python 3.11+
- Docker and Docker Compose
- pip (package manager for Python)
For the fastest setup, use our quick start script:
git clone https://github.com/your-username/raven-api.git
cd raven-api
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
./scripts/quick-start.shThis will automatically:
- Install all dependencies
- Start PostgreSQL in Docker
- Run database migrations
- Optionally seed the database
Then start the API:
uvicorn main:app --reloadThe easiest way to get PostgreSQL running:
-
Clone the repository:
git clone https://github.com/your-username/raven-api.git cd raven-api -
Create a virtual environment and install dependencies:
python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate pip install -r requirements.txt
-
Start PostgreSQL with Docker Compose:
./scripts/setup-postgresql-compose.sh
-
Run database migrations:
export DATABASE_URI="postgresql://raven_user:raven_password@localhost:5432/raven_db" alembic upgrade head
If you have an existing SQLite database and want to migrate to PostgreSQL:
-
Clone the repository and setup virtual environment (steps 1-2 from Option 1)
-
Run the complete migration script:
./scripts/migrate_to_postgresql.sh
If you prefer manual Docker commands:
-
Start PostgreSQL container:
./scripts/setup-postgresql.sh
-
Configure environment variables:
cp .env.example .env # Edit .env with your PostgreSQL configuration -
Run database migrations:
export DATABASE_URI="postgresql://raven_user:raven_password@localhost:5432/raven_db" alembic upgrade head
# Start PostgreSQL (Docker Compose)
docker compose -f docker-compose.postgres.yml up -d
# Stop PostgreSQL
docker compose -f docker-compose.postgres.yml down
# View PostgreSQL logs
docker compose -f docker-compose.postgres.yml logs -f
# Remove PostgreSQL and data
docker compose -f docker-compose.postgres.yml down -v
# Connect to PostgreSQL directly
docker exec -it raven-postgres psql -U raven_user -d raven_db- GET /raven-api/v1/auth/me - Get current user information
- GET /raven-api/v1/workspaces?user_id= - Filter workspaces by user (creator or team member)
- DELETE /raven-api/v1/workspaces/{id} - Delete a workspace
- GET /raven-api/v1/permits - List all permits
- PUT /raven-api/v1/permits/{id} - Update a permit
- DELETE /raven-api/v1/permits/{id} - Delete a permit
- GET /raven-api/v1/permits/team/{team_id} - Get permits by team
To run the API in development mode:
uvicorn main:app --reloadThe API will be available at http://localhost:8000.
Interactive documentation will be available at:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
For production deployment in Kubernetes clusters:
# Build Docker image
./scripts/build-docker.sh
# Deploy to Kubernetes
./scripts/deploy-k8s.shThis will:
- β Create namespace and apply configurations
- β Deploy PostgreSQL with persistent storage
- β Initialize database with required tables
- β Deploy the API with health checks
- β Configure Istio (if available) for advanced networking
# Apply configurations manually
kubectl apply -k kubernetes/
# For production environment
kubectl apply -k kubernetes/overlays/production/# Check pod status
kubectl get pods -n raven-api
# View API logs
kubectl logs -f deployment/raven-api -n raven-api
# Test API health
kubectl port-forward service/raven-api-service 8000:8000 -n raven-api
curl http://localhost:8000/raven-api/v1/health/π Detailed Kubernetes documentation: docs/kubernetes-deployment.md
For simple Docker deployment:
# Build image
docker build -t raven-api .
# Run with Docker Compose (includes PostgreSQL)
docker compose -f docker-compose.postgres.yml up -d
# Run standalone container
docker run -p 8000:8000 \
-e DATABASE_URI="postgresql://user:pass@host:5432/db" \
raven-apiraven-api/
β
βββ app/ # Main package
β βββ api/ # Route and endpoint definitions
β β βββ endpoints/ # Endpoints organized by resource
β βββ config/ # Project configuration
β βββ db/ # Database configuration
β βββ models/ # SQLAlchemy models
β βββ schemas/ # Pydantic schemas for validation
β βββ services/ # Business logic
β βββ utils/ # Utilities and helper functions
β
βββ kubernetes/ # Kubernetes deployment manifests
β βββ deployment.yaml # API deployment definition
β βββ service.yaml # Service definition
β βββ postgres-deployment.yaml # PostgreSQL deployment
β βββ configmap.yaml # Configuration and DB initialization
β βββ secrets.yaml # Secrets for configuration
β βββ kustomization.yaml # Kustomize base configuration
β βββ gateway.yaml # Istio Gateway configuration
β βββ gateway-with-https.yaml # Istio Gateway with HTTPS support
β βββ virtual-service.yaml # Istio VirtualService configuration
β βββ overlays/ # Environment-specific configurations
β βββ production/ # Production overlay
β
βββ scripts/ # Utility scripts
β βββ build-docker.sh # Build and push Docker images
β βββ create_tables.py # Create database tables
β βββ deploy-k8s.sh # Deploy to Kubernetes
β βββ migrate_data.py # Migrate data from SQLite to PostgreSQL
β βββ migrate_to_postgresql.sh # Complete migration script
β βββ quick-start.sh # Quick development setup
β βββ seed_db.py # Database seeder
β βββ setup-postgresql.sh # Setup PostgreSQL with Docker
β βββ setup-postgresql-compose.sh # Setup PostgreSQL with Docker Compose
β βββ verify-k8s.sh # Verify Kubernetes cluster readiness
β
βββ docs/ # Documentation
β βββ kubernetes-deployment.md # Detailed Kubernetes guide
β
βββ migrations/ # Database migrations
βββ tests/ # Unit and integration tests
βββ .env.example # Environment variables template
βββ docker-compose.postgres.yml # Docker Compose for PostgreSQL
βββ docker-entrypoint.sh # Docker entry point script
βββ Dockerfile # Definition for building the image
βββ main.py # Application entry point
βββ raven-ctl.sh # Legacy control script for Istio/MicroK8s
βββ pyproject.toml # Configuration for Python tools
βββ requirements.txt # Project dependencies
To run tests:
pytestTo build the Docker image for the application:
docker build -t raven-api:latest .The project includes a comprehensive control script (raven-ctl.sh) that simplifies the deployment, exposure, and management of the RAVEN API on Kubernetes with Istio.
-
Make sure you have a MicroK8s or Kubernetes cluster with Istio installed.
-
Make the control script executable:
chmod +x raven-ctl.sh
-
Use the script with different commands:
# Display help and available commands ./raven-ctl.sh help # Deploy the API (builds and deploys the application) ./raven-ctl.sh deploy # Expose the API to the internet (HTTP) ./raven-ctl.sh expose # Configure HTTPS with TLS certificate ./raven-ctl.sh secure # Verify API accessibility from multiple paths ./raven-ctl.sh verify # Check the status of the deployment ./raven-ctl.sh status # Restart the deployment (useful for troubleshooting) ./raven-ctl.sh restart # Clean up the deployment ./raven-ctl.sh cleanup
-
The script supports various options:
# Skip image building ./raven-ctl.sh deploy --skip-build # Build without using Docker cache ./raven-ctl.sh deploy --no-cache # Specify a custom registry ./raven-ctl.sh deploy --registry=myregistry.example.com # Specify a custom hostname ./raven-ctl.sh expose --hostname=api.example.com
If you prefer to deploy manually, follow these steps:
-
Make sure you have a Kubernetes cluster with Istio installed.
-
Apply the Kubernetes manifests:
# Create namespace and enable Istio injection kubectl create namespace raven kubectl label namespace raven istio-injection=enabled # Apply secrets first kubectl apply -f kubernetes/secrets.yaml -n raven # Apply the rest of the resources kubectl apply -f kubernetes/deployment.yaml -n raven kubectl apply -f kubernetes/service.yaml -n raven kubectl apply -f kubernetes/gateway.yaml -n raven kubectl apply -f kubernetes/virtual-service.yaml -n raven
-
Verify the deployment:
kubectl get pods -n raven kubectl get svc -n raven kubectl get virtualservice -n raven kubectl get gateway -n raven
Once deployed with Istio, you can use the integrated tools for monitoring:
- Kiali: For service mesh visualization
- Jaeger/Zipkin: For distributed tracing
- Prometheus/Grafana: For metrics and dashboards
To expose the RAVEN API to the internet, you can use the control script:
# Expose with HTTP
./raven-ctl.sh expose
# Configure HTTPS
./raven-ctl.sh secureThe script will guide you through the following options:
- Using MetalLB (recommended for local environments): Configures MetalLB to provide LoadBalancer capabilities with a fixed IP.
- Using NodePort: Exposes the API through a specific port on the nodes.
- Manual configuration: For advanced scenarios or cloud environments.
The improved exposure functionality includes:
- Automatic IP detection: The script automatically detects your server's IP interfaces and shows them for selection.
- Fixed IP allocation: When using MetalLB, the script can configure it to use your server's fixed IP address.
- Service type detection: The script checks the current configuration and offers appropriate options.
- Verification loop: Instead of waiting a fixed time, the script actively checks if the IP has been assigned.
After exposing the API, you can verify its accessibility with:
./raven-ctl.sh verifyThis command runs comprehensive diagnostics to check accessibility from different paths:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββ
β URL β Estado β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββ€
β http://host01.idea.lst.tfo.upm.es/raven-api/v1/health/ β β
OK β
β https://host01.idea.lst.tfo.upm.es/raven-api/v1/health/ β β
OK β
β http://192.168.1.100/ (con Host: host01.idea.lst.tfo.upm.es) β β
OK β
β Port-forward al servicio interno β β
OK β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββ
The verification includes:
- Tests all possible ways to access the API (hostname, IP, NodePort, port-forward)
- Verifies the readiness of all pods before testing
- Shows response codes for each access method
- Provides detailed troubleshooting information when access fails
- Displays logs from problematic pods
- Checks both HTTP and HTTPS access when configured
- Formats JSON response for better readability
If you experience issues with the deployment, you can use several tools:
-
Check the status:
./raven-ctl.sh status- Shows the current state of all pods, services, and gateways
- Attempts to access the health endpoint inside the pod
- Multiple verification methods are attempted if one fails
-
Verify external access:
./raven-ctl.sh verify- Tests accessibility from various entry points
-
Restart the deployment:
./raven-ctl.sh restart- Performs a rolling restart of all pods
- Waits for the restart to complete
- Verifies the status after restart
-
Common issues and solutions:
- Pods not starting: Check for image pull errors or resource constraints
- API not accessible: Verify Istio Gateway and VirtualService configuration
- HTTPS not working: Ensure the TLS certificate is properly created and referenced
When deploying to production:
- Use valid TLS certificates from a trusted CA instead of self-signed certificates.
- Configure proper access controls and authentication.
- Secure sensitive environment variables using Kubernetes secrets.
- Consider implementing network policies to restrict traffic.
Copyright 2025 Universidad PolitΓ©cnica de Madrid Copyright 2025 RAVEN Team
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.