#!/bin/bash
# Simple script to deploy a mon + osd for testing/debugging
# (based to some extent on a script by Loic Dachary mentioned here: 
#  http://dachary.org/?p=2374)
#
set -e
set -u

ACTION=${1-'all'}

MONIP=192.168.1.64
MONHOST=zmori
CEPH_DIR=/var/lib/ceph
CEPH_ETC=/etc/ceph
OSD_DATAPATH=/data1/cephdata
OSD_JOURNALPATH=/dev/sdc1

conf_create() {
    echo "write conf file"
    cat >> ${CEPH_ETC}/ceph.conf <<EOF
[global]
fsid = $(uuidgen)
mon_initial_members = ${MONHOST}
mon_host = ${MONIP}
auth cluster required = none
auth service required = none
auth client required = none
filestore_xattr_use_omap = true
osd pool default size = 1
[osd]
debug filestore = 20
debug osd = 20
debug journal = 20
debug monc = 20
EOF
}


mon_create() {
    echo "create the mon ${MONHOST}"
    ceph-mon --id ${MONHOST} --mkfs --keyring /dev/null
    start ceph-mon id=${MONHOST}
    sleep 5
    ceph -s
}


osd_create() {
    echo "create a single osd on ${MONHOST}"
    OSD_ID=$(ceph osd create)
    echo "osd has id ${OSD_ID}, add to crushmap"
    ceph osd crush add osd.${OSD_ID} 1 root=default host=${MONHOST}

    echo "make osd filesystem"
    ceph-osd --id ${OSD_ID} --mkjournal --mkfs --osd-data ${OSD_DATAPATH} --osd-journal ${OSD_JOURNALPATH}
    echo "making links for journal and osd data"
    ln -s ${OSD_JOURNALPATH} ${OSD_DATAPATH}/journal
    ln -s ${OSD_DATAPATH}  ${CEPH_DIR}/osd/ceph-${OSD_ID}

    echo "start osd ${OSD_ID}"
    start ceph-osd id=${OSD_ID}
    sleep 5
    ceph -s
}


clean() {
    echo "stopping ceph (may fail)"
    set +e
    stop ceph-osd id=0
    stop ceph-mon id=$MONHOST
    stop ceph-all
    set -e

    echo "removing ceph files"
    rm -f /etc/ceph/* /var/log/ceph/*
    rm -rf /var/lib/ceph/* /data?/cephdata/*

    echo "creating empty directories"
    cd /var/lib/ceph; sudo mkdir bootstrap-mds  bootstrap-osd mds  mon  osd  tmp; cd -
}


# main entry

if [ $USER != 'root' ]; then
    echo "Must be root to run this"
    exit 1
fi

case ${ACTION} in
    "conf") conf_create;;
    "mon") mon_create;;
    "osd") osd_create;;
    "clean") clean;;
    "all") clean
           conf_create
           mon_create
           osd_create
           ;;
esac
