Skip to content

Commit

Permalink
dotnet: improved debugging in vscode
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra authored and WadeBarnes committed Aug 19, 2020
1 parent fbe11bf commit 9eeb060
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DOCKERHOST=192.168.65.3
LEDGER_URL=http://192.168.65.3:9000
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ Thumbs.db

# Agent log files
.logs

# Env file used for debugging
.env
60 changes: 54 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,64 @@
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
"name": ".NET Backchannel - Acme",
"type": "docker",
"containerName": "acme_agent",
"request": "launch",
"serverReadyAction": {
"pattern": "NEVER_OPEN",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
},
"preLaunchTask": "dotnet:debug:acme",
"removeContainerAfterDebug": true,
"netCore": {
"appProject": "${workspaceFolder}/aries-backchannels/dotnet/server/DotNet.Backchannel.csproj"
}
},
{
"name": ".NET Backchannel - Bob",
"type": "docker",
"containerName": "bob_agent",
"request": "launch",
"serverReadyAction": {
"pattern": "NEVER_OPEN",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
},
"preLaunchTask": "dotnet:debug:bob",
"removeContainerAfterDebug": true,
"netCore": {
"appProject": "${workspaceFolder}/aries-backchannels/dotnet/server/DotNet.Backchannel.csproj"
}
},
{
"name": ".NET Backchannel - Faber",
"type": "docker",
"containerName": "faber_agent",
"request": "launch",
"serverReadyAction": {
"pattern": "NEVER_OPEN",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
},
"preLaunchTask": "dotnet:debug:faber",
"removeContainerAfterDebug": true,
"netCore": {
"appProject": "${workspaceFolder}/aries-backchannels/dotnet/server/DotNet.Backchannel.csproj"
}
},
{
"name": "Docker .NET Core Launch",
"name": ".NET Backchannel - Mallory",
"type": "docker",
"containerName": "mallory_agent",
"request": "launch",
"preLaunchTask": "dotnet:docker-run:debug",
"serverReadyAction": {
"pattern": "NEVER_OPEN",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
},
"preLaunchTask": "dotnet:debug:mallory",
"removeContainerAfterDebug": true,
"netCore": {
"appProject": "${workspaceFolder}/aries-backchannels/dotnet/server/DotNet.Backchannel.csproj"
Expand Down
72 changes: 62 additions & 10 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"label": "dotnet:docker-build:debug",
"dependsOn": ["dotnet:build"],
"dockerBuild": {
"tag": "dotnet-agent-backchannel:dev",
"tag": "dotnet-agent-backchannel:debug",
"target": "base",
"dockerfile": "${workspaceFolder}/aries-backchannels/dotnet/Dockerfile.dotnet",
"context": "${workspaceFolder}/aries-backchannels",
Expand All @@ -55,19 +55,71 @@
},
{
"type": "docker-run",
"label": "dotnet:docker-run:debug",
"label": "dotnet:debug:acme",
"dependsOn": ["dotnet:docker-build:debug"],
"dockerRun": {
"remove": true,
"containerName": "acme_agent",
"envFiles": ["${workspaceFolder}/.env"],
"env": {
"DOCKERHOST": "${DOCKERHOST}",
"LEDGER_URL": "http://${DOCKERHOST}:9000"
"BACKCHANNEL_PORT": "9020"
},
"ports": [
{
"containerPort": 9020,
"hostPort": 9020
}
]
// Expects integer, but string works
"ports": [{ "containerPort": "9020-9029", "hostPort": "9020-9029" }]
},
"netCore": {
"appProject": "${workspaceFolder}/aries-backchannels/dotnet/server/DotNet.Backchannel.csproj",
"enableDebugging": true
}
},
{
"type": "docker-run",
"label": "dotnet:debug:bob",
"dependsOn": ["dotnet:docker-build:debug"],
"dockerRun": {
"remove": true,
"envFiles": ["${workspaceFolder}/.env"],
"containerName": "bob_agent",
"env": {
"BACKCHANNEL_PORT": "9030"
},
"ports": [{ "containerPort": "9030-9039", "hostPort": "9030-9039" }]
},
"netCore": {
"appProject": "${workspaceFolder}/aries-backchannels/dotnet/server/DotNet.Backchannel.csproj",
"enableDebugging": true
}
},
{
"type": "docker-run",
"label": "dotnet:debug:faber",
"dependsOn": ["dotnet:docker-build:debug"],
"dockerRun": {
"remove": true,
"envFiles": ["${workspaceFolder}/.env"],
"containerName": "faber_agent",
"env": {
"BACKCHANNEL_PORT": "9040"
},
"ports": [{ "containerPort": "9040-9049", "hostPort": "9040-9049" }]
},
"netCore": {
"appProject": "${workspaceFolder}/aries-backchannels/dotnet/server/DotNet.Backchannel.csproj",
"enableDebugging": true
}
},
{
"type": "docker-run",
"label": "dotnet:debug:mallory",
"dependsOn": ["dotnet:docker-build:debug"],
"dockerRun": {
"remove": true,
"envFiles": ["${workspaceFolder}/.env"],
"containerName": "mallory_agent",
"env": {
"BACKCHANNEL_PORT": "9050"
},
"ports": [{ "containerPort": "9050-9059", "hostPort": "9050-9059" }]
},
"netCore": {
"appProject": "${workspaceFolder}/aries-backchannels/dotnet/server/DotNet.Backchannel.csproj",
Expand Down
17 changes: 13 additions & 4 deletions Debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

## VSCode

### dotnet
### .NET

1. Follow the [Prerequisites](https://code.visualstudio.com/docs/containers/debug-netcore#_prerequisites) steps from this [VSCode tutorial](https://code.visualstudio.com/docs/containers/debug-netcore).
1. Set the `${DOCKERHOST}` variable in `tasks.json`. Theoretically VSCode allows to run commands to resolve variables at runtime, however this does not seem to work at the moment. You can retrieve the docker host by running `./manage dockerhost`.
1. Launch the "Docker .NET Core Launch" debugger from the Run view
1. Enjoy. You can now debug and set breakpoints
2. The docker containers need the `$DOCKERHOST` variable, and currently VSCode doesn't offer a way to do this dynamically.
1. `cp .env.example .env`
2. Replace `DOCKERHOST` with the output of `./manage dockerhost`, also replace the IP in `LEDGER_URL` with the output.
3. Launch the desired debuggers from the Run view. You can launch multiple debuggers at the same time:
- .NET Backchannel - Acme
- .NET Backchannel - Bob
- .NET Backchannel - Faber
- .NET Backchannel - Mallory
4. Run the tests using the manage script. The manage script will automatically detect if any of the agents is already running and skip the startup. You must make sure the backchannels that are passed to the `./manage run` script are the same as the backchannels started with the debugger.
- If you now, for example, run `./manage run -d dotnet -t @T001-API10-RFC0160` it will run the tests using the backchannels you started from the debugger.

For more information on debugging in VSCode see [the docs](https://code.visualstudio.com/docs/editor/debugging).
12 changes: 10 additions & 2 deletions aries-backchannels/dotnet/server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ public static void Main(int p = 9020, bool i = false)
// Catch all unhandled exceptions
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Program.SetEnvironment(p).Wait();
CreateHostBuilder(p).Build().Run();
var port = p;

// When debugging with VSCode we can't pass the -p argument.
if (Environment.GetEnvironmentVariable("BACKCHANNEL_PORT") != null)
{
port = Int32.Parse(Environment.GetEnvironmentVariable("BACKCHANNEL_PORT"));
}

Program.SetEnvironment(port).Wait();
CreateHostBuilder(port).Build().Run();
}

public static async Task SetEnvironment(int port)
Expand Down
31 changes: 21 additions & 10 deletions manage
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,34 @@ waitForAgent(){
)
}

startAgent() {
local NAME=$1
local CONTAINER_NAME=$2
local IMAGE_NAME=$3
local PORT_RANGE=$4
local BACKCHANNEL_PORT=$5

if [ ! "$(docker ps -q -f name=$CONTAINER_NAME)" ]; then
echo "Starting ${NAME} Agent ..."
docker run -d --rm --name "${CONTAINER_NAME}" --expose "${PORT_RANGE}" -p "${PORT_RANGE}:${PORT_RANGE}" -e "DOCKERHOST=${DOCKERHOST}" -e "LEDGER_URL=http://${DOCKERHOST}:9000" "${IMAGE_NAME}" -p "${BACKCHANNEL_PORT}" -i false >/dev/null
else
echo "${NAME} Agent already running, skipping..."
fi
}

runTests() {
runArgs=${@}

export ACME_AGENT=${ACME_AGENT:-${ACME}-agent-backchannel}
export BOB_AGENT=${BOB_AGENT:-${BOB}-agent-backchannel}
export FABER_AGENT=${FABER_AGENT:-${FABER}-agent-backchannel}
export MALLORY_AGENT=${MALLORY_AGENT:-${MALLORY}-agent-backchannel}

echo "Starting Acme Agent ..."
docker run -d --rm --name acme_agent --expose 9020-9029 -p 9020-9029:9020-9029 -e "DOCKERHOST=${DOCKERHOST}" -e "LEDGER_URL=http://${DOCKERHOST}:9000" ${ACME_AGENT} -p 9020 -i false >/dev/null
echo "Starting Bob Agent ..."
docker run -d --rm --name bob_agent --expose 9030-9039 -p 9030-9039:9030-9039 -e "DOCKERHOST=${DOCKERHOST}" -e "LEDGER_URL=http://${DOCKERHOST}:9000" ${BOB_AGENT} -p 9030 -i false >/dev/null
echo "Starting Faber Agent ..."
docker run -d --rm --name faber_agent --expose 9040-9049 -p 9040-9049:9040-9049 -e "DOCKERHOST=${DOCKERHOST}" -e "LEDGER_URL=http://${DOCKERHOST}:9000" ${FABER_AGENT} -p 9040 -i false >/dev/null
echo "Starting Mallory Agent ..."
docker run -d --rm --name mallory_agent --expose 9050-9059 -p 9050-9059:9050-9059 -e "DOCKERHOST=${DOCKERHOST}" -e "LEDGER_URL=http://${DOCKERHOST}:9000" ${MALLORY_AGENT} -p 9050 -i false >/dev/null


startAgent Acme acme_agent "$ACME_AGENT" "9020-9029" 9020
startAgent Bob bob_agent "$BOB_AGENT" "9030-9039" 9030
startAgent Faber faber_agent "$FABER_AGENT" "9040-9049" 9040
startAgent Mallory mallory_agent "$MALLORY_AGENT" "9050-9059" 9050

echo
waitForAgent Acme 9020
waitForAgent Bob 9030
Expand Down
2 changes: 1 addition & 1 deletion openapi-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ servers:
port:
description: >
The port to use for the backchannel
* Alice: 9020
* Acme: 9020
* Bob: 9030
* Faber: 9040
* Mallory: 9050
Expand Down

0 comments on commit 9eeb060

Please sign in to comment.