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

#Copyright (c) KylinSoft Co., Ltd. [2021-2025]. All rights reserved.
#File name: GBase8s
#Author: LiangXin
#Version: 1
#Description: Manages Gbase database  as Linux-HA resource 
#Date: 2020.8.31
#Others: 
#Function List: 
#1.gb_status(): report whether the GBase8s database is running
#2.gb_monitor():report whether the GBase8s database seems to be working
#3.gb_start():start the GBase8s database
#4.gb_stop():stop the GBase8s database

#History: 
#1. Date: 2020.8.31 Author: LiangXin Modification:add GBase8s ocf script 

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

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

gb_usage() {
	methods=`gb_methods`
	methods=`echo $methods | tr ' ' '|'`

	echo "
	usage: $0 ($methods)

	$0 manages GBase Dynamic Server instance as an High-Availability resource.

	The 'start' operation starts the GBase8s database.
	The 'stop' operation stops the GBase8s database.
	The 'status' operation reports whether the GBase8s database is running
	The 'monitor' operation reports whether the GBase8s 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
    "
}

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

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

<longdesc lang="en">
OCF resource agent to manage an GBase Dynamic Server instance as an High-Availability resource.
</longdesc>
<shortdesc lang="en">Manages an GBase Dynamic Server instance</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>
!
}

gb_log() {
	gblogger="ocf"
	gbdebug=false

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

gb_debug() {
	gb_log info "called gb_debug"	

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

gb_start() {
	gb_log info "called gbase_start"
	
	gb_status
	stat=$?

	case $stat in
		$OCF_SUCCESS)
			gb_log info "gbase_start: GBase instance already running: $stat"
			rc=$OCF_SUCCESS;;
	
		# GBase8s instance in undefined state - exit with error.
		$OCF_ERR_GENERIC)
			gb_log error "gbase_start: GBase instance in undefined state: $stat"
			gb_debug
			rc=$OCF_ERR_GENERIC;;	

		# GBase8s instance not running
		$OCF_NOT_RUNNING)
			gb_log info "gbase_start: executing 'oninit' now..."	
			su - gbasedbt -c "oninit -vy"
			stat=$?
			gb_log info "gbase_start: done executing 'oninit': $stat"
			
			if [ $stat -eq 0 ]; then
				stat=$OCF_ERR_GENERIC
				while [ $stat -ne $OCF_SUCCESS ]; do
					gb_status
					stat=$?
				done
				gb_log info "gbase_start: GBase instance successfully started: $stat"
				rc=$OCF_SUCCESS
			else
				gb_log error "gbase_start: starting GBase instance failed: $stat"
				gb_debug
				rc=$OCF_ERR_GENERIC			
			fi
			;;

		# Unexpected state - return OCF_ERR_UNIMPLEMENTED error.
		*) 
			gb_log error "gbase_start: unexpected state returned from gb_status: $stat"
			gb_debug
			rc=$OCF_ERR_UNIMPLEMENTED;;				

	esac
	return $rc
}

gb_stop() {
	
	gb_log info "caled gb_stop"

	gb_status
	stat=$?

	case $stat in
        $OCF_NOT_RUNNING)
			gb_log info "gbase_stop: GBase instance is not running: $stat"
            rc=$OCF_SUCCESS;;

        $OCF_ERR_GENERIC)
			gb_log error "gbase_stop: GBase instance in undefined state: $stat"
			gb_debug
            rc=$OCF_ERR_GENERIC;;

        $OCF_SUCCESS)
			gb_log info "gbase_stop: running 'onmode -ky' now..."
			su - gbasedbt -c "onmode -ky"
			stat=$?
			gb_log info "gbase_stop: done running 'onmode -ky' now: $stat"

			if [ $stat -eq 0 ]; then
				gb_status
                stat=$?

                if [ $stat -eq $OCF_NOT_RUNNING ]; then
					gb_log info "gbase_stop: GBase instance successfully stopped: $stat"
                    rc=$OCF_SUCCESS

                else
					gb_log error "gbase_stop: stopping GBase instance failed: $stat"
                    gb_debug
                    rc=$OCF_ERR_GENERIC
                fi
				
            else
				gb_log error "gbase_stop: stopping GBase instance (by executing 'onmode -ky') failed: $stat"
                gb_debug
                rc=$OCF_ERR_GENERIC
            fi
            ;;

        *)
			gb_log error "gbase_stop: unexpected state returned from gb_status: $stat"
			gb_debug
            rc=$OCF_ERR_UNIMPLEMENTED;;
		
	esac
	return $rc
}

gb_status() {
	
	gb_log info "called gb_status"

	stat=`su - gbasedbt -c "onstat -"`
	
	case $stat in
		*"On-Line"*)
			gb_log info "gb_status: GBase instance running: $stat"
			rc=$OCF_SUCCESS;;

		*"shared memory not initialized"*)
			gb_log info "gb_status: GBase instance not running: $stat"
			rc=$OCF_NOT_RUNNING;;

		*)
			gb_log error "gb_status: GBase instance status undefined: $stat"
			rc=$OCF_ERR_GENERIC;;
	esac
	return $rc
}

gb_monitor() {
	
	gb_log info "called gb_monitor" 

	gb_status
	stat=$?

	case $stat in
        $OCF_NOT_RUNNING)
			gb_log info "gb_monitor: GBase instance is not running: $stat"
            rc=$OCF_NOT_RUNNING;;

        $OCF_ERR_GENERIC)
			gb_log error "gb_monitor: GBase instance in undefined state: $stat"
			gb_debug
            rc=$OCF_ERR_GENERIC;;

        $OCF_SUCCESS)
			gb_log info "gb_monitor: GBase instance is running (before executing sql test query)"
                    	rc=$OCF_SUCCESS
            		;;

        *)
			gb_log error "gb_monitor: unexpected state returned from gb_status: $stat"
			gb_debug
            rc=$OCF_ERR_UNIMPLEMENTED;;
		
	esac
	return $rc
}




###
#
# M A I N   S E C T I O N
#
###
source /etc/profile
case "$1" in
	usage)
		gb_usage
		exit $?;;
	meta-data)
		gb_meta_data
		exit $?;;
esac

case "$1" in

	start)	
		gb_start
		exit $?;;

	stop)	
		gb_stop
		exit $?;;

	status)	
		gb_status
		exit $?;;

	monitor)	
		gb_monitor
		exit $?;;

	methods)
		gb_methods
		exit $?;;

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

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