Skip to content

VSCode Breakpoints and Create auto Netbox User for developer and Chapter for Debeloper #80

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
29 changes: 29 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE

# Part of Setup.py and therefore important for plugin installation.
# README.md
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Is required for debugging Docker-Compose in VSCode.
{
"configurations": [
{

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

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

"django": true,

"justMyCode": true,
}
]
}
19 changes: 19 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Is required for debugging Docker-Compose in VSCode.
{
"version": "2.0.0",
"tasks": [
{
"label": "docker-run: debug",
"type": "docker-compose",
"dockerCompose": {
"up": {
"detached": true,
"build": true
},
"files": [
"${workspaceFolder}/develop/docker-compose.yml",
]
}
}
]
}
30 changes: 27 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
# General: The Makefile is used for plugin development is used to simplify the execution of command sequences.
# Recommended VSCode plugin: Makefile-Tools (extension ID: ms-vscode.makefile-tools)
# To trigger a Makefile command (target), the following line must be written in the terminal window of VSCode, for example:
# make cbuild

# Parameter
PYTHON_VER?=3.12
NETBOX_VER?=v4.0.2
NETBOX_VER?=v4.3.0

NAME=netbox-qrcode

COMPOSE_FILE=./develop/docker-compose.yml
COMPOSE_FILE_DEBUG=./develop/docker-compose-debug.yml
BUILD_NAME=netbox_qrcode
VERFILE=./netbox_qrcode/version.py


# Build Docker with the specific Python and Netbox version
cbuild:
docker-compose -f ${COMPOSE_FILE} \
-p ${BUILD_NAME} build \
--build-arg netbox_ver=${NETBOX_VER} \
--build-arg python_ver=${PYTHON_VER}

debug:
# Start Docker with terminal window output
debug: cbuild
@echo "Starting Netbox .. "
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} up

# Start Docker with terminal window output.
# Brakepoints in e.g. Python files are supported in VSCode. Changes in Python and HTML are applied after saving.
debug-vscode: cbuild
@echo "Starting Netbox debug for VSCode.. "
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} -f ${COMPOSE_FILE_DEBUG} up
# --build

# Start Docker without connecting to the terminal window. (Runs independently of the terminal window.).
start:
@echo "Starting Netbox in detached mode.. "
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} up -d

# Stop Docker Container
stop:
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} down

# Stop Docker and remove containers
destroy:
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} down
docker volume rm -f ${BUILD_NAME}_pgdata_netbox_qrcode

# Calls the Netbox shell. Exit with exit()
# NetBox includes a Python shell within which objects can be directly queried, created, modified, and deleted.
nbshell:
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} run netbox python manage.py nbshell

# Calls the Python shell. Exit with exit()
shell:
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} run netbox python manage.py shell

# To create the Django Superuser to be able to log on to Netbox Web.
adduser:
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} run netbox python manage.py createsuperuser

# Django collectstatic
collectstatic:
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} run netbox python manage.py collectstatic

