-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck-requirements.sh
More file actions
478 lines (418 loc) · 16.9 KB
/
check-requirements.sh
File metadata and controls
478 lines (418 loc) · 16.9 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/bin/bash
# =============================================================================
# Requirements Verification Script for mflix Sample Application
# Python/FastAPI Backend
# =============================================================================
#
# This script checks that all necessary requirements are installed to run
# the mflix sample application with the Python/FastAPI backend.
#
# Usage:
# ./check-requirements-python.sh # Check all requirements (post-setup)
# ./check-requirements-python.sh --pre # Check only runtime requirements (pre-setup)
# ./check-requirements-python.sh --setup # Check and auto-setup missing items
# ./check-requirements-python.sh --help # Show help message
#
# =============================================================================
# Exit on error (but handle arithmetic expressions carefully)
set -e
# =============================================================================
# Configuration
# =============================================================================
SERVER_DIR="server"
CLIENT_DIR="client"
PYTHON_MIN_VERSION="3.11"
NODE_MIN_VERSION="18"
# =============================================================================
# Colors
# =============================================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# =============================================================================
# Counters
# =============================================================================
CHECKS_PASSED=0
CHECKS_FAILED=0
CHECKS_WARNED=0
# =============================================================================
# Helper Functions
# =============================================================================
print_header() {
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE} $1${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}
print_section() {
echo ""
echo -e "${YELLOW}▸ $1${NC}"
}
check_pass() {
echo -e " ${GREEN}✓${NC} $1"
CHECKS_PASSED=$((CHECKS_PASSED + 1))
}
check_fail() {
echo -e " ${RED}✗${NC} $1"
CHECKS_FAILED=$((CHECKS_FAILED + 1))
}
check_warn() {
echo -e " ${YELLOW}⚠${NC} $1"
CHECKS_WARNED=$((CHECKS_WARNED + 1))
}
check_info() {
echo -e " ${BLUE}→${NC} $1"
}
command_exists() {
command -v "$1" &>/dev/null
}
version_gte() {
# Returns 0 (true) if $1 >= $2 using version sorting
[ "$(printf '%s\n' "$2" "$1" | sort -V | head -n1)" = "$2" ]
}
show_help() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --pre Check only runtime requirements (use before setup)"
echo " --setup Attempt to automatically set up missing requirements"
echo " --help Show this help message"
echo ""
echo "This script checks that all necessary requirements are installed"
echo "to run the mflix sample application with the Python/FastAPI backend."
echo ""
echo "Use --pre before starting setup to verify you have the required runtime."
echo "Use without flags after completing setup to verify everything is ready."
exit 0
}
# =============================================================================
# Check Runtime Requirements (Pre-Setup)
# =============================================================================
check_runtime_requirements() {
print_section "Runtime Requirements"
# Check Python version
if command_exists python3; then
local python_version
python_version=$(python3 --version 2>&1 | grep -oE '[0-9]+\.[0-9]+' | head -1)
if version_gte "$python_version" "$PYTHON_MIN_VERSION"; then
check_pass "Python $python_version installed (>= $PYTHON_MIN_VERSION required)"
else
check_fail "Python $python_version installed but >= $PYTHON_MIN_VERSION required"
check_info "Install Python $PYTHON_MIN_VERSION+ from https://www.python.org/downloads/"
fi
else
check_fail "Python 3 not installed"
check_info "Install Python $PYTHON_MIN_VERSION+ from https://www.python.org/downloads/"
fi
# Check pip
if command_exists pip3 || python3 -m pip --version &>/dev/null; then
check_pass "pip installed"
else
check_fail "pip not installed"
check_info "Install pip: python3 -m ensurepip --upgrade"
fi
}
# =============================================================================
# Check Python/FastAPI Backend Requirements (Full)
# =============================================================================
check_backend_requirements() {
print_section "Python/FastAPI Backend Requirements"
local server_dir="$SCRIPT_DIR/$SERVER_DIR"
# Check Python version
if command_exists python3; then
local python_version
python_version=$(python3 --version 2>&1 | grep -oE '[0-9]+\.[0-9]+' | head -1)
if version_gte "$python_version" "$PYTHON_MIN_VERSION"; then
check_pass "Python $python_version installed (>= $PYTHON_MIN_VERSION required)"
else
check_fail "Python $python_version installed but >= $PYTHON_MIN_VERSION required"
check_info "Install Python $PYTHON_MIN_VERSION+ from https://www.python.org/downloads/"
fi
else
check_fail "Python 3 not installed"
check_info "Install Python $PYTHON_MIN_VERSION+ from https://www.python.org/downloads/"
return
fi
# Check pip
if command_exists pip3 || python3 -m pip --version &>/dev/null; then
check_pass "pip installed"
else
check_fail "pip not installed"
check_info "Install pip: python3 -m ensurepip --upgrade"
fi
# Check virtual environment
local venv_dir="$server_dir/.venv"
if [[ -d "$venv_dir" ]]; then
check_pass "Python virtual environment exists at $SERVER_DIR/.venv"
# Check if venv activation script exists
if [[ -f "$venv_dir/bin/activate" ]]; then
check_pass "Virtual environment activation script exists"
else
check_warn "Virtual environment activation script missing"
fi
else
check_warn "Python virtual environment not found at $SERVER_DIR/.venv"
if [[ "$SETUP_MODE" == true ]]; then
check_info "Creating virtual environment..."
if python3 -m venv "$venv_dir"; then
check_pass "Virtual environment created"
else
check_fail "Failed to create virtual environment"
fi
else
check_info "Create with: cd $SERVER_DIR && python3 -m venv .venv"
fi
fi
# Check Python dependencies
local requirements_file="$server_dir/requirements.txt"
if [[ -f "$requirements_file" ]]; then
check_pass "requirements.txt found"
# Check if key dependencies are installed
if [[ -d "$venv_dir" ]]; then
local pip_cmd="$venv_dir/bin/pip"
if [[ -f "$pip_cmd" ]]; then
# Check FastAPI
if "$pip_cmd" show fastapi &>/dev/null; then
check_pass "FastAPI installed in virtual environment"
else
check_warn "FastAPI not installed in virtual environment"
if [[ "$SETUP_MODE" == true ]]; then
check_info "Installing dependencies..."
if "$pip_cmd" install -r "$requirements_file" &>/dev/null; then
check_pass "Dependencies installed"
else
check_fail "Failed to install dependencies"
fi
else
check_info "Install with: source $SERVER_DIR/.venv/bin/activate && pip install -r $SERVER_DIR/requirements.txt"
fi
fi
# Check PyMongo
if "$pip_cmd" show pymongo &>/dev/null; then
check_pass "PyMongo installed in virtual environment"
else
check_warn "PyMongo not installed in virtual environment"
fi
fi
fi
else
check_fail "requirements.txt not found at $SERVER_DIR/requirements.txt"
fi
}
# =============================================================================
# Check Environment Configuration
# =============================================================================
check_env_configuration() {
print_section "Environment Configuration"
local server_dir="$SCRIPT_DIR/$SERVER_DIR"
local env_file="$server_dir/.env"
local env_example="$server_dir/.env.example"
# Check .env file
if [[ -f "$env_file" ]]; then
check_pass ".env file exists"
# Check MONGODB_URI
if grep -q "^MONGODB_URI=" "$env_file" 2>/dev/null; then
local mongo_uri
mongo_uri=$(grep "^MONGODB_URI=" "$env_file" | cut -d'=' -f2-)
if [[ -n "$mongo_uri" ]] && [[ "$mongo_uri" != *"<"*">"* ]]; then
check_pass "MONGODB_URI is configured"
else
check_fail "MONGODB_URI is not configured (still has placeholder value)"
check_info "Update MONGODB_URI in $SERVER_DIR/.env with your MongoDB connection string"
fi
else
check_fail "MONGODB_URI not found in .env"
check_info "Add MONGODB_URI to $SERVER_DIR/.env"
fi
# Check VOYAGE_API_KEY (optional)
if grep -q "^VOYAGE_API_KEY=" "$env_file" 2>/dev/null; then
local voyage_key
voyage_key=$(grep "^VOYAGE_API_KEY=" "$env_file" | cut -d'=' -f2-)
if [[ -n "$voyage_key" ]] && [[ "$voyage_key" != "your_voyage_api_key" ]]; then
check_pass "VOYAGE_API_KEY is configured"
else
check_info "VOYAGE_API_KEY not configured (optional - needed for vector search)"
fi
else
check_info "VOYAGE_API_KEY not set (optional - needed for vector search)"
fi
# Check CORS_ORIGINS (optional)
if grep -q "^CORS_ORIGINS=" "$env_file" 2>/dev/null; then
check_pass "CORS_ORIGINS is configured"
else
check_info "CORS_ORIGINS not set (will use default: http://localhost:3000)"
fi
# Check PORT (optional)
if grep -q "^PORT=" "$env_file" 2>/dev/null; then
check_pass "PORT is configured"
else
check_info "PORT not set (will use default: 3001)"
fi
else
check_warn ".env file not found"
if [[ -f "$env_example" ]]; then
if [[ "$SETUP_MODE" == true ]]; then
check_info "Creating .env from .env.example..."
if cp "$env_example" "$env_file"; then
check_pass ".env file created from .env.example"
check_warn "Please update the placeholder values in $SERVER_DIR/.env"
else
check_fail "Failed to create .env file"
fi
else
check_info "Copy .env.example to .env: cp $SERVER_DIR/.env.example $SERVER_DIR/.env"
fi
else
check_fail "No .env.example found to use as template"
fi
fi
}
# =============================================================================
# Check Frontend Requirements
# =============================================================================
check_frontend_requirements() {
print_section "Frontend Requirements (Next.js)"
local client_dir="$SCRIPT_DIR/$CLIENT_DIR"
# Check Node.js
if command_exists node; then
local node_version
node_version=$(node --version | sed 's/v//')
local node_major
node_major=$(echo "$node_version" | cut -d. -f1)
if [[ "$node_major" -ge "$NODE_MIN_VERSION" ]]; then
check_pass "Node.js installed (version $node_version, >= $NODE_MIN_VERSION required)"
else
check_fail "Node.js version $node_version is below minimum required ($NODE_MIN_VERSION+)"
check_info "Install Node.js $NODE_MIN_VERSION+: https://nodejs.org/"
fi
else
check_fail "Node.js not installed"
check_info "Install Node.js $NODE_MIN_VERSION+: https://nodejs.org/"
return
fi
# Check npm
if command_exists npm; then
local npm_version
npm_version=$(npm --version)
check_pass "npm installed (version $npm_version)"
else
check_fail "npm not installed"
check_info "npm should come with Node.js installation"
return
fi
# Check client directory
if [[ ! -d "$client_dir" ]]; then
check_warn "Client directory not found: $CLIENT_DIR"
check_info "Frontend may be in a separate repository"
return
fi
# Check client dependencies
if [[ -d "$client_dir/node_modules" ]]; then
check_pass "Frontend dependencies installed"
# Check Next.js
if [[ -d "$client_dir/node_modules/next" ]]; then
check_pass "Next.js dependency installed"
else
check_warn "Next.js not found in dependencies"
fi
# Check React
if [[ -d "$client_dir/node_modules/react" ]]; then
check_pass "React dependency installed"
else
check_warn "React not found in dependencies"
fi
else
check_warn "Frontend dependencies not installed"
if [[ "$SETUP_MODE" == true ]]; then
check_info "Installing frontend dependencies..."
if (cd "$client_dir" && npm install &>/dev/null); then
check_pass "Frontend dependencies installed successfully"
else
check_fail "Failed to install frontend dependencies"
fi
else
check_info "Run: cd $CLIENT_DIR && npm install"
fi
fi
}
# =============================================================================
# Print Summary
# =============================================================================
print_summary() {
print_header "Summary"
echo ""
echo -e " ${GREEN}Passed:${NC} $CHECKS_PASSED"
echo -e " ${RED}Failed:${NC} $CHECKS_FAILED"
echo -e " ${YELLOW}Warnings:${NC} $CHECKS_WARNED"
echo ""
if [[ $CHECKS_FAILED -eq 0 ]]; then
echo -e "${GREEN}All required checks passed!${NC}"
if [[ $CHECKS_WARNED -gt 0 ]]; then
echo -e "${YELLOW}There are some warnings to review.${NC}"
fi
else
echo -e "${RED}Some checks failed. Please address the issues above.${NC}"
if [[ "$SETUP_MODE" != true ]]; then
echo -e "${BLUE}Tip: Run with --setup flag to auto-fix some issues${NC}"
fi
fi
echo ""
}
# =============================================================================
# Main Execution
# =============================================================================
# Get script directory and change to it
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Default options
SETUP_MODE=false
PRE_CHECK_MODE=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--pre)
PRE_CHECK_MODE=true
shift
;;
--setup)
SETUP_MODE=true
shift
;;
--help|-h)
show_help
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Print banner
echo ""
echo -e "${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ mflix Sample Application - Requirements Check ║${NC}"
echo -e "${BLUE}║ Python/FastAPI Backend ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════════════════╝${NC}"
if [[ "$PRE_CHECK_MODE" == true ]]; then
echo -e "${YELLOW}Pre-setup check - verifying runtime requirements only${NC}"
elif [[ "$SETUP_MODE" == true ]]; then
echo -e "${YELLOW}Running in setup mode - will attempt to fix issues${NC}"
fi
# Run checks based on mode
if [[ "$PRE_CHECK_MODE" == true ]]; then
check_runtime_requirements
else
check_backend_requirements
check_env_configuration
check_frontend_requirements
fi
# Print summary
print_summary
# Exit with appropriate code
if [[ $CHECKS_FAILED -gt 0 ]]; then
exit 1
fi
exit 0