torrent-gateway/scripts/install_native.sh
enki b3204ea07a
Some checks are pending
CI Pipeline / Run Tests (push) Waiting to run
CI Pipeline / Lint Code (push) Waiting to run
CI Pipeline / Security Scan (push) Waiting to run
CI Pipeline / Build Docker Images (push) Blocked by required conditions
CI Pipeline / E2E Tests (push) Blocked by required conditions
first commit
2025-08-18 00:40:15 -07:00

305 lines
8.4 KiB
Bash
Executable File

#!/bin/bash
# Native Installation Script
# Complete setup for Torrent Gateway without Docker
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
echo "🚀 Torrent Gateway Native Installation"
echo "======================================"
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "❌ This script must be run as root"
echo "Please run: sudo $0"
exit 1
fi
# Parse arguments
ENABLE_MONITORING=false
SKIP_BUILD=false
while [[ $# -gt 0 ]]; do
case $1 in
--with-monitoring)
ENABLE_MONITORING=true
shift
;;
--skip-build)
SKIP_BUILD=true
shift
;;
--help)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --with-monitoring Install Prometheus, Grafana, and AlertManager"
echo " --skip-build Skip building the application (use existing binary)"
echo " --help Show this help message"
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
echo "Configuration:"
echo " Monitoring: $ENABLE_MONITORING"
echo " Skip build: $SKIP_BUILD"
echo ""
cd "$PROJECT_ROOT"
# Step 1: Install system dependencies
echo "📦 Installing system dependencies..."
apt-get update
apt-get install -y \
golang-go \
git \
sqlite3 \
redis-server \
nginx \
logrotate \
curl \
jq \
bc \
htop \
tree \
unzip \
wget
# Verify Go installation
if ! command -v go &> /dev/null; then
echo "❌ Go installation failed"
exit 1
fi
GO_VERSION=$(go version | grep -o 'go[0-9.]*' | head -1)
echo "✅ Go $GO_VERSION installed"
# Step 2: Build application
if [ "$SKIP_BUILD" = false ]; then
echo "🔨 Building Torrent Gateway..."
# Install Go dependencies
go mod download
# Build binary
go build -o bin/gateway \
-ldflags "-X main.version=$(git describe --tags --always 2>/dev/null || echo 'dev') -X main.buildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ) -s -w" \
cmd/gateway/main.go
if [ ! -f "bin/gateway" ]; then
echo "❌ Build failed"
exit 1
fi
echo "✅ Application built successfully"
else
echo "⏭️ Skipping build (using existing binary)"
if [ ! -f "bin/gateway" ]; then
echo "❌ No existing binary found. Remove --skip-build or build first."
exit 1
fi
fi
# Step 3: Setup systemd service
echo "⚙️ Setting up systemd service..."
./scripts/setup_systemd.sh $([ "$ENABLE_MONITORING" = true ] && echo "--with-monitoring")
# Step 4: Configure Redis
echo "🔧 Optimizing Redis configuration..."
cat > /etc/redis/redis.local.conf << 'EOF'
# Torrent Gateway specific Redis config
maxmemory 512mb
maxmemory-policy allkeys-lru
save 900 1
save 300 10
save 60 10000
EOF
# Include local config in main Redis config
if ! grep -q "include /etc/redis/redis.local.conf" /etc/redis/redis.conf; then
echo "include /etc/redis/redis.local.conf" >> /etc/redis/redis.conf
fi
# Step 5: Setup monitoring (if requested)
if [ "$ENABLE_MONITORING" = true ]; then
echo "📊 Installing monitoring components..."
# Install Node Exporter for system metrics
NODE_EXPORTER_VERSION="1.7.0"
cd /tmp
wget "https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz"
tar -xzf "node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz"
mkdir -p /opt/node_exporter
cp "node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter" /opt/node_exporter/
# Create node_exporter systemd service
cat > /etc/systemd/system/node-exporter.service << 'EOF'
[Unit]
Description=Node Exporter
After=network.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/opt/node_exporter/node_exporter
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable node-exporter
systemctl start node-exporter
echo "✅ Node Exporter installed and started"
fi
# Step 6: Configure firewall
echo "🔒 Configuring firewall..."
if command -v ufw &> /dev/null; then
# Allow SSH
ufw allow ssh
# Allow HTTP/HTTPS
ufw allow 80/tcp
ufw allow 443/tcp
# Allow monitoring ports (only from localhost)
if [ "$ENABLE_MONITORING" = true ]; then
ufw allow from 127.0.0.1 to any port 9090 # Prometheus
ufw allow from 127.0.0.1 to any port 3000 # Grafana
ufw allow from 127.0.0.1 to any port 9100 # Node Exporter
fi
# Enable firewall (only if not already enabled)
if ! ufw status | grep -q "Status: active"; then
echo "y" | ufw enable
fi
echo "✅ Firewall configured"
else
echo "⚠️ UFW not available, skipping firewall configuration"
fi
# Step 7: Create maintenance scripts
echo "🛠️ Creating maintenance scripts..."
# Create backup cron job
cat > /etc/cron.d/torrent-gateway << 'EOF'
# Torrent Gateway maintenance cron jobs
# Daily backup at 2 AM
0 2 * * * root /opt/torrent-gateway/scripts/backup.sh > /var/log/torrent-gateway-backup.log 2>&1
# Database maintenance at 3 AM
0 3 * * * root /opt/torrent-gateway/scripts/migrate.sh > /var/log/torrent-gateway-migrate.log 2>&1
# Health check every 5 minutes
*/5 * * * * root /opt/torrent-gateway/scripts/health_check.sh > /var/log/torrent-gateway-health.log 2>&1 || true
EOF
# Create log cleanup script
cat > /opt/torrent-gateway/scripts/cleanup.sh << 'EOF'
#!/bin/bash
# Cleanup Script
# Removes old logs and temporary files
set -e
INSTALL_DIR="/opt/torrent-gateway"
cd "$INSTALL_DIR"
echo "🧹 Cleaning up old files..."
# Remove old log files (older than 30 days)
find logs/ -name "*.log" -mtime +30 -delete 2>/dev/null || true
# Remove old backups (keep last 30)
cd backups/
ls -t gateway_backup_*.tar.gz 2>/dev/null | tail -n +31 | xargs rm -f || true
ls -t database_*.sql 2>/dev/null | tail -n +31 | xargs rm -f || true
# Clean up temporary chunk files
find data/chunks/ -name "*.tmp" -mtime +1 -delete 2>/dev/null || true
echo "✅ Cleanup completed"
EOF
chmod +x /opt/torrent-gateway/scripts/cleanup.sh
# Add weekly cleanup to cron
echo "0 4 * * 0 root /opt/torrent-gateway/scripts/cleanup.sh > /var/log/torrent-gateway-cleanup.log 2>&1" >> /etc/cron.d/torrent-gateway
# Step 8: Final service startup
echo "🚀 Starting all services..."
# Start dependencies first
systemctl start redis-server
systemctl start nginx
if [ "$ENABLE_MONITORING" = true ]; then
systemctl start prometheus
systemctl start grafana-server
fi
# Start main service
/opt/torrent-gateway/scripts/start.sh
# Wait for service to be ready
echo "⏳ Waiting for services to be ready..."
timeout 60 bash -c 'until curl -sf http://localhost/api/health; do sleep 2; done'
# Run health checks
echo "🏥 Running health checks..."
/opt/torrent-gateway/scripts/health_check.sh
if [ $? -eq 0 ]; then
echo ""
echo "🎉 Installation completed successfully!"
echo ""
echo "📊 Service Information:"
echo " Status: systemctl status torrent-gateway"
echo " Logs: journalctl -u torrent-gateway -f"
echo " Config: /opt/torrent-gateway/"
echo ""
echo "🌐 Access URLs:"
echo " Gateway API: http://localhost/api/"
echo " Admin Panel: http://localhost/admin"
if [ "$ENABLE_MONITORING" = true ]; then
echo " Prometheus: http://localhost:9090"
echo " Grafana: http://localhost:3000 (admin/admin)"
fi
echo ""
echo "🔧 Management Commands:"
echo " Start: sudo systemctl start torrent-gateway"
echo " Stop: sudo systemctl stop torrent-gateway"
echo " Restart: sudo systemctl restart torrent-gateway"
echo " Status: sudo systemctl status torrent-gateway"
echo ""
echo "💾 Backup & Restore:"
echo " Backup: sudo /opt/torrent-gateway/scripts/backup.sh"
echo " Restore: sudo /opt/torrent-gateway/scripts/restore.sh <timestamp>"
echo ""
echo "📝 Logs and Monitoring:"
echo " App logs: sudo journalctl -u torrent-gateway -f"
echo " System logs: sudo tail -f /var/log/syslog"
echo " Health: sudo /opt/torrent-gateway/scripts/health_check.sh"
else
echo "❌ Installation completed but health checks failed"
echo "Check logs: journalctl -u torrent-gateway"
exit 1
fi