#!/bin/bash
# Create Ceph OSDs on all 6x 250GB drives
# Run on R630-01 as root

set -e

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

DRIVES=("sdc" "sdd" "sde" "sdf" "sdg" "sdh")

echo "=========================================="
echo "Create Ceph OSDs on All 6x 250GB Drives"
echo "=========================================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo -e "${RED}ERROR: Please run as root${NC}"
    exit 1
fi

echo -e "${BLUE}=== Step 1: Remove Partitions from Drives ===${NC}"
echo "-----------------------------------"

for drive in "${DRIVES[@]}"; do
    if [ ! -b "/dev/$drive" ]; then
        echo -e "${YELLOW}  /dev/$drive not found, skipping...${NC}"
        continue
    fi
    
    echo -e "${BLUE}  Preparing /dev/$drive...${NC}"
    
    # Unmount any partitions
    umount /dev/${drive}* 2>/dev/null || true
    
    # Remove partitions/filesystem signatures
    echo "    Removing partitions and filesystem signatures..."
    wipefs -a "/dev/$drive" 2>/dev/null || true
    
    echo -e "${GREEN}    ✓ /dev/$drive prepared${NC}"
done
echo ""

echo -e "${BLUE}=== Step 2: Create Ceph OSDs ===${NC}"
echo "-----------------------------------"

osd_count=0
failed_drives=()

for drive in "${DRIVES[@]}"; do
    if [ ! -b "/dev/$drive" ]; then
        continue
    fi
    
    echo -e "${BLUE}  Creating OSD on /dev/$drive...${NC}"
    
    # Try to create OSD
    if ceph-volume lvm create --data "/dev/$drive" 2>&1 | tee /tmp/ceph-osd-${drive}.log; then
        echo -e "${GREEN}    ✓ OSD created on /dev/$drive${NC}"
        osd_count=$((osd_count + 1))
    else
        echo -e "${YELLOW}    ⚠ OSD creation had issues${NC}"
        failed_drives+=("$drive")
        echo "    Check /tmp/ceph-osd-${drive}.log for details"
    fi
    echo ""
done

echo "=========================================="
echo -e "${GREEN}OSD Creation Summary${NC}"
echo "=========================================="
echo ""
echo "Successfully created: $osd_count OSD(s) out of ${#DRIVES[@]} drives"

if [ ${#failed_drives[@]} -gt 0 ]; then
    echo -e "${YELLOW}Failed drives: ${failed_drives[@]}${NC}"
fi
echo ""

echo -e "${BLUE}=== Step 3: Verify Ceph Status ===${NC}"
echo "-----------------------------------"

echo "OSD Tree:"
ceph osd tree 2>/dev/null || echo -e "${YELLOW}Cannot get OSD tree${NC}"
echo ""

echo "Ceph Health:"
ceph health 2>/dev/null || echo -e "${YELLOW}Cannot get health${NC}"
echo ""

echo "OSD Details:"
ceph osd df 2>/dev/null || echo -e "${YELLOW}Cannot get OSD details${NC}"
echo ""

OSD_COUNT=$(ceph osd tree 2>/dev/null | grep -c "osd\." || echo "0")
echo "Current OSD count: $OSD_COUNT"

if [ "$OSD_COUNT" -ge 3 ]; then
    echo -e "${GREEN}  ✓ SUCCESS: Have 3+ OSDs (fixes TOO_FEW_OSDS error)${NC}"
else
    echo -e "${YELLOW}  ⚠ Still need more OSDs (currently have $OSD_COUNT, need 3+)${NC}"
fi
echo ""

