debug with VScode a fastAPI project built with docker-compose with a postgresql database - visual-studio-code

I would like to be able to make my code stop on breakpoints with VScode. My project is built with docker-compose and works without debugging on port 8000.
Here are my configurations files:
docker-compose:
version: '3.4'
services:
murmurside:
image: murmurside
build: ./murmur_side
command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m uvicorn app.main:app --host 0.0.0.0 --port 8000"]
volumes:
- ./murmur_side/:/murmur_side/
ports:
- 8000:8000
- 5678:5678
environment:
- DATABASE_URL=postgresql://USERNAME:PASSWORD#db/fastapi_db_2
db:
image: postgres:13-alpine
volumes:
- postgres_data2:/var/lib/postgresql/data/
expose:
- 5432
environment:
- POSTGRES_USER=USERNAME
- POSTGRES_PASSWORD=PASSWORD
- POSTGRES_DB=fastapi_db_2
volumes:
postgres_data2:
dockerfile :
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10-slim
EXPOSE 8000
WORKDIR /murmur_side
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
COPY . /murmur_side
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /murmur_side
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "-k", "uvicorn.workers.UvicornWorker", "app.main:app"]
launch.json :
I tested a 'launch' configuration but the debugger then bumps on the database related code. It does not seem to properly link to the database : after DATABASE_URL = os.getenv("DATABASE_URL") DATABASE_URL stays empty.
{
"configurations": [
{
"name": "Docker: Python - Fastapi",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "fastapi"
}
}
]
}
I also tested an 'attach' configuration. In that case, I get a debugger container launched at a random port but I get nothing when I browse to 127.0.0.1:randomPort
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 8000 # I also tried with 5678
},
"preLaunchTask": "docker-run: debug",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/murmur_side"
}
]
}
]
}
On this project I see that they added database credentials in the tasks.json. But I did not find any documentation stating that elsewhere. I tried that but I am unsure for the options other than username, password and dbname since I did not have to mention them in the docker-compose. Maybe I am also mistaking on the ports since there are several ones.
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "sd4timapi_pip_debug:latest",
"dockerfile": "${workspaceFolder}/murmur_side/Dockerfile",
"context": "${workspaceFolder}/murmur_side/",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": [
"docker-build"
],
"dockerRun": { # I also tried without this section
"image": "sd4timapi_pip_debug:latest",
"volumes": [
{
"containerPath": "/murmur_side/",
"localPath": "${workspaceFolder}/murmur_side/"
}
],
"ports": [
{
"containerPort": 8000,
"hostPort": 8001, # because it doesn't allow me to put 8000 : "port is already allocated"
"protocol": "tcp"
}
],
"env": {
"APP_PORT": "8000", #UNSURE
"DEBUG": "TRUE",
"ENVIRONMENT": "local", #UNSURE
"POSTGRES_USER": "USERNAME",
"POSTGRES_PASS": "PASSWORD",
"POSTGRES_DBNAME": "fastapi_db_2",
"POSTGRES_HOST": "db_host", #UNSURE
"POSTGRES_PORT": "5432", #UNSURE
"POSTGRES_APPLICATION_NAME": "sd4timapi_pip_debug", #UNSURE
}
},
"python": {
"args": [
"app.main:app",
"--host",
"0.0.0.0",
"--port",
"8000"
],
"module": "uvicorn"
}
}
]
}

This works for me, for debugging my FastAPI app in VS Code.
Here's the ".vscode/launch.json" file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
// Debug with FastAPI
{
"name": "Python: FastAPI",
"type": "python",
"request": "launch",
"module": "uvicorn",
"args": [
"app.main:app",
"--host",
"0.0.0.0",
"--port",
"81",
"--reload"
],
"jinja": true,
"justMyCode": false
},
]
}
I get the following messages in the VS Code terminal logs, and then I can debug with breakpoints, etc.
Go to http://localhost:81/docs in your browser since I deployed to port 81.
INFO: Will watch for changes in these directories: ['/workspace']
INFO: Uvicorn running on http://0.0.0.0:81 (Press CTRL+C to quit)
INFO: Started reloader process [13707] using WatchFiles
INFO: Started server process [13801]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:36070 - "GET /docs HTTP/1.1" 200 OK

Related

How to attach debugger to yarn start command

I have the following command yarn start-package with package being the project for package
I have configured the following launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Package Debuger",
"preLaunchTask": "yarn start-package"
}
]
}
I have configured the following tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "yarn start-package",
"type": "npm",
"script": "start",
"path": "packages/package",
"problemMatcher": []
}
]
}
Now whenever i start debugger, it will never attach because the prelaunch task is starting the developing server and never finishes. How can i attach the debugger to the yarn start-package command correctly?
Note: I am using wsl for running the yarn command if it matters

