Files
ginxsom/docs/NEW_DEPLOY_SCRIPT.md
2025-12-13 14:53:25 -04:00

12 KiB

New deploy_lt.sh Script

This is the complete new deployment script for static binary deployment. Save this as deploy_lt.sh in the project root.

#!/bin/bash
set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_status() { 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"; }

# Parse command line arguments
FRESH_INSTALL=false
MIGRATE_DATA=true
if [[ "$1" == "--fresh" ]]; then
    FRESH_INSTALL=true
    MIGRATE_DATA=false
elif [[ "$1" == "--no-migrate" ]]; then
    MIGRATE_DATA=false
fi

# Configuration
REMOTE_HOST="laantungir.net"
REMOTE_USER="ubuntu"

# New paths (static binary deployment)
REMOTE_BINARY_DIR="/usr/local/bin/ginxsom"
REMOTE_BINARY_PATH="$REMOTE_BINARY_DIR/ginxsom-fcgi"
REMOTE_DB_DIR="/var/lib/ginxsom"
REMOTE_DB_PATH="$REMOTE_DB_DIR/ginxsom.db"
REMOTE_BLOB_DIR="/var/www/blobs"
REMOTE_SOCKET="/tmp/ginxsom-fcgi.sock"

# Old paths (for migration)
OLD_BINARY_PATH="/home/ubuntu/ginxsom/ginxsom.fcgi"
OLD_DB_PATH="/home/ubuntu/ginxsom/db/ginxsom.db"
OLD_BLOB_DIR="/var/www/html/blossom"

# Local paths
LOCAL_BINARY="build/ginxsom-fcgi_static_x86_64"

print_status "=========================================="
print_status "Ginxsom Static Binary Deployment"
print_status "=========================================="
print_status "Target: $REMOTE_HOST"
print_status "Binary: $REMOTE_BINARY_PATH"
print_status "Database: $REMOTE_DB_PATH"
print_status "Blobs: $REMOTE_BLOB_DIR"
print_status "Fresh install: $FRESH_INSTALL"
print_status "Migrate data: $MIGRATE_DATA"
print_status "=========================================="
echo ""

# Step 1: Verify local binary exists
print_status "Step 1: Verifying local static binary..."
if [[ ! -f "$LOCAL_BINARY" ]]; then
    print_error "Static binary not found: $LOCAL_BINARY"
    print_status "Please run: ./build_static.sh"
    exit 1
fi

# Verify it's actually static
if ldd "$LOCAL_BINARY" 2>&1 | grep -q "not a dynamic executable\|statically linked"; then
    print_success "Binary is static"
else
    print_warning "Binary may not be fully static - proceeding anyway"
fi

BINARY_SIZE=$(du -h "$LOCAL_BINARY" | cut -f1)
print_success "Found static binary ($BINARY_SIZE)"
echo ""

# Step 2: Upload binary to server
print_status "Step 2: Uploading binary to server..."
scp "$LOCAL_BINARY" $REMOTE_USER@$REMOTE_HOST:/tmp/ginxsom-fcgi_new || {
    print_error "Failed to upload binary"
    exit 1
}
print_success "Binary uploaded to /tmp/ginxsom-fcgi_new"
echo ""

# Step 3: Setup directories and install binary
print_status "Step 3: Setting up directories and installing binary..."
ssh $REMOTE_USER@$REMOTE_HOST << EOF
    set -e
    
    # Create binary directory
    echo "Creating binary directory..."
    sudo mkdir -p $REMOTE_BINARY_DIR
    
    # Create database directory
    echo "Creating database directory..."
    sudo mkdir -p $REMOTE_DB_DIR/backups
    sudo chown www-data:www-data $REMOTE_DB_DIR
    sudo chmod 755 $REMOTE_DB_DIR
    
    # Create blob storage directory
    echo "Creating blob storage directory..."
    sudo mkdir -p $REMOTE_BLOB_DIR
    sudo chown www-data:www-data $REMOTE_BLOB_DIR
    sudo chmod 755 $REMOTE_BLOB_DIR
    
    echo "Directories created successfully"
EOF

if [ $? -ne 0 ]; then
    print_error "Failed to create directories"
    exit 1
fi
print_success "Directories created"
echo ""

# Step 4: Migrate data if requested
if [ "$MIGRATE_DATA" = true ] && [ "$FRESH_INSTALL" = false ]; then
    print_status "Step 4: Migrating existing data..."
    ssh $REMOTE_USER@$REMOTE_HOST << EOF
        set -e
        
        # Migrate database
        if [ -f $OLD_DB_PATH ]; then
            echo "Migrating database from $OLD_DB_PATH..."
            sudo cp $OLD_DB_PATH $REMOTE_DB_PATH
            sudo chown www-data:www-data $REMOTE_DB_PATH
            sudo chmod 644 $REMOTE_DB_PATH
            echo "Database migrated"
        elif [ -f $OLD_BLOB_DIR/ginxsom.db ]; then
            echo "Migrating database from $OLD_BLOB_DIR/ginxsom.db..."
            sudo cp $OLD_BLOB_DIR/ginxsom.db $REMOTE_DB_PATH
            sudo chown www-data:www-data $REMOTE_DB_PATH
            sudo chmod 644 $REMOTE_DB_PATH
            echo "Database migrated"
        else
            echo "No existing database found - will be created on first run"
        fi
        
        # Migrate blobs
        if [ -d $OLD_BLOB_DIR ] && [ "\$(ls -A $OLD_BLOB_DIR 2>/dev/null)" ]; then
            echo "Migrating blobs from $OLD_BLOB_DIR..."
            # Copy only blob files (SHA256 hashes with extensions)
            sudo find $OLD_BLOB_DIR -type f -regextype posix-extended -regex '.*/[a-f0-9]{64}\.[a-z0-9]+' -exec cp {} $REMOTE_BLOB_DIR/ \; 2>/dev/null || true
            sudo chown -R www-data:www-data $REMOTE_BLOB_DIR
            BLOB_COUNT=\$(ls -1 $REMOTE_BLOB_DIR | wc -l)
            echo "Migrated \$BLOB_COUNT blob files"
        else
            echo "No existing blobs found"
        fi
EOF
    
    if [ $? -eq 0 ]; then
        print_success "Data migration completed"
    else
        print_warning "Data migration had issues - check manually"
    fi
    echo ""
elif [ "$FRESH_INSTALL" = true ]; then
    print_status "Step 4: Fresh install - removing existing data..."
    ssh $REMOTE_USER@$REMOTE_HOST << EOF
        sudo rm -f $REMOTE_DB_PATH
        sudo rm -rf $REMOTE_BLOB_DIR/*
        echo "Existing data removed"
EOF
    print_success "Fresh install prepared"
    echo ""
else
    print_status "Step 4: Skipping data migration (--no-migrate)"
    echo ""
fi

# Step 5: Install minimal dependencies
print_status "Step 5: Installing minimal dependencies..."
ssh $REMOTE_USER@$REMOTE_HOST << 'EOF'
    set -e
    
    # Check if spawn-fcgi is installed
    if ! command -v spawn-fcgi &> /dev/null; then
        echo "Installing spawn-fcgi..."
        sudo apt-get update -qq
        sudo apt-get install -y spawn-fcgi
        echo "spawn-fcgi installed"
    else
        echo "spawn-fcgi already installed"
    fi
EOF

if [ $? -eq 0 ]; then
    print_success "Dependencies verified"
else
    print_error "Failed to install dependencies"
    exit 1
fi
echo ""

# Step 6: Stop existing service and install new binary
print_status "Step 6: Stopping existing service and installing new binary..."
ssh $REMOTE_USER@$REMOTE_HOST << EOF
    set -e
    
    # Stop any existing ginxsom processes
    echo "Stopping existing ginxsom processes..."
    sudo pkill -f ginxsom-fcgi || true
    sleep 2
    
    # Remove old socket
    sudo rm -f $REMOTE_SOCKET
    
    # Install new binary
    echo "Installing new binary..."
    sudo mv /tmp/ginxsom-fcgi_new $REMOTE_BINARY_PATH
    sudo chmod +x $REMOTE_BINARY_PATH
    sudo chown root:root $REMOTE_BINARY_PATH
    
    echo "Binary installed successfully"
EOF

if [ $? -eq 0 ]; then
    print_success "Binary installed"
else
    print_error "Failed to install binary"
    exit 1
fi
echo ""

# Step 7: Start ginxsom FastCGI process
print_status "Step 7: Starting ginxsom FastCGI process..."
ssh $REMOTE_USER@$REMOTE_HOST << EOF
    set -e
    
    echo "Starting ginxsom FastCGI with configuration:"
    echo "  Binary: $REMOTE_BINARY_PATH"
    echo "  Database: $REMOTE_DB_PATH"
    echo "  Storage: $REMOTE_BLOB_DIR"
    echo "  Socket: $REMOTE_SOCKET"
    echo ""
    
    sudo spawn-fcgi \
        -M 666 \
        -u www-data \
        -g www-data \
        -s $REMOTE_SOCKET \
        -U www-data \
        -G www-data \
        -d $REMOTE_DB_DIR \
        -- $REMOTE_BINARY_PATH \
           --db-path $REMOTE_DB_PATH \
           --storage-dir $REMOTE_BLOB_DIR
    
    # Give it a moment to start
    sleep 2
    
    # Verify process is running
    if [ -S $REMOTE_SOCKET ]; then
        echo "FastCGI socket created successfully"
        ls -la $REMOTE_SOCKET
    else
        echo "ERROR: Socket not created"
        exit 1
    fi
    
    # Check if process is running
    if pgrep -f ginxsom-fcgi > /dev/null; then
        echo "Process is running (PID: \$(pgrep -f ginxsom-fcgi))"
    else
        echo "WARNING: Process not found by pgrep (may be normal for FastCGI)"
    fi
EOF

if [ $? -eq 0 ]; then
    print_success "FastCGI process started"
else
    print_error "Failed to start FastCGI process"
    exit 1
fi
echo ""

# Step 8: Test nginx configuration and reload
print_status "Step 8: Testing and reloading nginx..."
ssh $REMOTE_USER@$REMOTE_HOST << 'EOF'
    # Test nginx configuration
    if sudo nginx -t 2>&1; then
        echo "Nginx configuration test passed"
        sudo nginx -s reload
        echo "Nginx reloaded successfully"
    else
        echo "WARNING: Nginx configuration test failed"
        echo "You may need to update nginx configuration manually"
        echo "See docs/STATIC_DEPLOYMENT_PLAN.md for details"
    fi
EOF

if [ $? -eq 0 ]; then
    print_success "Nginx reloaded"
else
    print_warning "Nginx reload had issues - check configuration"
fi
echo ""

# Step 9: Test deployment
print_status "Step 9: Testing deployment..."
echo ""

# Wait a moment for service to fully start
sleep 2

# Test health endpoint
echo "Testing health endpoint..."
if curl -k -s --max-time 10 "https://blossom.laantungir.net/health" | grep -q "OK"; then
    print_success "✓ Health check passed"
else
    print_warning "✗ Health check failed - checking response..."
    curl -k -v --max-time 10 "https://blossom.laantungir.net/health" 2>&1 | head -10
fi

# Test root endpoint
echo ""
echo "Testing root endpoint..."
if curl -k -s --max-time 10 "https://blossom.laantungir.net/" | grep -q "Ginxsom"; then
    print_success "✓ Root endpoint responding"
else
    print_warning "✗ Root endpoint not responding as expected"
fi

echo ""
print_status "=========================================="
print_success "Deployment completed!"
print_status "=========================================="
echo ""
print_status "Service Information:"
echo "  URL: https://blossom.laantungir.net"
echo "  Binary: $REMOTE_BINARY_PATH"
echo "  Database: $REMOTE_DB_PATH"
echo "  Blobs: $REMOTE_BLOB_DIR"
echo "  Socket: $REMOTE_SOCKET"
echo ""
print_status "Test Commands:"
echo "  Health: curl -k https://blossom.laantungir.net/health"
echo "  Info: curl -k https://blossom.laantungir.net/"
echo "  Upload: ./tests/file_put_bud02.sh"
echo ""
print_status "Server Commands:"
echo "  Check status: ssh $REMOTE_USER@$REMOTE_HOST 'ps aux | grep ginxsom-fcgi'"
echo "  View logs: ssh $REMOTE_USER@$REMOTE_HOST 'sudo journalctl -f | grep ginxsom'"
echo "  Restart: ssh $REMOTE_USER@$REMOTE_HOST 'sudo pkill ginxsom-fcgi && sudo spawn-fcgi ...'"
echo ""

if [ "$FRESH_INSTALL" = true ]; then
    print_warning "Fresh install completed - database and blobs have been reset"
fi

if [ "$MIGRATE_DATA" = true ] && [ "$FRESH_INSTALL" = false ]; then
    print_status "Data migration completed - verify blob count and database"
    echo "  Check blobs: ssh $REMOTE_USER@$REMOTE_HOST 'ls -la $REMOTE_BLOB_DIR | wc -l'"
    echo "  Check DB: ssh $REMOTE_USER@$REMOTE_HOST 'sudo -u www-data sqlite3 $REMOTE_DB_PATH \"SELECT COUNT(*) FROM blobs;\"'"
fi

echo ""
print_status "For nginx configuration updates, see: docs/STATIC_DEPLOYMENT_PLAN.md"
print_status "=========================================="

Usage

# Normal deployment with data migration
./deploy_lt.sh

# Fresh install (removes all data)
./deploy_lt.sh --fresh

# Deploy without migrating data
./deploy_lt.sh --no-migrate

Key Changes from Old Script

  1. No remote compilation - uploads pre-built static binary
  2. New directory structure - follows FHS standards
  3. Minimal dependencies - only spawn-fcgi needed
  4. Data migration - automatically migrates from old locations
  5. Simplified process - ~30 seconds vs ~5-10 minutes