-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrh
More file actions
executable file
·1082 lines (951 loc) · 39 KB
/
rh
File metadata and controls
executable file
·1082 lines (951 loc) · 39 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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Function to display help
show_help() {
echo -e "${CYAN}"
echo " ____ _ _ _____ ____ ___ ____ "
echo " | _ \| | | | ____/ ___|_ _/ ___| "
echo " | |_) | |_| | _| \___ \| |\___ \ "
echo " | _ <| _ | |___ ___) | | ___) |"
echo " |_| \_\_| |_|_____|____/___|____/ "
echo -e "${NC}"
echo ""
echo -e "${WHITE}Rhesis CLI - Development Server Manager${NC}"
echo -e "${PURPLE}════════════════════════════════════════${NC}"
echo ""
echo -e "${YELLOW}Usage:${NC}"
echo -e " ${GREEN}./rh start${NC} - Start all services with Docker (zero-config)"
echo -e " ${GREEN}./rh stop${NC} - Stop all Docker services"
echo -e " ${GREEN}./rh restart${NC} - Restart all Docker services"
echo -e " ${GREEN}./rh logs${NC} - View logs from all services"
echo -e " ${GREEN}./rh delete${NC} - Delete all services, images, volumes, and data"
echo ""
echo -e "${YELLOW}Local Development:${NC}"
echo -e " ${GREEN}./rh dev init${NC} - Initialize env files (one-time setup)"
echo -e " ${GREEN}./rh dev up${NC} - Start dev infrastructure (postgres:11000, redis:11001)"
echo -e " ${GREEN}./rh dev down${NC} - Stop dev infrastructure"
echo -e " ${GREEN}./rh dev clean${NC} - Remove containers and volumes (resets database)"
echo -e " ${GREEN}./rh dev status${NC} - Show dev environment status"
echo -e " ${GREEN}./rh dev backend${NC} - Start the backend server (local)"
echo -e " ${GREEN}./rh dev frontend${NC} - Start the frontend server (local)"
echo -e " ${GREEN}./rh dev chatbot${NC} - Start the chatbot server (local)"
echo -e " ${GREEN}./rh dev docs${NC} - Start the documentation server (local)"
echo -e " ${GREEN}./rh dev worker${NC} - Start the Celery worker (local)"
echo -e " ${GREEN}./rh dev polyphemus${NC} - Start the Polyphemus service (local)"
echo ""
echo -e "${YELLOW}Testing:${NC}"
echo -e " ${GREEN}./rh test frontend${NC} - Run frontend tests"
echo ""
echo -e "${YELLOW}Worktree:${NC}"
echo -e " ${GREEN}./rh worktree <name>${NC} - Create a worktree with shared env/config"
echo -e " ${GREEN}./rh worktree <name> --remove${NC} - Remove worktree and delete branch"
echo -e " ${GREEN}./rh worktree <name> --load${NC} - Launch shell in worktree"
echo -e " ${GREEN}./rh worktree --list${NC} - List all worktrees"
echo ""
echo -e "${YELLOW}Other:${NC}"
echo -e " ${GREEN}./rh help${NC} - Show this help message"
echo ""
echo -e "${YELLOW}Examples:${NC}"
echo -e " ${BLUE}./rh start${NC} # Start all services with Docker"
echo -e " ${BLUE}./rh dev init${NC} # Initialize environment files (first time)"
echo -e " ${BLUE}./rh dev up${NC} # Bring up dev infrastructure"
echo -e " ${BLUE}./rh dev backend${NC} # Start backend locally (without Docker)"
echo -e " ${BLUE}./rh dev frontend${NC} # Start frontend locally (without Docker)"
echo -e " ${BLUE}./rh stop${NC} # Stop all Docker services"
echo -e " ${BLUE}./rh delete${NC} # Delete everything (services, images, volumes, data)"
echo -e " ${BLUE}./rh logs backend${NC} # View backend logs"
echo -e " ${BLUE}./rh test frontend${NC} # Run frontend tests"
echo ""
}
# ============================================================================
# Common utility functions
# ============================================================================
# Function to check if Docker is running
check_docker() {
if ! docker info > /dev/null 2>&1; then
echo -e "${RED}❌ Error: Docker is not running${NC}"
echo -e "${YELLOW} Please start Docker Desktop and try again${NC}"
exit 1
fi
}
# Function to check if uv is installed
check_uv() {
if ! command -v uv &> /dev/null; then
echo -e "${RED}❌ Error: uv is not installed${NC}"
echo -e "${YELLOW} Install uv with: curl -LsSf https://astral.sh/uv/install.sh | sh${NC}"
echo -e "${YELLOW} Or visit: https://docs.astral.sh/uv/getting-started/installation/${NC}"
exit 1
fi
}
# Function to check if Node.js and npm are installed
check_node() {
if ! command -v node &> /dev/null; then
echo -e "${RED}❌ Error: Node.js is not installed${NC}"
echo -e "${YELLOW} Install Node.js from: https://nodejs.org/${NC}"
exit 1
fi
if ! command -v npm &> /dev/null; then
echo -e "${RED}❌ Error: npm is not installed${NC}"
echo -e "${YELLOW} npm should come with Node.js. Reinstall Node.js from: https://nodejs.org/${NC}"
exit 1
fi
}
# Function to generate encryption key using Docker
generate_encryption_key() {
docker run --rm mirror.gcr.io/library/python:3.10.17-slim sh -c \
"pip install --quiet --no-cache-dir cryptography > /dev/null 2>&1 && \
python3 -c 'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())'" 2>/dev/null
}
# ============================================================================
# Dev environment functions
# ============================================================================
# Dev environment configuration (distinct from prod:5432/6379 and test:10001/10002)
DEV_POSTGRES_PORT=11000
DEV_REDIS_PORT=11001
DEV_ENV_MARKER="# Generated by ./rh dev init"
DEV_BACKEND_ENV="$SCRIPT_DIR/apps/backend/.env"
DEV_FRONTEND_ENV="$SCRIPT_DIR/apps/frontend/.env.local"
# Function to check if env file was generated by dev script
is_dev_generated() {
local file="$1"
[ -f "$file" ] && head -n1 "$file" | grep -qF "$DEV_ENV_MARKER"
}
# Function to check if env files are initialized
check_env_files_exist() {
[ -f "$DEV_BACKEND_ENV" ] && [ -f "$DEV_FRONTEND_ENV" ]
}
# Function to prompt for overwrite
prompt_overwrite() {
local file="$1"
echo -e "${YELLOW}⚠️ File exists: $file${NC}"
echo -e "${YELLOW} This file was NOT generated by ./rh dev init.${NC}"
read -p "$(echo -e ${YELLOW}Overwrite? [y/N]: ${NC})" -r
[[ $REPLY =~ ^[Yy]$ ]]
}
# Function to create/update backend .env file
create_backend_env() {
local env_file="$DEV_BACKEND_ENV"
local encryption_key="$1"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Check if file exists and wasn't generated by us
if [ -f "$env_file" ] && ! is_dev_generated "$env_file"; then
if ! prompt_overwrite "$env_file"; then
echo -e "${BLUE} Skipping $env_file${NC}"
return 0
fi
fi
echo -e "${GREEN}Creating $env_file${NC}"
cat > "$env_file" << EOF
$DEV_ENV_MARKER on $timestamp
QUICK_START=true
# Database (dev ports)
SQLALCHEMY_DATABASE_URL=postgresql://rhesis-user:rhesis-password@localhost:${DEV_POSTGRES_PORT}/rhesis-db
SQLALCHEMY_DB_DRIVER=postgresql
SQLALCHEMY_DB_PORT=${DEV_POSTGRES_PORT}
SQLALCHEMY_DB_USER=rhesis-user
SQLALCHEMY_DB_PASS=rhesis-password
SQLALCHEMY_DB_HOST=localhost:${DEV_POSTGRES_PORT}
SQLALCHEMY_DB_NAME=rhesis-db
DB_ENCRYPTION_KEY=${encryption_key}
# Redis (dev ports)
REDIS_URL=redis://:rhesis-redis-pass@localhost:${DEV_REDIS_PORT}/0
BROKER_URL=redis://:rhesis-redis-pass@localhost:${DEV_REDIS_PORT}/0
CELERY_RESULT_BACKEND=redis://:rhesis-redis-pass@localhost:${DEV_REDIS_PORT}/1
# Environment
ENVIRONMENT=development
BACKEND_ENV=development
LOG_LEVEL=DEBUG
# URLs
FRONTEND_URL=http://localhost:3000
BACKEND_URL=http://localhost:8080
# JWT
JWT_SECRET_KEY=dev-jwt-secret-key-change-in-production
JWT_ALGORITHM=HS256
JWT_ACCESS_TOKEN_EXPIRE_MINUTES=10080
EOF
}
# Function to create/update frontend .env.local file
create_frontend_env() {
local env_file="$DEV_FRONTEND_ENV"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Check if file exists and wasn't generated by us
if [ -f "$env_file" ] && ! is_dev_generated "$env_file"; then
if ! prompt_overwrite "$env_file"; then
echo -e "${BLUE} Skipping $env_file${NC}"
return 0
fi
fi
echo -e "${GREEN}Creating $env_file${NC}"
cat > "$env_file" << EOF
$DEV_ENV_MARKER on $timestamp
NEXT_PUBLIC_QUICK_START=true
NEXT_PUBLIC_API_BASE_URL=http://localhost:8080
NEXT_PUBLIC_APP_URL=http://localhost:3000
BACKEND_URL=http://localhost:8080
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=dev-nextauth-secret-change-in-production
NEXT_TELEMETRY_DISABLED=1
EOF
}
# Function to initialize dev environment files (one-time setup)
dev_init() {
echo -e "${GREEN}Initializing Rhesis Development Environment...${NC}"
echo ""
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
# Check prerequisites
check_docker
# Check if files already exist
if check_env_files_exist; then
echo -e "${YELLOW}⚠️ Environment files already exist:${NC}"
echo -e " - ${BLUE}apps/backend/.env${NC}"
echo -e " - ${BLUE}apps/frontend/.env.local${NC}"
echo ""
echo -e "${YELLOW}Consider backing up these files before continuing if they contain${NC}"
echo -e "${YELLOW} custom configuration or secrets you want to preserve.${NC}"
echo ""
read -p "$(echo -e ${YELLOW}Do you want to reinitialize? This will overwrite existing files. [y/N]: ${NC})" -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${GREEN}✅ Keeping existing configuration${NC}"
return 0
fi
fi
# Step 1: Generate encryption key
echo -e "${YELLOW}Generating encryption key...${NC}"
ENCRYPTION_KEY=$(generate_encryption_key)
if [ -z "$ENCRYPTION_KEY" ]; then
echo -e "${RED}❌ Error: Failed to generate encryption key${NC}"
exit 1
fi
echo -e "${GREEN}✅ Encryption key generated${NC}"
# Step 2: Create env files
echo ""
echo -e "${YELLOW}Creating environment files...${NC}"
create_backend_env "$ENCRYPTION_KEY"
create_frontend_env
# Step 3: Display success message and backup reminder
echo ""
echo -e "${PURPLE}════════════════════════════════════════════════${NC}"
echo -e "${GREEN}✅ Environment files created!${NC}"
echo -e "${PURPLE}════════════════════════════════════════════════${NC}"
echo ""
echo -e "${CYAN}Created files:${NC}"
echo -e " - ${WHITE}apps/backend/.env${NC}"
echo -e " - ${WHITE}apps/frontend/.env.local${NC}"
echo ""
echo -e "${CYAN}Next step:${NC}"
echo -e " ${GREEN}./rh dev up${NC} # Start dev infrastructure (postgres, redis)"
echo ""
}
# Function to start dev infrastructure
dev_up() {
echo -e "${GREEN}Starting Rhesis Development Infrastructure...${NC}"
echo ""
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
# Check prerequisites
check_docker
check_uv
check_node
# Check if env files exist
if ! check_env_files_exist; then
echo -e "${RED}❌ Environment files not found!${NC}"
echo ""
echo -e "${YELLOW}Please run initialization first:${NC}"
echo -e " ${GREEN}./rh dev init${NC}"
echo ""
echo -e "${CYAN}This will create the following files:${NC}"
echo -e " - ${WHITE}apps/backend/.env${NC}"
echo -e " - ${WHITE}apps/frontend/.env.local${NC}"
echo ""
exit 1
fi
# Check if env files were generated by us (have the marker)
if ! is_dev_generated "$DEV_BACKEND_ENV" || ! is_dev_generated "$DEV_FRONTEND_ENV"; then
echo -e "${RED}❌ Environment files exist but were not created by ./rh dev init${NC}"
echo -e " - ${BLUE}apps/backend/.env${NC}"
echo -e " - ${BLUE}apps/frontend/.env.local${NC}"
echo ""
echo -e "${YELLOW}Using manually created files may cause port or configuration mismatches.${NC}"
echo -e "${YELLOW}Please initialize proper dev environment files first:${NC}"
echo -e " ${GREEN}./rh dev init${NC}"
echo ""
exit 1
fi
# Start postgres and redis with dev ports
echo -e "${YELLOW}Starting dev containers (postgres:${DEV_POSTGRES_PORT}, redis:${DEV_REDIS_PORT})...${NC}"
# Start existing containers or create new ones
docker start rhesis-dev-postgres 2>/dev/null || \
docker run -d \
--name rhesis-dev-postgres \
-e POSTGRES_DB=rhesis-db \
-e POSTGRES_USER=rhesis-user \
-e POSTGRES_PASSWORD=rhesis-password \
-p ${DEV_POSTGRES_PORT}:5432 \
mirror.gcr.io/pgvector/pgvector:pg16
docker start rhesis-dev-redis 2>/dev/null || \
docker run -d \
--name rhesis-dev-redis \
-p ${DEV_REDIS_PORT}:6379 \
mirror.gcr.io/library/redis:7-alpine \
redis-server --requirepass rhesis-redis-pass
# Wait for postgres to be ready
echo -e "${BLUE}Waiting for PostgreSQL to be ready...${NC}"
for i in {1..30}; do
if docker exec rhesis-dev-postgres pg_isready -U rhesis-user -d rhesis-db > /dev/null 2>&1; then
echo -e "${GREEN}✅ PostgreSQL is ready${NC}"
break
fi
sleep 1
done
# Display success message
echo ""
echo -e "${PURPLE}════════════════════════════════════════════════${NC}"
echo -e "${GREEN}✅ Development infrastructure ready!${NC}"
echo -e "${PURPLE}════════════════════════════════════════════════${NC}"
echo ""
echo -e "${CYAN}Running services:${NC}"
echo -e " PostgreSQL: ${WHITE}localhost:${DEV_POSTGRES_PORT}${NC}"
echo -e " Redis: ${WHITE}localhost:${DEV_REDIS_PORT}${NC}"
echo -e " ${BLUE}Run ${GREEN}./rh dev status${BLUE} to check environment anytime${NC}"
echo ""
echo -e "${CYAN}Start services:${NC}"
echo -e " ${GREEN}./rh dev backend${NC} or ${GREEN}cd apps/backend && ./start.sh${NC}"
echo -e " ${GREEN}./rh dev frontend${NC} or ${GREEN}cd apps/frontend && ./start.sh${NC}"
echo -e " ${GREEN}./rh dev worker${NC} or ${GREEN}cd apps/worker && ./start.sh${NC} (optional)"
echo ""
echo -e "${YELLOW}Stop infrastructure (postgres, redis):${NC}"
echo -e " ${GREEN}./rh dev down${NC} # Stop containers"
echo -e " ${GREEN}./rh dev clean${NC} # Remove containers and volumes (resets database)"
echo ""
}
# Function to stop dev infrastructure
dev_down() {
echo -e "${YELLOW}Stopping Rhesis Development Infrastructure...${NC}"
echo ""
# Stop and remove containers
docker stop rhesis-dev-postgres rhesis-dev-redis 2>/dev/null || true
docker rm rhesis-dev-postgres rhesis-dev-redis 2>/dev/null || true
echo -e "${GREEN}✅ Development infrastructure stopped${NC}"
echo ""
echo -e "${CYAN}To start again:${NC}"
echo -e " ${GREEN}./rh dev up${NC}"
echo ""
}
# Function to clean dev infrastructure (remove containers and volumes)
dev_clean() {
echo -e "${RED}Cleaning Rhesis Development Infrastructure...${NC}"
echo ""
echo -e "${YELLOW}This will remove:${NC}"
echo -e " - Dev containers (rhesis-dev-postgres, rhesis-dev-redis)"
echo -e " - Associated Docker volumes (DATABASE DATA WILL BE LOST!)"
echo ""
read -p "$(echo -e ${YELLOW}Are you sure? [y/N]: ${NC})" -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${GREEN}✅ Clean cancelled${NC}"
return 0
fi
# Stop containers first
echo -e "${YELLOW}Stopping containers...${NC}"
docker stop rhesis-dev-postgres rhesis-dev-redis 2>/dev/null || true
# Remove containers
echo -e "${YELLOW}Removing containers...${NC}"
docker rm rhesis-dev-postgres rhesis-dev-redis 2>/dev/null || true
# Remove volumes
echo -e "${YELLOW}Removing volumes...${NC}"
docker volume rm rhesis-dev-postgres-data 2>/dev/null || true
docker volume rm rhesis-dev-redis-data 2>/dev/null || true
# Also try to remove any anonymous volumes that might have been created
# by finding volumes with rhesis-dev prefix
docker volume ls -q --filter "name=rhesis-dev" 2>/dev/null | xargs -r docker volume rm 2>/dev/null || true
echo ""
echo -e "${GREEN}✅ Development infrastructure cleaned${NC}"
echo ""
echo -e "${CYAN}To set up again:${NC}"
echo -e " ${GREEN}./rh dev up${NC} # Start fresh containers"
echo ""
}
# Function to show dev environment status
dev_status() {
echo -e "${CYAN}Rhesis Development Environment Status${NC}"
echo -e "${PURPLE}════════════════════════════════════════════════${NC}"
echo ""
# Check env files
echo -e "${YELLOW}Environment Files:${NC}"
if [ -f "$DEV_BACKEND_ENV" ]; then
if is_dev_generated "$DEV_BACKEND_ENV"; then
local created_at=$(head -n1 "$DEV_BACKEND_ENV" | sed 's/.*on //')
echo -e " ${GREEN}✅${NC} apps/backend/.env (initialized: $created_at)"
else
echo -e " ${GREEN}✅${NC} apps/backend/.env (manually created)"
fi
else
echo -e " ${RED}❌${NC} apps/backend/.env (missing)"
fi
if [ -f "$DEV_FRONTEND_ENV" ]; then
if is_dev_generated "$DEV_FRONTEND_ENV"; then
local created_at=$(head -n1 "$DEV_FRONTEND_ENV" | sed 's/.*on //')
echo -e " ${GREEN}✅${NC} apps/frontend/.env.local (initialized: $created_at)"
else
echo -e " ${GREEN}✅${NC} apps/frontend/.env.local (manually created)"
fi
else
echo -e " ${RED}❌${NC} apps/frontend/.env.local (missing)"
fi
echo ""
# Check containers
echo -e "${YELLOW}Infrastructure:${NC}"
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "rhesis-dev-postgres"; then
echo -e " PostgreSQL: ${GREEN}✅ Running${NC} (localhost:${DEV_POSTGRES_PORT})"
else
echo -e " PostgreSQL: ${RED}Stopped${NC}"
fi
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "rhesis-dev-redis"; then
echo -e " Redis: ${GREEN}✅ Running${NC} (localhost:${DEV_REDIS_PORT})"
else
echo -e " Redis: ${RED}Stopped${NC}"
fi
echo ""
# Show next steps based on status
if ! check_env_files_exist; then
echo -e "${YELLOW}Next step:${NC}"
echo -e " ${GREEN}./rh dev init${NC} # Initialize environment files"
elif ! docker ps --format '{{.Names}}' 2>/dev/null | grep -q "rhesis-dev-postgres"; then
echo -e "${YELLOW}Next step:${NC}"
echo -e " ${GREEN}./rh dev up${NC} # Start dev infrastructure"
else
echo -e "${YELLOW}Start services:${NC}"
echo -e " ${GREEN}./rh dev backend${NC} # Start backend"
echo -e " ${GREEN}./rh dev frontend${NC} # Start frontend"
echo -e " ${GREEN}./rh dev worker${NC} # Start worker (optional)"
fi
echo ""
}
# Docker Compose project name for isolation
# Using a unique project name to distinguish containers created by ./rh start
# from manually created containers. This prevents ./rh delete from removing
# containers created outside of the rh script.
PROJECT_NAME="rhesis-quickstart"
# Function to start all services with Docker Compose
start_all() {
echo -e "${GREEN}Starting all Rhesis services with Docker...${NC}"
echo -e "${BLUE}Using zero-configuration local setup${NC}"
echo ""
echo "#####################################################################################"
echo ""
echo -e "${CYAN}Telemetry Notice:${NC}"
echo -e "${WHITE} Rhesis AI automatically collects anonymous usage statistics to help improve the platform.${NC}"
echo -e "${WHITE} To disable telemetry, set: ${YELLOW}OTEL_RHESIS_TELEMETRY_ENABLED=false${NC}"
echo ""
echo "#####################################################################################"
echo ""
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
check_docker
# Check if .env.docker.local exists and has DB_ENCRYPTION_KEY
if [ ! -f ".env.docker.local" ] || ! grep -q "^DB_ENCRYPTION_KEY=.\+" .env.docker.local 2>/dev/null; then
echo -e "${YELLOW}🔑 Generating DB_ENCRYPTION_KEY...${NC}"
ENCRYPTION_KEY=$(generate_encryption_key)
if [ -z "$ENCRYPTION_KEY" ]; then
echo -e "${RED}❌ Error: Failed to generate encryption key${NC}"
exit 1
fi
# Create or update .env.docker.local
if [ ! -f ".env.docker.local" ]; then
# Create new file
echo "# Auto-generated local environment configuration" > .env.docker.local
echo "" >> .env.docker.local
echo "# Database Encryption" >> .env.docker.local
echo "DB_ENCRYPTION_KEY=$ENCRYPTION_KEY" >> .env.docker.local
echo "" >> .env.docker.local
echo "# Local Authentication Bypass" >> .env.docker.local
echo "QUICK_START=true" >> .env.docker.local
echo "NEXT_PUBLIC_QUICK_START=true" >> .env.docker.local
echo -e "${GREEN}✅ Created .env.docker.local with local configuration${NC}"
else
# Update existing file
if grep -q "^DB_ENCRYPTION_KEY=" .env.docker.local 2>/dev/null; then
# Replace existing line
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s|^DB_ENCRYPTION_KEY=.*|DB_ENCRYPTION_KEY=$ENCRYPTION_KEY|" .env.docker.local
else
sed -i "s|^DB_ENCRYPTION_KEY=.*|DB_ENCRYPTION_KEY=$ENCRYPTION_KEY|" .env.docker.local
fi
else
# Add new line
echo "" >> .env.docker.local
echo "DB_ENCRYPTION_KEY=$ENCRYPTION_KEY" >> .env.docker.local
fi
# Add QUICK_START if missing
if ! grep -q "^QUICK_START=" .env.docker.local 2>/dev/null; then
echo "QUICK_START=true" >> .env.docker.local
fi
# Add NEXT_PUBLIC_QUICK_START if missing
if ! grep -q "^NEXT_PUBLIC_QUICK_START=" .env.docker.local 2>/dev/null; then
echo "NEXT_PUBLIC_QUICK_START=true" >> .env.docker.local
fi
echo -e "${GREEN}✅ Local configuration updated in .env.docker.local${NC}"
fi
fi
echo ""
echo -e "${YELLOW}Starting services ...${NC}"
docker compose -p "$PROJECT_NAME" --env-file .env.docker.local up --build -d postgres redis backend worker frontend
if [ $? -eq 0 ]; then
echo ""
echo -e "${GREEN}✅ All services started successfully!${NC}"
echo ""
echo -e "${CYAN}Access the platform:${NC}"
echo -e " Frontend: ${WHITE}http://localhost:3000${NC} (auto-login enabled)"
echo -e " Backend: ${WHITE}http://localhost:8080/docs${NC}"
echo -e " Worker: ${WHITE}http://localhost:8081/health/basic${NC}"
echo ""
echo -e "${YELLOW}Useful commands:${NC}"
echo -e " View logs: ${GREEN}./rh logs${NC}"
echo -e " Stop all: ${GREEN}./rh stop${NC}"
echo -e " Restart all: ${GREEN}./rh restart${NC}"
echo ""
else
echo -e "${RED}❌ Error: Failed to start services${NC}"
exit 1
fi
}
# Function to stop all services
stop_all() {
echo -e "${YELLOW}Stopping all Rhesis services...${NC}"
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
docker compose -p "$PROJECT_NAME" down
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ All services stopped${NC}"
else
echo -e "${RED}❌ Error: Failed to stop services${NC}"
exit 1
fi
}
# Function to restart all services (recreates containers so .env.docker.local changes apply)
restart_all() {
echo -e "${YELLOW}Restarting all Rhesis services...${NC}"
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
check_docker
# Recreate containers so updated .env.docker.local variables are applied
docker compose -p "$PROJECT_NAME" --env-file .env.docker.local up -d --force-recreate postgres redis backend worker frontend
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ All services restarted with current .env.docker.local${NC}"
else
echo -e "${RED}❌ Error: Failed to restart services${NC}"
exit 1
fi
}
# Function to view logs
view_logs() {
echo -e "${BLUE}Viewing logs...${NC}"
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
if [ -z "$1" ]; then
# No service specified, show all logs
docker compose -p "$PROJECT_NAME" logs -f
else
# Show logs for specific service
docker compose -p "$PROJECT_NAME" logs -f "$1"
fi
}
# Function to delete all services, images, volumes, and data
delete_all() {
echo -e "${RED}⚠️ WARNING: This will delete Docker resources created by ./rh start!${NC}"
echo -e "${YELLOW}This includes:${NC}"
echo -e " - Containers managed by ./rh (project: ${PROJECT_NAME})"
echo -e " - Docker images built by this project"
echo -e " - Volumes created by this project (DATABASE DATA WILL BE LOST!)"
echo -e " - Networks created by this project"
echo -e " - .env.docker.local"
echo ""
echo -e "${BLUE}Note: Containers created manually will NOT be affected${NC}"
echo ""
echo -e "${RED}This action CANNOT be undone!${NC}"
echo ""
read -p "$(echo -e ${YELLOW}Are you sure you want to continue? Type \'yes\' to confirm: ${NC})" -r
echo ""
if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then
echo -e "${GREEN}✅ Deletion cancelled${NC}"
exit 0
fi
echo -e "${YELLOW}Deleting Docker resources for this project...${NC}"
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
# Use docker compose with explicit project name to ensure we only delete resources from THIS project
# This prevents accidentally deleting containers with similar names from other projects
# Stop and remove containers, networks, and volumes managed by this compose file
docker compose -p "$PROJECT_NAME" down -v --rmi all 2>/dev/null || {
# Fallback: If project name doesn't match, try without -p flag
echo -e "${YELLOW}⚠️ Project-specific deletion failed, trying default method...${NC}"
docker compose down -v --rmi all
}
rm -f .env.docker.local
if [ $? -eq 0 ]; then
echo ""
echo -e "${GREEN}✅ Docker resources deleted successfully!${NC}"
echo ""
echo -e "${CYAN}To start fresh:${NC}"
echo -e " ${GREEN}./rh start${NC}"
echo ""
else
echo -e "${RED}❌ Error: Failed to delete all resources${NC}"
exit 1
fi
}
# Function to start backend
start_backend() {
echo -e "${GREEN}Starting Rhesis Backend...${NC}"
cd "$SCRIPT_DIR/apps/backend" || {
echo -e "${RED}❌ Error: Backend directory not found${NC}"
exit 1
}
check_uv
echo -e "${BLUE}Installing backend packages (uv sync)...${NC}"
uv sync || {
echo -e "${RED}❌ Error: uv sync failed${NC}"
exit 1
}
source .venv/bin/activate || {
echo -e "${RED}❌ Error: Failed to activate virtual environment${NC}"
exit 1
}
echo -e "${GREEN}✅ Python environment activated${NC}"
if [ -f "start.sh" ]; then
./start.sh
else
echo -e "${RED}❌ Error: Backend start.sh not found${NC}"
exit 1
fi
}
# Function to start frontend
start_frontend() {
echo -e "${GREEN}Starting Rhesis Frontend...${NC}"
cd "$SCRIPT_DIR/apps/frontend" || {
echo -e "${RED}❌ Error: Frontend directory not found${NC}"
exit 1
}
if [ -f "start.sh" ]; then
./start.sh
else
echo -e "${RED}❌ Error: Frontend start.sh not found${NC}"
exit 1
fi
}
# Function to start chatbot
start_chatbot() {
echo -e "${GREEN}Starting Rhesis Chatbot...${NC}"
cd "$SCRIPT_DIR/apps/chatbot" || {
echo -e "${RED}❌ Error: Chatbot directory not found${NC}"
exit 1
}
echo -e "${GREEN}Starting Chatbot service...${NC}"
echo -e "${BLUE}Service will be available at: http://localhost:8000${NC}"
echo -e "${BLUE}API docs will be available at: http://localhost:8000/docs${NC}"
echo -e "${YELLOW}Press Ctrl+C to stop the service${NC}"
echo ""
# Run uvicorn with reload for development
exec uv run uvicorn client:app --host 0.0.0.0 --port 8000 --reload
}
# Function to start docs
start_docs() {
echo -e "${GREEN}Starting Rhesis Documentation...${NC}"
cd "$SCRIPT_DIR/docs/src" || {
echo -e "${RED}❌ Error: Documentation directory not found${NC}"
exit 1
}
if [ -f "start.sh" ]; then
./start.sh
else
echo -e "${RED}❌ Error: Docs start.sh not found${NC}"
exit 1
fi
}
# Function to start polyphemus
start_polyphemus() {
echo -e "${GREEN}Starting Rhesis Polyphemus...${NC}"
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
# Check if virtual environment exists
if [ ! -d ".venv" ]; then
echo -e "${YELLOW}Creating virtual environment...${NC}"
uv venv || {
echo -e "${RED}❌ Error: Failed to create virtual environment${NC}"
exit 1
}
fi
# Activate virtual environment
source .venv/bin/activate || {
echo -e "${RED}❌ Error: Failed to activate virtual environment${NC}"
exit 1
}
# Install SDK first with huggingface extras (required by polyphemus)
echo -e "${YELLOW}Installing SDK with HuggingFace support...${NC}"
uv pip install -e "sdk[huggingface]" || {
echo -e "${RED}❌ Error: Failed to install SDK${NC}"
exit 1
}
# Install Penelope
echo -e "${YELLOW}Installing Penelope...${NC}"
uv pip install -e penelope || {
echo -e "${RED}❌ Error: Failed to install Penelope${NC}"
exit 1
}
# Install Backend
echo -e "${YELLOW}Installing Backend...${NC}"
uv pip install -e apps/backend || {
echo -e "${RED}❌ Error: Failed to install Backend${NC}"
exit 1
}
# Install Polyphemus (all dependencies now resolved)
echo -e "${YELLOW}Installing Polyphemus...${NC}"
uv pip install -e apps/polyphemus || {
echo -e "${RED}❌ Error: Failed to install Polyphemus${NC}"
exit 1
}
echo -e "${GREEN}Starting Polyphemus service...${NC}"
echo -e "${BLUE}Service will be available at: http://localhost:8082${NC}"
echo -e "${BLUE}API docs will be available at: http://localhost:8082/docs${NC}"
echo -e "${YELLOW}Press Ctrl+C to stop the service${NC}"
echo ""
# Run uvicorn
exec uvicorn rhesis.polyphemus.main:app --host 0.0.0.0 --port 8082 --reload
}
# Function to start worker
start_worker() {
echo -e "${GREEN}Starting Rhesis Worker...${NC}"
# Kill any existing celery workers first
echo -e "${YELLOW}Checking for existing Celery workers...${NC}"
if pgrep -f celery > /dev/null; then
echo -e "${YELLOW}Stopping existing Celery workers...${NC}"
pkill -9 -f celery
sleep 1
echo -e "${GREEN}✅ Existing workers stopped${NC}"
else
echo -e "${BLUE}No existing workers found${NC}"
fi
# Clear Python cache
echo -e "${YELLOW}Clearing Python cache...${NC}"
cd "$SCRIPT_DIR" || {
echo -e "${RED}❌ Error: Script directory not found${NC}"
exit 1
}
find apps/backend -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null
find apps/backend -type f -name "*.pyc" -delete 2>/dev/null
echo -e "${GREEN}✅ Cache cleared${NC}"
cd "$SCRIPT_DIR/apps/backend" || {
echo -e "${RED}❌ Error: Backend directory not found${NC}"
exit 1
}
LOG_FILE="$SCRIPT_DIR/celery.log"
FLOWER_PORT="${FLOWER_PORT:-5555}"
WORKER_PROJECT="$SCRIPT_DIR/apps/worker"
echo -e "${GREEN}Starting new Celery worker...${NC}"
echo -e "${BLUE}Logs will be written to: ${LOG_FILE}${NC}"
echo -e "${YELLOW}Press Ctrl+C to stop the worker and Flower${NC}"
echo ""
# Flower is declared in apps/worker; backend cwd keeps .env loading consistent.
export PYTHONPATH="${SCRIPT_DIR}/apps/backend/src${PYTHONPATH:+:$PYTHONPATH}"
FLOWER_PID=""
WORKER_PID=""
cleanup_worker_stack() {
if [ -n "${WORKER_PID:-}" ] && kill -0 "$WORKER_PID" 2>/dev/null; then
echo -e "\n${YELLOW}Stopping Celery worker (PID ${WORKER_PID})...${NC}"
kill -TERM "$WORKER_PID" 2>/dev/null || true
wait "$WORKER_PID" 2>/dev/null || true
fi
if [ -n "${FLOWER_PID:-}" ] && kill -0 "$FLOWER_PID" 2>/dev/null; then
echo -e "${YELLOW}Stopping Flower (PID ${FLOWER_PID})...${NC}"
kill -TERM "$FLOWER_PID" 2>/dev/null || true
wait "$FLOWER_PID" 2>/dev/null || true
fi
}
trap 'cleanup_worker_stack; exit 130' INT
trap 'cleanup_worker_stack; exit 143' TERM
echo -e "${GREEN}Starting Flower (Celery monitor)...${NC}"
uv run --project "$WORKER_PROJECT" celery -A rhesis.backend.worker.app flower --port="$FLOWER_PORT" &
FLOWER_PID=$!
echo -e "${BLUE}Flower dashboard: http://127.0.0.1:${FLOWER_PORT}/${NC}"
echo -e "${BLUE}Override the port with: ${WHITE}FLOWER_PORT=<port> ./rh dev worker${NC}"
echo ""
# Set CELERY_WORKER_NAME environment variable (using hostname-UUID for uniqueness)
# Generate unique worker ID using hostname + UUID
# Format: worker@hostname-uuid (e.g., worker@server1-a1b2c3d4)
# UUID ensures no collisions even with rapid worker restarts
WORKER_UUID=$(python3 -c "import uuid; print(str(uuid.uuid4())[:8])")
export CELERY_WORKER_NAME="worker@$(hostname)-${WORKER_UUID}"
echo -e "${GREEN}✅ CELERY_WORKER_NAME set to: ${CELERY_WORKER_NAME}${NC}"
uv run celery -A rhesis.backend.worker.app worker \
--pool threads \
--loglevel=DEBUG \
--queues=celery,execution,telemetry,architect \
--concurrency=${CELERY_WORKER_CONCURRENCY:-2} \
--optimization=fair \
-E &
WORKER_PID=$!
wait "$WORKER_PID"
WORKER_RC=$?
cleanup_worker_stack
exit "$WORKER_RC"
}
# Function to run frontend tests
test_frontend() {
echo -e "${GREEN}Running Frontend Tests...${NC}"
cd "$SCRIPT_DIR/apps/frontend" || {
echo -e "${RED}❌ Error: Frontend directory not found${NC}"
exit 1
}
# Check if node_modules exists
if [ ! -d "node_modules" ]; then
echo -e "${YELLOW}Installing dependencies first...${NC}"
npm install || {
echo -e "${RED}❌ Error: Failed to install dependencies${NC}"
exit 1
}
fi
# Run tests
echo -e "${BLUE}Executing tests...${NC}"
npm test || {
echo -e "${RED}❌ Error: Tests failed${NC}"
exit 1
}
echo -e "${GREEN}✅ Frontend tests completed!${NC}"
}
# Parse command line arguments
case "$1" in
"start")
start_all
;;
"dev")
case "$2" in
"init")
dev_init
;;
"up")
dev_up
;;
"down")
dev_down
;;
"clean")
dev_clean
;;
"status")
dev_status
;;
"backend")
start_backend
;;
"frontend")
start_frontend