#!/bin/bash
# Script to wipe all 6x 250GB drives AND create Ceph OSDs on them
# Run on R630-01 as root
# WARNING: This will destroy all data on these drives!

set -e

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

# Drives to process
DRIVES=("sdc" "sdd" "sde" "sdf" "sdg" "sdh")

echo "=========================================="
echo "Wipe All 250GB Drives and Create Ceph OSDs"
echo "=========================================="
echo ""
echo -e "${RED}WARNING: This will destroy ALL data on these drives!${NC}"
echo "Drives to process: ${DRIVES[@]}"
echo ""
echo "This script will:"
echo "  1. Remove drives from ubuntu-vg"
echo "  2. Wipe all 6 drives"
echo "  3. Create Ceph OSDs on all 6 drives"
echo ""
read -p "Are you sure you want to continue? (type 'yes' to confirm): " confirm

if [ "$confirm" != "yes" ]; then
    echo "Aborted"
    exit 0
fi

echo ""
echo -e "${BLUE}=== Step 1: Removing from ubuntu-vg ===${NC}"
echo "-----------------------------------"

# Check if ubuntu-vg exists
if vgs ubuntu-vg &>/dev/null; then
    echo "Unmounting logical volumes..."
    umount /dev/ubuntu-vg/* 2>/dev/null || true
    
    echo "Deactivating ubuntu-vg..."
    vgchange -a n ubuntu-vg 2>/dev/null || true
    
    echo "Removing physical volumes from ubuntu-vg..."
    for drive in "${DRIVES[@]}"; do
        if pvs 2>/dev/null | grep -q "/dev/${drive}3.*ubuntu-vg"; then
            echo "  Removing /dev/${drive}3..."
            pvremove "/dev/${drive}3" -y -ff 2>/dev/null || true
        fi
    done
    echo -e "${GREEN}  ✓ Removed from ubuntu-vg${NC}"
else
    echo -e "${YELLOW}  ubuntu-vg not found${NC}"
fi
echo ""

echo -e "${BLUE}=== Step 2: Wiping All 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}  Wiping /dev/$drive...${NC}"
    
    # Unmount any partitions
    umount /dev/${drive}* 2>/dev/null || true
    
    # Wipe filesystem signatures
    wipefs -a "/dev/$drive" 2>/dev/null || true
    
    # Zero out first 100MB
    dd if=/dev/zero of="/dev/$drive" bs=1M count=100 status=progress 2>/dev/null || true
    
    echo -e "${GREEN}    ✓ /dev/$drive wiped${NC}"
done
echo ""

echo -e "${BLUE}=== Step 3: Creating Ceph OSDs ===${NC}"
echo "-----------------------------------"

osd_count=0
for drive in "${DRIVES[@]}"; do
    if [ ! -b "/dev/$drive" ]; then
        continue
    fi
    
    echo -e "${BLUE}  Creating OSD on /dev/$drive...${NC}"
    
    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 "${RED}    ✗ Failed to create OSD on /dev/$drive${NC}"
        echo "    Check /tmp/ceph-osd-${drive}.log for details"
    fi
    echo ""
done

echo "=========================================="
echo -e "${GREEN}Process Complete!${NC}"
echo "=========================================="
echo ""
echo "Created $osd_count OSD(s) out of ${#DRIVES[@]} drives"
echo ""

echo "Verifying Ceph status..."
echo "OSD Tree:"
ceph osd tree
echo ""
echo "Ceph Health:"
ceph health
echo ""
echo "OSD Details:"
ceph osd df
echo ""

if [ "$osd_count" -gt 0 ]; then
    echo -e "${GREEN}✓ Successfully created $osd_count OSD(s)!${NC}"
    echo ""
    echo "You should now have at least 3 OSDs, which fixes the TOO_FEW_OSDS error."
else
    echo -e "${RED}✗ No OSDs were created. Check logs above for errors.${NC}"
fi
echo ""

