#!/bin/bash
# Create ceph-rbd storage pool on r630-01
# RBD (Ceph Block Device) supports VM disk images, unlike CephFS

set -e

echo "═══════════════════════════════════════════════════════════════════════════════"
echo "  CREATE CEPH RBD STORAGE POOL ON r630-01"
echo "═══════════════════════════════════════════════════════════════════════════════"
echo ""

# Check if running on r630-01
HOSTNAME=$(hostname)
if [ "$HOSTNAME" != "r630-01" ]; then
    echo "⚠️  WARNING: This script should be run on r630-01"
    echo "   Current hostname: $HOSTNAME"
    echo ""
    echo "To run remotely via SSH:"
    echo "  ssh root@192.168.11.11 'bash -s' < $0"
    echo ""
    read -p "Continue anyway? (y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

echo "Step 1: Checking Ceph cluster status..."
if ! command -v ceph &> /dev/null; then
    echo "❌ ERROR: ceph command not found. Ceph packages may not be installed."
    exit 1
fi

CEPH_STATUS=$(ceph -s 2>&1 | head -5)
echo "$CEPH_STATUS"
echo ""

echo "Step 2: Checking if RBD pool exists..."
RBD_POOL_EXISTS=$(ceph osd pool ls 2>/dev/null | grep -c "^rbd$" || echo "0")
if [ "$RBD_POOL_EXISTS" = "0" ]; then
    echo "⚠️  RBD pool 'rbd' does not exist"
    echo ""
    echo "Step 3: Creating RBD pool..."
    # Create RBD pool with appropriate PG count
    # For small clusters, use 32 PGs
    ceph osd pool create rbd 32 32 2>&1 || {
        echo "❌ Failed to create RBD pool"
        exit 1
    }
    echo "✅ RBD pool created"
    
    # Initialize the pool for RBD
    echo "Step 4: Initializing RBD pool..."
    rbd pool init rbd 2>&1 || {
        echo "⚠️  Pool initialization failed or already initialized"
    }
else
    echo "✅ RBD pool 'rbd' already exists"
fi

echo ""
echo "Step 5: Checking if RBD storage is already added to Proxmox..."
if pvesm status 2>/dev/null | grep -q "ceph-rbd"; then
    echo "✅ Storage 'ceph-rbd' already exists in Proxmox"
    pvesm status | grep "ceph-rbd"
else
    echo "Adding ceph-rbd storage to Proxmox..."
    # Add RBD storage - RBD supports 'images' content type for VM disks
    pvesm add rbd ceph-rbd \
        --pool rbd \
        --nodes r630-01 \
        --content images \
        --monhost 192.168.11.10:6789,192.168.11.11:6789 \
        --username admin 2>&1 || {
        echo "⚠️  Failed to add via pvesm. Trying alternative method..."
        echo ""
        echo "Alternative: Add via Web UI:"
        echo "   Datacenter → Storage → Add → RBD"
        echo "   ID: ceph-rbd"
        echo "   Pool: rbd"
        echo "   Nodes: r630-01"
        echo "   Content: images"
        echo ""
        echo "Or via API (from your workstation):"
        echo "   curl -k -X POST -H \"Authorization: PVEAPIToken=...\" \\"
        echo "     \"https://192.168.11.11:8006/api2/json/storage\" \\"
        echo "     -d \"storage=ceph-rbd&type=rbd&pool=rbd&nodes=r630-01&content=images&monhost=192.168.11.10:6789,192.168.11.11:6789&username=admin\""
        exit 1
    }
    echo "✅ RBD storage added successfully"
fi

echo ""
echo "═══════════════════════════════════════════════════════════════════════════════"
echo "  VERIFICATION"
echo "═══════════════════════════════════════════════════════════════════════════════"
echo ""
echo "Checking storage status..."
pvesm status | grep -E "ceph-rbd|NAME" || echo "Storage not found in pvesm status"

echo ""
echo "Checking RBD pool status..."
ceph osd pool ls | grep "^rbd$" && echo "✅ RBD pool exists" || echo "❌ RBD pool not found"

echo ""
echo "✅ Setup complete! Storage 'ceph-rbd' should now be available for VM deployment."
echo ""
echo "📝 Next Steps:"
echo "   Update your VM configurations to use 'ceph-rbd' instead of 'ceph-fs'"
echo "   Example: storage: \"ceph-rbd\""

