#!/bin/bash
#set -x 
source /usr/share/ceph-tools/_ct-common

help_msg () {
  printf """
usage: $0 [-h] $CT_HELP_DESCR HOSTS
 
Description         Add OSDs on 3 servers
 
Optional
$CT_HELP_MSG
  -p PREFIX, --PREFIX
                    Prefix off Host

Positional
  HOSTS             List of Hosts (must be equal to 3)

Example
  $0 $CT_HELP_EXAMPLE -p bdb2-cephosd- {1..3}
"""
}

declare -a Z1
declare -a Z2
declare -a Z3

get_osd_by_host() {
  _OSDS=$(ceph osd status ${1} | grep "new" | awk '{print $2}')
  for OSD in $_OSDS
  do
    OSDS="${OSDS} $OSD"
  done
  echo $OSDS
}


PREFIX="bdb2-cephosd-"

ct_help_min $# 3
ct_help $@
shift $?
while [ $# -ge 3 ]; do
    key="$1"
    case $key in
        -p|--prefix)
            PREFIX=$2
            shift 2
            ;;
        *)
            if [ $# -ne 3 ]
            then
              echo "The number of hosts must be equal to 3"
              ct_on_exit 1
            fi
            Z1=($(get_osd_by_host ${PREFIX}${1}))
            Z2=($(get_osd_by_host ${PREFIX}${2}))
            Z3=($(get_osd_by_host ${PREFIX}${3}))
            break
            ;;
    esac
done

echo ${1}: adding OSDs ${Z1[@]}
echo ${2}: adding OSDs ${Z2[@]}
echo ${3}: adding OSDs ${Z3[@]}

for o in ${!Z1[@]}; do
  ct_healthy_wait
  ID=${Z1[${o}]}
  ceph osd in $ID
  sleep 5
  ID=${Z2[${o}]}
  ceph osd in $ID
  sleep 5
  ID=${Z3[${o}]}
  ceph osd in $ID
  sleep 20
done
ct_healthy_wait
echo End adding OSDs for hosts $@

ct_on_exit 0