Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging Docker Compose for Netbox-qrcode #76

Open
LHBL2003 opened this issue Jun 14, 2024 · 7 comments
Open

Debugging Docker Compose for Netbox-qrcode #76

LHBL2003 opened this issue Jun 14, 2024 · 7 comments

Comments

@LHBL2003
Copy link
Contributor

I know this is more of a general knowledge, but I'm a C# / Visual Studio .Net kid.

I would like to develop one or the other to the plugin, but I don't know how to debug this.

I can get Docker Compose to work and with the help of your Github code I can always get it to start as the current Netbox version.

I also discovered a video on how to get Docker debugged with Python.
https://www.youtube.com/watch?v=jtBVppyfDbE

However, I don't understand how to get the debugging to work in the downloaded Gitub project.

Can you perhaps make a short video or explain this?

THX

@LHBL2003
Copy link
Contributor Author

So I think I'm on the right track, but apparently there's something I need to do to get Netbox to load during debug.
The debug in vscode stays in RUN, but I get the error message that the module Netbox is missing. Which leads me to the conclusion that you have to specify somehow in the debug that Netbox is also loaded.

I hope you use vscode :) and maybe you have the right .vscode files for me. So the files launch.json and tasks.json which are necessary for debugging.

Because that would make my life easier to help with the development of the plugin. I haven't done anything with Python yet, but if you can debug something it's a lot easier.

Exception has occurred: ModuleNotFoundError
No module named 'netbox'
  File "/source/netbox_qrcode/__init__.py", line 1, in <module>
    from netbox.plugins import PluginConfig
ModuleNotFoundError: No module named 'netbox'

I have created the following folder in the root folder:
.vscode

File: launch.json

{
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "netbox_qrcode"
                    }
                ],
                "projectType": "django"
            }
        }
    ]
}

File tasks.json:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "docker-build",
			"label": "docker-build",
			"platform": "python",
			"dockerBuild": {
				"tag": "debugdockercompose:latest",
				"dockerfile": "${workspaceFolder}/develop/Dockerfile",
				"context": "${workspaceFolder}",
				"pull": true
			}
		},
		{
			"type": "docker-run",
			"label": "docker-run: debug",
			"dependsOn": [
				"docker-build"
			],
			"python": {
				"args": [
					"runserver",
					"0.0.0.0:8000",
					"--nothreading",
					"--noreload"
				],
				"file": "/source/netbox_qrcode/__init__.py"
			}
		}
	]
}

@LHBL2003
Copy link
Contributor Author

@k01ek

I know you have little time, but I wanted to ask you if you use your development system with vscode and you also use the debug in vscode?

If so, I would be happy if you could send me your project, e.g. via ZIP, so that I can see what I am doing wrong?

Mail: Mister . Hempel at Gmail . com

@LHBL2003
Copy link
Contributor Author

LHBL2003 commented Jul 2, 2024

No one has any ideas?
That is apparently also wrong. This starts the containers for me now, but I can't get any further. I have no idea how to do this correctly.

File: launch.json

{
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "/source"
                    }
                ],
                "projectType": "django"
            }
        }
    ]
}

File tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        { 
            "label": "docker-run: debug",
            "type": "docker-compose",
            "dockerCompose": {
            "up": {
                "detached": true,
                "build": true
            },
            "files": [
                "${workspaceFolder}/develop/docker-compose.yml",
                ]
            }
        }
    ]
  }

image

@LHBL2003
Copy link
Contributor Author

LHBL2003 commented Jul 2, 2024

Maybe someone can save me from my torment. ;(

That is apparently also wrong. I can get to Settings.py but it cannot find Configuration.py because it is missing in the container. I also only have one container, as the Docker file is apparently not being processed. That was my attempt before the last post.

File: launch.json

{
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "/source"
                    }
                ],
                "projectType": "django"
            }
        }
    ]
}

File tasks.json:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "docker-build",
			"label": "docker-build",
			"platform": "python",
			"dockerBuild": {
				"tag": "debugdockercompose:latest",
				"dockerfile": "${workspaceFolder}/develop/Dockerfile",
				"context": "${workspaceFolder}",
				"pull": true
			}
		},
		{
			"type": "docker-run",
			"label": "docker-run: debug",
			"dependsOn": [
				"docker-build"
			],
			"python": {
				"args": [
					"runserver",
					"0.0.0.0:8000",
					"--nothreading",
					"--noreload"
				],
				"file": "/opt/netbox/netbox/manage.py"
			}
		}
	]
}

image
image

@LHBL2003
Copy link
Contributor Author

LHBL2003 commented Jul 2, 2024

I think I've now taken a step in the right direction, but it still doesn't work :D :(

I found this post.
Debug Django in a docker-compose container with VS Code:
https://miaoz2001.medium.com/debug-django-in-a-docker-compose-container-with-vs-code-88598dfc8be3

launch.json

{
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },


            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/source"
                }
            ],
            "django": true,
        }
    ]
}

Tasks.json

{
    "version": "2.0.0",
    "tasks": [
        { 
            "label": "docker-run: debug",
            "type": "docker-compose",
            "dockerCompose": {
            "up": {
                "detached": true,
                "build": true
            },
            "files": [
                "${workspaceFolder}/develop/docker-compose.yml",
                ]
            }
        }
    ]
  }

docker-compose-debug.yml

---
version: '3'
services:
  netbox:
    build:
      context: ../
      dockerfile: develop/Dockerfile
    command: >
      ["sh", "-c", "pip install debugpy -t /tmp && 
                    python manage.py migrate &&
                    python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 
                    python manage.py runserver 0.0.0.0:8000"
    ports:
      - '8000:8000'
      - '5678:5678'
    depends_on:

... The rest as in the docker-compose.yml

vsCode Terminal:

docker-compose -f develop/docker-compose.yml -f develop/docker-compose-debug.yml up --build

Now everything starts and the start also looks different in the vscode terminal view (more colorful). I can log on to http://localhost:8000/. But there is no waiting at breakpoints.

If I now click on Run and Debug (play symbol) I get this:

image

@LHBL2003
Copy link
Contributor Author

LHBL2003 commented Jul 3, 2024

I can't believe I got it :D
I'll create another empty VM and set everything up step by step so that others can also benefit from it.

image

@LHBL2003
Copy link
Contributor Author

LHBL2003 commented Jul 3, 2024

Memo to myself. Links where I had asked something about it:
netbox-community/netbox#16637
netbox-community/netbox#9397

microsoft/debugpy#1618 (Current location with a working solution. Until I integrate it into the plugin.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant