|
|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set -e |
|
|
|
|
|
|
|
|
RED='\033[0;31m' |
|
|
GREEN='\033[0;32m' |
|
|
YELLOW='\033[1;33m' |
|
|
BLUE='\033[0;34m' |
|
|
NC='\033[0m' |
|
|
|
|
|
|
|
|
PROJECT_NAME="cidadao-ai" |
|
|
BACKUP_DIR="/backups" |
|
|
DEPLOY_ENV=${1:-production} |
|
|
|
|
|
echo -e "${BLUE}π Starting CidadΓ£o.AI deployment...${NC}" |
|
|
|
|
|
|
|
|
if [ "$EUID" -eq 0 ]; then |
|
|
echo -e "${RED}β Do not run this script as root${NC}" |
|
|
exit 1 |
|
|
fi |
|
|
|
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
source .env |
|
|
|
|
|
|
|
|
echo -e "${YELLOW}π Creating directories...${NC}" |
|
|
mkdir -p data logs infrastructure/nginx/ssl |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
if [ -d ".git" ]; then |
|
|
echo -e "${YELLOW}π₯ Pulling latest changes...${NC}" |
|
|
git pull origin main |
|
|
fi |
|
|
|
|
|
|
|
|
echo -e "${YELLOW}ποΈ Building and starting services...${NC}" |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
echo -e "${YELLOW}β³ Waiting for services to be ready...${NC}" |
|
|
sleep 30 |
|
|
|
|
|
|
|
|
echo -e "${YELLOW}π Running health checks...${NC}" |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
echo -e "${YELLOW}π Running database migrations...${NC}" |
|
|
|
|
|
|
|
|
|
|
|
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}" |