Bug 18964: Add --debugger option to koha-plack
authorTomas Cohen Arazi <tomascohen@theke.io>
Fri, 21 Jul 2017 19:28:31 +0000 (16:28 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 23 Jan 2018 19:05:54 +0000 (16:05 -0300)
CAVEAT: This patch is only about allowing koha-plack to start a debugging
session on a remote target. Configuring all the needed pieces to make it
work is out of the scope of the bug. Testing should be focused (mainly) on
making sure this introduces NO REGRESSIONS on regular use (i.e. not debugging).

This patch adds the following option switches to koha-plack:

--debugger
----------
Toggle debugging

--debugger-key
--------------
Some IDEs require a key, this needs to match because they are picky about it

--debugger-location
-------------------
This option switch is used to specify the host:port your IDE is listening at.
Inside kohadevbox (using Vdebug with Vim) this would be localhost:9000. Outside
kohadevbox it would be 192.168.50.1:9000

--debugger-path
---------------
This is the path in which you installed the Komodo Remote Debugger library [1]. In kohadevbox
you could put the contents of the downloaded .tar.gz in /home/vagrant/dbgp/perllib. That
perllib seems to be required for things to work [2].

You can test with a simple CLI script things work:

PERL5LIB=/home/vagrant/dbgp/perllib:$PERL5LIB \
PERLDB="BEGIN { require q(/home/vagrant/dbgp/perllib/perl5db.pl) }" \
PERLDB_OPTS="RemotePort=192.168.50.1:9000" perl -d t/Prices.t

If you see action on your IDE, you are on the right track on the IDE side.

To test:
- Apply this patch
- Enable remote debugging on your IDE, on port 9000 (or adjust the command
  below to match your IDE's listening port).
- Download the Komodo Remote Debugger package, and place the package's contents
  in /home/vagrant/dbgp/perllib (you should see perl5db.pl in there).
- Run:
  $ sudo koha-plack --stop kohadev
  $ sudo kohaclone/debian/koha-plack --start \
                                     --debugger \
                                     --debugger-path /home/vagrant/dbgp/perllib \
                                     --debugger-location 192.168.50.1:9000 \
                                     kohadev
=> SUCCESS: You IDE/tool gets a connection from the Plack process.
- Sign off :-D

The explanation on how to use it assumes you are running kohadevbox.

WARNING: The main difficulty I found was setting the right dir/file mappings.

[1] http://code.activestate.com/komodo/remotedebugging/
[2] https://github.com/Komodo/KomodoEdit/issues/644#issuecomment-236268012

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

debian/scripts/koha-plack

index 5e1fc9c..186c162 100755 (executable)
@@ -1,6 +1,7 @@
 #!/bin/bash
 #
 # Copyright 2015 Theke Solutions
+# Copyright 2016 Koha-Suomi
 #
 # This file is part of Koha.
 #
@@ -51,6 +52,11 @@ $scriptname -h|--help
     --restart             Restart the plack daemon for the specified instances
     --enable              Enable plack for the specified instances
     --disable             Disable plack for the specified instances
+    --debugger            Enable running Plack in debug mode
+    --debugger-key        Specify the key the IDE is expecting
+    --debugger-location   Specify the host:port for your debugger tool (defaults
+                          to localhost:9000)
+    --debugger-path       Specify the path for the debugger library
     --quiet|-q            Make the script quiet about non existent instance names
                           (useful for calling from another scripts).
     --help|-h             Display this help message
@@ -79,13 +85,24 @@ start_plack()
     PLACK_WORKERS=$(run_safe_xmlstarlet $instancename plack_workers)
     [ -z $PLACK_WORKERS ] && PLACK_WORKERS="2"
 
-    STARMANOPTS="-M FindBin --max-requests ${PLACK_MAX_REQUESTS} --workers ${PLACK_WORKERS} \
+    environment="deployment"
+    daemonize="--daemonize"
+    logging="--access-log /var/log/koha/${instancename}/plack.log \
+             --error-log /var/log/koha/${instancename}/plack-error.log"
+    max_requests_and_workers="--max-requests ${PLACK_MAX_REQUESTS} --workers ${PLACK_WORKERS}"
+
+    if [ "$debug_mode" = "yes" ]; then
+        environment="development"
+        daemonize=""
+        logging="" # remote debugger takes care
+        max_requests_and_workers="--workers 1"
+        STARMAN="/usr/bin/perl -d ${STARMAN}"
+    fi
+
+    STARMANOPTS="-M FindBin ${max_requests_and_workers} \
                  --user=${instancename}-koha --group ${instancename}-koha \
-                 --pid ${PIDFILE} \
-                 --daemonize \
-                 --access-log /var/log/koha/${instancename}/plack.log \
-                 --error-log /var/log/koha/${instancename}/plack-error.log \
-                 -E deployment --socket ${PLACKSOCKET} ${PSGIFILE}"
+                 --pid ${PIDFILE} ${daemonize} ${logging} \
+                 -E ${environment} --socket ${PLACKSOCKET} ${PSGIFILE}"
 
     if ! is_plack_running ${instancename}; then
         export KOHA_CONF="/etc/koha/sites/${instancename}/koha-conf.xml"
@@ -239,6 +256,10 @@ set_action()
 STARMAN=$(which starman)
 op=""
 quiet="no"
+debug_mode="no"
+debugger_key=""
+debugger_location="localhost:9000"
+debugger_path=""
 
 # Read command line parameters
 while [ $# -gt 0 ]; do
@@ -264,6 +285,18 @@ while [ $# -gt 0 ]; do
         --disable)
             set_action "disable"
             shift ;;
+        --debugger)
+            debug_mode="yes"
+            shift ;;
+        --debugger-key)
+            debugger_key="$2"
+            shift 2 ;;
+        --debugger-location)
+            debugger_location="$2"
+            shift 2 ;;
+        --debugger-path)
+            debugger_path="$2"
+            shift 2 ;;
         -*)
             die "Error: invalid option switch ($1)" ;;
         *)
@@ -282,7 +315,23 @@ if [ $# -gt 0 ]; then
         if is_instance $name; then
 
             adjust_paths_dev_install $name
-            export DEV_INSTALL KOHA_HOME PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
+            export DEV_INSTALL
+            export KOHA_HOME
+            PERL5LIB=$PERL5LIB:$KOHA_HOME/installer:$KOHA_HOME/lib/installer
+            # If debug mode is enabled, add the debugger lib path
+            # to PERL5LIB if appropriate
+            if [ "$debug_mode" = "yes" ]; then
+                if [ "$debugger_path" != "" ]; then
+                    PERL5LIB="${debugger_path}":$PERL5LIB
+                fi
+                export PERL5DB="BEGIN { require q(${debugger_path}/perl5db.pl) }"
+                export PERLDB_OPTS="RemotePort=${debugger_location} async=1 LogFile=/var/log/koha/${name}/plack-debug.log"
+                export DBGP_IDEKEY=${debugger_key}
+                export PLACK_DEBUG=1
+                export PERL5OPT="-d"
+            fi
+
+            export PERL5LIB
 
             case $op in
                 "start")