Skip to content

Commit

Permalink
Remove DebugPy (#10692)
Browse files Browse the repository at this point in the history
* Remove DebugPy

* Fix linter

* Update setEnv.sh

* Update dev compose with debug and python warnings

* Remove kubernetes part of this as well
  • Loading branch information
Maffooch authored Aug 26, 2024
1 parent cffb416 commit a6efe61
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 171 deletions.
60 changes: 0 additions & 60 deletions docker-compose.override.debug.yml

This file was deleted.

12 changes: 8 additions & 4 deletions docker-compose.override.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@ services:
volumes:
- '.:/app:z'
environment:
PYTHONWARNINGS: always # We are strict during development so Warnings needs to be more verbose
PYTHONWARNINGS: error # We are strict about Warnings during development
DD_DEBUG: 'True'
DD_ADMIN_USER: "${DD_ADMIN_USER:-admin}"
DD_ADMIN_PASSWORD: "${DD_ADMIN_PASSWORD:-admin}"
DD_EMAIL_URL: "smtp://mailhog:1025"
celeryworker:
volumes:
- '.:/app:z'
environment:
PYTHONWARNINGS: always # We are strict during development so Warnings needs to be more verbose
PYTHONWARNINGS: error # We are strict about Warnings during development
DD_DEBUG: 'True'
DD_EMAIL_URL: "smtp://mailhog:1025"
celerybeat:
volumes:
- '.:/app:z'
environment:
PYTHONWARNINGS: always # We are strict during development so Warnings needs to be more verbose
PYTHONWARNINGS: error # We are strict about Warnings during development
DD_DEBUG: 'True'
initializer:
volumes:
- '.:/app:z'
environment:
PYTHONWARNINGS: always # We are strict during development so Warnings needs to be more verbose
PYTHONWARNINGS: error # We are strict about Warnings during development
DD_DEBUG: 'True'
DD_ADMIN_USER: "${DD_ADMIN_USER:-admin}"
DD_ADMIN_PASSWORD: "${DD_ADMIN_PASSWORD:-admin}"
nginx:
Expand Down
14 changes: 0 additions & 14 deletions docker/setEnv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
target_dir="${0%/*}/.."
override_link='docker-compose.override.yml'
override_file_dev='docker-compose.override.dev.yml'
override_file_debug='docker-compose.override.debug.yml'
override_file_unit_tests='docker-compose.override.unit_tests.yml'
override_file_unit_tests_cicd='docker-compose.override.unit_tests_cicd.yml'
override_file_integration_tests='docker-compose.override.integration_tests.yml'
Expand Down Expand Up @@ -77,19 +76,6 @@ function set_dev {
fi
}

function set_debug {
get_current
if [ "${current_env}" != debug ]
then
docker compose down
rm -f ${override_link}
ln -s ${override_file_debug} ${override_link}
echo "Now using 'debug' configuration."
else
echo "Already using 'debug' configuration."
fi
}

function set_unit_tests {
get_current
if [ "${current_env}" != unit_tests ]
Expand Down
32 changes: 0 additions & 32 deletions dojo/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""
import logging
import os
import socket

from django.core.wsgi import get_wsgi_application

Expand All @@ -27,37 +26,6 @@
# os.environ["DJANGO_SETTINGS_MODULE"] = "dojo.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dojo.settings.settings")


# Shouldn't apply to docker-compose dev mode (1 process, 1 thread), but may be needed when enabling debugging in other contexts
def is_debugger_listening(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
return s.connect_ex(("127.0.0.1", port))


debugpy_port = os.environ.get("DD_DEBUG_PORT") or 3000

# Checking for RUN_MAIN for those that want to run the app locally with the python interpreter instead of uwsgi
if os.environ.get("DD_DEBUG") == "True" and not os.getenv("RUN_MAIN") and is_debugger_listening(debugpy_port) != 0:
logger.info(f"DD_DEBUG is set to True, setting remote debugging on port {debugpy_port}")
try:
import debugpy # noqa: T100

# Required, otherwise debugpy will try to use the uwsgi binary as the python interpreter - https://github.com/microsoft/debugpy/issues/262
debugpy.configure({
"python": "python",
"subProcess": True,
})
debugpy.listen(("0.0.0.0", debugpy_port)) # noqa: T100
if os.environ.get("DD_DEBUG_WAIT_FOR_CLIENT") == "True":
logger.info(f"Waiting for the debugging client to connect on port {debugpy_port}")
debugpy.wait_for_client() # noqa: T100
logger.debug("Debugging client connected, resuming execution")
except RuntimeError as e:
if str(e) != "Can't listen for client connections: [Errno 98] Address already in use":
logger.exception(e)
except Exception as e:
logger.exception(e)

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
Expand Down
51 changes: 1 addition & 50 deletions readme-docs/DOCKER.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,56 +118,7 @@ id -u

## Run with Docker Compose in development mode with debugpy (remote debug)

The debug mode, offers out of the box a debugging server listening on port 3000

```zsh
# switch to debug configuration
docker/setEnv.sh debug
# then use docker-compose as usual
./dc-up.sh
```

This will run the application based on merged configurations from `docker-compose.yml` and `docker-compose.override.debug.yml`.

Alternatively (if using docker for windows for example), you can copy the override file over (and re-create the containers):
```
cp docker-compose.override.debug.yml docker-compose.override.yml
./dc-down.sh
./dc-up.sh
```

The default configuration assumes port 3000 by default for debug.

But you can pass additional environment variables:
- `DD_DEBUG_PORT` to define a different port
- `DD_DEBUG_WAIT_FOR_CLIENT` - That's if you want to debugger to wait, right before calling `django.core.wsgi.get_wsgi_application()`


### VS code
Add the following python debug configuration (You would have to install the `ms-python.python`. Other setup may work.)

```
{
"name": "Remote DefectDojo",
"type": "python",
"request": "attach",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"port": 3000,
"host": "localhost"
}
```

You can now launch the remote debug from VS Code, place your breakpoints and step through the code.

> At present, 2 caveats:
> - Static will not be present. You would have to `docker cp` them over from the nginx container
> - For some reason, the page loading may hang. You can stop the loading and reload, the page will ultimately appear.
Some users have found value in using debugpy. A short guide to setting this up can be found [here](https://testdriven.io/blog/django-debugging-vs-code/)

## Access the application
Navigate to <http://localhost:8080> where you can log in with username admin.
Expand Down
10 changes: 0 additions & 10 deletions readme-docs/KUBERNETES.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,6 @@ docker build --build-arg http_proxy=http://myproxy.com:8080 --build-arg https_pr
docker build --build-arg http_proxy=http://myproxy.com:8080 --build-arg https_proxy=http://myproxy.com:8080 -t defectdojo/defectdojo-nginx -f Dockerfile.nginx .
```

### Debug uWSGI with ptvsd

You can set breakpoints in code that is handled by uWSGI. The feature is meant to be used when you run locally on minikube, and mimics [what can be done with docker-compose](DOCKER.md#run-with-docker-compose-in-development-mode-with-ptvsd-remote-debug).

The port is currently hard-coded to 3000.

- In `values.yaml`, ensure the value for `enable_ptvsd` is set to `true` (the default is `false`). Make sure the change is taken into account in your deployment.
- Have `DD_DEBUG` set to `True`.
- Port forward port 3000 to the pod, such as `kubectl port-forward defectdojo-django-7886f49466-7cwm7 3000`.

### Upgrade the chart

If you want to change kubernetes configuration of use an updated docker image (evolution of defectDojo code), upgrade the application:
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ titlecase==2.4.1
social-auth-app-django==5.4.2
social-auth-core==4.5.4
gitpython==3.1.43
debugpy==1.8.5
python-gitlab==4.9.0
cpe==1.3.0
packageurl-python==0.15.6
Expand Down

0 comments on commit a6efe61

Please sign in to comment.