-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal-run.sh
More file actions
executable file
·938 lines (801 loc) · 27.4 KB
/
Copy pathlocal-run.sh
File metadata and controls
executable file
·938 lines (801 loc) · 27.4 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
#!/bin/bash
# Spring Microservices Management Script for Sustain-a-thon
# Version: 3.0
set -e
# Configuration
PROJECT_NAME="sustain-a-thon"
SERVICES=("service-registry" "api-gateway" "auth" "users")
SERVICE_PORTS=(8761 8080 8082 8083)
MAVEN_PID_DIR="/tmp/sustain-a-thon-$$"
ENV_FILE=".env.hackathon"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}ℹ️ ${NC} $1"
}
log_success() {
echo -e "${GREEN}✅${NC} $1"
}
log_warning() {
echo -e "${YELLOW}⚠️${NC} $1"
}
log_error() {
echo -e "${RED}❌${NC} $1"
}
log_header() {
echo ""
echo -e "${GREEN}🚀 $1${NC}"
echo ""
}
# Loading spinner function
show_spinner() {
local pid=$1
local delay=0.1
local spinstr='|/-\\'
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
# Countdown function
countdown() {
local seconds=$1
local message="${2:-Waiting}"
for ((i=seconds; i>0; i--)); do
printf "\r${YELLOW}⏳${NC} $message... ${i}s remaining"
sleep 1
done
printf "\r${GREEN}✅${NC} $message completed! \n"
}
# Function to check if port is in use
check_port() {
local port=$1
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
return 0
else
return 1
fi
}
# Function to kill process on port
kill_port() {
local port=$1
if check_port $port; then
local pid=$(lsof -Pi :$port -sTCP:LISTEN -t)
if [ ! -z "$pid" ]; then
log_warning "Killing process $pid on port $port"
kill -9 $pid 2>/dev/null || true
sleep 2
fi
fi
}
# Function to load environment variables
load_env() {
if [ -f "$ENV_FILE" ]; then
log_info "Loading environment variables from $ENV_FILE"
# Use set -a to automatically export variables, then source the file
set -a
source $ENV_FILE
set +a
log_success "Environment variables loaded"
else
log_warning "Environment file $ENV_FILE not found, using defaults"
fi
}
# Function to check prerequisites
check_prerequisites() {
log_header "Checking Prerequisites"
# Check Java
if command -v java &> /dev/null; then
local java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
log_success "Java found: $java_version"
else
log_error "Java not found. Please install Java 21 or higher."
exit 1
fi
# Check Maven
if command -v mvn &> /dev/null; then
local maven_version=$(mvn -version | head -n 1)
log_success "Maven found: $maven_version"
else
log_error "Maven not found. Please install Maven 3.6 or higher."
exit 1
fi
# Check Docker
if command -v docker &> /dev/null; then
local docker_version=$(docker --version)
log_success "Docker found: $docker_version"
else
log_warning "Docker not found. Docker commands will not be available."
fi
# All prerequisites checked - MySQL dependency removed
log_info "All microservices configured to run without database dependencies"
}
# Function to build all services
build_services() {
log_header "Building All Services"
for service in "${SERVICES[@]}"; do
if [ -d "$service" ]; then
log_info "Building $service..."
cd "$service"
# Create temp file for error output
local error_file="/tmp/build_error_${service}_$$.log"
# Clean and compile with error capture
if mvn clean compile -q > "$error_file" 2>&1; then
log_success "$service built successfully"
rm -f "$error_file"
else
log_error "Failed to build $service"
echo -e "${RED}📋 Build Error Details for $service:${NC}"
echo -e "${RED}═══════════════════════════════════════${NC}"
cat "$error_file" | head -20
echo -e "${RED}═══════════════════════════════════════${NC}"
echo -e "${YELLOW}💡 Full error log saved to: $error_file${NC}"
cd ..
return 1
fi
cd ..
else
log_error "Service directory $service not found"
return 1
fi
done
log_success "All services built successfully"
}
# Function to build single service
build_single_service() {
local service=$1
if [ -z "$service" ]; then
log_error "Service name is required"
log_info "Available services: ${SERVICES[*]}"
return 1
fi
# Check if service exists in array
local service_exists=false
for s in "${SERVICES[@]}"; do
if [ "$s" = "$service" ]; then
service_exists=true
break
fi
done
if [ "$service_exists" = false ]; then
log_error "Service '$service' not found"
log_info "Available services: ${SERVICES[*]}"
return 1
fi
if [ -d "$service" ]; then
log_info "Building $service..."
cd "$service"
# Create temp file for error output
local error_file="/tmp/build_error_${service}_$$.log"
# Clean and compile with error capture
if mvn clean compile > "$error_file" 2>&1; then
log_success "$service built successfully"
rm -f "$error_file"
cd ..
return 0
else
log_error "Failed to build $service"
echo -e "${RED}📋 Build Error Details for $service:${NC}"
echo -e "${RED}═══════════════════════════════════════${NC}"
cat "$error_file"
echo -e "${RED}═══════════════════════════════════════${NC}"
cd ..
return 1
fi
else
log_error "Service directory $service not found"
return 1
fi
}
# Function to get service port
get_service_port() {
local service=$1
for i in "${!SERVICES[@]}"; do
if [ "${SERVICES[$i]}" = "$service" ]; then
echo "${SERVICE_PORTS[$i]}"
return 0
fi
done
return 1
}
# Function to validate service name
validate_service() {
local service=$1
if [ -z "$service" ]; then
log_error "Service name is required"
log_info "Available services: ${SERVICES[*]}"
return 1
fi
# Check if service exists in array
for s in "${SERVICES[@]}"; do
if [ "$s" = "$service" ]; then
return 0
fi
done
log_error "Service '$service' not found"
log_info "Available services: ${SERVICES[*]}"
return 1
}
# Function to start individual service
start_service() {
local service=$1
local port=$2
if [ ! -d "$service" ]; then
log_error "Service directory $service not found"
return 1
fi
# Kill any existing process on the port
kill_port $port
cd "$service"
# Create PID directory if it doesn't exist
mkdir -p "$MAVEN_PID_DIR"
# Start the service in background
nohup mvn spring-boot:run \
-Dspring-boot.run.jvmArguments="-Xmx512m -Xms256m" \
> "$MAVEN_PID_DIR/${service}.log" 2>&1 &
local pid=$!
echo $pid > "$MAVEN_PID_DIR/${service}.pid"
cd ..
# Wait with spinner and check if service started
printf "${YELLOW}⏳${NC} Starting $service"
for i in {1..20}; do
if check_port $port; then
printf "\n"
log_success "$service started successfully (PID: $pid, Port: $port)"
return 0
fi
printf "."
sleep 0.5
done
printf "\n"
log_error "$service failed to start on port $port"
echo -e "${RED}📋 Startup Error Details for $service:${NC}"
echo -e "${RED}═══════════════════════════════════════${NC}"
if [ -f "$MAVEN_PID_DIR/${service}.log" ]; then
tail -20 "$MAVEN_PID_DIR/${service}.log" | grep -E "ERROR|Exception|Failed|error"
else
echo "No log file found at $MAVEN_PID_DIR/${service}.log"
fi
echo -e "${RED}═══════════════════════════════════════${NC}"
echo -e "${YELLOW}💡 Full startup log: $MAVEN_PID_DIR/${service}.log${NC}"
return 1
}
# Function to start single service by name
start_single_service() {
local service=$1
if ! validate_service "$service"; then
return 1
fi
local port=$(get_service_port "$service")
if [ $? -ne 0 ]; then
log_error "Could not find port for service $service"
return 1
fi
log_header "Starting Single Service: $service"
load_env
# Build the service first
build_single_service "$service"
if [ $? -ne 0 ]; then
log_error "Failed to build $service"
return 1
fi
# Check if service-registry is running (required for other services)
if [ "$service" != "service-registry" ]; then
if ! check_port 8761; then
log_warning "service-registry is not running. Starting it first..."
start_single_service "service-registry"
if [ $? -ne 0 ]; then
log_error "Failed to start service-registry"
return 1
fi
sleep 5
fi
fi
start_service "$service" "$port"
return $?
}
# Function to start all services
start_all() {
log_header "Starting All Services"
load_env
# Start services in order (service-registry first)
for i in "${!SERVICES[@]}"; do
local service="${SERVICES[$i]}"
local port="${SERVICE_PORTS[$i]}"
start_service "$service" "$port"
if [ $? -ne 0 ]; then
log_error "Failed to start $service. Stopping startup process."
return 1
fi
# Extra wait for service-registry to be fully up
if [ "$service" = "service-registry" ]; then
printf "${YELLOW}⏳${NC} Waiting for service-registry to be fully ready"
for i in {1..15}; do
printf "."
sleep 1
done
printf "\n"
fi
done
log_success "All services started successfully!"
show_status
}
# Function to stop individual service
stop_service() {
local service=$1
local port=$2
log_info "Stopping $service..."
# Check PID file
if [ -f "$MAVEN_PID_DIR/${service}.pid" ]; then
local pid=$(cat "$MAVEN_PID_DIR/${service}.pid")
if kill -0 $pid 2>/dev/null; then
kill $pid
sleep 5
# Force kill if still running
if kill -0 $pid 2>/dev/null; then
kill -9 $pid
fi
log_success "$service stopped (PID: $pid)"
fi
rm -f "$MAVEN_PID_DIR/${service}.pid"
fi
# Also kill any process on the port
kill_port $port
}
# Function to stop single service by name
stop_single_service() {
local service=$1
if ! validate_service "$service"; then
return 1
fi
local port=$(get_service_port "$service")
if [ $? -ne 0 ]; then
log_error "Could not find port for service $service"
return 1
fi
log_header "Stopping Single Service: $service"
stop_service "$service" "$port"
log_success "$service stopped successfully"
}
# Function to restart single service by name
restart_single_service() {
local service=$1
if ! validate_service "$service"; then
return 1
fi
log_header "Restarting Single Service: $service"
# Stop the service first
stop_single_service "$service"
# Wait a moment
sleep 3
# Start the service
start_single_service "$service"
}
# Function to stop all services
stop_all() {
log_header "Stopping All Services"
# Stop services in reverse order
for ((i=${#SERVICES[@]}-1; i>=0; i--)); do
local service="${SERVICES[$i]}"
local port="${SERVICE_PORTS[$i]}"
stop_service "$service" "$port"
done
# Clean up PID directory
rm -rf "$MAVEN_PID_DIR"
log_success "All services stopped"
}
# Function to restart all services
restart_all() {
log_header "Restarting All Services"
stop_all
sleep 3
start_all
}
# Function to show service status
show_status() {
log_header "Service Status"
printf "%-20s %-10s %-15s %-10s\n" "SERVICE" "PORT" "STATUS" "PID"
printf "%-20s %-10s %-15s %-10s\n" "-------" "----" "------" "---"
for i in "${!SERVICES[@]}"; do
local service="${SERVICES[$i]}"
local port="${SERVICE_PORTS[$i]}"
local status="STOPPED"
local pid="N/A"
if check_port $port; then
status="RUNNING"
if [ -f "$MAVEN_PID_DIR/${service}.pid" ]; then
pid=$(cat "$MAVEN_PID_DIR/${service}.pid")
else
pid=$(lsof -Pi :$port -sTCP:LISTEN -t)
fi
printf "%-20s %-10s ${GREEN}%-15s${NC} %-10s\n" "$service" "$port" "$status" "$pid"
else
status="STOPPED"
printf "%-20s %-10s ${RED}%-15s${NC} %-10s\n" "$service" "$port" "$status" "$pid"
fi
done
echo ""
log_info "Health check endpoints:"
for i in "${!SERVICES[@]}"; do
local service="${SERVICES[$i]}"
local port="${SERVICE_PORTS[$i]}"
if check_port $port; then
case $service in
"service-registry")
echo " $service: http://localhost:$port/api/v1/service/health"
;;
"api-gateway")
echo " $service: http://localhost:$port/health"
;;
"auth")
echo " $service: http://localhost:$port/api/v1/auth/health"
;;
"users")
echo " $service: http://localhost:$port/api/v1/users/health"
;;
esac
fi
done
}
# Function to run with Docker Compose
docker_up() {
log_header "Starting Services with Docker Compose"
if ! command -v docker-compose &> /dev/null && ! command -v docker &> /dev/null; then
log_error "Docker or docker-compose not found"
return 1
fi
load_env
# Use docker compose (newer) or docker-compose (older)
if command -v docker &> /dev/null && docker compose version &> /dev/null; then
docker compose --env-file $ENV_FILE up -d
elif command -v docker-compose &> /dev/null; then
docker-compose --env-file $ENV_FILE up -d
else
log_error "Neither 'docker compose' nor 'docker-compose' found"
return 1
fi
log_success "Services started with Docker Compose"
}
# Function to stop Docker Compose
docker_down() {
log_header "Stopping Docker Compose Services"
if command -v docker &> /dev/null && docker compose version &> /dev/null; then
docker compose down
elif command -v docker-compose &> /dev/null; then
docker-compose down
else
log_error "Neither 'docker compose' nor 'docker-compose' found"
return 1
fi
log_success "Docker Compose services stopped"
}
# Function to test health endpoint
test_health_endpoint() {
local service=$1
local port=$2
local url=""
# Set correct health endpoint for each service
case $service in
"service-registry")
url="http://localhost:$port/api/v1/service/health"
;;
"api-gateway")
url="http://localhost:$port/health"
;;
"auth")
url="http://localhost:$port/api/v1/auth/health"
;;
"users")
url="http://localhost:$port/api/v1/users/health"
;;
*)
log_error "Unknown service: $service"
return 1
;;
esac
if command -v curl &> /dev/null; then
local response=$(curl -s -o /dev/null -w "%{http_code}" "$url" 2>/dev/null)
if [ "$response" = "200" ]; then
log_success "$service health check passed (HTTP $response)"
return 0
else
log_error "$service health check failed (HTTP $response)"
return 1
fi
else
log_warning "curl not found, skipping health check for $service"
return 0
fi
}
# Function for quick test
quick_test() {
log_header "Quick Test - Full Service Validation"
# Check prerequisites first
check_prerequisites
if [ $? -ne 0 ]; then
log_error "Prerequisites check failed. Aborting quick test."
return 1
fi
load_env
# Check which services are running and start missing ones
log_info "Checking service status..."
local services_to_start=()
for i in "${!SERVICES[@]}"; do
local service="${SERVICES[$i]}"
local port="${SERVICE_PORTS[$i]}"
if check_port $port; then
log_success "$service is already running on port $port"
else
log_info "$service is not running, will start it"
services_to_start+=("$service")
fi
done
# Start missing services in correct order
if [ ${#services_to_start[@]} -gt 0 ]; then
log_header "Starting Missing Services"
build_services
if [ $? -ne 0 ]; then
log_error "Build failed. Aborting quick test."
return 1
fi
# Start services in correct order: service-registry -> api-gateway -> auth -> users
local start_order=("service-registry" "api-gateway" "auth" "users")
for service in "${start_order[@]}"; do
# Check if this service needs to be started
for missing_service in "${services_to_start[@]}"; do
if [ "$service" = "$missing_service" ]; then
local port=$(get_service_port "$service")
log_info "Starting $service..."
start_service "$service" "$port"
if [ $? -ne 0 ]; then
log_error "Failed to start $service. Aborting quick test."
return 1
fi
# Extra wait for service-registry
if [ "$service" = "service-registry" ]; then
log_info "Waiting for service-registry to be fully ready..."
sleep 15
fi
break
fi
done
done
else
log_success "All services are already running"
fi
# Wait 30 seconds with countdown
log_header "Waiting for Services to Stabilize"
countdown 30 "Services stabilizing"
# Test health endpoints
log_header "Testing Health Endpoints"
local all_tests_passed=true
for i in "${!SERVICES[@]}"; do
local service="${SERVICES[$i]}"
local port="${SERVICE_PORTS[$i]}"
log_info "Testing $service health endpoint..."
test_health_endpoint "$service" "$port"
if [ $? -ne 0 ]; then
all_tests_passed=false
fi
done
# Show final results
log_header "Quick Test Results"
if [ "$all_tests_passed" = true ]; then
log_success "All health checks passed! ✨"
log_info "Services are ready for development"
else
log_error "Some health checks failed! ❌"
log_info "Service failed to start - check if port is already in use"
fi
# Stop all services
log_header "Cleaning Up - Stopping All Services"
stop_all
if [ "$all_tests_passed" = true ]; then
log_success "Quick test completed successfully! 🎉"
return 0
else
log_error "Quick test completed with failures! 💥"
return 1
fi
}
# Function to perform comprehensive health check
health_check() {
log_header "Health Check - Testing All Endpoints"
local failed_services=()
local passed_count=0
local total_count=0
echo -e "${BLUE}Testing all service health endpoints...${NC}"
echo ""
for i in "${!SERVICES[@]}"; do
local service="${SERVICES[$i]}"
local port="${SERVICE_PORTS[$i]}"
local url=""
total_count=$((total_count + 1))
# Set correct health endpoint for each service
case $service in
"service-registry")
url="http://localhost:$port/api/v1/service/health"
;;
"api-gateway")
url="http://localhost:$port/health"
;;
"auth")
url="http://localhost:$port/api/v1/auth/health"
;;
"users")
url="http://localhost:$port/api/v1/users/health"
;;
esac
printf "%-20s %-10s " "$service" "$port"
if ! check_port $port; then
echo -e "${RED}❌ FAILED${NC} - Service not running"
failed_services+=("$service: Service not running on port $port")
continue
fi
if command -v curl &> /dev/null; then
local temp_file="/tmp/health_check_${service}_$$.json"
local http_code=$(curl -s -o "$temp_file" -w "%{http_code}" "$url" 2>/dev/null)
if [ "$http_code" = "200" ]; then
# Check if response is valid JSON and has status field
if jq -e '.status' "$temp_file" > /dev/null 2>&1; then
local status=$(jq -r '.status' "$temp_file" 2>/dev/null)
if [ "$status" = "true" ]; then
echo -e "${GREEN}✅ PASSED${NC} - HTTP $http_code"
passed_count=$((passed_count + 1))
else
echo -e "${RED}❌ FAILED${NC} - Status: false"
local message=$(jq -r '.message // "Unknown error"' "$temp_file" 2>/dev/null)
failed_services+=("$service: Health check returned status=false - $message")
fi
else
echo -e "${YELLOW}⚠️ WARNING${NC} - Invalid JSON response"
failed_services+=("$service: Invalid JSON response from health endpoint")
fi
else
echo -e "${RED}❌ FAILED${NC} - HTTP $http_code"
local error_msg="HTTP $http_code"
if [ -f "$temp_file" ] && [ -s "$temp_file" ]; then
local response_msg=$(jq -r '.message // .error // "No error message"' "$temp_file" 2>/dev/null || cat "$temp_file" | head -1)
error_msg="$error_msg - $response_msg"
fi
failed_services+=("$service: $error_msg")
fi
rm -f "$temp_file"
else
echo -e "${YELLOW}⚠️ SKIPPED${NC} - curl not available"
fi
done
echo ""
echo -e "${BLUE}Health Check Summary:${NC}"
echo -e "${GREEN}✅ Passed: $passed_count/$total_count services${NC}"
if [ ${#failed_services[@]} -eq 0 ]; then
echo -e "${GREEN}🎉 All services are healthy!${NC}"
return 0
else
echo -e "${RED}❌ Failed: ${#failed_services[@]}/$total_count services${NC}"
echo ""
echo -e "${RED}Failed Services Details:${NC}"
for failure in "${failed_services[@]}"; do
echo -e "${RED} • $failure${NC}"
done
echo ""
echo -e "${YELLOW}💡 Troubleshooting Tips:${NC}"
echo -e "${YELLOW} • Check if services are running: ./local-run.sh status${NC}"
echo -e "${YELLOW} • Restart failed services: ./local-run.sh restart <service-name>${NC}"
return 1
fi
}
# Function to show help
show_help() {
echo "Usage: $0 [COMMAND] [SERVICE] [OPTIONS]"
echo ""
echo "Commands:"
echo " start [service] Start all services or a specific service"
echo " stop [service] Stop all services or a specific service"
echo " restart [service] Restart all services or a specific service"
echo " build [service] Build all services or a specific service"
echo " status Show service status"
echo " health Comprehensive health check of all endpoints"
echo " test Quick test - start services, test health, stop services"
echo " docker-up Start services with Docker Compose"
echo " docker-down Stop Docker Compose services"
echo " check Check prerequisites"
echo " help Show this help message"
echo ""
echo "Available Services:"
echo " ${SERVICES[*]}"
echo ""
echo "Examples:"
echo " $0 start # Start all services"
echo " $0 start auth # Start only auth service"
echo " $0 stop users # Stop only users service"
echo " $0 restart api-gateway # Restart only api-gateway service"
echo " $0 build service-registry # Build only service-registry"
echo " $0 docker-up # Start with Docker"
echo ""
}
# Main script logic
main() {
case "${1:-help}" in
"start")
if [ -n "$2" ]; then
# Start single service
start_single_service "$2"
else
# Start all services
start_all
fi
;;
"stop")
if [ -n "$2" ]; then
# Stop single service
stop_single_service "$2"
else
# Stop all services
stop_all
fi
;;
"restart")
if [ -n "$2" ]; then
# Restart single service
check_prerequisites
restart_single_service "$2"
else
# Restart all services
check_prerequisites
build_services
restart_all
fi
;;
"build")
if [ -n "$2" ]; then
# Build single service
check_prerequisites
build_single_service "$2"
else
# Build all services
check_prerequisites
build_services
fi
;;
"status")
show_status
;;
"health")
health_check
;;
"test")
quick_test
;;
"docker-up")
docker_up
;;
"docker-down")
docker_down
;;
"check")
check_prerequisites
;;
"help"|"--help"|"-h"|"")
show_help
;;
*)
log_error "Unknown command: $1"
show_help
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"