-
Notifications
You must be signed in to change notification settings - Fork 134
Debugging with PDB
IMPORTANT this page is OUTDATED see https://ansible.readthedocs.io/projects/galaxy-ng/en/latest/dev/getting_started/
As galaxy_ng dev environment runs on containers and the API starts with
gunicorn
with multiple workers, it is difficult to run a pdb
session
in the default environment.
Debugging sometimes is crucial for API development, so there are some tweaks to get a running environment where you can debug normally using any PDB based tool.
First step is to start all the dependency containers EXCEPT THE API (the one we want to debug)
./compose up resource-manager worker content-app
You can use
./compose up -d
if you want it to run in background, it is recommended to have it running on a visible terminal so you can watch logs and messages.
NOTE: Wait at least until all TCP services are up, must be in 30 seconds or less.
To enable the debugging mode we need to ensure API is served using the Django dev server with a single worker and also we want to install the debugging dependencies of our choice.
- ipdb
Easier, all ipython features, no remote connection - sdb
recommended
Feature full, has its own client, colorized, remote connection, SIGTRAP support to debug running processes - remote_pdb
Same as pdb, remote connection using nc, socat, telnet
On a separate terminal start the django server with debugging enabled.
./compose run --rm --service-ports api /bin/bash -c "pip install ipython ipdb && ./entrypoint.sh manage runserver 0.0.0.0:8000"
# Run only bash session on container
./compose run --rm --service-ports api /bin/bash
# Once in container console install needed deps.
pip install ipdb
# then run django-admin commands directly.
# serve the API
django-admin runserver 0.0.0.0:8000
# tests
django-admin test -v 2 galaxy_ng.tests.unit
- TODO: @alikins will add instruction here.
On the codeline you want to debug add:
...
__import__("ipdb").set_trace()
...
Save the code and make an api call to trigger the debugging session.
The debug console will open on the same console where you started the Django server.
On a separate terminal start the django server with debugging enabled.
./compose run --rm -p "5001:8000" -p "6899-6999:6899-6999" -e SDB_HOST=0.0.0.0 -e SDB_PORT=6899 -e "SDB_NOTIFY_HOST=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')" api /bin/bash -c "pip install sdb && ./entrypoint.sh manage runserver 0.0.0.0:8000"
NOTE ON a Mac, If it doesn't work, try looking at
ifconfig
orip addr | grep docker
and set your docker host IP directly.
Ia separate terminal on your local host.
$ pip install sdb
$ sdb-listen
listening for sdb notifications on :6899...
On the codeline you want to debug add:
...
__import__("sdb").set_trace()
...
Save the code and make an api call to trigger the debugging session.
The debug session opens on the terminal where sdb-listen
is running.
Use exit
to stop the debugger.
SIGTRAP allows the debugging of a running process without the need to specify a breakpoint.
On the top of the code you want to debug.
import sdb
sdb.sigtrap()
Then kill the process with -5 (do that starting an exec on the API container).
kill -5 <pid-of-process>
The debug session opens on the terminal where sdb-listen
is running.
sdb includes a few additional debugger aliases that make interactive debugging more pleasant:
- Prefix commands with an integer to repeat them. For example, 10n is the same as running next 10 times in a row.
- ? is the same as calling dir()
- ?? can be added to the end of a function call to view its source lines
./compose run --rm -p "5001:8000" -p "4444:4444" -e REMOTE_PDB_HOST=0.0.0.0 -e REMOTE_PDB_PORT=4444 api /bin/bash -c "pip install remote-pdb && ./entrypoint.sh manage runserver 0.0.0.0:8000"
...
__import__("remote_pdb").set_trace()
...
Save the code and make an api call to trigger the debugging session.
socat readline tcp:127.0.0.1:4444
or
nc -C 127.0.0.1 4444
To exit the debugger use exit
or CTRL-D
and ensure to press Ctrl-C
after the main debugger exits to release the django server for subsequent calls.
Sponsored by Red Hat, Inc.