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

#Copyright (c) KylinSoft Co., Ltd. [2021-2025]. All rights reserved.
#File name: uxdb
#Author: ZouZhimin
#Version: 2
#Description: Manages uxsino database as Linux-HA resource 
#Date: 2022.10.27
#Others: For uxsino version 7.0
#Function List: 
#1.uxsino_status(): report whether the database is running
#2.uxsino_monitor():report whether the database seems to be working
#3.uxsino_start():start the database
#4.uxsino_stop():stop the database

#History: 
#1. Date: 2021.12.20 Author: ZouZhimin Modification:add uxdb ocf script 
#2. Date: 2022.10.27 Author: ZouZhimin Modification:delete encryption_keyparameter
#********************************************
 
#######################################################################
# Initialization:

#. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs
#. /opt/NeoShineHA/lib/ocf/resource.d/heartbeat/.ocf-shellfuncs

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

uxsql_command=`find $OCF_RESKEY_UXSINODB_HOME -name "ux_ctl"`
export PATH="/opt/uxsinodb-2.1/bin:${PATH}"
export LD_LIBRARY_PATH="/opt/uxsinodb-2.1/bin:${LD_LIBRARY_PATH}"

usage() {
  cat <<UEND
	usage: $0 (start|stop|validate-all|meta-data|monitor)

	$0 manages a DM Database as an HA resource.

	The 'start' operation starts the database.
	The 'stop' operation stops the database.
	The 'status' operation reports whether the database is running
	The 'monitor' operation reports whether the database seems to be working
	The 'validate-all' operation reports whether the parameters are valid

UEND
}

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

<longdesc lang="en">
Resource script for uxsinodb. 
It manages uxsinodb as an HA resource.
</longdesc>
<shortdesc lang="en">uxsinodb resource agent</shortdesc>

<parameters>

<parameter name="UXSINODB_HOME" unique="0" required="1">
<longdesc lang="en">
Directory containing uxsino
</longdesc>
<shortdesc lang="en">uxsinodb installdir</shortdesc>
<content type="string" default="/opt/uxdbinstall/dbsql" />
</parameter>

<parameter name="DATA_HOME" unique="0" required="1">
<longdesc lang="en">
uxsinodb data directory
</longdesc>
<shortdesc lang="en">uxsinodb data directory</shortdesc>
<content type="string" default="/opt/uxdbinstall/dbsql/data/dbhome_1" />
</parameter>


</parameters>
<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" start-delay="10" />
<action name="validate-all" timeout="5" />
<action name="meta-data" timeout="5" />
</actions>
</resource-agent>
END
}

uxsino_validate() {
# checking the parameters
    if [ ! -f $OCF_RESKEY_UXSINODB_HOME/bin/ux_ctl -o ! -x $OCF_RESKEY_UXSINODB_HOME/bin/ux_ctl ]; then
	exit $OCF_ERR_ARGS;
    fi
}

uxsino_status() {
	pid_num_uxsino=`ps -ef | grep $OCF_RESKEY_UXSINODB_HOME | grep -v grep | wc -l`
	
		if [ $pid_num_uxsino -ge 1 ]; then
             ocf_log debug "uxsino server is running"
             return $OCF_SUCCESS
        else 
             ocf_log debug "uxsino server is not runnning"
             return $OCF_NOT_RUNNING
        fi
}
uxsino_monitor() {
    uxsino_status
    rc=$?
    if [ ! $rc -eq 0 ]; then
        ocf_log err "uxsino server not running"
        return $OCF_NOT_RUNNING
     else
	return $OCF_SUCCESS
    fi
}

uxsino_start() {
    uxsino_status
    if [ $? -eq 0 ]; then
	ocf_log info "uxsino server already running"
	return $OCF_SUCCESS
    else
        # touch $OCF_RESKEY_UXSINODB_HOME/bin/encryption_key
        # echo $OCF_RESKEY_encryption_key > $OCF_RESKEY_UXSINODB_HOME/bin/encryption_key
        # chown -R uxdb:uxdb $OCF_RESKEY_UXSINODB_HOME/bin/encryption_key
        su - uxdb -c "$OCF_RESKEY_UXSINODB_HOME/bin/ux_ctl -D $OCF_RESKEY_DATA_HOME start"
        ret=$?
        sleep 3
	    uxsino_status
        if [ $ret -eq 0 -a $pid_num_uxsino -ge 1 ]; then
            su - uxdb -c "echo $OCF_RESKEY_UXSINODB_HOME >/var/log/uxsino.log"
            ocf_log debug "uxsino server start ok"
            echo "uxsino server start ok"
            return $OCF_SUCCESS
        else
            ocf_log debug "uxsino server start failed"
            echo "uxsino server start failed"
            return $OCF_ERR_GENERIC
        fi
    fi	
}

uxsino_stop() {
   uxsino_status
   if [ $? -eq 7 ];then
        ocf_log debug "uxdb server already stopped"
        return $OCF_SUCCESS
   else
		su - uxdb -c "$OCF_RESKEY_UXSINODB_HOME/bin/ux_ctl -D $OCF_RESKEY_DATA_HOME stop"
   fi
   uxsino_status
   if [ $? -eq 7 ]; then
        ocf_log debug "uxdb server stop ok"
        echo "uxdb server stop ok"
        # su - uxdb -c "rm -rf $OCF_RESKEY_UXSINODB_HOME/bin/encryption_key"
        return $OCF_SUCCESS
   else
        ocf_log debug "uxdb server stop failed"
        return $OCF_ERR_GENERIC
   fi
}

# What kind of method was invoked?
case "$1" in
  start)        uxsino_validate; 
		uxsino_start;;
  stop)		uxsino_stop;;
  status)	uxsino_status;;
  monitor)	uxsino_monitor;;

  meta-data)	meta_data; exit $OCF_SUCCESS;;
  validate-all)	uxsino_validate; exit $OCF_SUCCESS;;

 *)		usage
		exit $OCF_ERR_UNIMPLEMENTED;;
esac
exit $?