# Migrate the Netbox system depending on the Django model
migrations:
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} up -d postgres
docker-compose -f ${COMPOSE_FILE} -p ${BUILD_NAME} \
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ Developing tools for this project based on [ntc-netbox-plugin-onboarding](https:

Issues and pull requests are welcomed.

## Development

You would like to help with the development of the plugin. Here you can find further information such as how to set up debugging in vscode and much more.

- [Go to Development >>](docs/README_Subpages/README_Development.md)

## Screenshots

Device QR code with text label
Expand Down
1 change: 1 addition & 0 deletions develop/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt

RUN pip install --upgrade pip
RUN pip install setuptools

# -------------------------------------------------------------------------------------
# Install NetBox
Expand Down
34 changes: 34 additions & 0 deletions develop/docker-compose-debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This Docker Compose Debug File supports you with debugging in VSCode For more details see README.md

# Overwrites parts of the service from the Docker-Compose file:
---
services:
netbox:
# The following commands are executed
# - Install Current Version of DebugPy for Debug in Visual Studio Code https://github.com/microsoft/debugpy
# - PYDEVD_DISABLE_FILE_VALIDATION=1 --> You’re telling the debugger to skip this validation and proceed with debugging
# - Migrate Netbox
# - Create a user (superuser) for Netbox / Django so that you can log in directly to Netbox. More Infos: makesuperuser.py
# - Start Netbox with Modul DebugPy (Xfrozen_modules=off --> Disable Frozen Moduls in DebugPy Mode; 5678 is Debug Port)
command: >
sh -c "echo Run Netbox Django in debug mode &&
pip install debugpy &&
PYDEVD_DISABLE_FILE_VALIDATION=1 &&
python manage.py migrate &&
python manage.py makesuperuser &&
python -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000 --insecure"
ports:
- '8000:8000'
- '5678:5678'
worker:
# Enables the synchronization of source code files with the Docker container during debugging.
# Without this option, changed files in the Docker container are only transferred to VSCode.
# Currently HTML files are changed live. After saving a *.py file, the container is automatically rebuilt. I don't know if this can be optimized. Greetings LHBL2003.
# https://www.docker.com/blog/docker-compose-experiment-sync-files-and-automatically-rebuild-services-with-watch-mode/
x-develop:
watch:
- action: sync
path: ../netbox_qrcode
target: /source/netbox_qrcode
#- action: rebuild
#path: package.json
10 changes: 9 additions & 1 deletion develop/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ services:
build:
context: ../
dockerfile: develop/Dockerfile

# The following commands are executed
# - Migrate Netbox
# - Create a user (superuser) for Netbox / Django so that you can log in directly to Netbox. More Infos: makesuperuser.py
# - Start Netbox
command: >
sh -c "python manage.py migrate &&
python manage.py makesuperuser &&
python manage.py runserver 0.0.0.0:8000"
ports:
- '8000:8000'
Expand All @@ -18,6 +24,8 @@ services:
volumes:
- ./configuration.py:/opt/netbox/netbox/netbox/configuration.py
- ../netbox_qrcode:/source/netbox_qrcode
# For automatic setup of the Django / Netbox SuperUser
- ../develop/management:/source/netbox_qrcode/management
tty: true
worker:
build:
Expand All @@ -33,7 +41,7 @@ services:
- ../netbox_qrcode:/source/netbox_qrcode
tty: true
postgres:
image: postgres:12
image: postgres:14
env_file: dev.env
volumes:
- pgdata_netbox_qrcode:/var/lib/postgresql/data
Expand Down
Empty file.
Empty file.
37 changes: 37 additions & 0 deletions develop/management/commands/makesuperuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This script creates the Django Super User which is also used for the first login to Netbox.
# This means that no user needs to be created in the development phase, as this is done when Docker-Compose is started.
# The script is terminated prematurely if a user already exists.
# User name and password see parameters below "Username" and "password"
#
# This script must be located in the Django plugin path .../management/comands at development time.
# e.g.: Docker-Compose volumes: ../develop/management:/source/netbox_qrcode/management
# e.g.: Docker-Compose command: "python manage.py makesuperuser".

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand

User = get_user_model()

class bcolors:
PURPLE = '\033[95m'
RED = '\033[91m'

class Command(BaseCommand):
def handle(self, *args, **options):
username = 'admin'
email = 'admin@admin.local'
password = 'admin'
try:
u = None
if not User.objects.filter(username=username).exists() and not User.objects.filter(is_superuser=True).exists():
print("admin user not found, creating one")

u = User.objects.create_superuser(username, email, password)
print(f"{bcolors.PURPLE}===================================")
print(f"{bcolors.PURPLE}A superuser for NetBox / Django '{username}' was created with email '{email}' and password '{password}'")
print(f"{bcolors.PURPLE}===================================")
else:
print(f"{bcolors.PURPLE}A superuser for NetBox / Django Admin user already exists or was created during the first Docker-Compose start.")
print(u)
except Exception as e:
print(f"{bcolors.RED}There was an error: {e}")
49 changes: 49 additions & 0 deletions docs/README_Subpages/README_Development.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[<< Back to README start](/README.md)
# Development
## Contributing
Developing tools for this project based on [ntc-netbox-plugin-onboarding](https://github.com/networktocode/ntc-netbox-plugin-onboarding) repo.

Issues and pull requests are welcomed.

## Recommended extensions for VSCode:
With the extension ID you can search for the extension more easily.
- GitHub Pull Request (Extension ID: GitHub.vscode-pull-request-github)
- Docker (Extension ID: ms-azuretools.vscode-docker)
- Python (Extension ID: ms-python.python)
- Python Debugger (Extension ID: ms-python.debugpy)
- Django (Extension ID: batisteo.vscode-django)
- Makefile-Tools (extension ID: ms-vscode.makefile-tools)

## Easy start of the development environment
Take a look at the topic "Makefile" and pay attention to the "Makefile" file in the project to be able to quickly start Netbox with the Docker-Compose.

## Default Login (Netbox, DB, PostgreSQL, Redis)
If the project is started via Docker-Compose, the first login to Netbox is possible with the login data from the makesuperuser.py file.
Other login data for the database, PostgreSQL and Redis can be found in the dev.env file.

Default Netbox Login (When start via Docker-Compose):
- User: admin
- PW: admin

## Debugging with Breakpoint in VSCode
The following describes how to start the debugging mode in VSCode.

*Start Container*
Write "make debug-vscode" without quotation marks in terminal window of VSCode (See also Makefile)
Wait until all containers are started and Starting development server at http://0.0.0.0:8000/ is displayed.

*Start Debug:*
- Go to "Run and Debug" and start the debug "Docker: Python -Django" (The footer of vsCode should become Orang.)
- Set a breakpoint in the code
- Open the NetBox page with the plugin.
- VSCode will stop at the stopping point.

If you change something in an HTML file, this will be displayed immediately after a reload of the website. If you change something in a *.py file, the web server is automatically restarted after saving the file. It may be advisable to deactivate this in VSCode under File --> Auto Save so that the web server does not restart so often.

*Helpful documentary:*
- https://medium.com/django-unleashed/debug-django-application-in-docker-container-using-vscode-ca5967340262
- https://testdriven.io/blog/django-debugging-vs-code/
- https://github.com/microsoft/debugpy
- https://docs.python.org/3/using/cmdline.html#cmdoption-X

[<< Back to README start](/README.md)