-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlaunch.sh
More file actions
executable file
·2294 lines (1962 loc) · 83.8 KB
/
launch.sh
File metadata and controls
executable file
·2294 lines (1962 loc) · 83.8 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'
BLUE='\033[0;34m'
PINK='\033[0;35m'
NC='\033[0m'
# Function for prefixed logging
log_with_service_name() {
local service_name=$1
local color=$2
while IFS= read -r line; do
echo -e "${color}[$service_name]${NC} $line"
done
}
# Function to detect architecture
get_architecture() {
arch=$(uname -m)
case $arch in
x86_64|amd64)
echo "amd64"
;;
aarch64|arm64)
echo "arm64"
;;
*)
echo "Unsupported architecture: $arch" >&2
exit 1
;;
esac
}
get_surrealdb_version() {
surreal -V | awk '{print $6}'
}
install_surrealdb() {
echo "Installing SurrealDB..." | log_with_service_name "SurrealDB" $GREEN
if [ "$os" = "Darwin" ]; then
SURREALDB_INSTALL_PATH="/Users/$(whoami)/.surrealdb"
else
SURREALDB_INSTALL_PATH="/home/$(whoami)/.surrealdb"
fi
mkdir -p "$SURREALDB_INSTALL_PATH"
SURREALDB_BINARY="$SURREALDB_INSTALL_PATH/surreal"
# Check if SurrealDB is already installed
if [ -f /usr/local/bin/surreal ]; then
CURRENT_VERSION=$(get_surrealdb_version)
if [[ $CURRENT_VERSION == 2.* ]]; then
echo "SurrealDB version 2 ($CURRENT_VERSION) is already installed." | log_with_service_name "SurrealDB" $GREEN
return
else
echo "Removing existing SurrealDB version $CURRENT_VERSION..." | log_with_service_name "SurrealDB" $YELLOW
sudo rm /usr/local/bin/surreal
fi
fi
echo "Installing SurrealDB version 2..." | log_with_service_name "SurrealDB" $GREEN
# Install SurrealDB
curl -sSf https://install.surrealdb.com > /tmp/surreal_install.sh
sh /tmp/surreal_install.sh -- "$SURREALDB_INSTALL_PATH"
if [ -f "$SURREALDB_BINARY" ]; then
echo "Moving SurrealDB binary to /usr/local/bin..." | log_with_service_name "SurrealDB" $GREEN
sudo mv "$SURREALDB_BINARY" /usr/local/bin/surreal
if [ $? -ne 0 ]; then
echo "Failed to move SurrealDB binary. Trying to copy instead..." | log_with_service_name "SurrealDB" $YELLOW
sudo cp "$SURREALDB_BINARY" /usr/local/bin/surreal
if [ $? -ne 0 ]; then
echo "Failed to copy SurrealDB binary. Installation failed." | log_with_service_name "SurrealDB" $RED
exit 1
fi
fi
echo "SurrealDB binary installed in /usr/local/bin." | log_with_service_name "SurrealDB" $GREEN
else
echo "SurrealDB binary not found at $SURREALDB_BINARY. Installation may have failed." | log_with_service_name "SurrealDB" $RED
exit 1
fi
# Verify installed version
if command -v surreal &> /dev/null; then
INSTALLED_VERSION=$(get_surrealdb_version)
if [[ $INSTALLED_VERSION == 2.* ]]; then
echo "SurrealDB version 2 ($INSTALLED_VERSION) installed successfully." | log_with_service_name "SurrealDB" $GREEN
else
echo "Failed to install SurrealDB version 2. Got version: $INSTALLED_VERSION" | log_with_service_name "SurrealDB" $RED
exit 1
fi
else
echo "SurrealDB command not found. Installation failed." | log_with_service_name "SurrealDB" $RED
exit 1
fi
}
# Function for manual installation of Ollama
linux_install_ollama() {
set -a
source .env
set +a
# Echo start Ollama
echo "Installing Ollama..." | log_with_service_name "Ollama" $RED
# Set up directory paths based on current location
CURRENT_DIR=$(pwd)
PROJECT_MODELS_DIR="$CURRENT_DIR/node/inference/ollama/models"
# Create the directory structure
sudo mkdir -p "$PROJECT_MODELS_DIR"
echo "Created models directory at: $PROJECT_MODELS_DIR" | log_with_service_name "Ollama" $RED
install_ollama_app() {
sudo curl -fsSL https://ollama.com/install.sh | sh
}
create_ollama_service() {
# Create systemd service file
cat > /tmp/ollama.service << EOF
[Unit]
Description=Ollama Service
After=network-online.target
[Service]
ExecStart=/bin/bash -c 'exec $(which ollama) serve'
Environment=OLLAMA_MODELS=/var/lib/ollama/models
User=ollama
Group=ollama
Restart=always
RestartSec=3
[Install]
WantedBy=default.target
EOF
sudo mv /tmp/ollama.service /etc/systemd/system/ollama.service
}
create_ollama_directories() {
# Create ollama user and group
sudo useradd -r -s /bin/false ollama 2>/dev/null || true
# Create and set permissions for ollama directories
sudo mkdir -p /usr/share/ollama
sudo mkdir -p /var/lib/ollama/models
sudo chown -R ollama:ollama /usr/share/ollama /var/lib/ollama
sudo chmod -R 755 /usr/share/ollama /var/lib/ollama
# Create project directory structure
sudo mkdir -p "$(dirname "$PROJECT_MODELS_DIR")"
# Remove existing directory or link if it exists
sudo rm -rf "$PROJECT_MODELS_DIR"
# Create symbolic link from project directory to system models
sudo ln -sf /var/lib/ollama/models "$PROJECT_MODELS_DIR"
# Set permissions for the project directory and link
sudo chown -R ollama:ollama "$(dirname "$PROJECT_MODELS_DIR")"
sudo chmod -R 755 "$(dirname "$PROJECT_MODELS_DIR")"
}
restart_ollama_linux() {
sudo systemctl daemon-reload
sudo systemctl enable ollama
sudo systemctl start ollama
}
# Get current version if Ollama exists
local current_version=""
current_version=$(ollama -v 2>/dev/null | grep -oP '(?:client version is |version is )(\K[\d.]+)')
# Get latest version from GitHub release
local latest_version=""
latest_version=$(curl -sf https://api.github.com/repos/ollama/ollama/releases/latest |
grep '"tag_name":' |
sed -E 's/.*"v([^"]+)".*/\1/')
if [ -z "$latest_version" ]; then
echo "Failed to get latest version from GitHub" | log_with_service_name "Ollama" $RED
exit 1
fi
echo "Current version: $current_version" | log_with_service_name "Ollama" $RED
echo "Latest version: $latest_version" | log_with_service_name "Ollama" $RED
# Case 1: Ollama not installed
if ! command -v ollama >/dev/null 2>&1; then
echo "Installing Ollama..." | log_with_service_name "Ollama" $RED
install_ollama_app
echo "Ollama installed successfully" | log_with_service_name "Ollama" $RED
# Case 2: Version needs update
elif [ -z "$current_version" ] || [ "$current_version" != "$latest_version" ]; then
echo "Updating Ollama from ${current_version:-unknown} to ${latest_version}" | log_with_service_name "Ollama" $RED
install_ollama_app
echo "Ollama updated successfully" | log_with_service_name "Ollama" $RED
# Case 3: Latest version already installed
else
echo "Latest version of Ollama (${current_version}) is already installed" | log_with_service_name "Ollama" $RED
fi
# Create and install systemd service file
create_ollama_service
# Create necessary directories and set permissions
create_ollama_directories
# Restart the service
restart_ollama_linux
# Pull Ollama models
echo "Pulling Ollama models: $OLLAMA_MODELS" | log_with_service_name "Ollama" $RED
IFS=',' read -ra MODELS <<< "$OLLAMA_MODELS"
for model in "${MODELS[@]}"; do
echo "Pulling model: $model" | log_with_service_name "Ollama" $RED
ollama pull "$model"
done
}
pull_ollama_models() {
# Pull Ollama models
echo "Pulling Ollama models: $OLLAMA_MODELS" | log_with_service_name "Ollama" $RED
IFS=',' read -ra MODELS <<< "$OLLAMA_MODELS"
for model in "${MODELS[@]}"; do
echo "Pulling model: $model" | log_with_service_name "Ollama" $RED
ollama pull "$model"
done
}
darwin_install_ollama() {
echo "Installing Ollama..." | log_with_service_name "Ollama" $RED
# Get current version if Ollama.app exists
local current_version=""
if [ -d "/Applications/Ollama.app" ]; then
current_version=$(defaults read /Applications/Ollama.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "")
fi
# Get latest version from GitHub release
# local latest_version=""
# TODO (@enricorotundo): remove the hardcoded version
local latest_version="0.5.11"
# latest_version=$(curl -sf https://api.github.com/repos/ollama/ollama/releases/latest |
# grep '"tag_name":' |
# sed -E 's/.*"v([^"]+)".*/\1/')
if [ -z "$latest_version" ]; then
echo "Failed to get latest version from GitHub" | log_with_service_name "Ollama" $RED
exit 1
fi
echo "Current version: $current_version" | log_with_service_name "Ollama" $RED
echo "Latest version: $latest_version" | log_with_service_name "Ollama" $RED
install_ollama_app() {
local temp_dir=$(mktemp -d)
curl -L https://github.com/ollama/ollama/releases/download/v${latest_version}/Ollama-darwin.zip -o "$temp_dir/ollama.zip"
unzip -o "$temp_dir/ollama.zip" -d "$temp_dir"
sudo rm -rf "/Applications/Ollama.app"
sudo mv "$temp_dir/Ollama.app" "/Applications/"
rm -rf "$temp_dir"
}
# Case 1: Ollama not installed
if [ ! -d "/Applications/Ollama.app" ]; then
echo "Ollama is not installed. Installing..." | log_with_service_name "Ollama" $RED
install_ollama_app
echo "Ollama installed successfully" | log_with_service_name "Ollama" $RED
# Case 2: Version needs update
elif [ -z "$current_version" ] || [ "$current_version" != "$latest_version" ]; then
echo "Updating Ollama from ${current_version:-unknown} to ${latest_version}" | log_with_service_name "Ollama" $RED
pkill -f "Ollama" || true
install_ollama_app
echo "Ollama updated successfully" | log_with_service_name "Ollama" $RED
# Case 3: Latest version already installed
else
echo "Latest version of Ollama (${current_version}) is already installed" | log_with_service_name "Ollama" $RED
fi
# Start Ollama.app
echo "Starting Ollama..." | log_with_service_name "Ollama" $RED
open -a /Applications/Ollama.app
sleep 2
# Verify Ollama is running
if ! pgrep -f "Ollama" >/dev/null; then
echo "Failed to start Ollama" | log_with_service_name "Ollama" $RED
exit 1
fi
pull_ollama_models
echo "Ollama is now running" | log_with_service_name "Ollama" $RED
}
# Function to install Miniforge
linux_install_miniforge() {
echo "Checking for Miniforge installation..." | log_with_service_name "Miniforge" $BLUE
# Check if Miniforge is already installed
if [ -d "$HOME/miniforge3" ]; then
echo "Miniforge is already installed." | log_with_service_name "Miniforge" $BLUE
else
# Installation process
echo "Installing Miniforge..." | log_with_service_name "Miniforge" $BLUE
arch=$(get_architecture)
if [ "$arch" = "amd64" ]; then
MINIFORGE_INSTALLER="Miniforge3-Linux-x86_64.sh"
else
MINIFORGE_INSTALLER="Miniforge3-Linux-aarch64.sh"
fi
curl -sSL "https://github.com/conda-forge/miniforge/releases/latest/download/$MINIFORGE_INSTALLER" -o $MINIFORGE_INSTALLER
chmod +x $MINIFORGE_INSTALLER
./$MINIFORGE_INSTALLER -b
rm $MINIFORGE_INSTALLER
"$HOME/miniforge3/bin/conda" init bash
fi
# Ensure Miniforge's bin directory is in PATH in .bashrc
if ! grep -q 'miniforge3/bin' ~/.bashrc; then
echo 'export PATH="$HOME/miniforge3/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
echo "Miniforge path added to .bashrc" | log_with_service_name "Miniforge" $BLUE
fi
# Activate Miniforge environment
eval "$($HOME/miniforge3/bin/conda shell.bash hook)"
conda activate
# Check which Python is in use
PYTHON_PATH=$(which python)
if [[ $PYTHON_PATH == *"$HOME/miniforge3"* ]]; then
echo "Miniforge Python is in use." | log_with_service_name "Miniforge" $BLUE
else
echo "Miniforge Python is not in use. Activating..." | log_with_service_name "Miniforge" $BLUE
# Assuming there is a default environment to activate or using base
conda activate base
# Check which Python is in use again
PYTHON_PATH=$(which python)
echo "Python path: $PYTHON_PATH" | log_with_service_name "Miniforge" $BLUE
fi
}
darwin_install_miniforge() {
echo "Checking for Miniforge installation..." | log_with_service_name "Miniforge" $BLUE
# Check if Miniforge is already installed
if [ -d "$HOME/miniforge3" ]; then
echo "Miniforge is already installed." | log_with_service_name "Miniforge" $BLUE
else
# Installation process
echo "Installing Miniforge..." | log_with_service_name "Miniforge" $BLUE
arch=$(get_architecture)
if [ "$arch" = "amd64" ]; then
MINIFORGE_INSTALLER="Miniforge3-Darwin-x86_64.sh"
else
MINIFORGE_INSTALLER="Miniforge3-Darwin-$arch.sh"
fi
curl -sSL "https://github.com/conda-forge/miniforge/releases/latest/download/$MINIFORGE_INSTALLER" -o $MINIFORGE_INSTALLER
chmod +x $MINIFORGE_INSTALLER
./$MINIFORGE_INSTALLER -b
rm $MINIFORGE_INSTALLER
"$HOME/miniforge3/bin/conda" init zsh
fi
# Ensure Miniforge's bin directory is in PATH in .zshrc
if ! grep -q 'miniforge3/bin' ~/.zshrc; then
echo 'export PATH="$HOME/miniforge3/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
echo "Miniforge path added to .zshrc" | log_with_service_name "Miniforge" $BLUE
fi
# Activate Miniforge environment
eval "$($HOME/miniforge3/bin/conda shell.zsh hook)"
conda activate
# Check which Python is in use
PYTHON_PATH=$(which python)
if [[ $PYTHON_PATH == *"$HOME/miniforge3"* ]]; then
echo "Miniforge Python is in use." | log_with_service_name "Miniforge" $BLUE
else
echo "Miniforge Python is not in use. Activating..." | log_with_service_name "Miniforge" $BLUE
# Assuming there is a default environment to activate or using base
conda activate base
# Check which Python is in use again
PYTHON_PATH=$(which python)
echo "Python path: $PYTHON_PATH" | log_with_service_name "Miniforge" $BLUE
fi
}
# Fuction to clean the node
linux_clean_node() {
# Eccho start cleaning
echo "Cleaning node..." | log_with_service_name "Node" $BLUE
# Remove node/agents if it exists
if [ -d "node/agents" ]; then
rm -rf node/agents
fi
if ! dpkg -l | grep -q "make"; then
echo "Make not found. Installing Make..."
sudo apt-get update
sudo apt-get install -y make
else
echo "Make is already installed. Skipping installation."
fi
make pyproject-clean
}
darwin_clean_node() {
# Echo start cleaning
echo "Cleaning node..." | log_with_service_name "Node" $BLUE
# Remove node_modules if it exists
if [ -d "node_modules" ]; then
rm -rf node_modules
echo "Removed node_modules directory." | log_with_service_name "Node" $BLUE
fi
# Check if Homebrew is installed
if ! command -v brew >/dev/null 2>&1; then
echo "Homebrew not found. Installing Homebrew..." | log_with_service_name "Homebrew" $NC
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Install make using Homebrew
if ! command -v make >/dev/null 2>&1; then
echo "Installing make..." | log_with_service_name "Make" $NC
brew install make
else
echo "Make is already installed." | log_with_service_name "Make" $NC
fi
# Run make pyproject-clean
if [ -f "Makefile" ]; then
echo "Running make pyproject-clean..." | log_with_service_name "Node" $BLUE
make pyproject-clean
else
echo "Makefile not found. Skipping make pyproject-clean." | log_with_service_name "Node" $BLUE
fi
}
# Function to check if Docker is running
check_docker() {
# Echo start checking Docker
echo "Checking Docker..." | log_with_service_name "Docker" $RED
if ! docker info >/dev/null 2>&1; then
echo "Docker does not seem to be running, please start Docker first." | log_with_service_name "Docker" $RED
exit 1
fi
}
linux_install_docker() {
echo "Checking for Docker installation..." | log_with_service_name "Docker" $RED
set -a
source .env
set +a
if [ "$DOCKER_JOBS" = "True" ]; then
echo "Docker jobs are enabled." | log_with_service_name "Docker" $RED
else
echo "Docker jobs are disabled." | log_with_service_name "Docker" $RED
return
fi
if docker >/dev/null 2>&1; then
echo "Docker is already installed." | log_with_service_name "Docker" $RED
else
echo "Installing Docker." | log_with_service_name "Docker" $RED
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo chmod 666 /var/run/docker.sock
echo "Docker installed." | log_with_service_name "Docker" $RED
echo "Starting Docker." | log_with_service_name "Docker" $RED
fi
echo "Checking if Docker is running..." | log_with_service_name "Docker" $RED
if docker info >/dev/null 2>&1; then
echo "Docker is already running." | log_with_service_name "Docker" $RED
else
echo "Starting Docker." | log_with_service_name "Docker" $RED
sudo systemctl enable docker.service
sudo systemctl start docker
fi
if ! sudo docker info >/dev/null 2>&1; then
echo "Docker still does not seem to be running. Exiting..." | log_with_service_name "Docker" $RED
exit 1
fi
echo "Docker is running." | log_with_service_name "Docker" $RED
}
darwin_install_docker() {
echo "Checking for Docker installation..." | log_with_service_name "Docker" $RED
set -a
source .env
set +a
if [ "$DOCKER_JOBS" = "True" ]; then
echo "Docker jobs are enabled." | log_with_service_name "Docker" $RED
else
echo "DOCKER_JOBS: $DOCKER_JOBS" | log_with_service_name "Docker" $RED
echo "Docker jobs are disabled." | log_with_service_name "Docker" $RED
return
fi
if docker --version >/dev/null 2>&1; then
echo "Docker is already installed." | log_with_service_name "Docker" $RED
# Try to start Docker even if it's installed
echo "Starting Docker..." | log_with_service_name "Docker" $RED
open -a Docker
else
echo "Installing Docker..." | log_with_service_name "Docker" $RED
if ! command -v brew >/dev/null 2>&1; then
echo "Homebrew not found. Installing Homebrew..." | log_with_service_name "Homebrew" $NC
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
brew install --cask docker
echo "Docker installed." | log_with_service_name "Docker" $RED
echo "Starting Docker..." | log_with_service_name "Docker" $RED
open -a Docker
fi
# Wait for Docker to start with timeout
echo "Waiting for Docker to start..." | log_with_service_name "Docker" $RED
timeout=60
start_time=$(date +%s)
while ! docker info >/dev/null 2>&1; do
current_time=$(date +%s)
elapsed=$((current_time - start_time))
if [ $elapsed -gt $timeout ]; then
echo "Timeout after ${timeout} seconds waiting for Docker" | log_with_service_name "Docker" $RED
return 1
fi
echo "Still waiting... (${elapsed}s)" | log_with_service_name "Docker" $RED
sleep 5
done
echo "Docker is running." | log_with_service_name "Docker" $RED
return 0
}
# Function to start RabbitMQ on Linux
linux_start_rabbitmq() {
echo "Starting RabbitMQ on Linux..." | log_with_service_name "RabbitMQ" $PINK
# Load the .env file
set -a
source .env
set +a
# Check if RabbitMQ is already installed
if ! dpkg -l | grep -q "rabbitmq-server"; then
echo "RabbitMQ not found. Installing RabbitMQ..."
sudo apt-get update
sudo apt-get install -y rabbitmq-server
else
echo "RabbitMQ is already installed. Skipping installation."
fi
# Enable the management plugin
sudo rabbitmq-plugins enable rabbitmq_management
# Start RabbitMQ
sudo systemctl enable rabbitmq-server
sudo systemctl start rabbitmq-server
# Check if the user already exists
if sudo rabbitmqctl list_users | grep -q "^$RMQ_USER"; then
echo "User '$RMQ_USER' already exists. Skipping user creation."
else
# Create a new user with the credentials from .env
sudo rabbitmqctl add_user "$RMQ_USER" "$RMQ_PASSWORD"
sudo rabbitmqctl set_user_tags "$RMQ_USER" administrator
sudo rabbitmqctl set_permissions -p / "$RMQ_USER" ".*" ".*" ".*"
fi
echo "RabbitMQ started with management console on default port." | log_with_service_name "RabbitMQ" $PINK
}
# Function to start RabbitMQ on macOS
darwin_start_rabbitmq() {
echo "Starting RabbitMQ on macOS..." | log_with_service_name "RabbitMQ" $PINK
# Load the .env file
set -a
source .env
set +a
# Install RabbitMQ using Homebrew
if ! brew list rabbitmq &>/dev/null; then
echo "RabbitMQ not found. Installing RabbitMQ..."
brew install rabbitmq
else
echo "RabbitMQ is already installed. Skipping installation."
fi
# Enable the management plugin
rabbitmq-plugins enable rabbitmq_management
# Start RabbitMQ
brew services start rabbitmq
# Check if the user already exists
if rabbitmqctl list_users | grep -q "^$RMQ_USER"; then
echo "User '$RMQ_USER' already exists. Skipping user creation."
else
# Create a new user with the credentials from .env
rabbitmqctl add_user "$RMQ_USER" "$RMQ_PASSWORD"
rabbitmqctl set_user_tags "$RMQ_USER" administrator
rabbitmqctl set_permissions -p / "$RMQ_USER" ".*" ".*" ".*"
fi
echo "RabbitMQ started with management console on default port." | log_with_service_name "RabbitMQ" $PINK
}
setup_uv() {
echo "Setting up uv..." | log_with_service_name "uv" "$BLUE"
# Ensure the script is running within a Conda environment
if [ -z "$CONDA_DEFAULT_ENV" ]; then
echo "Not inside a Conda environment. Activating Miniforge..." | log_with_service_name "uv" "$BLUE"
source "$HOME/miniforge3/bin/activate"
conda activate
fi
# Install uv if it's not already installed
if ! command -v uv >/dev/null 2>&1; then
echo "Installing uv..." | log_with_service_name "uv" "$BLUE"
curl -LsSf https://astral.sh/uv/install.sh | sh
else
echo "uv is already installed." | log_with_service_name "uv" "$BLUE"
fi
os="$(uname)"
if [ "$os" = "Darwin" ]; then
export PATH="$HOME/.cargo/bin:$PATH"
else
export PATH="$HOME/.cargo/bin:$PATH"
fi
# Create a virtual environment in the project directory
echo "Creating virtual environment with uv..." | log_with_service_name "uv" "$BLUE"
uv venv .venv
# Install pip in the virtual environment
echo "Installing pip in the virtual environment..." | log_with_service_name "uv" "$BLUE"
source .venv/bin/activate
curl -sSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
rm get-pip.py
# Install dependencies
echo "Installing dependencies with uv..." | log_with_service_name "uv" "$BLUE"
# Set up PostgreSQL environment variables for building psycopg
if [ "$os" = "Darwin" ]; then
export PATH="/opt/homebrew/opt/postgresql@17/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/postgresql@17/lib"
export CPPFLAGS="-I/opt/homebrew/opt/postgresql@17/include"
else
export PATH="/usr/lib/postgresql/16/bin:$PATH"
export LDFLAGS="-L/usr/lib/postgresql/16/lib"
export CPPFLAGS="-I/usr/lib/postgresql/16/include"
fi
# Install dependencies from pyproject.toml
uv pip install .
# Install surrealdb version 0.3.2 specifically
echo "Installing surrealdb==0.3.2..." | log_with_service_name "uv" "$BLUE"
uv pip install surrealdb==0.3.2
# Verify the presence of a .venv folder within the project directory
if [ -d ".venv" ]; then
echo ".venv folder is present in the project folder." | log_with_service_name "uv" "$BLUE"
else
echo "uv virtual environment setup failed." | log_with_service_name "uv" "$BLUE"
exit 1
fi
}
# Function to install Python 3.12 on Linux
install_python312() {
echo "Checking for Python 3.12 installation..." | log_with_service_name "Python" $NC
if python3.12 --version >/dev/null 2>&1; then
echo "Python 3.12 is already installed." | log_with_service_name "Python" $NC
return
fi
os="$(uname)"
if [ "$os" = "Darwin" ]; then
echo "Installing Python 3.12..." | log_with_service_name "Python" $NC
brew install python@3.12
python3.12 --version
else
echo "Installing Python 3.12..." | log_with_service_name "Python" $NC
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install -y python3.12
python3.12 --version
fi
echo "Python 3.12 is installed." | log_with_service_name "Python" $NC
}
# Function to start the Hub SurrealDB
start_hub_surrealdb() {
PWD=$(pwd)
echo "LOCAL_HUB: $LOCAL_HUB" | log_with_service_name "Config"
if [ "$LOCAL_HUB" == "true" ]; then
echo "Running Hub DB (SurrealDB) locally..." | log_with_service_name "HubDBSurreal" $RED
INIT_PYTHON_PATH="$PWD/node/storage/hub/init_hub.py"
chmod +x "$INIT_PYTHON_PATH"
uv run python "$INIT_PYTHON_PATH" 2>&1
PYTHON_EXIT_STATUS=$?
if [ $PYTHON_EXIT_STATUS -ne 0 ]; then
echo "Hub DB (SurrealDB) initialization failed. Python script exited with status $PYTHON_EXIT_STATUS." | log_with_service_name "HubDBSurreal" $RED
exit 1
fi
# Check if Hub DB (SurrealDB) is running
if curl -s http://localhost:$HUB_DB_SURREAL_PORT/health > /dev/null; then
echo "Hub DB (SurrealDB) is running successfully." | log_with_service_name "HubDBSurreal" $RED
else
echo "Hub DB failed to start. Please check the logs." | log_with_service_name "HubDBSurreal" $RED
exit 1
fi
else
echo "Not running Hub DB (SurrealDB) locally..." | log_with_service_name "HubDBSurreal" $RED
fi
uv run python "$PWD/node/storage/hub/init_hub.py" --user 2>&1
PYTHON_EXIT_STATUS=$?
echo "PYTHON_EXIT_STATUS: $PYTHON_EXIT_STATUS"
if [ $PYTHON_EXIT_STATUS -ne 0 ]; then
echo "Hub DB (SurrealDB) sign in flow failed. Python script exited with status $PYTHON_EXIT_STATUS." | log_with_service_name "HubDBSurreal" $RED
exit 1
fi
}
start_local_surrealdb() {
PWD=$(pwd)
echo "Running Local DB..." | log_with_service_name "LocalDBPostgres" $RED
INIT_PYTHON_PATH="$PWD/node/storage/db/init_db.py"
chmod +x "$INIT_PYTHON_PATH"
uv run python "$INIT_PYTHON_PATH" 2>&1
PYTHON_EXIT_STATUS=$?
if [ $PYTHON_EXIT_STATUS -ne 0 ]; then
echo "Local DB initialization failed. Python script exited with status $PYTHON_EXIT_STATUS." | log_with_service_name "LocalDBPostgres" $RED
exit 1
fi
# Check if Local DB is running
if curl -s http://localhost:$SURREALDB_PORT/health > /dev/null; then
echo "Local DB is running successfully." | log_with_service_name "LocalDBPostgres" $RED
else
echo "Local DB failed to start. Please check the logs." | log_with_service_name "LocalDBPostgres" $RED
exit 1
fi
}
# Function to check and copy the .env file
check_and_copy_env() {
if [ ! -f .env ]; then
cp .env.example .env
echo ".env file created from .env.example"
else
echo ".env file already exists."
fi
}
# Function to check and set the private key
check_and_set_private_key() {
check_and_set_hub_credentials
os="$(uname)"
if [[ -f .env ]]; then
if [ "$os" = "Darwin" ]; then
key_file_value=$(grep '^PRIVATE_KEY=' .env | awk -F'=' '{print $2}')
else
key_file_value=$(grep -oP '(?<=^PRIVATE_KEY=).*' .env)
fi
if [[ -n "$key_file_value" && -f "$key_file_value" ]]; then
echo "PRIVATE_KEY already set and file exists."
return
fi
else
touch .env
fi
read -p "No valid PRIVATE_KEY set. Would you like to generate one? (yes/no): " response
if [[ "$response" == "yes" ]]; then
source .env
key_file=$(uv run python scripts/generate_user.py "$HUB_USERNAME")
key_file=$(echo "$key_file" | tr -d '\n')
key_file=$(basename "$key_file")
if [ "$os" = "Darwin" ]; then
sed -i '' "s/^PRIVATE_KEY=.*/PRIVATE_KEY=$key_file/" .env
else
sed -i "/^PRIVATE_KEY=/c\PRIVATE_KEY=$key_file" .env
fi
echo "Key pair generated and saved to PEM file. PRIVATE_KEY set in .env."
else
echo "Key pair generation aborted."
fi
}
load_env_file() {
CURRENT_DIR=$(pwd)
ENV_FILE="$CURRENT_DIR/.env"
# Debug: Check if .env file exists and permissions
if [ -f "$ENV_FILE" ]; then
echo ".env file found." | log_with_service_name "Config"
# Load .env file
set -a
. "$ENV_FILE"
set +a
. .venv/bin/activate
else
echo ".env file does not exist in $CURRENT_DIR." | log_with_service_name "Config"
exit 1
fi
}
linux_start_servers() {
# Echo start Servers
echo "Starting Servers..." | log_with_service_name "Server" $BLUE
# Get the config from the .env file
node_communication_protocol=${NODE_COMMUNICATION_PROTOCOL:-"ws"} # Default to ws if not set
num_node_communication_servers=${NUM_NODE_COMMUNICATION_SERVERS:-1} # Default to 1 if not set
start_port=${NODE_COMMUNICATION_PORT:-7002} # Starting port for alternate servers
echo "Node communication protocol: $node_communication_protocol" | log_with_service_name "Server" $BLUE
echo "Number of node communication servers: $num_node_communication_servers" | log_with_service_name "Server" $BLUE
echo "Node communication servers start from port: $start_port" | log_with_service_name "Server" $BLUE
# Define paths
USER_NAME=$(whoami)
CURRENT_DIR=$(pwd)
PYTHON_APP_PATH="$CURRENT_DIR/.venv/bin/python"
WORKING_DIR="$CURRENT_DIR/node"
ENVIRONMENT_FILE_PATH="$CURRENT_DIR/.env"
# First create HTTP server service (always port 7001)
HTTP_SERVICE_FILE="nodeapp_http.service"
echo "Starting HTTP server on port 7001..." | log_with_service_name "Server" $BLUE
# Create the systemd service file for HTTP server
cat <<EOF > /tmp/$HTTP_SERVICE_FILE
[Unit]
Description=Node HTTP Server
After=network.target
[Service]
ExecStart=$PYTHON_APP_PATH $WORKING_DIR/server/server.py --communication-protocol http --port 7001
WorkingDirectory=$CURRENT_DIR
EnvironmentFile=$ENVIRONMENT_FILE_PATH
User=$USER_NAME
Restart=always
TimeoutStopSec=90
KillMode=mixed
KillSignal=SIGTERM
SendSIGKILL=yes
# Environment variables for shutdown behavior
Environment=UVICORN_TIMEOUT=30
Environment=UVICORN_GRACEFUL_SHUTDOWN=30
Environment=PATH=$HOME/.local/bin:$HOME/.cargo/bin:${PATH}
[Install]
WantedBy=multi-user.target
EOF
# Move the HTTP service file and start it
sudo mv /tmp/$HTTP_SERVICE_FILE /etc/systemd/system/
sudo systemctl daemon-reload
if sudo systemctl enable $HTTP_SERVICE_FILE && sudo systemctl start $HTTP_SERVICE_FILE; then
echo "HTTP server service started successfully on port 7001." | log_with_service_name "Server" $BLUE
else
echo "Failed to start HTTP server service." | log_with_service_name "Server" $RED
return 1
fi
# Track all ports for .env file
ports="7001"
# Create services for additional servers
for ((i=0; i<num_node_communication_servers; i++)); do
current_port=$((start_port + i))
SERVICE_FILE="nodeapp_${node_communication_protocol}_${current_port}.service"
ports="${ports},${current_port}"
echo "Starting $node_communication_protocol node communication server on port $current_port..." | log_with_service_name "Server" $BLUE
# Create the systemd service file
cat <<EOF > /tmp/$SERVICE_FILE
[Unit]
Description=Node $node_communication_protocol Node Communication Server on port $current_port
After=network.target nodeapp_http.service
[Service]
ExecStart=$PYTHON_APP_PATH $WORKING_DIR/server/server.py --communication-protocol $node_communication_protocol --port $current_port
WorkingDirectory=$CURRENT_DIR
EnvironmentFile=$ENVIRONMENT_FILE_PATH
User=$USER_NAME
Restart=always
TimeoutStopSec=3
KillMode=mixed
KillSignal=SIGTERM
SendSIGKILL=yes
Environment=PATH=$HOME/.local/bin:$HOME/.cargo/bin:${PATH}
[Install]
WantedBy=multi-user.target
EOF
# Move the service file and start it
sudo mv /tmp/$SERVICE_FILE /etc/systemd/system/
sudo systemctl daemon-reload
if sudo systemctl enable $SERVICE_FILE && sudo systemctl start $SERVICE_FILE; then
echo "$node_communication_protocol server service started successfully on port $current_port." | log_with_service_name "Server" $BLUE
else
echo "Failed to start $node_communication_protocol server service on port $current_port." | log_with_service_name "Server" $RED
fi
done
NODE_COMMUNICATION_PORTS=$ports
}
darwin_start_servers() {
# Echo start Node
echo "Starting Servers..." | log_with_service_name "Server" $BLUE
# Get the config from the .env file
node_communication_protocol=${NODE_COMMUNICATION_PROTOCOL:-"ws"} # Default to ws if not set
num_node_communication_servers=${NUM_NODE_COMMUNICATION_SERVERS:-1} # Default to 1 if not set
start_port=${NODE_COMMUNICATION_PORT:-7002} # Starting port for alternate servers
echo "Node communication protocol: $node_communication_protocol" | log_with_service_name "Server" $BLUE
echo "Number of node communication servers: $num_node_communication_servers" | log_with_service_name "Server" $BLUE
echo "Node communication servers start from port: $start_port" | log_with_service_name "Server" $BLUE
# Define paths
USER_NAME=$(whoami)
CURRENT_DIR=$(pwd)
PYTHON_APP_PATH="$CURRENT_DIR/.venv/bin/python" # Changed to use Python directly
WORKING_DIR="$CURRENT_DIR/node"
ENVIRONMENT_FILE_PATH="$CURRENT_DIR/.env"
SURRREALDB_PATH="/usr/local/bin"
# First create HTTP server plist (always port 7001)
HTTP_PLIST_FILE="com.example.nodeapp.http.plist"
echo "Starting HTTP server on port 7001..." | log_with_service_name "Server" $BLUE
PLIST_PATH=~/Library/LaunchAgents/$HTTP_PLIST_FILE