#!/bin/bash # Start Hive development environment # This script starts all required services in the correct order using Hive service commands set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" MULGA_ROOT="$(cd "$PROJECT_ROOT/.." && pwd)" # Configuration paths CONFIG_DIR="$PROJECT_ROOT/config" DATA_DIR="$PROJECT_ROOT/data" LOGS_DIR="$DATA_DIR/logs" echo "๐Ÿš€ Starting Hive development environment..." echo "Project root: $PROJECT_ROOT" echo "Data directory: $DATA_DIR" # Create necessary directories mkdir -p "$DATA_DIR"/{predastore,viperblock,logs} mkdir -p /mnt/ramdisk 2>/dev/null || echo "โš ๏ธ /mnt/ramdisk not available, using $DATA_DIR/viperblock" # Change to project root for all commands cd "$PROJECT_ROOT" # Function to start service in background start_service() { local name="$1" local command="$2" local pidfile="$LOGS_DIR/$name.pid" local logfile="$LOGS_DIR/$name.log" echo "๐Ÿ“ก Starting $name..." echo " Command: $command" # Start service and capture PID nohup $command > "$logfile" 2>&1 & local pid=$! echo $pid > "$pidfile" echo " PID: $pid, Log: $logfile" # Brief pause to let service start sleep 2 } # Function to start service in foreground (for final daemon) start_service_foreground() { local name="$1" local command="$2" echo "๐Ÿ“ก Starting $name in foreground..." echo " Command: $command" # Setup signal handler to stop background services when daemon stops trap 'echo ""; echo "๐Ÿ›‘ Stopping all services..."; ./scripts/stop-dev.sh; exit 0' INT TERM # Start the service in foreground $command } # Function to check if service is responsive check_service() { local name="$1" local port="$2" local max_attempts=10 local attempt=1 echo "๐Ÿ” Checking $name connectivity on port $port..." while [ $attempt -le $max_attempts ]; do if nc -z localhost "$port" 2>/dev/null; then echo " โœ… $name is responding on port $port" return 0 fi echo " โณ Attempt $attempt/$max_attempts - waiting for $name..." sleep 2 ((attempt++)) done echo " โš ๏ธ $name may not be responding on port $port (continuing anyway)" return 1 } # 1๏ธโƒฃ Start NATS server echo "" echo "1๏ธโƒฃ Starting NATS server..." start_service "nats" "go run cmd/hive/main.go service nats start" check_service "NATS" "4222" # 2๏ธโƒฃ Start Predastore echo "" echo "2๏ธโƒฃ Starting Predastore..." PREDASTORE_CMD="go run cmd/hive/main.go service predastore \ --base-path $DATA_DIR/predastore/ \ --config-path $CONFIG_DIR/predastore/predastore.toml \ --tls-cert $CONFIG_DIR/server.pem \ --tls-key $CONFIG_DIR/server.key \ --debug start" start_service "predastore" "$PREDASTORE_CMD" check_service "Predastore" "8443" # 3๏ธโƒฃ Start Viperblock echo "" echo "3๏ธโƒฃ Starting Viperblock..." # Determine base directory for Viperblock data if [ -d "/mnt/ramdisk" ] && [ -w "/mnt/ramdisk" ]; then VB_BASE_DIR="/mnt/ramdisk/" else VB_BASE_DIR="$DATA_DIR/viperblock/" fi # Check if NBD plugin exists NBD_PLUGIN_PATH="$MULGA_ROOT/viperblock/lib/nbdkit-viperblock-plugin.so" if [ ! -f "$NBD_PLUGIN_PATH" ]; then echo " โš ๏ธ NBD plugin not found at $NBD_PLUGIN_PATH" echo " Building Viperblock first..." cd "$MULGA_ROOT/viperblock" make build cd "$PROJECT_ROOT" fi VIPERBLOCK_CMD="go run cmd/hive/main.go service viperblock start \ --nats-host 0.0.0.0:4222 \ --access-key AKIAIOSFODNN7EXAMPLE \ --secret-key wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY \ --base-dir $VB_BASE_DIR \ --plugin-path $NBD_PLUGIN_PATH" start_service "viperblock" "$VIPERBLOCK_CMD" # 4๏ธโƒฃ Start Hive Gateway/Daemon echo "" echo "4๏ธโƒฃ Starting Hive Gateway..." # Use the same base directory as Viperblock for consistency HIVE_BASE_DIR="$VB_BASE_DIR" # Check if we should use air for hot reloading if command -v air >/dev/null 2>&1 && [ -f ".air.toml" ]; then echo " ๐Ÿ”ฅ Using air for hot reloading" start_service_foreground "hive-air" "air" else echo " ๐Ÿ”จ Starting Hive daemon with go run" # Check if config file exists if [ ! -f "$CONFIG_DIR/hive.toml" ]; then echo " โš ๏ธ Config file not found at $CONFIG_DIR/hive.toml" echo " You may need to create this file or use a different config path" fi # Start Hive daemon directly with go run (in foreground) HIVE_CMD="go run cmd/hive/main.go --config $CONFIG_DIR/hive.toml --base-dir $HIVE_BASE_DIR daemon" echo "" echo "๐Ÿ”— Service endpoints will be:" echo " - NATS: nats://localhost:4222" echo " - Predastore: https://localhost:8443" echo " - Hive Gateway: https://localhost:9999" echo "" echo "๐Ÿ“Š Monitor background service logs:" echo " tail -f $LOGS_DIR/*.log" echo "" echo "๐Ÿงช Test with AWS CLI (once daemon is running):" echo " aws --endpoint-url https://localhost:9999 --no-verify-ssl ec2 describe-instances" echo "" # Use foreground function which includes signal handling start_service_foreground "hive-daemon" "$HIVE_CMD" fi # This will only be reached if air/daemon exits normally echo "" echo "๐Ÿ›‘ Hive development environment stopped"