How do I setup VSCode to run and debug KeystoneJS application

I am trying to setup VSCode to run and debug my KeystoneJS application.
I currently run the application using npm run dev or yarn dev - in package.json, the dev script is set like this:
"scripts": {
"dev": "cross-env NODE_ENV=development DISABLE_LOGGING=true keystone dev"
},
If I try to run cross-env NODE_ENV=development DISABLE_LOGGING=true keystone dev from my prompt, I get the error, command not found. I would love to understand why this is not working...
I tried to setup my debug configuration in launch.json like this:
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/keystone",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceFolder}",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"PORT":"3030",
"NODE_ENV":"development",
"DISABLE_LOGGING":"true"
}
}
]
}
but it returns the error
this is how you can do this by changing npm script for dev
"dev": "cross-env PORT=4000 NODE_ENV=development NODE_OPTIONS=--inspect DISABLE_LOGGING=true keystone dev",
NODE_OPTIONS=--inspect or NODE_OPTIONS=--inspect-brk does the magic.
You must do this after cross-env as indicated above and not like below.
"dev": "NODE_OPTIONS=--inspect cross-env PORT=4000 NODE_ENV=development DISABLE_LOGGING=true keystone dev", (does not work)
Edit: 8 may
You can use following config in launch.json in vscode, (ideally the npm script should be called debug)
With NPM
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"dev"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**"
]
}
]
}
with YARN
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
],
"port": 9229,
"skipFiles": [
"<node_internals>/**"
]
}
]
}
change port only if you change port in NODE_OPTIONS in package.json script

What are the correct beginsPattern and endsPattern for a background Task in VSCode?

I have a static website (i.e. just html and client side JavaScript) that I serve with python while debugging locally. I have a VSCode task that will start python correctly and am trying to set that task as the preLaunchTask on a Debugger for Chrome launch task. The desired behavior is that whenever I start debugging the serve task below ensures the site is being served.
If I understand background tasks correctly one can set a beginsPattern and endsPattern to signal state changes.
I am expecting that when python echos
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
to stdout that the problemMatcher below would signal to the launch task that it had started. Instead, the launch task waits forever, and doesn't proceed until the task's shell command is terminated.
Can tasks be configured to achieve this sort of behavior?
Launch Configuration
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8000",
"webRoot": "${workspaceFolder}/webroot",
"preLaunchTask": "serve"
}
]
}
Serve Task
{
"version": "2.0.0",
"tasks": [
{
"label": "serve",
"type": "shell",
"command": "python3 -m http.server",
"windows": {
"command": "py -m http.server"
},
"isBackground": true,
"options": {
"cwd": "${workspaceFolder}/webroot"
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "dedicated"
},
"problemMatcher": {
"owner": "custom",
"pattern":[
{
"regexp": "^([^\\s].*)$",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"activeOnStart": true,
"beginsPattern":"^Serving HTTP (.*)$",
"endsPattern":"^Keyboard interrupt received, exiting(.*)$"
}
}
}
]
}
So we also had a similar problem: wanted to set up a Debugger on a Django app running inside Docker. On my setup, the debugger launched a preLaunchTask which starts the remote interpreter debugger, among other things (like installing ptvsd.
Original Steps:
preLaunchTask calls a script (./run-debug.sh).
This script calls remote debugger with this command:
docker container exec -it my_app python debug.py runserver --noreload --nothreading 0.0.0.0:8000
On the debug.py file, there's a print statement to know that the debugger started.
That didn't work, apparently, VSCode doesn't catch the output of the debugger. Instead, on the run-debug.sh file I added an echo statement: Starting debugger session: which VSCode caught ^_^. That fixed the issue for me.
tasks.json, relevant problem matcher:
"problemMatcher": {
"pattern": [
{
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"beginsPattern": "^Starting debugger session:",
"endsPattern": ".",
}
}
run-debug.sh script, relevant part:
# Start remote process
echo 'Starting debugger session:' #VSCode beginsPattern will catch this!
docker container exec -it my_app python debug.py runserver --noreload --nothreading 0.0.0.0:8000

Configuring Laravel Homestead (Vagrant) and xdebug on Visual Code

I have been trying to configure xDebug on VSCode with the "PHP Debug" extension to work with my local Homestead. Somehow it doesn't work at all. I've been trying different configuration changes but to no success.
Here are my config on the Vagrant box and in VSCode:
/etc/php/7.1/fpm/conf.d/20-xdebug.ini
zend_extension=/usr/lib/php/20160303/xdebug.so
xdebug.remote_enable = 1
xdebug.remote_connect_back = 0
xdebug.remote_host = 192.168.0.104
xdebug.remote_port = 9000
xdebug.max_nesting_level = 512
xdebug.remote_handler = "dbgp"
xdebug.remote_log=/var/log/xdebug.log
And here is my config in VSCode:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"serverSourceRoot": "/home/vagrant/projects/Projectname",
"localSourceRoot": "${workspaceRoot}",
"port": 9000,
"log": true
}
]
}
When I start a debugging session in VSCode I get this output in the debug window:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
But when running my app it doesn't stop at any of my breakpoints.
Any idea on what could be wrong? Do I need to map any port of my VM to my host in Homestead.yaml?
I know i'm late to answer this question, but maybe this post helps others to save time:
I was able to debug in PHP code VSCode using vagrant doing the following steps:
Install the VSCode extension "PHP Debug"
Edit the "launch.json" file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"localSourceRoot": "/your/host/php/dev/path/to/project-name",
"serverSourceRoot": "/home/vagrant/dev/project-name"
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
Connect via ssh to the homestead machine executing
vagrant ssh
go to the folder
cd /etc/php/7.1/fpm/conf.d/
Edit the file "20-xdebug.ini" and validate that the port 9000 is configured
Execute sudo vim 20-xdebug.ini.
Edit the lines:
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.max_nesting_level = 500
xdebug.remote_port = 9000
Save and exit.
Press the [Escape] key.
Type : SHIFT Z Z
Reboot PHP executing:
sudo service php7.1-fpm restart
hope it helps!

