#!/bin/bash # Lily LLM API Deployment Script # This script automates the deployment of the Lily LLM API server 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="lily-llm-api" DOCKER_COMPOSE_FILE="docker-compose.yml" BACKUP_DIR="./backup" LOG_DIR="./logs" # Logging function log() { echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" } success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # Check if Docker is installed check_docker() { if ! command -v docker &> /dev/null; then error "Docker is not installed. Please install Docker first." exit 1 fi if ! command -v docker-compose &> /dev/null; then error "Docker Compose is not installed. Please install Docker Compose first." exit 1 fi success "Docker and Docker Compose are available" } # Create necessary directories create_directories() { log "Creating necessary directories..." mkdir -p $BACKUP_DIR mkdir -p $LOG_DIR mkdir -p ./data mkdir -p ./models mkdir -p ./uploads success "Directories created successfully" } # Backup existing data backup_data() { if [ -d "./data" ] && [ "$(ls -A ./data)" ]; then log "Creating backup of existing data..." timestamp=$(date +"%Y%m%d_%H%M%S") tar -czf "$BACKUP_DIR/backup_$timestamp.tar.gz" ./data success "Backup created: backup_$timestamp.tar.gz" fi } # Build Docker images build_images() { log "Building Docker images..." docker-compose -f $DOCKER_COMPOSE_FILE build --no-cache success "Docker images built successfully" } # Start services start_services() { log "Starting services..." docker-compose -f $DOCKER_COMPOSE_FILE up -d # Wait for services to be ready log "Waiting for services to be ready..." sleep 30 # Check if API is responding if curl -f http://localhost:8001/health > /dev/null 2>&1; then success "Services started successfully" else error "API is not responding. Check logs with: docker-compose logs" exit 1 fi } # Stop services stop_services() { log "Stopping services..." docker-compose -f $DOCKER_COMPOSE_FILE down success "Services stopped successfully" } # Restart services restart_services() { log "Restarting services..." docker-compose -f $DOCKER_COMPOSE_FILE restart success "Services restarted successfully" } # Show logs show_logs() { log "Showing logs..." docker-compose -f $DOCKER_COMPOSE_FILE logs -f } # Show status show_status() { log "Service status:" docker-compose -f $DOCKER_COMPOSE_FILE ps echo "" log "API Health Check:" if curl -f http://localhost:8001/health > /dev/null 2>&1; then success "API is healthy" else error "API is not responding" fi } # Clean up cleanup() { log "Cleaning up..." docker-compose -f $DOCKER_COMPOSE_FILE down -v docker system prune -f success "Cleanup completed" } # Update deployment update_deployment() { log "Updating deployment..." # Stop existing services stop_services # Backup data backup_data # Build new images build_images # Start services start_services success "Deployment updated successfully" } # Main deployment function deploy() { log "Starting Lily LLM API deployment..." # Check prerequisites check_docker # Create directories create_directories # Backup existing data backup_data # Build and start services build_images start_services success "Deployment completed successfully!" echo "" log "Services are now running:" echo " - API Server: http://localhost:8001" echo " - Health Check: http://localhost:8001/health" echo " - Flower (Celery Monitor): http://localhost:5555" echo "" log "Useful commands:" echo " - View logs: ./scripts/deploy.sh logs" echo " - Check status: ./scripts/deploy.sh status" echo " - Stop services: ./scripts/deploy.sh stop" echo " - Update deployment: ./scripts/deploy.sh update" } # Main script logic case "${1:-deploy}" in "deploy") deploy ;; "start") start_services ;; "stop") stop_services ;; "restart") restart_services ;; "update") update_deployment ;; "logs") show_logs ;; "status") show_status ;; "cleanup") cleanup ;; "build") build_images ;; *) echo "Usage: $0 {deploy|start|stop|restart|update|logs|status|cleanup|build}" echo "" echo "Commands:" echo " deploy - Full deployment (default)" echo " start - Start services" echo " stop - Stop services" echo " restart - Restart services" echo " update - Update deployment" echo " logs - Show logs" echo " status - Show service status" echo " cleanup - Clean up containers and volumes" echo " build - Build Docker images" exit 1 ;; esac