Advertisement
Guest User

1c initscript

a guest
Nov 30th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 15.97 KB | None | 0 0
  1. #!/bin/bash
  2. #------------------------------------------------------------
  3. # 1C:Enterprise server configuration parameters
  4. #------------------------------------------------------------
  5.  
  6. # 1C:Enterprise server keytab file.
  7. # default - usr1cv83.keytab file in 1C:Enterprise server
  8. #           installation directory
  9. #
  10. #SRV1CV8_KEYTAB=
  11.  
  12. # Number of the cluster port created by default during first
  13. # launch of ragent
  14. #
  15. # default - 1540
  16. #
  17. #SRV1CV8_PORT=
  18.  
  19. # Number of cluster agent main port. This port is used by the
  20. # cluster console to address the central server. Cluster agent
  21. # port is also specified as the IP port of the working server.
  22. #
  23. # default - 1541
  24. #
  25. #SRV1CV8_REGPORT=                                                                                                            
  26.                                                                                                                              
  27. # Port range for connection pool                                                                                              
  28. # example values:                                                                                                            
  29. #   45:49                                                                                                                    
  30. #   45:67,70:72,77:90                                                                                                        
  31. #                                                                                                                            
  32. # default - 1560:1691                                                                                                        
  33. #
  34. #SRV1CV8_RANGE=
  35.  
  36. # 1C:Enterprise server configuration debug mode
  37. # 0 - default - off
  38. # 1 - on
  39. #
  40. #SRV1CV8_DEBUG=
  41.  
  42. # Path to directory with claster data
  43. # default - $HOMEDIR/.1cv83/1C/1Cv83
  44. #
  45. #SRV1CV8_DATA=
  46.  
  47. # Security level:
  48. # 0 - default - unprotected connections
  49. # 1 - protected connections only for the time of user
  50. #     authentication
  51. # 2 - permanently protected connections
  52. #
  53. #SRV1CV8_SECLEV=
  54.  
  55. #------------------------------------------------------------
  56. # end of config
  57. #------------------------------------------------------------
  58.  
  59. #########################################
  60. ########### init starts here ############
  61. #########################################
  62.  
  63. # chkconfig: 35 74 36
  64. # description: Starts and stops the 1C:Enterprise daemons
  65.  
  66. #------------------------------------------------------------
  67. # global macros. generated by install script
  68. #------------------------------------------------------------
  69. G_CONF_STYLE=deb
  70. G_VER_ARCH=x86_64
  71. G_VER_MAJOR=8
  72. G_VER_MINOR=3
  73. G_VER_BUILD=6
  74. G_VER_RELEASE=2390
  75. G_BINDIR="/opt/1C/v${G_VER_MAJOR}.${G_VER_MINOR}/${G_VER_ARCH}"
  76. #------------------------------------------------------------
  77.  
  78. G_VER_SHORT=${G_VER_MAJOR}.${G_VER_MINOR}
  79. G_TITLE="1C:Enterprise ${G_VER_SHORT} server"
  80.  
  81. #------------------------------------------------------------
  82. # this values can be passed from outside, so perform "z-check"
  83. #------------------------------------------------------------
  84. [ -z "$SRV1CV8_USER"      ] && SRV1CV8_USER=usr1cv83
  85. [ -z "$SRV1CV8_BINDIR"    ] && SRV1CV8_BINDIR="$G_BINDIR"
  86. [ -z "$SRV1CV8_PIDFILE"   ] && SRV1CV8_PIDFILE="/var/run/srv1cv${G_VER_MAJOR}${G_VER_MINOR}.pid"
  87. [ -z "$SRV1CV8_KEYTAB"    ] && SRV1CV8_KEYTAB="$SRV1CV8_BINDIR/$SRV1CV8_USER.keytab"
  88. [ -z "$SRV1CV8_WAITSTART" ] && SRV1CV8_WAITSTART=5
  89. [ -z "$SRV1CV8_WAITSTOP"  ] && SRV1CV8_WAITSTOP=5
  90.  
  91. #------------------------------------------------------------
  92. # builds ragent's command line from configuration parameters
  93. #------------------------------------------------------------
  94. function buildCommandLine() {
  95.     local cmdline="$SRV1CV8_BINDIR/ragent -daemon"
  96.     [ ! -z "$SRV1CV8_PORT" ] && cmdline="$cmdline -port $SRV1CV8_PORT"
  97.     [ ! -z "$SRV1CV8_REGPORT" ] && cmdline="$cmdline -regport $SRV1CV8_REGPORT"
  98.     [ ! -z "$SRV1CV8_DATA" ] && cmdline="$cmdline -d \"$SRV1CV8_DATA\""
  99.     [ ! -z "$SRV1CV8_RANGE" ] && cmdline="$cmdline -range $SRV1CV8_RANGE"
  100.     [ ! -z "$SRV1CV8_SECLEV" ] && cmdline="$cmdline -seclev $SRV1CV8_SECLEV"
  101.     [ "x$SRV1CV8_DEBUG" == "x1" ] && cmdline="$cmdline -debug"
  102.     echo $cmdline
  103. }
  104. #------------------------------------------------------------
  105. # checks if process with passed pid exists
  106. #------------------------------------------------------------
  107. function checkpid() {
  108.     ps -p $* > /dev/null
  109. }
  110. #------------------------------------------------------------
  111. # waits SRV1CV8_WAITSTOP seconds for process termination,
  112. # then kills it
  113. #------------------------------------------------------------
  114. function delayedkill() {
  115.     local mypid=$1
  116.     local delay=$2
  117.     kill $mypid 2>/dev/null
  118.     checkpid $mypid && sleep $delay || return 0
  119.     if checkpid $mypid; then
  120.         kill -9 $mypid
  121.         logWarning "Process refused to die... So it was killed. May be you should increase SRV1CV8_WAITSTOP variable?";
  122.     fi
  123.     return 0
  124. }
  125. #------------------------------------------------------------
  126. # extracts specified param from value from given command-line
  127. #------------------------------------------------------------
  128. function extractParam() {
  129.     local param=$1
  130.     shift
  131.  
  132.     local tmp=$*
  133.     if [ ! -z "$*" ]; then
  134.         if [ ${tmp:0:1} == "-" ]; then
  135.             tmp=${tmp:1}
  136.             local val=${tmp#*$param }
  137.             # if old and new strings are equal
  138.             # we don't have needed param in cmdline
  139.             # so return
  140.             [ "$val" == "$tmp" ] && return;
  141.             val=${val%% -*}
  142.             echo "$val"
  143.         fi
  144.     fi
  145. }
  146. #------------------------------------------------------------
  147. # call it when something fails
  148. #------------------------------------------------------------
  149. function failure() {
  150.     [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo FAILED
  151.     return 0
  152. }
  153. #------------------------------------------------------------
  154. # extracts data dir from ragent's command line or (if it's
  155. # empty), builds it from users's home dir
  156. #------------------------------------------------------------
  157. function getDataDir() {
  158.     local cmdline=`getRagentRealCmdLine`
  159.     local datadir="$SRV1CV8_DATA"
  160.     [ ! -z "$cmdline" ] && datadir=`extractParam d $cmdline`
  161.  
  162.     if [ -z "$datadir" ]; then
  163.         local line=`grep ^$SRV1CV8_USER: /etc/passwd`
  164.         local homedir=${line#*:*:*:*:*:}
  165.         homedir=${homedir%:*}
  166.         datadir="$homedir/.1cv${G_VER_MAJOR}${G_VER_MINOR}/1C/1Cv${G_VER_MAJOR}${G_VER_MINOR}"
  167.     fi
  168.  
  169.     echo $datadir
  170. }
  171. #------------------------------------------------------------
  172. # returns ragent configuration debug flag
  173. #------------------------------------------------------------
  174. function getDebugStatus() {
  175.     local cmdline=`getRagentRealCmdLine`
  176.     local debugFlag="$SRV1CV8_DEBUG"
  177.     echo "$cmdline" | grep "\-debug" >/dev/null && debugFlag="1"
  178.     [ ! -z "$debugFlag" ] && echo $debugFlag || echo "0"
  179. }
  180. #------------------------------------------------------------
  181. # returns ragent port range
  182. #------------------------------------------------------------
  183. function getPortRange() {
  184.     local cmdline=`getRagentRealCmdLine`
  185.     local range="$SRV1CV8_RANGE"
  186.     [ ! -z "$cmdline" ] && range=`extractParam range $cmdline`
  187.     [ ! -z "$range" ] && echo $range || echo 1560:1591
  188. }
  189. #------------------------------------------------------------
  190. # returns ragent main port
  191. #------------------------------------------------------------
  192. function getRagentPort() {
  193.     local cmdline=`getRagentRealCmdLine`
  194.     local port="$SRV1CV8_PORT"
  195.     [ ! -z "$cmdline" ] && port=`extractParam port $cmdline`
  196.     [ ! -z "$port" ] && echo $port || echo 1540
  197. }
  198. #------------------------------------------------------------
  199. # get's running ragent command-line
  200. #------------------------------------------------------------
  201. function getRagentRealCmdLine() {
  202.     local cmdline=`buildCommandLine`
  203.     local mypid=`getRagentPid "$SRV1CV8_USER" "$cmdline"`
  204.     local result=""
  205.     [ ! -z "$mypid" ] && result=`ps --pid $mypid -o cmd= | sed -e "s/.*-daemon//"`
  206.     echo "$result"
  207. }
  208. #------------------------------------------------------------
  209. # tries to get pid of ragent process, started by this script
  210. #------------------------------------------------------------
  211. function getRagentPid() {
  212.     local user=$1
  213.     shift
  214.  
  215.     # remove quotes in passed CMDLINE because
  216.     # command line of process doesn't contains'em
  217.     # even if they were passed.
  218.     local cmdline=`echo $* | sed -e "s/\"//g"`
  219.  
  220.     ps -C ragent -opid=,user=,cmd= | while read curline; do
  221.         local  curPID=`echo $curline | sed -e "s/ .*//"`
  222.         local curline=`echo $curline | sed -e "s/$curPID //"`
  223.         local  curUSR=`echo $curline | sed -e "s/ .*//"`
  224.         local  curCMD=`echo $curline | sed -e "s/$curUSR //"`
  225.         if [ "$curCMD" == "$cmdline" ] && [ "$curUSR" == "$user" ]; then
  226.             echo $curPID
  227.         fi
  228.     done
  229. }
  230. #------------------------------------------------------------
  231. # returns ragent reg port
  232. #------------------------------------------------------------
  233. function getRegPort() {
  234.     local cmdline=`getRagentRealCmdLine`
  235.     local regport="$SRV1CV8_REGPORT"
  236.     [ ! -z "$cmdline" ] && regport=`extractParam regport $cmdline`
  237.     [ ! -z "$regport" ] && echo $regport || echo 1541
  238. }
  239. #------------------------------------------------------------
  240. # returns name of script
  241. #------------------------------------------------------------
  242. function getScriptName() {
  243.     local myname=`basename $0`
  244.     [ ${myname:0:1} = "S" -o ${myname:0:1} = "K" ] && myname=${myname:3}
  245.     echo $myname
  246. }
  247. #------------------------------------------------------------
  248. # returns ragent security level
  249. #------------------------------------------------------------
  250. function getSecLevel() {
  251.     local cmdline=`getRagentRealCmdLine`
  252.     local seclev="$SRV1CV8_SECLEV"
  253.     [ ! -z "$cmdline" ] && seclev=`extractParam seclev $cmdline`
  254.     [ ! -z "$seclev" ] && echo $seclev || echo 0
  255. }
  256. #------------------------------------------------------------
  257. # displays useful information about ragent
  258. #------------------------------------------------------------
  259. function info() {
  260.     echo "$G_TITLE info:"
  261.     echo -n "  Data dir: " && getDataDir
  262.     echo -n " Main port: " && getRagentPort
  263.     echo -n "   RegPort: " && getRegPort
  264.     echo -n "Port range: " && getPortRange
  265.     echo -n "Debug mode: " && getDebugStatus
  266.     echo -n "Sec. level: " && getSecLevel
  267.     return 0
  268. }
  269. #------------------------------------------------------------
  270. # check if our ragent running
  271. #------------------------------------------------------------
  272. function isRagentRunning() {
  273.     local mypid=`getRagentPid $*`
  274.     [ ! -z "$mypid" ] && checkpid "$mypid"
  275. }
  276. #------------------------------------------------------------
  277. # talks to stderr, and then logs failure to stdout
  278. #------------------------------------------------------------
  279. function logError() {
  280.     echo -n "  Error: " >&2
  281.     echo $* >&2
  282.     failure
  283. }
  284. #------------------------------------------------------------
  285. # put some additional non-critical debug info to stderr
  286. #------------------------------------------------------------
  287. function logWarning() {
  288.     echo -n "Warning: " >&2
  289.     echo $* >&2
  290. }
  291. #------------------------------------------------------------
  292. # restarts server
  293. #------------------------------------------------------------
  294. function restart() {
  295.     stop
  296.     start
  297. }
  298. #------------------------------------------------------------
  299. # displays command-line help
  300. #------------------------------------------------------------
  301. function showUsage() {
  302.     local myname=$1
  303.     echo "Usage: $myname [-c config] start|stop|restart|status|info"
  304. }
  305. #------------------------------------------------------------
  306. # starts ragent
  307. #------------------------------------------------------------
  308. function start() {
  309.     echo -n "Starting $G_TITLE: "
  310.  
  311.     if [ ! -z "$SRV1CV8_DATA" ]; then
  312.         if [ -e "$SRV1CV8_DATA" ]; then
  313.             [ ! -d "$SRV1CV8_DATA" ] && { logError "SRV1CV8_DATA \"$SRV1CV8_DATA\" is a file, not a directory!"; return 0; }
  314.         fi
  315.     fi
  316.  
  317.     [ ! -f "$SRV1CV8_BINDIR/ragent" ] && { logError "ragent file does not exists!" ; return 0; }
  318.     [ ! -x "$SRV1CV8_BINDIR/ragent" ] && { logError "ragent file is not executable!"; return 0; }
  319.  
  320.     local cmd2run=`buildCommandLine`
  321.  
  322.     if isRagentRunning "$SRV1CV8_USER" "$cmd2run"; then
  323.         logWarning "already started!";
  324.     else
  325.         # run our process
  326.         if [ -z "$SRV1CV8_USER" ]; then
  327.             export KRB5_KTNAME="$SRV1CV8_KEYTAB"
  328.             $cmd2run
  329.         else
  330.             su -s /bin/bash  - "$SRV1CV8_USER" -c "KRB5_KTNAME=\"$SRV1CV8_KEYTAB\" $cmd2run"
  331.         fi
  332.  
  333.         sleep $SRV1CV8_WAITSTART # wait a bit before check
  334.         ! isRagentRunning "$SRV1CV8_USER" "$cmd2run" && { logError "service failed to start!"; return 0; }
  335.        
  336.         # check if pidfile exists and remove it if neccessary
  337.         [ -f "$SRV1CV8_PIDFILE" ] && logWarning "pid file existed on server start. it can mean that last run failed..."    
  338.     fi
  339.  
  340.     local mypid=`getRagentPid $SRV1CV8_USER $cmd2run`
  341.     echo ${mypid} > "$SRV1CV8_PIDFILE"
  342.  
  343.     success
  344.     return 0
  345. }
  346. #------------------------------------------------------------
  347. # displays ragent status
  348. #------------------------------------------------------------
  349. function status() {
  350.     echo "$G_TITLE status:"
  351.     echo -n "Init script: "
  352.     if [ -f "$SRV1CV8_PIDFILE" ]; then
  353.         echo STARTED.
  354.  
  355.         local cmd2run=`buildCommandLine`
  356.         echo -n "     Ragent: "
  357.         ! isRagentRunning "$SRV1CV8_USER" "$cmd2run" && echo -n "NOT "
  358.         echo "RUNNING."
  359.     else
  360.         echo NOT STARTED.
  361.     fi
  362.     return 0
  363. }
  364. #------------------------------------------------------------
  365. # stops ragent
  366. #------------------------------------------------------------
  367. function stop() {
  368.     echo -n "Stopping $G_TITLE: "
  369.  
  370.     [ -f "$SRV1CV8_PIDFILE" ] && rm "$SRV1CV8_PIDFILE"
  371.  
  372.     local cmd2run=`buildCommandLine`
  373.     if isRagentRunning "$SRV1CV8_USER" "$cmd2run"; then
  374.         mypid=`getRagentPid $SRV1CV8_USER $cmd2run`
  375.         if [ ! -z "$mypid" ]; then
  376.             local childpids=`ps --ppid ${mypid} -o pid=`
  377.             delayedkill $mypid $SRV1CV8_WAITSTOP
  378.             for childpid in $childpids; do
  379.                 delayedkill $childpid $SRV1CV8_WAITSTOP
  380.             done
  381.         fi
  382.     else
  383.         logWarning "server not running!"
  384.     fi
  385.  
  386.     success
  387.     return
  388. }
  389. #------------------------------------------------------------
  390. # functions says something succeded
  391. #------------------------------------------------------------
  392. function success() {
  393.     [ "$BOOTUP" != "verbose" -a -z "$LSB" ] && echo OK
  394.     return 0
  395. }
  396.  
  397. #------------------------------------------------------------
  398. # script's main function
  399. #------------------------------------------------------------
  400. function main() {
  401.     local myname=`getScriptName`
  402.     local dirname=`dirname $0`
  403.     local realdir=$(cd "$dirname"; pwd)
  404.  
  405.     # check if someone passed different config throug command-line option
  406.     local configFile=
  407.     [ "$1" == "-c" ] && { configFile="$2"; shift 2; }
  408.  
  409.     local action=
  410.  
  411.     case $1 in
  412.         '')
  413.             showUsage $myname
  414.             return 1
  415.             ;;
  416.         --help)
  417.             showUsage $myname
  418.             return 0
  419.             ;;
  420.         info)
  421.             action=info
  422.             ;;
  423.         restart)
  424.             action=restart
  425.             ;;
  426.         start)
  427.             action=start
  428.             ;;
  429.         status)
  430.             action=status
  431.             ;;
  432.         stop)
  433.             action=stop
  434.             ;;
  435.         *)
  436.             showUsage $myname
  437.             return 1;
  438.             ;;
  439.     esac
  440.     # check for config file existence
  441.     if [ -z "$configFile" ]; then
  442.         configFile="/etc/sysconfig/$myname"
  443.         [ -e "$configFile" ] && source "$configFile"
  444.     else
  445.         [ -f "$configFile" ] && source "$configFile"
  446.     fi
  447.  
  448.     $action
  449. }
  450.  
  451. #invoke function main
  452. main $*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement