forked from sentient-agi/ROMA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·2126 lines (1798 loc) · 69.6 KB
/
setup.sh
File metadata and controls
executable file
·2126 lines (1798 loc) · 69.6 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
# SentientResearchAgent Unified Setup Script
# Supports both Docker and Native installation (macOS and Ubuntu/Debian)
set -e # Exit on error
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Helper functions
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# OS detection helpers
OS_FAMILY="unknown"
detect_os() {
uname_s="$(uname -s 2>/dev/null || echo "")"
case "$uname_s" in
Darwin)
OS_FAMILY="macos"
;;
Linux)
if [[ -f /etc/debian_version ]]; then
OS_FAMILY="debian"
else
OS_FAMILY="linux"
fi
;;
*)
OS_FAMILY="unknown"
;;
esac
}
open_url() {
local url="$1"
detect_os
case "$OS_FAMILY" in
macos)
command -v open >/dev/null 2>&1 && open "$url" || true
;;
debian|linux)
command -v xdg-open >/dev/null 2>&1 && xdg-open "$url" >/dev/null 2>&1 || true
;;
*)
;;
esac
}
get_shell_profile() {
# Prefer zsh on macOS, otherwise bash
if [ -n "$ZSH_VERSION" ] || [ "$(basename "$SHELL")" = "zsh" ]; then
echo "$HOME/.zshrc"
else
echo "$HOME/.bashrc"
fi
}
append_to_profile_once() {
local line="$1"
local profile
profile="$(get_shell_profile)"
touch "$profile"
if ! grep -Fqx "$line" "$profile" 2>/dev/null; then
echo "$line" >> "$profile"
fi
}
# ASCII Banner
show_banner() {
cat << "EOF"
____ _ _ _
/ ___| ___ _ __ | |_(_) ___ _ __ | |_
\___ \ / _ \ '_ \| __| |/ _ \ '_ \| __|
___) | __/ | | | |_| | __/ | | | |_
|____/ \___|_| |_|\__|_|\___|_| |_|\__|
Research Agent - Setup
EOF
echo ""
}
# ============================================
# PATH VALIDATION AND SECURITY FUNCTIONS
# ============================================
validate_mount_path() {
local path="$1"
local path_description="$2"
if [ -z "$path" ]; then
print_error "Mount path cannot be empty"
return 1
fi
# Must be absolute path
if [[ "$path" != /* ]]; then
print_error "$path_description must be an absolute path (start with /)"
return 1
fi
# No parent directory traversal
if [[ "$path" == *"../"* || "$path" == *"/.."* || "$path" == *"/../"* ]]; then
print_error "$path_description contains dangerous parent directory references"
return 1
fi
# No dangerous system paths
case "$path" in
"/" | "/boot" | "/boot/"* | "/etc" | "/etc/"* | "/sys" | "/sys/"* | "/proc" | "/proc/"*)
print_error "$path_description points to protected system directory: $path"
return 1
;;
"/usr" | "/usr/"* | "/lib" | "/lib/"* | "/sbin" | "/sbin/"*)
print_error "$path_description points to system directory: $path"
return 1
;;
esac
# Check for potentially dangerous characters
if [[ "$path" =~ [[:space:]$\`\|\&\;\(\)\<\>] ]]; then
print_error "$path_description contains potentially dangerous characters"
return 1
fi
# Path length sanity check
if [ ${#path} -gt 255 ]; then
print_error "$path_description is too long (max 255 characters)"
return 1
fi
# Must not be just /data or other too-generic paths that could conflict
case "$path" in
"/data" | "/tmp" | "/var" | "/opt")
print_warning "$path_description is a very generic path that may conflict with system use"
read -p "Are you sure you want to use '$path'? (y/n): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
return 1
fi
;;
esac
return 0
}
create_mount_directory() {
local mount_dir="$1"
print_info "Creating mount directory: $mount_dir"
# Check if directory already exists
if [ -d "$mount_dir" ]; then
print_info "Mount directory already exists: $mount_dir"
# Check if current user can write to it
if [ -w "$mount_dir" ]; then
print_success "Mount directory is writable by current user"
return 0
else
print_warning "Mount directory exists but is not writable by current user"
print_info "Attempting to fix permissions..."
fi
fi
# Create directory with proper permissions
if [[ "$mount_dir" == /opt/* ]]; then
# For /opt paths, create with sudo and set proper ownership
print_info "Creating /opt directory requires elevated permissions"
if sudo mkdir -p "$mount_dir"; then
print_success "Mount directory created: $mount_dir"
# Set ownership to current user
if sudo chown "$USER:$(id -gn)" "$mount_dir"; then
print_success "Mount directory ownership set to current user"
else
print_warning "Could not set ownership, mount may not be writable"
fi
# Set appropriate permissions (755 - owner can read/write/execute, group and others can read/execute)
if sudo chmod 755 "$mount_dir"; then
print_success "Mount directory permissions set correctly"
else
print_warning "Could not set permissions"
fi
else
print_error "Failed to create mount directory: $mount_dir"
print_error "Please ensure you have sudo privileges or create the directory manually:"
print_error " sudo mkdir -p $mount_dir"
print_error " sudo chown $USER:$(id -gn) $mount_dir"
print_error " sudo chmod 755 $mount_dir"
return 1
fi
else
# For other paths, try to create normally
if mkdir -p "$mount_dir"; then
print_success "Mount directory created: $mount_dir"
else
print_error "Failed to create mount directory: $mount_dir"
return 1
fi
fi
# Final verification
if [ -d "$mount_dir" ] && [ -w "$mount_dir" ]; then
print_success "Mount directory is ready: $mount_dir"
return 0
else
print_error "Mount directory creation succeeded but directory is not writable"
return 1
fi
}
sanitize_env_value() {
local value="$1"
# Remove quotes and trim whitespace
value=$(echo "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//;s/^["'\'']*//;s/["'\'']*$//')
echo "$value"
}
parse_boolean() {
local value="$1"
local default_value="${2:-false}"
if [ -z "$value" ]; then
echo "$default_value"
return 0
fi
# Convert to lowercase for comparison
value=$(echo "$value" | tr '[:upper:]' '[:lower:]')
case "$value" in
"true" | "yes" | "1" | "on" | "enabled")
echo "true"
;;
"false" | "no" | "0" | "off" | "disabled")
echo "false"
;;
*)
print_warning "Invalid boolean value '$value', using default '$default_value'"
echo "$default_value"
;;
esac
}
safe_env_extract() {
local env_file="$1"
local var_name="$2"
local default_value="$3"
if [ ! -f "$env_file" ]; then
echo "$default_value"
return 0
fi
# Use grep with error handling and sanitize the result
local value
value=$(grep "^${var_name}=" "$env_file" 2>/dev/null | cut -d'=' -f2- | head -n1)
if [ -n "$value" ]; then
sanitize_env_value "$value"
else
echo "$default_value"
fi
}
# ============================================
# LOCAL S3 MOUNTING FUNCTIONS
# ============================================
check_fuse_support() {
print_info "Checking FUSE support..."
if [[ "$OS_TYPE" == "macos" ]]; then
# Check for macFUSE (formerly osxfuse)
if [ ! -e /Library/Filesystems/macfuse.fs ]; then
print_warning "macFUSE is not installed"
# Check if Homebrew is available
if command -v brew &> /dev/null; then
print_info "Installing macFUSE via Homebrew..."
if brew install --cask macfuse; then
print_success "macFUSE installed successfully"
print_warning "You may need to restart your terminal or reboot to use macFUSE"
print_info "If FUSE mounting fails, please reboot your system"
else
print_error "Failed to install macFUSE via Homebrew"
print_info "Manual installation: https://osxfuse.github.io/"
return 1
fi
else
print_error "Homebrew not found. Please install macFUSE manually"
print_info "Install macFUSE from: https://osxfuse.github.io/"
print_info "Or install Homebrew first, then run: brew install --cask macfuse"
return 1
fi
else
print_success "macFUSE detected"
fi
elif [[ "$OS_TYPE" == "linux" ]]; then
# Check for FUSE kernel module
if ! lsmod | grep -q fuse; then
print_warning "FUSE module not loaded, attempting to load..."
if ! sudo modprobe fuse 2>/dev/null; then
print_error "Failed to load FUSE kernel module"
print_info "Install FUSE: sudo apt-get install fuse (Ubuntu/Debian)"
return 1
fi
fi
# Check for FUSE userspace tools
if ! command -v fusermount &> /dev/null; then
print_warning "FUSE userspace tools not found, attempting to install..."
if command -v apt &> /dev/null; then
if sudo apt update && sudo apt install -y fuse; then
print_success "FUSE installed successfully"
else
print_error "Failed to install FUSE via apt"
return 1
fi
elif command -v yum &> /dev/null; then
if sudo yum install -y fuse; then
print_success "FUSE installed successfully"
else
print_error "Failed to install FUSE via yum"
return 1
fi
else
print_error "Cannot auto-install FUSE - unknown package manager"
print_info "Please install FUSE manually for your distribution"
return 1
fi
fi
# Check user permissions for FUSE
if ! groups "$USER" | grep -q fuse; then
print_warning "User '$USER' not in 'fuse' group"
print_info "Adding user to fuse group..."
if sudo usermod -a -G fuse "$USER" 2>/dev/null; then
print_warning "User added to fuse group. Please log out and back in, or run: newgrp fuse"
else
print_error "Failed to add user to fuse group"
return 1
fi
fi
print_success "FUSE support verified"
fi
return 0
}
install_goofys() {
print_info "Installing goofys for high-performance S3 mounting..."
# Check FUSE support first
if ! check_fuse_support; then
print_error "FUSE support check failed - cannot install goofys"
return 1
fi
# Check if goofys is already installed (check multiple locations)
if command -v goofys &> /dev/null; then
local goofys_path=$(command -v goofys)
local goofys_version=$(goofys --help 2>&1 | head -1 || echo "version unknown")
print_success "goofys already installed at: $goofys_path"
print_info "Version info: $goofys_version"
return 0
fi
# Also check common locations where it might be installed
local common_paths=("/usr/local/bin/goofys" "$HOME/go/bin/goofys" "/opt/homebrew/bin/goofys")
for path in "${common_paths[@]}"; do
if [ -x "$path" ]; then
print_success "goofys found at: $path"
# Add to PATH if not already there
if ! command -v goofys &> /dev/null; then
local dir=$(dirname "$path")
export PATH="$PATH:$dir"
print_info "Added $dir to PATH for current session"
fi
return 0
fi
done
# Try multiple installation methods for goofys
print_info "Trying multiple methods to install goofys..."
# Method 1: Try pre-built binary from GitHub releases
if install_goofys_binary; then
return 0
fi
# Method 2: Try building from source with Go
if install_goofys_from_source; then
return 0
fi
# Method 3: Try installing Go and building goofys
if install_go_and_build_goofys; then
return 0
fi
print_error "All goofys installation methods failed"
print_info "You may need to install goofys manually:"
print_info "1. Install Go: https://golang.org/doc/install"
print_info "2. Run: go install github.com/kahing/goofys@latest"
print_info "3. Ensure $HOME/go/bin is in your PATH"
return 1
}
install_goofys_binary() {
# GitHub releases only provide Linux x86_64 binaries
# Only attempt this for Linux x86_64 systems
if [[ "$OS_TYPE" != "linux" ]]; then
print_info "Method 1: Pre-built binaries only available for Linux, skipping for $OS_TYPE"
return 1
fi
local arch=$(uname -m)
if [[ "$arch" != "x86_64" ]]; then
print_info "Method 1: Pre-built binary only available for x86_64, skipping for $arch"
return 1
fi
print_info "Method 1: Installing pre-built binary for Linux x86_64..."
local goofys_url="https://github.com/kahing/goofys/releases/download/v0.24.0/goofys"
local install_dir="/usr/local/bin"
local temp_file="/tmp/goofys-$$"
# Create install directory if it doesn't exist
if [ ! -d "$install_dir" ]; then
sudo mkdir -p "$install_dir"
fi
# Download goofys binary
if curl -L "$goofys_url" -o "$temp_file" 2>/dev/null; then
# Verify the download was successful (binary should be >20MB)
if [ -s "$temp_file" ] && [ $(stat -c%s "$temp_file" 2>/dev/null) -gt 20000000 ]; then
# Test if binary is executable
if chmod +x "$temp_file" && "$temp_file" --help >/dev/null 2>&1; then
# Install the binary
if sudo mv "$temp_file" "$install_dir/goofys"; then
print_success "goofys pre-built binary installed successfully"
return 0
fi
fi
fi
rm -f "$temp_file"
fi
print_warning "Pre-built binary installation failed"
return 1
}
install_goofys_from_source() {
print_info "Method 2: Building goofys from source with Go..."
# Check if Go is available
if ! command -v go &> /dev/null; then
print_warning "Go compiler not found, will try to install Go next"
return 1
fi
# Check Go version (need 1.10+, but really should be 1.16+ for modern builds)
local go_version=$(go version 2>/dev/null | grep -o 'go[0-9]\+\.[0-9]\+' | sed 's/go//' | head -1)
if [ -z "$go_version" ]; then
print_warning "Could not determine Go version"
return 1
fi
local major_version=$(echo "$go_version" | cut -d'.' -f1)
local minor_version=$(echo "$go_version" | cut -d'.' -f2)
if [ "$major_version" -lt 1 ] || ([ "$major_version" -eq 1 ] && [ "$minor_version" -lt 16 ]); then
print_warning "Go version $go_version is old (recommend 1.16+), but trying anyway..."
fi
print_info "Building goofys from source with Go $go_version for $(uname -s)/$(uname -m)..."
# Set up Go environment
export GOPATH="${GOPATH:-$HOME/go}"
export PATH="$PATH:$GOPATH/bin:/opt/homebrew/bin"
# Create GOPATH if it doesn't exist
mkdir -p "$GOPATH/bin"
# Use PR #778 which fixes gopsutil v3 compatibility for macOS M1/ARM
print_info "Building goofys from PR #778 (gopsutil v3 fix)..."
local temp_dir="/tmp/goofys-build-$$"
mkdir -p "$temp_dir"
cd "$temp_dir"
# Clone and build PR #778 using modern Go approach
print_info "Building goofys with gopsutil v3 from PR #778..."
# Set GOPATH as the official method does
export GOPATH="${GOPATH:-$HOME/work}"
mkdir -p "$GOPATH/bin"
# Clone the PR branch
if git clone -b feature/upgrade-gopsutil https://github.com/chiehting/goofys.git; then
cd goofys
print_info "Building goofys using go install..."
# Use go install . to install from current directory
if go install .; then
print_success "Successfully built and installed goofys from PR #778"
else
print_error "Failed to build goofys from PR #778"
cd - && rm -rf "$temp_dir"
return 1
fi
else
print_error "Failed to clone goofys PR #778"
cd - && rm -rf "$temp_dir"
return 1
fi
# Clean up
cd - && rm -rf "$temp_dir"
# Check if build was successful - check both GOPATH and default Go bin
GOOFYS_PATH=""
if [ -f "$GOPATH/bin/goofys" ]; then
GOOFYS_PATH="$GOPATH/bin/goofys"
elif [ -f "$HOME/go/bin/goofys" ]; then
GOOFYS_PATH="$HOME/go/bin/goofys"
fi
if [ -n "$GOOFYS_PATH" ]; then
# Test the built binary
if "$GOOFYS_PATH" --help >/dev/null 2>&1; then
# Install to system location (try different approaches)
if [ -w /usr/local/bin ]; then
# User has write access
if cp "$GOOFYS_PATH" /usr/local/bin/goofys; then
print_success "goofys built and installed from source to /usr/local/bin"
return 0
fi
elif sudo -n true 2>/dev/null; then
# User has passwordless sudo
if sudo cp "$GOOFYS_PATH" /usr/local/bin/goofys; then
print_success "goofys built and installed from source to /usr/local/bin"
return 0
fi
else
# Just use it from GOPATH
print_success "goofys built successfully at $GOOFYS_PATH"
print_info "Note: goofys is available at $GOOFYS_PATH"
print_info "You may want to add $(dirname $GOOFYS_PATH) to your PATH"
return 0
fi
else
print_error "Built goofys binary is not working"
fi
else
print_error "goofys binary not found after build"
fi
print_warning "Source build failed"
return 1
}
install_go_and_build_goofys() {
print_info "Method 3: Installing Go compiler and building goofys..."
# Skip if Go is already installed
if command -v go &> /dev/null; then
print_info "Go already installed, skipping Go installation"
return 1
fi
if [[ "$OS_TYPE" == "macos" ]]; then
if command -v brew &> /dev/null; then
print_info "Installing Go via Homebrew..."
if brew install go; then
# Update PATH for current session
export PATH="$PATH:/usr/local/go/bin:$HOME/go/bin"
# Try to build goofys now that Go is installed
if install_goofys_from_source; then
return 0
fi
fi
fi
elif [[ "$OS_TYPE" == "linux" ]]; then
# Try to install Go via package manager
if command -v apt &> /dev/null; then
print_info "Installing Go via apt..."
if sudo apt update && sudo apt install -y golang-go; then
export PATH="$PATH:/usr/local/go/bin:$HOME/go/bin"
if install_goofys_from_source; then
return 0
fi
fi
elif command -v yum &> /dev/null; then
print_info "Installing Go via yum..."
if sudo yum install -y golang; then
export PATH="$PATH:/usr/local/go/bin:$HOME/go/bin"
if install_goofys_from_source; then
return 0
fi
fi
fi
fi
print_warning "Go installation and goofys build failed"
return 1
}
setup_persistent_mount() {
local bucket_name="$1"
local mount_dir="$2"
print_info "Setting up persistent S3 mount using goofys..."
if [[ "$OS_TYPE" == "macos" ]]; then
# Create launchd service for macOS
local service_file="$HOME/Library/LaunchAgents/com.sentient.mount.plist"
cat > "$service_file" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.sentient.mount</string>
<key>ProgramArguments</key>
<array>
<string>goofys</string>
<string>--stat-cache-ttl=10s</string>
<string>--type-cache-ttl=10s</string>
<string>$bucket_name</string>
<string>$mount_dir</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
EOF
launchctl load "$service_file" 2>/dev/null || true
print_success "launchd service created for persistent mounting"
elif [[ "$OS_TYPE" == "linux" ]]; then
# Create systemd service for Linux
local service_file="/etc/systemd/system/opt/sentient-mount.service"
sudo tee "$service_file" > /dev/null << EOF
[Unit]
Description=Sentient S3 Mount
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/bin/goofys --stat-cache-ttl=10s --type-cache-ttl=10s ${bucket_name} ${mount_dir}
ExecStop=/bin/fusermount -u ${mount_dir}
Restart=always
RestartSec=10
User=${USER}
Environment=HOME=${HOME}
Environment=AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
Environment=AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
Environment=AWS_REGION=${AWS_REGION}
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable sentient-mount.service 2>/dev/null || true
print_success "systemd service created for persistent mounting"
fi
}
validate_aws_credentials() {
print_info "Validating AWS credentials and S3 access..."
# Load AWS credentials from .env file
local aws_access_key=$(safe_env_extract ".env" "AWS_ACCESS_KEY_ID" "")
local aws_secret_key=$(safe_env_extract ".env" "AWS_SECRET_ACCESS_KEY" "")
local aws_region=$(safe_env_extract ".env" "AWS_REGION" "us-east-1")
local s3_bucket=$(safe_env_extract ".env" "S3_BUCKET_NAME" "")
# Check if credentials are available
if [ -z "$aws_access_key" ] || [ -z "$aws_secret_key" ] || [ -z "$s3_bucket" ]; then
print_error "Missing AWS credentials or S3 bucket name in .env file"
print_info "Required variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, S3_BUCKET_NAME"
return 1
fi
# Export credentials for AWS CLI
export AWS_ACCESS_KEY_ID="$aws_access_key"
export AWS_SECRET_ACCESS_KEY="$aws_secret_key"
export AWS_DEFAULT_REGION="$aws_region"
# Check if aws CLI is available
if ! command -v aws &> /dev/null; then
print_error "AWS CLI not found. Installing..."
if [[ "$OS_TYPE" == "macos" ]]; then
brew install awscli
elif [[ "$OS_TYPE" == "linux" ]]; then
# Install via package manager or pip
if command -v apt &> /dev/null; then
sudo apt update && sudo apt install -y awscli
elif command -v pip3 &> /dev/null; then
pip3 install awscli --user
else
print_error "Cannot install AWS CLI automatically"
return 1
fi
fi
fi
# Test AWS credentials
if ! aws sts get-caller-identity >/dev/null 2>&1; then
print_error "AWS credentials validation failed"
print_info "Please check your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in .env"
return 1
fi
print_success "AWS credentials validated"
# Test S3 bucket access
if ! aws s3 ls "s3://$s3_bucket" >/dev/null 2>&1; then
print_error "S3 bucket access failed for: $s3_bucket"
print_info "Please check bucket name and permissions"
return 1
fi
print_success "S3 bucket access validated: $s3_bucket"
return 0
}
verify_s3_mount() {
local mount_dir="$1"
local bucket_name="$2"
local max_wait=30
local count=0
print_info "Verifying S3 mount functionality..."
# Wait for mount to be ready
while [ $count -lt $max_wait ]; do
if mountpoint -q "$mount_dir" 2>/dev/null || mount | grep -q "$mount_dir"; then
break
fi
sleep 1
((count++))
done
if [ $count -ge $max_wait ]; then
print_error "Mount verification timed out"
return 1
fi
# Test directory listing - simplified for macOS compatibility
if command -v timeout >/dev/null 2>&1; then
# Use timeout if available (Linux)
if ! timeout 10 ls "$mount_dir" >/dev/null 2>&1; then
print_error "Cannot list mount directory contents"
return 1
fi
elif command -v gtimeout >/dev/null 2>&1; then
# Use gtimeout if available (macOS with coreutils)
if ! gtimeout 10 ls "$mount_dir" >/dev/null 2>&1; then
print_error "Cannot list mount directory contents"
return 1
fi
else
# Simple test for macOS - just check if ls succeeds
if ! ls "$mount_dir" >/dev/null 2>&1; then
print_error "Cannot list mount directory contents"
return 1
fi
fi
# Test write access with unique file
local test_file="$mount_dir/.sentient-mount-test-$(date +%s)-$$"
if ! echo "Mount test at $(date)" > "$test_file" 2>/dev/null; then
print_error "Cannot write to mounted directory"
return 1
fi
# Wait a moment for S3 sync, then verify in bucket
sleep 2
local test_key=$(basename "$test_file")
if [ -n "$bucket_name" ] && command -v aws &> /dev/null; then
if aws s3 ls "s3://$bucket_name/$test_key" >/dev/null 2>&1; then
print_success "S3 sync verification successful"
# Clean up test file
rm "$test_file" 2>/dev/null || true
else
print_warning "File not synced to S3 bucket (may take time)"
fi
else
# Clean up test file locally
rm "$test_file" 2>/dev/null || true
fi
print_success "S3 mount verification completed"
return 0
}
setup_local_s3_mounting() {
print_info "Setting up local S3 mounting..."
# Use safe environment extraction
S3_BUCKET_NAME=$(safe_env_extract ".env" "S3_BUCKET_NAME" "")
S3_MOUNT_DIR=$(safe_env_extract ".env" "S3_MOUNT_DIR" "/opt/sentient")
# Expand $HOME variable if present
S3_MOUNT_DIR=$(eval echo "$S3_MOUNT_DIR")
AWS_REGION=$(safe_env_extract ".env" "AWS_REGION" "us-east-1")
AWS_ACCESS_KEY_ID=$(safe_env_extract ".env" "AWS_ACCESS_KEY_ID" "")
AWS_SECRET_ACCESS_KEY=$(safe_env_extract ".env" "AWS_SECRET_ACCESS_KEY" "")
# Export AWS credentials for goofys
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_REGION
if [ -z "$S3_BUCKET_NAME" ] || [ "$S3_BUCKET_NAME" = "your-s3-bucket-name" ]; then
print_error "S3_BUCKET_NAME not configured in .env"
return 1
fi
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
print_error "AWS credentials not configured in .env"
return 1
fi
# Validate mount directory path
if ! validate_mount_path "$S3_MOUNT_DIR" "S3 mount directory"; then
return 1
fi
# Create mount directory with proper permissions
create_mount_directory "$S3_MOUNT_DIR"
if [ $? -ne 0 ]; then
return 1
fi
# Validate AWS credentials and S3 access
if ! validate_aws_credentials; then
print_error "AWS credentials validation failed"
return 1
fi
# Check FUSE support
if ! check_fuse_support; then
print_error "FUSE support not available"
return 1
fi
# Install goofys for high-performance S3 mounting
if ! install_goofys; then
print_error "Failed to install goofys"
return 1
fi
# Create mount directory with proper permissions
if [ ! -d "$S3_MOUNT_DIR" ]; then
if [[ "$S3_MOUNT_DIR" == /* ]]; then
# Absolute path, might need sudo
if ! mkdir -p "$S3_MOUNT_DIR" 2>/dev/null; then
sudo mkdir -p "$S3_MOUNT_DIR"
sudo chown $USER:$(id -gn) "$S3_MOUNT_DIR"
fi
else
# Relative path
mkdir -p "$S3_MOUNT_DIR"
fi
fi
print_info "Mounting S3 bucket '$S3_BUCKET_NAME' to '$S3_MOUNT_DIR'..."
# Check if already mounted
if mount | grep -q "$S3_MOUNT_DIR" || df | grep -q "$S3_MOUNT_DIR"; then
print_info "S3 bucket already mounted to $S3_MOUNT_DIR"
# Verify mount functionality
if verify_s3_mount "$S3_MOUNT_DIR"; then
print_success "Existing S3 mount verification passed"
return 0
else
print_warning "Existing mount verification failed, remounting..."
# Unmount and continue
diskutil unmount "$S3_MOUNT_DIR" 2>/dev/null || umount "$S3_MOUNT_DIR" 2>/dev/null || true
pkill -f "goofys.*$S3_MOUNT_DIR" 2>/dev/null || true
sleep 2
fi
fi
# Test mount S3 bucket to configured directory using goofys
print_info "Attempting to mount S3 bucket '$S3_BUCKET_NAME' to '$S3_MOUNT_DIR' using goofys..."
if goofys --stat-cache-ttl=10s --type-cache-ttl=10s "$S3_BUCKET_NAME" "$S3_MOUNT_DIR"; then
print_success "S3 bucket mounted successfully to $S3_MOUNT_DIR using goofys"
# Verify mount functionality
if verify_s3_mount "$S3_MOUNT_DIR"; then
print_success "S3 mount verification passed"
# Update .env if not already set
if ! grep -q "S3_MOUNT_ENABLED=true" .env; then
echo "S3_MOUNT_ENABLED=true" >> .env
fi
if ! grep -q "S3_MOUNT_DIR=" .env; then
echo "S3_MOUNT_DIR=$S3_MOUNT_DIR" >> .env
fi
# Setup persistent mounting
setup_persistent_mount "$S3_BUCKET_NAME" "$S3_MOUNT_DIR"
else
print_error "S3 mount verification failed"
# Attempt to unmount the failed mount
fusermount -u "$S3_MOUNT_DIR" 2>/dev/null || umount "$S3_MOUNT_DIR" 2>/dev/null || true
return 1
fi
else
print_error "Failed to mount S3 bucket - check AWS credentials and bucket access"
return 1
fi
}
ask_for_s3_mounting() {
if [ -f .env ] && grep -q "S3_BUCKET_NAME=" .env && ! grep -q "S3_BUCKET_NAME=your-s3-bucket-name" .env; then
echo ""
print_info "S3 credentials detected in .env"
read -p "Setup S3 mounting for data persistence? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Get mount directory from .env file or use default
mount_dir=$(safe_env_extract ".env" "S3_MOUNT_DIR" "/opt/sentient")
print_info "Using S3 mount directory from .env: $mount_dir"
# Update .env with mount directory if not already present
if ! grep -q "S3_MOUNT_DIR=" .env; then
echo "S3_MOUNT_DIR=$mount_dir" >> .env
print_info "Added S3_MOUNT_DIR=$mount_dir to .env"
fi
setup_local_s3_mounting
else
print_info "S3 mounting skipped"
fi
fi
}
# ============================================
# E2B TEMPLATE SETUP FUNCTIONS
# ============================================
check_e2b_requirements() {
print_info "Checking E2B requirements..."
# Check if E2B_API_KEY is set in .env
if [ -f .env ] && grep -q "E2B_API_KEY=" .env && ! grep -q "E2B_API_KEY=your_e2b_api_key_here" .env; then
print_success "E2B API key found in .env"
else
print_warning "E2B_API_KEY not configured in .env - E2B template build will be skipped"
return 1
fi
# Check if Node.js is available for E2B CLI
if ! command -v npm &> /dev/null; then
print_warning "npm not found - E2B CLI installation will be skipped"
return 1
fi
return 0
}