#!/bin/bash
# Create ceph-fs storage pool on r630-01
# This script must be run on r630-01 or via SSH

set -e

echo "═══════════════════════════════════════════════════════════════════════════════"
echo "  CREATE CEPHFS 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@r630-01.sankofa.nexus '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 MDS (Metadata Server) exists..."
MDS_COUNT=$(ceph mds stat 2>/dev/null | grep -c "up:" || echo "0")
if [ "$MDS_COUNT" = "0" ]; then
    echo "⚠️  No MDS running. Creating MDS..."
    pveceph mds create 2>&1 || {
        echo "⚠️  MDS creation failed or already exists"
        echo "Checking MDS status..."
        ceph mds stat 2>&1 | head -3
    }
else
    echo "✅ MDS is running"
    ceph mds stat 2>&1 | head -3
fi

echo ""
echo "Step 3: Checking if CephFS filesystem exists..."
CEPHFS_EXISTS=$(ceph fs ls 2>/dev/null | grep -c "ceph-fs" || echo "0")
if [ "$CEPHFS_EXISTS" = "0" ]; then
    echo "⚠️  CephFS filesystem 'ceph-fs' does not exist"
    echo ""
    echo "Step 4: Creating CephFS using pveceph (recommended method)..."
    echo "This will create the CephFS and add it to Proxmox storage automatically."
    pveceph fs create --name ceph-fs --pg_num 128 --add-storage 2>&1 || {
        echo "⚠️  pveceph fs create failed. Trying manual method..."
        echo ""
        echo "Step 4a: Checking if required pools exist..."
        DATA_POOL_EXISTS=$(ceph osd pool ls 2>/dev/null | grep -c "cephfs_data" || echo "0")
        META_POOL_EXISTS=$(ceph osd pool ls 2>/dev/null | grep -c "cephfs_metadata" || echo "0")
        
        if [ "$DATA_POOL_EXISTS" = "0" ] || [ "$META_POOL_EXISTS" = "0" ]; then
            echo "Creating required pools..."
            [ "$DATA_POOL_EXISTS" = "0" ] && ceph osd pool create cephfs_data 32 32
            [ "$META_POOL_EXISTS" = "0" ] && ceph osd pool create cephfs_metadata 16 16
        fi
        
        echo "Step 4b: Creating CephFS filesystem..."
        ceph fs new ceph-fs cephfs_metadata cephfs_data 2>&1 || {
            echo "⚠️  CephFS may already exist"
        }
        
        echo "Step 4c: Adding to Proxmox storage..."
        pvesm add cephfs ceph-fs \
            --nodes r630-01 \
            --content images,rootdir \
            --fs-name ceph-fs \
            --monhost 192.168.11.10:6789,192.168.11.11:6789 \
            --username admin 2>&1 || {
            echo "⚠️  Failed to add via pvesm. Try Web UI:"
            echo "   Datacenter → Storage → Add → CephFS"
            echo "   ID: ceph-fs, FS Name: ceph-fs, Nodes: r630-01"
        }
    }
else
    echo "✅ CephFS filesystem 'ceph-fs' already exists"
    
    # Check if it's added to Proxmox storage
    if ! pvesm status 2>/dev/null | grep -q "ceph-fs"; then
        echo ""
        echo "Step 4: Adding existing CephFS to Proxmox storage..."
        echo "Note: CephFS may not support 'images' content type for VM disks."
        echo "Trying without content types first..."
        
        # Try without content types (Proxmox will use defaults)
        pvesm add cephfs ceph-fs \
            --nodes r630-01 \
            --fs-name ceph-fs \
            --monhost 192.168.11.10:6789,192.168.11.11:6789 \
            --username admin 2>&1 || {
            echo "⚠️  Failed to add via pvesm without content types."
            echo "Trying with only rootdir (for containers)..."
            pvesm add cephfs ceph-fs \
                --nodes r630-01 \
                --content rootdir \
                --fs-name ceph-fs \
                --monhost 192.168.11.10:6789,192.168.11.11:6789 \
                --username admin 2>&1 || {
                echo "⚠️  Failed to add via pvesm. Use Web UI or API:"
                echo "   Web UI: Datacenter → Storage → Add → CephFS"
                echo "   ID: ceph-fs, FS Name: ceph-fs, Nodes: r630-01"
                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-fs&type=cephfs&nodes=r630-01&monhost=192.168.11.10:6789,192.168.11.11:6789&username=admin\""
            }
        }
    else
        echo "✅ Storage 'ceph-fs' already exists in Proxmox"
    fi
fi

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

echo ""
echo "✅ Setup complete! Storage 'ceph-fs' should now be available for VM deployment."

