-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-setup.sh
More file actions
executable file
·305 lines (255 loc) · 8.38 KB
/
docker-setup.sh
File metadata and controls
executable file
·305 lines (255 loc) · 8.38 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/bash
# Docker setup script for Invoice Reimbursement System
# This script helps set up and manage the Docker environment
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
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if Docker is installed and running
check_docker() {
print_status "Checking Docker installation..."
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
echo "Visit: https://docs.docker.com/get-docker/"
exit 1
fi
if ! docker info &> /dev/null; then
print_error "Docker is not running. Please start Docker first."
exit 1
fi
print_success "Docker is installed and running"
}
# Function to check if docker-compose is available
check_docker_compose() {
print_status "Checking Docker Compose..."
if command -v docker-compose &> /dev/null; then
COMPOSE_CMD="docker-compose"
elif docker compose version &> /dev/null; then
COMPOSE_CMD="docker compose"
else
print_error "Docker Compose is not available. Please install Docker Compose."
exit 1
fi
print_success "Docker Compose is available: $COMPOSE_CMD"
}
# Function to check environment variables
check_env() {
print_status "Checking environment configuration..."
if [ ! -f ".env" ]; then
if [ -f ".env.example" ]; then
print_warning ".env file not found. Copying from .env.example..."
cp .env.example .env
print_warning "Please edit .env file with your actual configuration values."
print_warning "Required: GOOGLE_API_KEY"
else
print_error ".env.example file not found. Cannot create .env file."
exit 1
fi
fi
# Check for required environment variables
source .env 2>/dev/null || true
if [ -z "$GOOGLE_API_KEY" ] || [ "$GOOGLE_API_KEY" = "your_gemini_api_key_here" ]; then
print_error "GOOGLE_API_KEY is not set in .env file."
print_error "Please set your Gemini API key in the .env file."
exit 1
fi
print_success "Environment configuration looks good"
}
# Function to build Docker images
build_images() {
print_status "Building Docker images..."
$COMPOSE_CMD build --no-cache
print_success "Docker images built successfully"
}
# Function to start services
start_services() {
local compose_file=${1:-docker-compose.yml}
print_status "Starting services with $compose_file..."
$COMPOSE_CMD -f $compose_file up -d
print_success "Services started successfully"
print_status "Waiting for services to be ready..."
# Wait for backend to be ready
for i in {1..30}; do
if curl -s http://localhost:8000/api/v1/health/quick > /dev/null; then
print_success "Backend is ready!"
break
fi
sleep 2
if [ $i -eq 30 ]; then
print_warning "Backend is taking longer than expected to start"
fi
done
# Wait for frontend to be ready
for i in {1..30}; do
if curl -s http://localhost:8501/_stcore/health > /dev/null; then
print_success "Frontend is ready!"
break
fi
sleep 2
if [ $i -eq 30 ]; then
print_warning "Frontend is taking longer than expected to start"
fi
done
}
# Function to stop services
stop_services() {
local compose_file=${1:-docker-compose.yml}
print_status "Stopping services..."
$COMPOSE_CMD -f $compose_file down
print_success "Services stopped successfully"
}
# Function to show logs
show_logs() {
local service=$1
local compose_file=${2:-docker-compose.yml}
if [ -n "$service" ]; then
$COMPOSE_CMD -f $compose_file logs -f $service
else
$COMPOSE_CMD -f $compose_file logs -f
fi
}
# Function to show status
show_status() {
local compose_file=${1:-docker-compose.yml}
print_status "Service status:"
$COMPOSE_CMD -f $compose_file ps
echo ""
print_status "Service URLs:"
echo " Frontend: http://localhost:8501"
echo " Backend API: http://localhost:8000"
echo " API Docs: http://localhost:8000/docs"
echo " Qdrant: http://localhost:6333"
}
# Function to clean up
cleanup() {
print_status "Cleaning up Docker resources..."
# Stop and remove containers
$COMPOSE_CMD -f docker-compose.yml down --remove-orphans 2>/dev/null || true
$COMPOSE_CMD -f docker-compose.dev.yml down --remove-orphans 2>/dev/null || true
# Remove unused images
docker image prune -f
print_success "Cleanup completed"
}
# Function to reset everything
reset() {
print_warning "This will remove all containers, volumes, and rebuild everything."
read -p "Are you sure? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
print_status "Resetting environment..."
# Stop services
$COMPOSE_CMD -f docker-compose.yml down --volumes --remove-orphans 2>/dev/null || true
$COMPOSE_CMD -f docker-compose.dev.yml down --volumes --remove-orphans 2>/dev/null || true
# Remove images
docker rmi $(docker images | grep -E "(invoice|qdrant)" | awk '{print $3}') 2>/dev/null || true
# Rebuild and start
build_images
start_services
print_success "Environment reset completed"
else
print_status "Reset cancelled"
fi
}
# Function to show help
show_help() {
echo "Docker Setup Script for Invoice Reimbursement System"
echo ""
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo ""
echo "Commands:"
echo " setup - Initial setup (check dependencies, build images, start services)"
echo " build - Build Docker images"
echo " start - Start services in production mode"
echo " start-dev - Start services in development mode (with hot reload)"
echo " stop - Stop services"
echo " restart - Restart services"
echo " status - Show service status and URLs"
echo " logs [service] - Show logs (optionally for specific service)"
echo " cleanup - Clean up Docker resources"
echo " reset - Reset entire environment (destructive)"
echo " help - Show this help message"
echo ""
echo "Examples:"
echo " $0 setup # Initial setup"
echo " $0 start-dev # Start in development mode"
echo " $0 logs backend # Show backend logs"
echo " $0 logs # Show all logs"
}
# Main script logic
main() {
case "${1:-setup}" in
"setup")
check_docker
check_docker_compose
check_env
build_images
start_services
show_status
;;
"build")
check_docker
check_docker_compose
build_images
;;
"start")
check_docker
check_docker_compose
start_services docker-compose.yml
show_status docker-compose.yml
;;
"start-dev")
check_docker
check_docker_compose
start_services docker-compose.dev.yml
show_status docker-compose.dev.yml
;;
"stop")
stop_services docker-compose.yml
stop_services docker-compose.dev.yml
;;
"restart")
stop_services docker-compose.yml
stop_services docker-compose.dev.yml
start_services docker-compose.yml
show_status docker-compose.yml
;;
"status")
show_status
;;
"logs")
show_logs $2
;;
"cleanup")
cleanup
;;
"reset")
reset
;;
"help"|"-h"|"--help")
show_help
;;
*)
print_error "Unknown command: $1"
show_help
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"