#!/bin/bash
# Complete Ceph setup script - run on each node
set -e

HOSTNAME=$(hostname -s)
PUBLIC_NETWORK="192.168.11.0/24"
CLUSTER_NETWORK="192.168.11.0/24"

echo "=== Ceph Setup on $HOSTNAME ==="

# Step 1: Install Ceph
echo "Installing Ceph packages..."
apt-get update
apt-get install -y ceph ceph-common ceph-base

# Step 2: Initialize cluster (only on ml110-01)
if [ "$HOSTNAME" = "ml110-01" ]; then
    if [ ! -f "/etc/pve/ceph.conf" ]; then
        echo "Initializing Ceph cluster..."
        pveceph init --network $PUBLIC_NETWORK --cluster-network $CLUSTER_NETWORK
    else
        echo "Ceph already initialized"
    fi
fi

# Step 3: Create monitor
echo "Creating monitor..."
pveceph mon create 2>&1 || echo "Monitor may already exist"

# Step 4: Start monitor
echo "Starting monitor service..."
systemctl enable ceph-mon@$HOSTNAME
systemctl start ceph-mon@$HOSTNAME
sleep 5

# Step 5: Verify
echo "Checking status..."
systemctl status ceph-mon@$HOSTNAME --no-pager | head -15

echo ""
echo "=== Setup Complete ==="
echo "Run 'ceph quorum_status' to verify quorum"
echo "Run 'ceph -s' to check cluster status"

