Advertisement
Guest User

koha-plack-daemon && prepare_remote_debugger.sh

a guest
Jan 16th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. root@koha_ci_2:/home/koha/Koha# cat /etc/init.d/koha-plack-daemon
  2. #!/bin/bash
  3. #
  4. # Copyright 2015 Theke Solutions
  5. #
  6. # This file is part of Koha.
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20.  
  21. ### BEGIN INIT INFO
  22. # Provides: koha-plack-daemon
  23. # Required-Start: $remote_fs
  24. # Required-Stop: $remote_fs
  25. # Default-Start: 2 3 4 5
  26. # Default-Stop: 0 1 6
  27. # Short-Description: Plack server daemon for fast http-handling
  28. ### END INIT INFO
  29.  
  30. set -e
  31.  
  32. . /lib/lsb/init-functions
  33.  
  34.  
  35. HELPER_FUNCTIONS="/home/koha/Koha/debian/scripts/koha-functions.sh"
  36.  
  37. RUNDIR="/var/run/koha"
  38. LOGDIR="/home/koha/koha-dev/var/log"
  39. PIDFILE="$RUNDIR/plack.pid"
  40. PLACKSOCKET="$RUNDIR/plack.sock"
  41. PSGIFILE="/home/koha/Koha/misc/plack/plack.psgi"
  42. MODE="deployment" #development|deployment|test
  43. LISTEN=":5000"
  44. #User and group must be the same as for apache2, eg. www-data. This is to prevent nasty surprises with priviledge escalation.
  45. #And so programs can communicate via the same unix-socket
  46. USER="www-data"
  47. GROUP="www-data"
  48.  
  49.  
  50. #Set Environment for koha-plack-daemon.sh
  51. export KOHA_CONF="/home/koha/koha-dev/etc/koha-conf.xml"
  52. export INTRANET_CGI_DIR="/home/koha/Koha"
  53. export OPAC_CGI_DIR="/home/koha/Koha/opac"
  54. export PERL_MODULE_DIR="/home/koha/Koha"
  55. export PLACK_DEBUG #This is set if debug-parameter is given or if mode is development
  56.  
  57.  
  58. #Make sure the pid-dir and log-dir exists
  59. mkdir -p $RUNDIR
  60. mkdir -p $LOGDIR
  61.  
  62. # include helper functions
  63. if [ -f "$HELPER_FUNCTIONS" ]; then
  64. . "$HELPER_FUNCTIONS"
  65. else
  66. echo "Error: $HELPER_FUNCTIONS not present." 1>&2
  67. exit 1
  68. fi
  69.  
  70. usage()
  71. {
  72. local scriptname=$(basename $0)
  73.  
  74. cat <<EOF
  75. $scriptname
  76.  
  77. This script lets you manage the plack daemon for your Koha instance.
  78.  
  79. Usage:
  80. $scriptname start|stop|restart|reload [development|deployement|test] [debug] [debugger]
  81. $scriptname -h|--help
  82.  
  83. start Start the plack daemon for the specified instances
  84. stop Stop the plack daemon for the specified instances
  85. restart Restart the plack daemon for the specified instances
  86. reload Reload the application without losing connections
  87. debug Trigger PLACK_DEBUG -environment variable
  88. debugger Start perl with the -d -flag
  89. deployment Set the plack mode
  90. development Set the plack mode
  91. test Set the plack mode
  92. --help|-h Display this help message
  93.  
  94. EXAMPLE
  95.  
  96. bash -x /etc/init.d/koha-plack-daemon start development debug debugger
  97.  
  98. EOF
  99. }
  100.  
  101. start_plack()
  102. {
  103. _check_and_fix_perms
  104.  
  105. DEBUGGER_STR=""
  106. test "$DEBUGGER" == "1" && DEBUGGER_STR="/usr/bin/perl -d "
  107.  
  108. STARMANOPTS="-M FindBin \
  109. --user=$USER --group=$GROUP \
  110. --pid ${PIDFILE} \
  111. -E $MODE \
  112. ${PSGIFILE} "
  113.  
  114. test "$MODE" != "deployment" && STARMANOPTS="$STARMANOPTS --workers 1"
  115.  
  116. test "$MODE" == "deployment" && STARMANOPTS="$STARMANOPTS --daemonize \
  117. --max-requests 50 \
  118. --workers 2 \
  119. --access-log $LOGDIR/plack.log \
  120. --error-log $LOGDIR/plack-error.log \
  121. "
  122.  
  123. test -n "$PLACKSOCKET" && STARMANOPTS="$STARMANOPTS --listen ${PLACKSOCKET} "
  124.  
  125. test -n "$LISTEN" && STARMANOPTS="$STARMANOPTS --listen ${LISTEN} "
  126.  
  127.  
  128.  
  129. if ! is_plack_running; then
  130. log_daemon_msg "Starting Plack daemon"
  131.  
  132. if ${DEBUGGER_STR} ${STARMAN} ${STARMANOPTS}; then
  133. log_end_msg 0
  134. else
  135. log_end_msg 1
  136. fi
  137. else
  138. log_daemon_msg "Error: Plack already running"
  139. log_end_msg 1
  140. fi
  141. }
  142.  
  143. stop_plack()
  144. {
  145. if is_plack_running; then
  146.  
  147. log_daemon_msg "Stopping Plack daemon"
  148.  
  149. if start-stop-daemon --pidfile ${PIDFILE} --stop; then
  150. log_end_msg 0
  151. else
  152. log_end_msg 1
  153. fi
  154. else
  155. log_daemon_msg "Error: Plack not running"
  156. log_end_msg 1
  157. fi
  158. }
  159.  
  160. restart_plack()
  161. {
  162. if is_plack_running; then
  163.  
  164. log_daemon_msg "Restarting Plack daemon"
  165.  
  166. if stop_plack && start_plack; then
  167. log_end_msg 0
  168. else
  169. log_end_msg 1
  170. fi
  171. else
  172. log_daemon_msg "Error: Plack not running"
  173. log_end_msg 1
  174. fi
  175. }
  176.  
  177. #Starman says he supports hot-reload. Maybe others do, but I only love starman.
  178. reload_starman()
  179. {
  180. if is_plack_running; then
  181.  
  182. log_daemon_msg "Hot-reloading starman daemon !!!"
  183.  
  184. if start-stop-daemon --pidfile ${PIDFILE} --stop --signal HUP; then
  185. log_end_msg 0
  186. else
  187. log_end_msg 1
  188. fi
  189. else
  190. log_daemon_msg "Error: starman not running"
  191. log_end_msg 1
  192. fi
  193. }
  194.  
  195. check_env_and_warn()
  196. {
  197. local apache_version_ok="no"
  198. local required_modules="headers proxy_http"
  199. local missing_modules=""
  200.  
  201. if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
  202. apache_version_ok="yes"
  203. fi
  204.  
  205. for module in ${required_modules}; do
  206. if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q ${module}; then
  207. missing_modules="${missing_modules}${module} "
  208. fi
  209. done
  210.  
  211. if [ "${apache_version_ok}" != "yes" ]; then
  212. warn "WARNING: koha-plack requires Apache 2.4.x and you don't have that."
  213. fi
  214.  
  215. if [ "${missing_modules}" != "" ]; then
  216. cat 1>&2 <<EOM
  217. WARNING: koha-plack requires some Apache modules that you are missing.
  218. You can install them with:
  219.  
  220. sudo a2enmod ${missing_modules}
  221.  
  222. EOM
  223.  
  224. fi
  225. }
  226.  
  227. _check_and_fix_perms()
  228. {
  229. local files="/var/log/koha/plack.log \
  230. /var/log/koha/plack-error.log"
  231.  
  232. for file in ${files}
  233. do
  234. if [ ! -e "${file}" ]; then
  235. touch ${file}
  236. fi
  237. chown "koha":"koha" ${file}
  238. done
  239. }
  240.  
  241. set_action()
  242. {
  243. if [ "$op" = "" ]; then
  244. op=$1
  245. else
  246. die "Error: only one action can be specified."
  247. fi
  248. }
  249.  
  250. STARMAN=$(which starman)
  251. op=""
  252. quiet="no"
  253.  
  254. # Read command line parameters
  255. while [ $# -gt 0 ]; do
  256.  
  257. case "$1" in
  258. -h|--help)
  259. usage ; exit 0 ;;
  260. start)
  261. set_action "start"
  262. shift ;;
  263. stop)
  264. set_action "stop"
  265. shift ;;
  266. restart)
  267. set_action "restart"
  268. shift ;;
  269. reload)
  270. set_action "reload"
  271. shift ;;
  272. debug)
  273. export PLACK_DEBUG=1
  274. shift ;;
  275. debugger)
  276. DEBUGGER="1"
  277. shift ;;
  278. deployment)
  279. MODE="deployment"
  280. shift ;;
  281. development)
  282. MODE="development"
  283. export PLACK_DEBUG=1
  284. shift ;;
  285. test)
  286. MODE="test"
  287. shift ;;
  288. -*)
  289. die "Error: invalid option switch ($1)" ;;
  290. *)
  291. # We expect the remaining stuff are the instance names
  292. break ;;
  293. esac
  294.  
  295. done
  296.  
  297. if [ -z $PERL5LIB ]; then
  298. PERL5LIB="/home/koha/Koha"
  299. fi
  300. export PERL5LIB
  301.  
  302. check_env_and_warn
  303.  
  304. case $op in
  305. "start")
  306. start_plack
  307. ;;
  308. "stop")
  309. stop_plack
  310. ;;
  311. "restart")
  312. restart_plack
  313. ;;
  314. "reload")
  315. reload_starman
  316. ;;
  317. "enable")
  318. enable_plack
  319. ;;
  320. "disable")
  321. disable_plack
  322. ;;
  323. *)
  324. usage
  325. ;;
  326. esac
  327.  
  328. exit 0
  329.  
  330. root@koha_ci_2:/home/koha/Koha# cat /home/kivilahtio/prepare_remote_debugger.sh
  331. #!/bin/bash
  332. # Ansible managed: /home/ansible/KSAnsible/roles/koha/templates/home_prepare_remote_debugger.sh.j2 on hephaestus
  333. #
  334. # Loads the remote debugging environment variables to the running shell
  335. #
  336. # then you can just start perl scripts with the -d -flag and hopefully receive the remote debuggin session.
  337. # see http://docs.activestate.com/komodo/4.4/debugger.html
  338.  
  339. #You must manually specify your remote connection settings
  340. REMOTEPORT=""
  341.  
  342. test -z $REMOTEPORT && echo "\$REMOTEPORT not defined" && \
  343. echo "You must define where a debugger is listening for remote connections" && \
  344. echo "eg.: REMOTEPORT=192.168.1.101:8000" && \
  345. echo "" && \
  346. exit 1
  347.  
  348. test $0 != "bash" && echo "Incorrect invocation: You must run me like:" && \
  349. echo ". prepare_remote_debugger.sh or source prepare_remote_debugger.sh" && \
  350. echo "" && \
  351. exit 1
  352.  
  353.  
  354.  
  355. export PERL5LIB=/opt/Komodo-PerlRemoteDebugging-10.1.4/:$PERL5LIB
  356. export PERLDB_OPTS=RemotePort=$REMOTEPORT async=1
  357. export DBGP_IDEKEY=1221
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement