-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
588 lines (501 loc) · 16.3 KB
/
install.sh
File metadata and controls
588 lines (501 loc) · 16.3 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
#!/bin/bash
set -e
# SeeSpot Installer
# Linux only - installs SeeSpot visualization server
# Usage: ./install.sh [--interactive] [--verbose] [--dry-run]
VERSION="0.5.1"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Parse command line arguments
YES_FLAG=true
VERBOSE=false
DRY_RUN=false
while [[ $# -gt 0 ]]; do
case $1 in
--interactive|-i)
YES_FLAG=false
shift
;;
--yes|-y)
YES_FLAG=true
shift
;;
--verbose|-v)
VERBOSE=true
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--help|-h)
echo "SeeSpot Installer"
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --interactive, -i Interactive mode (prompt for configuration)"
echo " --yes, -y Non-interactive mode (use defaults, same as default)"
echo " --verbose, -v Verbose output"
echo " --dry-run Show what would be done without making changes"
echo " --help, -h Show this help message"
echo ""
echo "Note: Non-interactive mode is the default. Use --interactive for prompts."
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_verbose() {
if [ "$VERBOSE" = true ]; then
echo -e "${BLUE}[VERBOSE]${NC} $1"
fi
}
# Check if running on Linux
check_os() {
log_info "Checking operating system..."
if [[ "$OSTYPE" != "linux-gnu"* ]]; then
log_error "This installer currently supports Linux only."
log_error "Detected OS: $OSTYPE"
exit 1
fi
log_success "Linux detected"
}
# Check for uv and install if missing
check_uv() {
log_info "Checking for uv..."
if command -v uv &> /dev/null; then
UV_VERSION=$(uv --version | awk '{print $2}')
log_success "uv is installed (version $UV_VERSION)"
return 0
else
log_warning "uv is not installed"
if [ "$YES_FLAG" = true ]; then
INSTALL_UV="y"
else
read -p "Would you like to install uv? [Y/n]: " INSTALL_UV
INSTALL_UV=${INSTALL_UV:-y}
fi
if [[ "$INSTALL_UV" =~ ^[Yy]$ ]]; then
log_info "Installing uv..."
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would execute: curl -LsSf https://astral.sh/uv/install.sh | sh"
return 0
fi
curl -LsSf https://astral.sh/uv/install.sh | sh
# Add to current session PATH
export PATH="$HOME/.cargo/bin:$PATH"
log_success "uv installed successfully"
else
log_error "uv is required to install SeeSpot. Please install it manually:"
log_error " curl -LsSf https://astral.sh/uv/install.sh | sh"
exit 1
fi
fi
}
# Check AWS credentials
check_aws_credentials() {
log_info "Checking AWS credentials..."
HAS_CREDS=false
# Check environment variables (standard credentials)
if [ ! -z "$AWS_ACCESS_KEY_ID" ] && [ ! -z "$AWS_SECRET_ACCESS_KEY" ]; then
log_success "AWS credentials found in environment variables"
HAS_CREDS=true
return 0
fi
# Check for ECS task role (Docker container with task role)
if [ ! -z "$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ]; then
log_success "ECS task role detected (Docker/ECS container)"
HAS_CREDS=true
return 0
fi
# Check ~/.aws/credentials
if [ -f "$HOME/.aws/credentials" ]; then
log_success "AWS credentials file found at ~/.aws/credentials"
HAS_CREDS=true
return 0
fi
# Check for IAM role (EC2 instance)
if curl -s -f -m 2 http://169.254.169.254/latest/meta-data/iam/security-credentials/ &> /dev/null; then
log_success "IAM instance role detected (EC2)"
HAS_CREDS=true
return 0
fi
if [ "$HAS_CREDS" = false ]; then
log_warning "No AWS credentials detected"
log_warning "SeeSpot requires access to S3 bucket: aind-open-data"
log_warning "Please set up AWS credentials before using SeeSpot:"
log_warning " 1. Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY"
log_warning " 2. AWS CLI: aws configure"
log_warning " 3. IAM role (if running on EC2)"
log_warning " 4. ECS task role (if running in Docker/ECS)"
log_warning ""
log_warning "You can continue installation and set up credentials later."
if [ "$YES_FLAG" = false ]; then
read -p "Continue anyway? [y/N]: " CONTINUE
if [[ ! "$CONTINUE" =~ ^[Yy]$ ]]; then
exit 1
fi
fi
fi
}
# Check disk space
check_disk_space() {
local TARGET_DIR=$1
log_info "Checking disk space at $TARGET_DIR..."
# Get available space in GB
AVAILABLE=$(df -BG "$TARGET_DIR" | tail -1 | awk '{print $4}' | sed 's/G//')
log_verbose "Available space: ${AVAILABLE}GB"
if [ "$AVAILABLE" -lt 50 ]; then
log_warning "Low disk space detected: ${AVAILABLE}GB available"
log_warning "Recommended: 50GB+ for S3 cache"
if [ "$YES_FLAG" = false ]; then
read -p "Continue anyway? [y/N]: " CONTINUE
if [[ ! "$CONTINUE" =~ ^[Yy]$ ]]; then
exit 1
fi
fi
else
log_success "Sufficient disk space available: ${AVAILABLE}GB"
fi
}
# Interactive configuration
configure() {
log_info "Configuring SeeSpot..."
# Default values
DEFAULT_CACHE_DIR="$HOME/capsule/scratch/.seespot/cache"
DEFAULT_PORT=5555
DEFAULT_HOST="0.0.0.0"
if [ "$YES_FLAG" = true ]; then
CACHE_DIR=$DEFAULT_CACHE_DIR
SERVER_PORT=$DEFAULT_PORT
SERVER_HOST=$DEFAULT_HOST
else
echo ""
read -p "Cache directory for S3 data [$DEFAULT_CACHE_DIR]: " CACHE_DIR
CACHE_DIR=${CACHE_DIR:-$DEFAULT_CACHE_DIR}
read -p "Server port [$DEFAULT_PORT]: " SERVER_PORT
SERVER_PORT=${SERVER_PORT:-$DEFAULT_PORT}
read -p "Server host [$DEFAULT_HOST]: " SERVER_HOST
SERVER_HOST=${SERVER_HOST:-$DEFAULT_HOST}
fi
# Expand ~ to actual home directory
CACHE_DIR="${CACHE_DIR/#\~/$HOME}"
log_verbose "Configuration:"
log_verbose " Cache directory: $CACHE_DIR"
log_verbose " Server host: $SERVER_HOST"
log_verbose " Server port: $SERVER_PORT"
}
# Create directories and config
setup_directories() {
log_info "Setting up directories..."
SEESPOT_DIR="$HOME/.seespot"
CONFIG_FILE="$SEESPOT_DIR/config.yaml"
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would create: $SEESPOT_DIR"
log_info "[DRY RUN] Would create: $CACHE_DIR"
return 0
fi
# Create SeeSpot directory
mkdir -p "$SEESPOT_DIR"
log_success "Created $SEESPOT_DIR"
# Create cache directory
mkdir -p "$CACHE_DIR"
log_success "Created cache directory: $CACHE_DIR"
# Create config file
cat > "$CONFIG_FILE" << EOF
# SeeSpot Configuration
# Generated by installer on $(date)
cache_dir: $CACHE_DIR
server:
host: $SERVER_HOST
port: $SERVER_PORT
aws:
# S3 bucket for data access
bucket: aind-open-data
# Optional: specify AWS profile name
# profile: default
# Optional: specify region
# region: us-west-2
EOF
log_success "Created config file: $CONFIG_FILE"
# Create install log
INSTALL_LOG="$SEESPOT_DIR/install.log"
echo "SeeSpot installation started at $(date)" > "$INSTALL_LOG"
echo "Version: $VERSION" >> "$INSTALL_LOG"
echo "Install directory: $SCRIPT_DIR" >> "$INSTALL_LOG"
echo "Cache directory: $CACHE_DIR" >> "$INSTALL_LOG"
}
# Install Python environment with uv
install_environment() {
log_info "Installing SeeSpot environment..."
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would create virtual environment with uv"
log_info "[DRY RUN] Would install dependencies"
return 0
fi
cd "$SCRIPT_DIR"
# Create virtual environment with Python 3.11+
log_info "Creating virtual environment..."
uv venv .venv --python 3.11
log_success "Virtual environment created"
# Install package
log_info "Installing SeeSpot package and dependencies..."
uv pip install -e .
log_success "SeeSpot package installed"
}
# Create launcher script
create_launcher() {
log_info "Creating launcher script..."
# try launcher in spot for code-ocean build
LAUNCHER="/usr/local/sbin/seespot"
# LAUNCHER="$HOME/.local/bin/seespot"
if [ "$DRY_RUN" = true ]; then
log_info "[DRY RUN] Would create launcher: $LAUNCHER"
return 0
fi
# Create .local/bin if it doesn't exist
# mkdir -p "$HOME/.local/bin"
# Create launcher script
cat > "$LAUNCHER" << 'EOF'
#!/bin/bash
# SeeSpot Launcher
SEESPOT_DIR="$HOME/.seespot"
CONFIG="$SEESPOT_DIR/config.yaml"
INSTALL_DIR="__INSTALL_DIR__"
VENV="$INSTALL_DIR/.venv"
PID_FILE="$SEESPOT_DIR/seespot.pid"
# Load config
load_config() {
if [ ! -f "$CONFIG" ]; then
echo "Error: Config file not found at $CONFIG"
exit 1
fi
# Parse YAML (simple parsing for our specific config)
SERVER_PORT=$(grep "port:" "$CONFIG" | awk '{print $2}')
SERVER_HOST=$(grep "host:" "$CONFIG" | awk '{print $2}')
SERVER_PORT=${SERVER_PORT:-5555}
SERVER_HOST=${SERVER_HOST:-0.0.0.0}
}
start_server() {
if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
echo "SeeSpot is already running (PID: $(cat "$PID_FILE"))"
exit 1
fi
load_config
echo "Starting SeeSpot on $SERVER_HOST:$SERVER_PORT..."
cd "$INSTALL_DIR/src"
source "$VENV/bin/activate"
nohup uvicorn see_spot.app:app --host "$SERVER_HOST" --port "$SERVER_PORT" > "$SEESPOT_DIR/seespot.log" 2>&1 &
echo $! > "$PID_FILE"
echo "SeeSpot started (PID: $(cat "$PID_FILE"))"
echo "Log file: $SEESPOT_DIR/seespot.log"
echo "Visit: http://localhost:$SERVER_PORT/unmixed-spots"
}
stop_server() {
if [ ! -f "$PID_FILE" ]; then
echo "SeeSpot is not running (no PID file)"
exit 1
fi
PID=$(cat "$PID_FILE")
if kill -0 "$PID" 2>/dev/null; then
echo "Stopping SeeSpot (PID: $PID)..."
kill "$PID"
rm "$PID_FILE"
echo "SeeSpot stopped"
else
echo "SeeSpot is not running (stale PID file)"
rm "$PID_FILE"
fi
}
status_server() {
if [ ! -f "$PID_FILE" ]; then
echo "SeeSpot is not running"
exit 1
fi
PID=$(cat "$PID_FILE")
if kill -0 "$PID" 2>/dev/null; then
load_config
echo "SeeSpot is running (PID: $PID)"
echo "URL: http://localhost:$SERVER_PORT/unmixed-spots"
else
echo "SeeSpot is not running (stale PID file)"
rm "$PID_FILE"
exit 1
fi
}
show_config() {
if [ -f "$CONFIG" ]; then
cat "$CONFIG"
else
echo "Config file not found: $CONFIG"
exit 1
fi
}
show_logs() {
LOG_FILE="$SEESPOT_DIR/seespot.log"
if [ -f "$LOG_FILE" ]; then
tail -f "$LOG_FILE"
else
echo "Log file not found: $LOG_FILE"
exit 1
fi
}
case "$1" in
start)
start_server
;;
stop)
stop_server
;;
restart)
stop_server
sleep 1
start_server
;;
status)
status_server
;;
config)
show_config
;;
logs)
show_logs
;;
*)
echo "SeeSpot Launcher"
echo "Usage: $0 {start|stop|restart|status|config|logs}"
echo ""
echo "Commands:"
echo " start - Start the SeeSpot server"
echo " stop - Stop the SeeSpot server"
echo " restart - Restart the SeeSpot server"
echo " status - Check if SeeSpot is running"
echo " config - Show configuration"
echo " logs - Tail the server logs"
exit 1
;;
esac
EOF
# Replace placeholder with actual install directory
sed -i "s|__INSTALL_DIR__|$SCRIPT_DIR|g" "$LAUNCHER"
# Make executable
chmod +x "$LAUNCHER"
log_success "Launcher script created: $LAUNCHER"
# Add ~/.local/bin to PATH if not already present
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
# Detect shell config file
if [ -f "$HOME/.bashrc" ]; then
SHELL_CONFIG="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
SHELL_CONFIG="$HOME/.bash_profile"
elif [ -f "$HOME/.zshrc" ]; then
SHELL_CONFIG="$HOME/.zshrc"
elif [ -f "$HOME/.profile" ]; then
SHELL_CONFIG="$HOME/.profile"
else
SHELL_CONFIG=""
fi
if [ -n "$SHELL_CONFIG" ] && [ -t 1 ]; then
# Interactive mode - ask user
log_info "~/.local/bin is not in your PATH"
if [ "$YES_FLAG" = false ]; then
read -p "Add PATH export to $SHELL_CONFIG? [Y/n]: " ADD_PATH
if [[ ! "$ADD_PATH" =~ ^[Nn]$ ]]; then
echo "" >> "$SHELL_CONFIG"
echo "# Added by SeeSpot installer" >> "$SHELL_CONFIG"
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$SHELL_CONFIG"
log_success "Added PATH export to $SHELL_CONFIG"
log_info "Run: source $SHELL_CONFIG"
fi
else
# Non-interactive - add automatically
if ! grep -q "export PATH=\"\$HOME/.local/bin:\$PATH\"" "$SHELL_CONFIG" 2>/dev/null; then
echo "" >> "$SHELL_CONFIG"
echo "# Added by SeeSpot installer" >> "$SHELL_CONFIG"
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$SHELL_CONFIG"
log_success "Added PATH export to $SHELL_CONFIG"
fi
fi
elif [ ! -t 1 ]; then
# Non-interactive (Docker/CI) - just log for reference
log_verbose "For Docker, add to Dockerfile: ENV PATH=\"\$HOME/.local/bin:\$PATH\""
fi
fi
}
# Print success message
print_success() {
echo ""
log_success "=========================================="
log_success "SeeSpot installation complete!"
log_success "=========================================="
echo ""
log_info "Configuration file: $HOME/.seespot/config.yaml"
log_info "Cache directory: $CACHE_DIR"
echo ""
log_info "To start SeeSpot:"
echo " seespot start"
echo ""
log_info "Other commands:"
echo " seespot stop - Stop the server"
echo " seespot status - Check server status"
echo " seespot logs - View server logs"
echo " seespot config - Show configuration"
echo ""
log_info "Server will be available at:"
echo " http://localhost:$SERVER_PORT/unmixed-spots"
echo ""
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
log_warning "Note: Add ~/.local/bin to your PATH:"
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
fi
}
# Main installation flow
main() {
echo ""
echo "=========================================="
echo " SeeSpot Installer v${VERSION}"
echo " Visualization for HCR Spot Data"
echo "=========================================="
echo ""
if [ "$DRY_RUN" = true ]; then
log_warning "Running in DRY RUN mode - no changes will be made"
echo ""
fi
check_os
check_uv
check_aws_credentials
configure
check_disk_space "$(dirname "$CACHE_DIR")"
setup_directories
install_environment
create_launcher
if [ "$DRY_RUN" = false ]; then
print_success
else
log_info "[DRY RUN] Installation simulation complete"
fi
}
# Run installer
main