Staging Deployment Guide

Date: August 11, 2025 Status: Production Ready Environment: Staging with Docker Compose Performance: <500ms search target (achieving ~60ms)

Overview

This guide covers the deployment of Piper Morgan to the staging environment, which provides a production-grade testing environment for MCP integration, performance validation, and deployment verification.

Prerequisites

Quick Start

One-Command Deployment

# Deploy complete staging environment
./scripts/deploy_staging.sh

# Verify deployment (14 comprehensive tests)
./scripts/verify_staging_deployment.sh

Manual Deployment

# Build and start services
docker-compose build
docker-compose up -d

# Verify all services are running
docker-compose ps

Architecture Overview

Service Stack

The staging environment consists of 8 containerized services:

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Nginx Proxy   │    │  Piper Morgan   │    │   PostgreSQL    │
│   Port: 8081    │◄──►│   Port: 8001    │◄──►│   Port: 5432    │
└─────────────────┘    └─────────────────┘    └─────────────────┘
         │                       │                       │
         │              ┌─────────────────┐              │
         │              │     Redis       │              │
         │              │   Port: 6379    │              │
         │              └─────────────────┘              │
         │                       │                       │
         │              ┌─────────────────┐              │
         │              │    ChromaDB     │              │
         │              │   Port: 8000    │              │
         │              └─────────────────┘              │
         │                       │                       │
         │              ┌─────────────────┐              │
         │              │    Prometheus   │              │
         │              │   Port: 9090    │              │
         │              └─────────────────┘              │
         │                       │                       │
         │              ┌─────────────────┐              │
         │              │     Grafana     │              │
         │              │   Port: 3001    │              │
         │              └─────────────────┘              │
         │                       │                       │
         │              ┌─────────────────┐              │
         │              │   Health Check  │              │
         │              │   Port: 8002    │              │
         │              └─────────────────┘              │

Key Features

Configuration

Environment Variables

# Copy and configure environment template
cp .env.example .env

# Key configuration variables
POSTGRES_DB=piper_morgan_staging
POSTGRES_USER=piper_user
POSTGRES_PASSWORD=secure_password_here
REDIS_URL=redis://redis:6379
CHROMA_HOST=chromadb
CHROMA_PORT=8000

Docker Compose Configuration

# Key service configurations
services:
  app:
    build: .
    ports:
      - "8001:8000"
    environment:
      - ENVIRONMENT=staging
      - LOG_LEVEL=INFO
    depends_on:
      - postgres
      - redis
      - chromadb

  nginx:
    image: nginx:alpine
    ports:
      - "8081:80"
    volumes:
      - ./nginx/staging.conf:/etc/nginx/nginx.conf

Deployment Steps

Step 1: Pre-Deployment Checks

# Verify system requirements
./scripts/check_system_requirements.sh

# Check port availability
./scripts/check_port_availability.sh

# Validate Docker installation
docker --version
docker-compose --version

Step 2: Database Initialization

# Initialize PostgreSQL database
docker-compose exec postgres psql -U piper_user -d piper_morgan_staging -f /docker-entrypoint-initdb.d/init.sql

# Verify database connection
docker-compose exec app python -c "from services.database import get_db; print('Database connection successful')"

Step 3: Service Deployment

# Start core services
docker-compose up -d postgres redis chromadb

# Wait for services to be ready
./scripts/wait_for_services.sh

# Deploy application
docker-compose up -d app

# Deploy monitoring stack
docker-compose up -d prometheus grafana

Step 4: Health Verification

# Run comprehensive health checks
./scripts/verify_staging_deployment.sh

# Expected output: 14/14 tests passing

Monitoring & Observability

Health Check Endpoints

Performance Metrics

Grafana Dashboards

Access Grafana at http://localhost:3001 (admin/admin):

Troubleshooting

Common Issues

Service Won’t Start

# Check service logs
docker-compose logs [service_name]

# Verify port conflicts
netstat -tulpn | grep :8001

# Check resource usage
docker stats

Database Connection Issues

# Verify PostgreSQL is running
docker-compose exec postgres pg_isready

# Check connection parameters
docker-compose exec app python -c "import os; print(os.getenv('DATABASE_URL'))"

Performance Issues

# Check MCP connection pool status
curl http://localhost:8001/health/mcp

# Monitor resource usage
docker stats --no-stream

Recovery Procedures

Service Restart

# Restart specific service
docker-compose restart [service_name]

# Restart all services
docker-compose restart

Database Recovery

# Backup current data
docker-compose exec postgres pg_dump -U piper_user piper_morgan_staging > backup.sql

# Restore from backup
docker-compose exec -T postgres psql -U piper_user -d piper_morgan_staging < backup.sql

Rollback Procedures

Automatic Rollback

The staging environment includes automatic rollback capabilities:

# Trigger rollback to previous version
./scripts/rollback_staging.sh

# Verify rollback success
./scripts/verify_staging_deployment.sh

Manual Rollback

# Stop current deployment
docker-compose down

# Restore previous version
git checkout HEAD~1
docker-compose up -d

# Verify rollback
./scripts/verify_staging_deployment.sh

Security Considerations

Network Security

Data Security

Performance Tuning

MCP Connection Pooling

# Monitor connection pool status
curl http://localhost:8001/health/mcp

# Expected metrics:
# - Active connections: 5-10
# - Pool size: 20
# - Connection wait time: <10ms

Database Optimization

# Check query performance
docker-compose exec postgres psql -U piper_user -d piper_morgan_staging -c "SELECT * FROM pg_stat_statements ORDER BY mean_time DESC LIMIT 10;"

Maintenance

Regular Tasks

Backup Strategy

# Automated daily backup
0 2 * * * /scripts/backup_staging.sh

# Manual backup
./scripts/backup_staging.sh

# Restore from backup
./scripts/restore_staging.sh backup_2025-08-11.sql

Support

Getting Help

Useful Commands

# Quick status check
./scripts/staging_status.sh

# Performance test
./scripts/performance_test.sh

# Full system reset
./scripts/reset_staging.sh

Status: Production Ready ✅ Performance: <500ms target (achieving ~60ms) ✅ Monitoring: Comprehensive health checks ✅ Rollback: Automated recovery procedures ✅