sensu-client check-memory sample work working

I am trying to get sensu working.
The following is the sensu-client.log
ubuntu#ip:~$ sudo tail -f /var/log/sensu/sensu-client.log
{"timestamp":"2016-09-27T16:07:37.628182-0400","level":"info","message":"completing checks in progress","checks_in_progress":[]}
{"timestamp":"2016-09-27T16:07:38.128912-0400","level":"info","message":"closing client tcp and udp sockets"}
{"timestamp":"2016-09-27T16:07:38.129275-0400","level":"warn","message":"stopping reactor"}
{"timestamp":"2016-09-27T16:07:39.224377-0400","level":"warn","message":"loading config file","file":"/etc/sensu/config.json"}
{"timestamp":"2016-09-27T16:07:39.224487-0400","level":"warn","message":"loading config files from directory","directory":"/etc/sensu/conf.d"}
{"timestamp":"2016-09-27T16:07:39.224528-0400","level":"warn","message":"loading config file","file":"/etc/sensu/conf.d/check_mem.json"}
{"timestamp":"2016-09-27T16:07:39.224573-0400","level":"warn","message":"config file applied changes","file":"/etc/sensu/conf.d/check_mem.json","changes":{}}
{"timestamp":"2016-09-27T16:07:39.224618-0400","level":"warn","message":"applied sensu client overrides","client":{"name":"localhost","address":"127.0.0.1","subscriptions":["test","client:localhost"]}}
{"timestamp":"2016-09-27T16:07:39.230963-0400","level":"warn","message":"loading extension files from directory","directory":"/etc/sensu/extensions"}
{"timestamp":"2016-09-27T16:07:39.231048-0400","level":"info","message":"configuring sensu spawn","settings":{"limit":12}}
/etc/sensu/client.json contains
{
"rabbitmq": {
"host": "ipaddressofsensuserver",
"port": 5672,
"user": "username",
"password": "password",
"vhost": "/sensu"
},
"api": {
"host": "localhost",
"port": 4567
},
"checks": {
"test": {
"command": "echo -n OK",
"subscribers": [
"test"
],
"interval": 60
},
"memory-percentage": {
"command": "check-memory-percent.sh -w 50 -c 70",
"interval": 10,
"subscribers": [
"test"
]
}
},
"client": {
"name": "localhost",
"address": "127.0.0.1",
"subscriptions": [
"test"
]
}
}
I have copied check-memory-present.sh into /etc/sensu/conf.d folder
I was expecting the log file to run check-memory-percent every 10 seconds. What am I missing here ?
The Sensu client cannot operate entirely independent of the server, but it can schedule its own checks to run and have them be sent to the server through the transport (RabbitMQ in this case). You'll have to add "standalone": true to the check configuration in order to have this take effect, and then restart the sensu-client service.
So, the file /etc/sensu/conf.d/check_mem.json should look something like:
"checks": {
"memory-percentage": {
"command": "/etc/sensu/conf.d/check-memory-percent.sh -w 50 -c 70",
"interval": 10,
"standalone": true
}
}
Remember to remove the block from /etc/sensu/client.json as well, as you may get unexpected results if you have the same check name defined multiple times.
In Client.json, under "client", you need to add the subscriptions. Like in the example here. It should match the definition of "subscribers" for your check.