#!/bin/bash
# Borrows heavily from Seán C. McCord's https://github.com/Ulexus/docker-ceph repository

ETCD_PORT=${ETCD_PORT:-4001}
ETCD="$HOST:$ETCD_PORT"
ETCD_PATH=${ETCD_PATH:-/deis/store}

HOSTNAME=`hostname`
MDS_NAME=$HOSTNAME

until confd -onetime -node $ETCD --confdir /app --interval 5 --quiet >/dev/null 2>&1 ; do
  echo "store-metadata: waiting for confd to write initial templates..."
  sleep 5
done

if ! etcdctl --no-sync -C $ETCD get ${ETCD_PATH}/filesystemSetupComplete >/dev/null 2>&1 ; then
  echo "store-metadata: The Ceph filesystem hasn't been created. Trying to obtain the lock to set up..."
  # let's rock and roll. we need to obtain a lock so we can ensure only one machine is trying to deploy the cluster
  if etcdctl --no-sync -C $ETCD mk ${ETCD_PATH}/filesystemSetupLock $HOSTNAME >/dev/null 2>&1 \
  || [[ `etcdctl --no-sync -C $ETCD get ${ETCD_PATH}/filesystemSetupLock` == "$HOSTNAME" ]] ; then
    echo "store-metadata: obtained the lock to proceed with setting up."

    PG_NUM=`etcdctl --no-sync -C $ETCD get /deis/store/pgNum`

    # even though we know setup hasn't completed, we could be upgrading an older cluster which
    # has the pools but not the filesystemSetupComplete key
    if ! ceph osd lspools | grep " data," ; then
      ceph osd pool create data ${PG_NUM}
    fi

    if ! ceph osd lspools | grep metadata ; then
      ceph osd pool create metadata ${PG_NUM}
    fi

    if ceph fs ls | grep "No filesystems enabled" ; then
      ceph fs new deis metadata data
    fi

    # mark setup as complete
    echo "store-metadata: filesystem setup complete."
    etcdctl --no-sync -C $ETCD set ${ETCD_PATH}/filesystemSetupComplete youBetcha >/dev/null
  else
    until etcdctl --no-sync -C $ETCD get ${ETCD_PATH}/filesystemSetupComplete >/dev/null 2>&1 ; do
      echo "store-metadata: waiting for another metadata to complete setup..."
      sleep 5
    done
  fi
fi

# Check to see if we are a new MDS
if [ ! -e /var/lib/ceph/mds/ceph-$MDS_NAME/keyring ]; then
  mkdir -p /var/lib/ceph/mds/ceph-${MDS_NAME}

  # See if we need to generate a key for the MDS
  if [ -e /etc/ceph/ceph.mds.keyring ]; then
    cp /etc/ceph/ceph.mds.keyring /var/lib/ceph/mds/ceph-${MDS_NAME}/keyring
  else
    # Generate the new MDS key
    ceph auth get-or-create mds.$MDS_NAME mds 'allow' osd 'allow *' mon 'allow profile mds' > /var/lib/ceph/mds/ceph-${MDS_NAME}/keyring
  fi
fi

echo "store-metadata: running..."
exec /usr/bin/ceph-mds -d -i ${MDS_NAME}
