#!/bin/bash
# integrate-ceph-proxmox.sh
# Integrates Ceph storage with Proxmox

set -euo pipefail

# Load environment variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "${SCRIPT_DIR}/../.env" ]; then
    source "${SCRIPT_DIR}/../.env"
fi

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

# Configuration
NODES=("192.168.11.10" "192.168.11.11")
NODE_HOSTNAMES=("ml110-01" "r630-01")
SSH_KEY="${SSH_KEY:-~/.ssh/sankofa_proxmox}"
CEPH_POOL="${CEPH_POOL:-rbd}"
CEPH_FS="${CEPH_FS:-cephfs}"

log() {
    echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
    exit 1
}

warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

check_ceph() {
    log "Checking Ceph cluster..."
    
    if ! ssh -i "${SSH_KEY}" -o StrictHostKeyChecking=no root@"${NODES[0]}" 'ceph -s' &>/dev/null; then
        error "Ceph cluster not accessible"
    fi
    
    info "Ceph cluster is accessible"
}

add_rbd_storage() {
    log "Adding RBD storage to Proxmox..."
    
    MON_HOSTS=$(IFS=','; echo "${NODES[*]}")
    
    for i in "${!NODES[@]}"; do
        node_ip="${NODES[$i]}"
        node_hostname="${NODE_HOSTNAMES[$i]}"
        log "Configuring RBD storage on ${node_hostname} (${node_ip})..."
        
        ssh -i "${SSH_KEY}" -o StrictHostKeyChecking=no root@"${node_ip}" << EOF
set -e

# Check if storage already exists
if pvesm status | grep -q "ceph-rbd"; then
    warn "RBD storage already exists on ${node_hostname}"
else
    # Add RBD storage
    pvesm add rbd ceph-rbd \\
        --pool ${CEPH_POOL} \\
        --monhost ${MON_HOSTS} \\
        --username admin \\
        --content images,rootdir \\
        --krbd 1
    
    info "RBD storage added on ${node_hostname}"
fi
EOF
    done
}

add_cephfs_storage() {
    log "Adding CephFS storage to Proxmox..."
    
    MON_HOSTS=$(IFS=','; echo "${NODES[*]}")
    
    for i in "${!NODES[@]}"; do
        node_ip="${NODES[$i]}"
        node_hostname="${NODE_HOSTNAMES[$i]}"
        log "Configuring CephFS storage on ${node_hostname} (${node_ip})..."
        
        ssh -i "${SSH_KEY}" -o StrictHostKeyChecking=no root@"${node_ip}" << EOF
set -e

# Check if storage already exists
if pvesm status | grep -q "ceph-fs"; then
    warn "CephFS storage already exists on ${node_hostname}"
else
    # Add CephFS storage
    pvesm add cephfs ceph-fs \\
        --monhost ${MON_HOSTS} \\
        --username admin \\
        --fsname ${CEPH_FS} \\
        --content iso,backup,snippets
    
    info "CephFS storage added on ${node_hostname}"
fi
EOF
    done
}

verify_storage() {
    log "Verifying storage configuration..."
    
    for i in "${!NODES[@]}"; do
        node_ip="${NODES[$i]}"
        node_hostname="${NODE_HOSTNAMES[$i]}"
        log "Storage on ${node_hostname} (${node_ip}):"
        ssh -i "${SSH_KEY}" -o StrictHostKeyChecking=no root@"${node_ip}" 'pvesm status | grep ceph || echo "No Ceph storage found"'
    done
}

main() {
    log "Integrating Ceph with Proxmox..."
    
    check_ceph
    add_rbd_storage
    add_cephfs_storage
    verify_storage
    
    log "Ceph-Proxmox integration complete!"
    info "Storage pools are now available in Proxmox Web UI"
}

if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
    main "$@"
fi

