Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #------------------------------------------------------------
- # 1C:Enterprise server configuration parameters
- #------------------------------------------------------------
- # 1C:Enterprise server keytab file.
- # default - usr1cv83.keytab file in 1C:Enterprise server
- # installation directory
- #
- #SRV1CV8_KEYTAB=
- # Number of the cluster port created by default during first
- # launch of ragent
- #
- # default - 1540
- #
- #SRV1CV8_PORT=
- # Number of cluster agent main port. This port is used by the
- # cluster console to address the central server. Cluster agent
- # port is also specified as the IP port of the working server.
- #
- # default - 1541
- #
- #SRV1CV8_REGPORT=
- # Port range for connection pool
- # example values:
- # 45:49
- # 45:67,70:72,77:90
- #
- # default - 1560:1691
- #
- #SRV1CV8_RANGE=
- # 1C:Enterprise server configuration debug mode
- # 0 - default - off
- # 1 - on
- #
- #SRV1CV8_DEBUG=
- # Path to directory with claster data
- # default - $HOMEDIR/.1cv83/1C/1Cv83
- #
- #SRV1CV8_DATA=
- # Security level:
- # 0 - default - unprotected connections
- # 1 - protected connections only for the time of user
- # authentication
- # 2 - permanently protected connections
- #
- #SRV1CV8_SECLEV=
- #------------------------------------------------------------
- # end of config
- #------------------------------------------------------------
- #########################################
- ########### init starts here ############
- #########################################
- # chkconfig: 35 74 36
- # description: Starts and stops the 1C:Enterprise daemons
- #------------------------------------------------------------
- # global macros. generated by install script
- #------------------------------------------------------------
- G_CONF_STYLE=deb
- G_VER_ARCH=x86_64
- G_VER_MAJOR=8
- G_VER_MINOR=3
- G_VER_BUILD=6
- G_VER_RELEASE=2390
- G_BINDIR="/opt/1C/v${G_VER_MAJOR}.${G_VER_MINOR}/${G_VER_ARCH}"
- #------------------------------------------------------------
- G_VER_SHORT=${G_VER_MAJOR}.${G_VER_MINOR}
- G_TITLE="1C:Enterprise ${G_VER_SHORT} server"
- #------------------------------------------------------------
- # this values can be passed from outside, so perform "z-check"
- #------------------------------------------------------------
- [ -z "$SRV1CV8_USER" ] && SRV1CV8_USER=usr1cv83
- [ -z "$SRV1CV8_BINDIR" ] && SRV1CV8_BINDIR="$G_BINDIR"
- [ -z "$SRV1CV8_PIDFILE" ] && SRV1CV8_PIDFILE="/var/run/srv1cv${G_VER_MAJOR}${G_VER_MINOR}.pid"
- [ -z "$SRV1CV8_KEYTAB" ] && SRV1CV8_KEYTAB="$SRV1CV8_BINDIR/$SRV1CV8_USER.keytab"
- [ -z "$SRV1CV8_WAITSTART" ] && SRV1CV8_WAITSTART=5
- [ -z "$SRV1CV8_WAITSTOP" ] && SRV1CV8_WAITSTOP=5
- #------------------------------------------------------------
- # builds ragent's command line from configuration parameters
- #------------------------------------------------------------
- function buildCommandLine() {
- local cmdline="$SRV1CV8_BINDIR/ragent -daemon"
- [ ! -z "$SRV1CV8_PORT" ] && cmdline="$cmdline -port $SRV1CV8_PORT"
- [ ! -z "$SRV1CV8_REGPORT" ] && cmdline="$cmdline -regport $SRV1CV8_REGPORT"
- [ ! -z "$SRV1CV8_DATA" ] && cmdline="$cmdline -d \"$SRV1CV8_DATA\""
- [ ! -z "$SRV1CV8_RANGE" ] && cmdline="$cmdline -range $SRV1CV8_RANGE"
- [ ! -z "$SRV1CV8_SECLEV" ] && cmdline="$cmdline -seclev $SRV1CV8_SECLEV"
- [ "x$SRV1CV8_DEBUG" == "x1" ] && cmdline="$cmdline -debug"
- echo $cmdline
- }
- #------------------------------------------------------------
- # checks if process with passed pid exists
- #------------------------------------------------------------
- function checkpid() {
- ps -p $* > /dev/null
- }
- #------------------------------------------------------------
- # waits SRV1CV8_WAITSTOP seconds for process termination,
- # then kills it
- #------------------------------------------------------------
- function delayedkill() {
- local mypid=$1
- local delay=$2
- kill $mypid 2>/dev/null
- checkpid $mypid && sleep $delay || return 0
- if checkpid $mypid; then
- kill -9 $mypid
- logWarning "Process refused to die... So it was killed. May be you should increase SRV1CV8_WAITSTOP variable?";
- fi
- return 0
- }
- #------------------------------------------------------------
- # extracts specified param from value from given command-line
- #------------------------------------------------------------
- function extractParam() {
- local param=$1
- shift
- local tmp=$*
- if [ ! -z "$*" ]; then
- if [ ${tmp:0:1} == "-" ]; then
- tmp=${tmp:1}
- local val=${tmp#*$param }
- # if old and new strings are equal
- # we don't have needed param in cmdline
- # so return
- [ "$val" == "$tmp" ] && return;
- val=${val%% -*}
- echo "$val"
- fi
- fi
- }
- #------------------------------------------------------------
- # call it when something fails
- #------------------------------------------------------------
- function failure() {
- [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo FAILED
- return 0
- }
- #------------------------------------------------------------
- # extracts data dir from ragent's command line or (if it's
- # empty), builds it from users's home dir
- #------------------------------------------------------------
- function getDataDir() {
- local cmdline=`getRagentRealCmdLine`
- local datadir="$SRV1CV8_DATA"
- [ ! -z "$cmdline" ] && datadir=`extractParam d $cmdline`
- if [ -z "$datadir" ]; then
- local line=`grep ^$SRV1CV8_USER: /etc/passwd`
- local homedir=${line#*:*:*:*:*:}
- homedir=${homedir%:*}
- datadir="$homedir/.1cv${G_VER_MAJOR}${G_VER_MINOR}/1C/1Cv${G_VER_MAJOR}${G_VER_MINOR}"
- fi
- echo $datadir
- }
- #------------------------------------------------------------
- # returns ragent configuration debug flag
- #------------------------------------------------------------
- function getDebugStatus() {
- local cmdline=`getRagentRealCmdLine`
- local debugFlag="$SRV1CV8_DEBUG"
- echo "$cmdline" | grep "\-debug" >/dev/null && debugFlag="1"
- [ ! -z "$debugFlag" ] && echo $debugFlag || echo "0"
- }
- #------------------------------------------------------------
- # returns ragent port range
- #------------------------------------------------------------
- function getPortRange() {
- local cmdline=`getRagentRealCmdLine`
- local range="$SRV1CV8_RANGE"
- [ ! -z "$cmdline" ] && range=`extractParam range $cmdline`
- [ ! -z "$range" ] && echo $range || echo 1560:1591
- }
- #------------------------------------------------------------
- # returns ragent main port
- #------------------------------------------------------------
- function getRagentPort() {
- local cmdline=`getRagentRealCmdLine`
- local port="$SRV1CV8_PORT"
- [ ! -z "$cmdline" ] && port=`extractParam port $cmdline`
- [ ! -z "$port" ] && echo $port || echo 1540
- }
- #------------------------------------------------------------
- # get's running ragent command-line
- #------------------------------------------------------------
- function getRagentRealCmdLine() {
- local cmdline=`buildCommandLine`
- local mypid=`getRagentPid "$SRV1CV8_USER" "$cmdline"`
- local result=""
- [ ! -z "$mypid" ] && result=`ps --pid $mypid -o cmd= | sed -e "s/.*-daemon//"`
- echo "$result"
- }
- #------------------------------------------------------------
- # tries to get pid of ragent process, started by this script
- #------------------------------------------------------------
- function getRagentPid() {
- local user=$1
- shift
- # remove quotes in passed CMDLINE because
- # command line of process doesn't contains'em
- # even if they were passed.
- local cmdline=`echo $* | sed -e "s/\"//g"`
- ps -C ragent -opid=,user=,cmd= | while read curline; do
- local curPID=`echo $curline | sed -e "s/ .*//"`
- local curline=`echo $curline | sed -e "s/$curPID //"`
- local curUSR=`echo $curline | sed -e "s/ .*//"`
- local curCMD=`echo $curline | sed -e "s/$curUSR //"`
- if [ "$curCMD" == "$cmdline" ] && [ "$curUSR" == "$user" ]; then
- echo $curPID
- fi
- done
- }
- #------------------------------------------------------------
- # returns ragent reg port
- #------------------------------------------------------------
- function getRegPort() {
- local cmdline=`getRagentRealCmdLine`
- local regport="$SRV1CV8_REGPORT"
- [ ! -z "$cmdline" ] && regport=`extractParam regport $cmdline`
- [ ! -z "$regport" ] && echo $regport || echo 1541
- }
- #------------------------------------------------------------
- # returns name of script
- #------------------------------------------------------------
- function getScriptName() {
- local myname=`basename $0`
- [ ${myname:0:1} = "S" -o ${myname:0:1} = "K" ] && myname=${myname:3}
- echo $myname
- }
- #------------------------------------------------------------
- # returns ragent security level
- #------------------------------------------------------------
- function getSecLevel() {
- local cmdline=`getRagentRealCmdLine`
- local seclev="$SRV1CV8_SECLEV"
- [ ! -z "$cmdline" ] && seclev=`extractParam seclev $cmdline`
- [ ! -z "$seclev" ] && echo $seclev || echo 0
- }
- #------------------------------------------------------------
- # displays useful information about ragent
- #------------------------------------------------------------
- function info() {
- echo "$G_TITLE info:"
- echo -n " Data dir: " && getDataDir
- echo -n " Main port: " && getRagentPort
- echo -n " RegPort: " && getRegPort
- echo -n "Port range: " && getPortRange
- echo -n "Debug mode: " && getDebugStatus
- echo -n "Sec. level: " && getSecLevel
- return 0
- }
- #------------------------------------------------------------
- # check if our ragent running
- #------------------------------------------------------------
- function isRagentRunning() {
- local mypid=`getRagentPid $*`
- [ ! -z "$mypid" ] && checkpid "$mypid"
- }
- #------------------------------------------------------------
- # talks to stderr, and then logs failure to stdout
- #------------------------------------------------------------
- function logError() {
- echo -n " Error: " >&2
- echo $* >&2
- failure
- }
- #------------------------------------------------------------
- # put some additional non-critical debug info to stderr
- #------------------------------------------------------------
- function logWarning() {
- echo -n "Warning: " >&2
- echo $* >&2
- }
- #------------------------------------------------------------
- # restarts server
- #------------------------------------------------------------
- function restart() {
- stop
- start
- }
- #------------------------------------------------------------
- # displays command-line help
- #------------------------------------------------------------
- function showUsage() {
- local myname=$1
- echo "Usage: $myname [-c config] start|stop|restart|status|info"
- }
- #------------------------------------------------------------
- # starts ragent
- #------------------------------------------------------------
- function start() {
- echo -n "Starting $G_TITLE: "
- if [ ! -z "$SRV1CV8_DATA" ]; then
- if [ -e "$SRV1CV8_DATA" ]; then
- [ ! -d "$SRV1CV8_DATA" ] && { logError "SRV1CV8_DATA \"$SRV1CV8_DATA\" is a file, not a directory!"; return 0; }
- fi
- fi
- [ ! -f "$SRV1CV8_BINDIR/ragent" ] && { logError "ragent file does not exists!" ; return 0; }
- [ ! -x "$SRV1CV8_BINDIR/ragent" ] && { logError "ragent file is not executable!"; return 0; }
- local cmd2run=`buildCommandLine`
- if isRagentRunning "$SRV1CV8_USER" "$cmd2run"; then
- logWarning "already started!";
- else
- # run our process
- if [ -z "$SRV1CV8_USER" ]; then
- export KRB5_KTNAME="$SRV1CV8_KEYTAB"
- $cmd2run
- else
- su -s /bin/bash - "$SRV1CV8_USER" -c "KRB5_KTNAME=\"$SRV1CV8_KEYTAB\" $cmd2run"
- fi
- sleep $SRV1CV8_WAITSTART # wait a bit before check
- ! isRagentRunning "$SRV1CV8_USER" "$cmd2run" && { logError "service failed to start!"; return 0; }
- # check if pidfile exists and remove it if neccessary
- [ -f "$SRV1CV8_PIDFILE" ] && logWarning "pid file existed on server start. it can mean that last run failed..."
- fi
- local mypid=`getRagentPid $SRV1CV8_USER $cmd2run`
- echo ${mypid} > "$SRV1CV8_PIDFILE"
- success
- return 0
- }
- #------------------------------------------------------------
- # displays ragent status
- #------------------------------------------------------------
- function status() {
- echo "$G_TITLE status:"
- echo -n "Init script: "
- if [ -f "$SRV1CV8_PIDFILE" ]; then
- echo STARTED.
- local cmd2run=`buildCommandLine`
- echo -n " Ragent: "
- ! isRagentRunning "$SRV1CV8_USER" "$cmd2run" && echo -n "NOT "
- echo "RUNNING."
- else
- echo NOT STARTED.
- fi
- return 0
- }
- #------------------------------------------------------------
- # stops ragent
- #------------------------------------------------------------
- function stop() {
- echo -n "Stopping $G_TITLE: "
- [ -f "$SRV1CV8_PIDFILE" ] && rm "$SRV1CV8_PIDFILE"
- local cmd2run=`buildCommandLine`
- if isRagentRunning "$SRV1CV8_USER" "$cmd2run"; then
- mypid=`getRagentPid $SRV1CV8_USER $cmd2run`
- if [ ! -z "$mypid" ]; then
- local childpids=`ps --ppid ${mypid} -o pid=`
- delayedkill $mypid $SRV1CV8_WAITSTOP
- for childpid in $childpids; do
- delayedkill $childpid $SRV1CV8_WAITSTOP
- done
- fi
- else
- logWarning "server not running!"
- fi
- success
- return
- }
- #------------------------------------------------------------
- # functions says something succeded
- #------------------------------------------------------------
- function success() {
- [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo OK
- return 0
- }
- #------------------------------------------------------------
- # script's main function
- #------------------------------------------------------------
- function main() {
- local myname=`getScriptName`
- local dirname=`dirname $0`
- local realdir=$(cd "$dirname"; pwd)
- # check if someone passed different config throug command-line option
- local configFile=
- [ "$1" == "-c" ] && { configFile="$2"; shift 2; }
- local action=
- case $1 in
- '')
- showUsage $myname
- return 1
- ;;
- --help)
- showUsage $myname
- return 0
- ;;
- info)
- action=info
- ;;
- restart)
- action=restart
- ;;
- start)
- action=start
- ;;
- status)
- action=status
- ;;
- stop)
- action=stop
- ;;
- *)
- showUsage $myname
- return 1;
- ;;
- esac
- # check for config file existence
- if [ -z "$configFile" ]; then
- configFile="/etc/sysconfig/$myname"
- [ -e "$configFile" ] && source "$configFile"
- else
- [ -f "$configFile" ] && source "$configFile"
- fi
- $action
- }
- #invoke function main
- main $*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement