#!/bin/sh
#
#
#*************************************************

#Copyright (c) KylinSoft Co., Ltd. [2021-2025]. All rights reserved.
#File name: Sequoiadb
#Author: XuXiaojuan
#Version: 2
#Description: Manages Sequoiadb  as Linux-HA resource 
#Date: 2024.3.27
#Others: 
#Function List: 
#1.seq_status(): report whether the Sequoiadb database is running
#2.seq_monitor():report whether the Sequoiadb database seems to be working
#3.seq_start():start the Sequoiadb database
#4.seq_stop():stop the Sequoiadb database

#History: 
#1. Date: 2023.12.4 Author: XuXiaojuan Modification:add Sequoiadb ocf script
#2. Date: 2024.3.27 Author: XuXiaojuan Modification:modify seq_status() 

#*************************************************

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

seq_usage() {
	methods=`seq_methods`
	methods=`echo $methods | tr ' ' '|'`

	echo "
	usage: $0 ($methods)

	$0 manages sequoiadb as an High-Availability resource.

	The 'start' operation starts the Sequoiadb database.
	The 'stop' operation stops the Sequoiadb database.
	The 'status' operation reports whether the Sequoiadb database is running
	The 'monitor' operation reports whether the Sequoiadb database seems to be working
	The 'methods' operation lists the methods $0 supports
	The 'usage' operation displays this text
	The 'meta-data' operation returns the meta-data (in XML) of this resource script
    "
}

seq_methods() {
	echo " 
	start
	stop
	status
	monitor
	methods
	usage
	meta-data
	"
}

seq_meta_data() {
    cat <<-!
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="seq">
<version>1.0</version>

<longdesc lang="en">
OCF resource agent to manage sequoiadb as an High-Availability resource.
</longdesc>
<shortdesc lang="en">Manages sequoiadb</shortdesc>

<actions>
<action name="start" timeout="120" />
<action name="stop" timeout="120" />
<action name="status" timeout="60" />
<action name="monitor" depth="0" timeout="30" interval="10" />
<action name="meta-data" timeout="5" />
<action name="methods" timeout="5" />
<action name="usage" timeout="5" />
</actions>

</resource-agent>
!
}

seq_log() {
	seqlogger="ocf"
	seqdebug=false

	if [ $# -eq 2 -a -n "$1" -a -n "$2" ]; then
		if [ "$seqdebug" = "true" -o "$1" = "error" ]; then
			case $seqlogger in
				echo)
					echo "`date +'%b %d %H:%M:%S'`: [$1] $2";;
				ocf|*)
					ocf_log "$1" "$2";;
			esac
		fi
	fi		
}

seq_debug() {
	seq_log info "called seq_debug"	

	seq_log info "this script is run as user: `id`"
	seq_log info "...in the current working directory: `pwd`"
}

seq_start() {

	seq_log info "called seq_start"
	seq_status
	stat=$?

	case $stat in
		$OCF_SUCCESS)
			seq_log info "seq_start: sequoiadb instance already running: $stat"
			rc=$OCF_SUCCESS;;
	
		$OCF_ERR_GENERIC)
			seq_log error "seq_start: sequoiadb instance in undefined state: $stat"
			seq_debug
			rc=$OCF_ERR_GENERIC;;	

		$OCF_NOT_RUNNING)
			seq_log info "seq_start: executing 'sdbstart' now..."	
			su - sdbadmin -c "sdbstart -t all"
			stat=$?
			seq_log info "seq_start: done executing 'sdbstart': $stat"
			
			if [ $stat -eq 0 ]; then
				stat=$OCF_ERR_GENERIC
				while [ $stat -ne $OCF_SUCCESS ]; do
					seq_status
					stat=$?
				done
				seq_log info "seq_start: sequoiadb instance successfully started: $stat"
				rc=$OCF_SUCCESS
			else
				seq_log error "seq_start: starting sequoiadb instance failed: $stat"
				seq_debug
				rc=$OCF_ERR_GENERIC			
			fi
			;;

		*) 
			seq_log error "seq_start: unexpected state returned from seq_status: $stat"
			seq_debug
			rc=$OCF_ERR_UNIMPLEMENTED;;				

	esac
	return $rc
}

