File size: 5,373 Bytes
526927a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/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