#!/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 "-k enable feature KAEzlib"
         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?" 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
	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
	
	if [ $master == 1 ];then
		echo|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
	pip3 install prettytable
	
	if [ $master == 1 ];then
		cd /etc/ceph
        	git clone https://github.com/ceph/ceph-deploy.git
                python ceph-deploy/setup.py
		command="ceph-deploy new ""$serverlist"
	        $command
        	rep -q "public_network" /etc/ceph/ceph.conf
	       	if [ $? -ne 0 ]; then
        	        sed -i '/\[global\]/a\public_network = 192.168.3.0/24' ceph.conf
	        fi
        	grep -q "cluster_network" /etc/ceph/ceph.conf
	        if [ $? -ne 0 ]; then
        	        sed -i '/\[global\]/a\cluster_network = 192.168.4.0/24' ceph.conf
	        fi
	fi

	if [ $fio == 1 ];then
	        yum -y install librbd-devel
        	cd /opt/boostkit-ceph
		tar -zxvf fio-3.26.tar.gz
	        cd fio-3.26/
        	./configure
	        make && make install
	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 KaeZlib() {
        if [ $disable == 0 ]; then
        	sh /opt/boostkit-ceph/KAEzlib/deploy_kae.sh install
		grep -q "compressor_zlib_winsize" /etc/ceph/ceph.conf
        	if [ $? -ne 0 ]; then
	                sed -i '/\[global\]/a\compressor_zlib_winsize = 15' /etc/ceph/ceph.conf
        	fi
	else
        	sh /opt/boostkit-ceph/KAEzlib/deploy_kae.sh uninstall
		sed -e '/compressor_zlib_winsize/d' /etc/ceph/ceph.conf
	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 -e '/osd_ec_partial_read/d' /etc/ceph/ceph.conf
		sed -e '/osd_ec_partial_write/d' /etc/ceph/ceph.conf
		sed -e '/osd_ec_partial_update/d' /etc/ceph/ceph.conf
		sed -e '/osd_ec_zero_opt/d' /etc/ceph/ceph.conf
                sed -e '/bluestore_kpsallocator_enable/d' /etc/ceph/ceph.conf
	fi
}

function swc() {
        if [ $disable == 0 ]; then
                ret=`lsmod|grep -w bcache`
		if [ $? -eq 0 ]; then
			sh /opt/boostkit-ceph/swc.sh
		fi
		systemctl start kps-swc
                systemctl enable kps-swc
        else
                systemctl stop kps-swc
                systemctl disable kps-swc
        fi
}

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

		i)
			InitCeph
			exit
			;;
		R)
                        disable=1
                        ;;
		p)
			IoPassThrough
			exit
			;;
		k)
			KaeZlib
			exit
			;;
		e)
			EcTurbo
			exit
			;;
		s)
			swc
			exit
			;;
		?)
			PrintHelp
			exit
			;;
	esac

done
