neural-thinker's picture
feat: clean HuggingFace deployment with essential files only
824bf31
raw
history blame
5.32 kB
#!/bin/bash
# CidadΓ£o.AI Deployment Script
# Automates the deployment process for production
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
PROJECT_NAME="cidadao-ai"
BACKUP_DIR="/backups"
DEPLOY_ENV=${1:-production}
echo -e "${BLUE}πŸš€ Starting CidadΓ£o.AI deployment...${NC}"
# Check if running as root
if [ "$EUID" -eq 0 ]; then
echo -e "${RED}❌ Do not run this script as root${NC}"
exit 1
fi
# Check dependencies
echo -e "${YELLOW}πŸ“‹ Checking dependencies...${NC}"
if ! command -v docker &> /dev/null; then
echo -e "${RED}❌ Docker is not installed${NC}"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
echo -e "${RED}❌ Docker Compose is not installed${NC}"
exit 1
fi
if ! command -v git &> /dev/null; then
echo -e "${RED}❌ Git is not installed${NC}"
exit 1
fi
echo -e "${GREEN}βœ… Dependencies check passed${NC}"
# Check environment file
if [ ! -f ".env" ]; then
echo -e "${YELLOW}⚠️ .env file not found, copying from template...${NC}"
if [ -f ".env.${DEPLOY_ENV}" ]; then
cp ".env.${DEPLOY_ENV}" .env
echo -e "${YELLOW}πŸ“ Please edit .env file with your configuration${NC}"
echo -e "${YELLOW}Press Enter when ready...${NC}"
read
else
echo -e "${RED}❌ No .env template found for environment: ${DEPLOY_ENV}${NC}"
exit 1
fi
fi
# Load environment variables
source .env
# Create necessary directories
echo -e "${YELLOW}πŸ“ Creating directories...${NC}"
mkdir -p data logs infrastructure/nginx/ssl
# Check SSL certificates
if [ ! -f "infrastructure/nginx/ssl/cert.pem" ] || [ ! -f "infrastructure/nginx/ssl/key.pem" ]; then
echo -e "${YELLOW}πŸ”’ SSL certificates not found, generating self-signed certificates...${NC}"
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout infrastructure/nginx/ssl/key.pem \
-out infrastructure/nginx/ssl/cert.pem \
-subj "/C=BR/ST=Brazil/L=Brasilia/O=Cidadao.AI/OU=IT/CN=cidadao.ai"
echo -e "${YELLOW}⚠️ Using self-signed certificates. Please replace with proper SSL certificates for production.${NC}"
fi
# Backup existing data (if any)
if [ -d "data" ] && [ "$(ls -A data)" ]; then
echo -e "${YELLOW}πŸ’Ύ Creating backup...${NC}"
BACKUP_NAME="${PROJECT_NAME}-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p "${BACKUP_DIR}"
tar -czf "${BACKUP_DIR}/${BACKUP_NAME}.tar.gz" data/
echo -e "${GREEN}βœ… Backup created: ${BACKUP_DIR}/${BACKUP_NAME}.tar.gz${NC}"
fi
# Pull latest changes (if in git repository)
if [ -d ".git" ]; then
echo -e "${YELLOW}πŸ“₯ Pulling latest changes...${NC}"
git pull origin main
fi
# Build and start services
echo -e "${YELLOW}πŸ—οΈ Building and starting services...${NC}"
# Build Docker images
echo -e "${YELLOW}πŸ“¦ Building API image...${NC}"
docker build -t cidadao-ai:latest -f deployment/Dockerfile .
echo -e "${YELLOW}πŸ‘· Building worker image...${NC}"
docker build -t cidadao-ai-worker:latest -f deployment/Dockerfile.worker .
echo -e "${YELLOW}πŸ€– Building ML service image...${NC}"
docker build -t cidadao-ai-ml:latest -f deployment/Dockerfile.ml .
if [ "${DEPLOY_ENV}" = "production" ]; then
docker-compose -f deployment/docker-compose.prod.yml down
docker-compose -f deployment/docker-compose.prod.yml up -d
else
docker-compose down
docker-compose up -d
fi
# Wait for services to be ready
echo -e "${YELLOW}⏳ Waiting for services to be ready...${NC}"
sleep 30
# Health checks
echo -e "${YELLOW}πŸ” Running health checks...${NC}"
# Check API health
if curl -f http://localhost:8000/health > /dev/null 2>&1; then
echo -e "${GREEN}βœ… API is healthy${NC}"
else
echo -e "${RED}❌ API health check failed${NC}"
docker-compose logs api
exit 1
fi
# Check database connection
if docker-compose exec -T postgres pg_isready -U cidadao -d cidadao_ai > /dev/null 2>&1; then
echo -e "${GREEN}βœ… Database is healthy${NC}"
else
echo -e "${RED}❌ Database health check failed${NC}"
docker-compose logs postgres
exit 1
fi
# Check Redis
if docker-compose exec -T redis redis-cli ping > /dev/null 2>&1; then
echo -e "${GREEN}βœ… Redis is healthy${NC}"
else
echo -e "${RED}❌ Redis health check failed${NC}"
docker-compose logs redis
exit 1
fi
# Run migrations (if available)
echo -e "${YELLOW}πŸ”„ Running database migrations...${NC}"
# docker-compose exec api python -m alembic upgrade head
# Show deployment summary
echo -e "${GREEN}πŸŽ‰ Deployment completed successfully!${NC}"
echo -e "${BLUE}πŸ“Š Service URLs:${NC}"
echo -e " β€’ Frontend: https://localhost (or your domain)"
echo -e " β€’ API: http://localhost:8000"
echo -e " β€’ API Docs: http://localhost:8000/docs"
echo -e " β€’ Grafana: http://localhost:3000 (admin / ${GRAFANA_PASSWORD})"
echo -e " β€’ Prometheus: http://localhost:9090"
echo -e "${BLUE}πŸ“ Next steps:${NC}"
echo -e " 1. Update DNS records to point to this server"
echo -e " 2. Replace self-signed SSL certificates with proper ones"
echo -e " 3. Configure firewall rules"
echo -e " 4. Set up monitoring alerts"
echo -e " 5. Schedule regular backups"
echo -e "${GREEN}βœ… CidadΓ£o.AI is now running in ${DEPLOY_ENV} mode!${NC}"