seq_stop() {
	seq_log info "caled seq_stop"

	seq_status
	stat=$?

	case $stat in
        $OCF_NOT_RUNNING)
			seq_log info "seq_stop: sequoiadb instance is not running: $stat"
            rc=$OCF_SUCCESS;;

        $OCF_ERR_GENERIC)
			seq_log error "seq_stop: sequoiadb instance in undefined state: $stat"
			seq_debug
            rc=$OCF_ERR_GENERIC;;

        $OCF_SUCCESS)
			seq_log info "seq_stop: running 'sdbstop -t all' now..."
			su - sdbadmin -c "sdbstop -t all"
			stat=$?
			seq_log info "seq_stop: done running 'sdbstop' now: $stat"

			if [ $stat -eq 0 ]; then
				seq_status
                stat=$?
                if [ $stat -eq $OCF_NOT_RUNNING ]; then
					seq_log info "seq_stop: sequoiadb instance successfully stopped: $stat"
                    rc=$OCF_SUCCESS
                else
					seq_log error "seq_stop: stopping sequoiadb instance failed: $stat"
                    seq_debug
                    rc=$OCF_ERR_GENERIC
                fi
            else
				seq_log error "seq_stop: stopping sequoiadb instance (by executing 'sdbstop -t all') failed: $stat"
                seq_debug
                rc=$OCF_ERR_GENERIC
            fi
            ;;
        *)
			seq_log error "seq_stop: unexpected state returned from seq_status: $stat"
			seq_debug
            rc=$OCF_ERR_UNIMPLEMENTED;;
		
	esac
	return $rc
}


seq_status() {
	
	seq_log info "called sdblist"
	stat=`su - sdbadmin -c "sdblist -l"`
	proc=`echo $stat|tail -n 1 |awk -F":" '{print $2}'`
	if [ $proc -gt 0 ];then
               seq_log info "seq_status: Seq instance running: $stat"
               rc=$OCF_SUCCESS
        elif [ $proc -eq 0 ];then
                seq_log info "seq_status: Seq instance not running: $stat"
                rc=$OCF_NOT_RUNNING
        else
                seq_log error "seq_status: Seq instance status undefined: $stat"
                rc=$OCF_ERR_GENERIC
        fi
	return $rc
}


seq_monitor() {
	
	seq_log info "called seq_monitor" 

	seq_status
	stat=$?

	case $stat in
        $OCF_NOT_RUNNING)
			seq_log info "seq_monitor: seq instance is not running: $stat"
            rc=$OCF_NOT_RUNNING;;

        $OCF_ERR_GENERIC)
			seq_log error "seq_monitor: seq instance in undefined state: $stat"
			seq_debug
            rc=$OCF_ERR_GENERIC;;

        $OCF_SUCCESS)
			seq_log info "seq_monitor: Seq instance is running (before executing sql test query)"
                    	rc=$OCF_SUCCESS
            		;;

        *)
			seq_log error "seq_monitor: unexpected state returned from seq_status: $stat"
			seq_debug
            rc=$OCF_ERR_UNIMPLEMENTED;;
		
	esac
	return $rc
}




###
#
# M A I N   S E C T I O N
#
###
case "$1" in
	usage)
		seq_usage
		exit $?;;
	meta-data)
		seq_meta_data
		exit $?;;
esac

case "$1" in

	start)	
		seq_start
		exit $?;;

	stop)	
		seq_stop
		exit $?;;

	status)	
		seq_status
		exit $?;;

	monitor)	
		seq_monitor
		exit $?;;

	methods)
		seq_methods
		exit $?;;

	*)
		seq_log error "mainsection: no or invalid command supplied: $1"
		exit $OCF_ERR_UNIMPLEMENTED;;

esac
###############################################################################
