#!/bin/bash

function PrintHelp() {
	 echo "usage: boostkit-ceph [options]"
	 echo "-i init ceph. we suggest to execute it first after installation."
	 echo "-p enable feature I/O passthrough"
         echo "-e enable feature EC Turbo"
	 echo "-R add this option to disable the corresponding feature"
}

function InitCeph() {
	while true
	do
		read -r -p "Is this master node?(y/n):" input
		case $input in
                [yY][eE][sS]|[yY])
                        master=1
              		read -r -p "Enter your servers separated by ' ':" serverlist
			servers=(${serverlist// /})
			read -r -p "Enter your clients separated by ' ':" clientlist
                        clients=(${clientlist// /})	
			break
			;;
                [nN][oO]|[nN])
                        master=0
			break
			;;
		*)
			;;
		esac
	done
	chown -R ceph:ceph /var/lib/ceph
	if [ `rpm -qa |grep compat-openssl | wc -l` -eq 0 ];then
		wget https://archives.fedoraproject.org/pub/archive/fedora/linux/releases/30/Everything/aarch64/os/Packages/c/compat-openssl10-1.0.2o-5.fc30.aarch64.rpm --no-check-certificate
		rpm -ivh compat-openssl10-1.0.2o-5.fc30.aarch64.rpm
	fi
	if [ $master == 1 ];then
		ssh-keygen -t rsa
        	for i in ${servers[@]};do ssh-copy-id $i;done
        	for i in ${clients[@]};do ssh-copy-id $i;done
	fi
	pip install /opt/boostkit-ceph/python/*
	
	if [ $master == 1 ];then
		cd /etc/ceph/ceph-deploy
		python setup.py install
		cd /etc/ceph/
		command="ceph-deploy new ""$serverlist"
	        $command
		if [[ ! $serverlist =~ " " ]];then
			sed -i '/\[global\]/a\osd_pool_default_size = 1' /etc/ceph/ceph.conf
			sed -i '/\[global\]/a\osd_pool_default_min_size = 1' /etc/ceph/ceph.conf
		fi
		ceph-deploy mon create-initial
		ceph-deploy --overwrite-conf admin $serverlist $clientlist
		ceph-deploy mgr create $serverlist
	fi
}

disable=0

function IoPassThrough() {
        if [ $disable == 0 ]; then
                systemctl start ceph-boost
                systemctl enable ceph-boost
        else
                systemctl stop ceph-boost
                systemctl disable ceph-boost
        fi
}

function EcTurbo() {
	if [ $disable == 0 ]; then
        	grep -q "osd_ec_partial_read" /etc/ceph/ceph.conf
	        if [ $? -ne 0 ]; then
        	        sed -i '/\[global\]/a\osd_ec_partial_read = true' /etc/ceph/ceph.conf
        	fi
	        grep -q "osd_ec_partial_write" /etc/ceph/ceph.conf
        	if [ $? -ne 0 ]; then
                	sed -i '/\[global\]/a\osd_ec_partial_write = true' /etc/ceph/ceph.conf
	        fi
        	grep -q "osd_ec_partial_update" /etc/ceph/ceph.conf
	        if [ $? -ne 0 ]; then
        	        sed -i '/\[global\]/a\osd_ec_partial_update = true' /etc/ceph/ceph.conf
	        fi
        	grep -q "osd_ec_zero_opt" /etc/ceph/ceph.conf
	        if [ $? -ne 0 ]; then
        	        sed -i '/\[global\]/a\osd_ec_zero_opt = true' /etc/ceph/ceph.conf
	        fi
        	grep -q "bluestore_kpsallocator_enable" /etc/ceph/ceph.conf
	        if [ $? -ne 0 ]; then
        	        sed -i '/\[global\]/a\bluestore_kpsallocator_enable = true' /etc/ceph/ceph.conf
	        fi
	else
        	sed -i '/osd_ec_partial_read/d' /etc/ceph/ceph.conf
		sed -i '/osd_ec_partial_write/d' /etc/ceph/ceph.conf
		sed -i '/osd_ec_partial_update/d' /etc/ceph/ceph.conf
		sed -i '/osd_ec_zero_opt/d' /etc/ceph/ceph.conf
                sed -i '/bluestore_kpsallocator_enable/d' /etc/ceph/ceph.conf
	fi
}

while getopts "hiRpkegs" arg
do
	case $arg in
		h)
			PrintHelp
			exit
			;;

		i)
			InitCeph
			exit
			;;
		R)
                        disable=1
                        ;;
		p)
			IoPassThrough
			exit
			;;
		e)
			EcTurbo
			exit
			;;
		?)
			PrintHelp
			exit
			;;
	esac
done
PrintHelp
