Chun Sing Tsui

Coding, Technology, and other Interests

DataPower GatewayScript Remote Debugging With Visual Studio Code

This short guide will show how to connect a remote debugger like Visual Studio Code or Chrome DevTools to help debug your GatewayScript, allowing you to inspect variables and step through lines of code to help with troubleshooting and development.

Enabling GatewayScript Remote Debugging

First we need to enable remote debugging. Search gatewayscript remote debugger and set to enabled. Make sure the port is accessible from your local machine.

For example, if DataPower is running on local Docker, use docker run ... -p 9229:9229 ... to expose the port. If running on Kubernetes/Openshift, use kubectl port-forward dp-pod 9229:9229 so it is accessible via localhost.

remote-debugging

Then in the script that you want to debug, add in the debugger; statement to trigger the debugger during execution.

gwscript-debugger

Also, we need to enable debugging for the GatewayScript Action.

action-debugging

Connecting with Visual Studio Code

First we need to create a launch.json file to tell VS Code how to connect to the DP Debugging interface:

vscode-debug-launch

Then use the node type and inspector protocol in the configuration as follows. Set timeout to be a larger number so it won’t time out while you trigger the policy/gatewayscript action.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Attach to DP",
            "address": "localhost",
            "port": "9229",
            "protocol": "inspector",
            "timeout": 100000
          }
    ]
}

Press Run to start the debugger, and then trigger your gatewayscript action. You’ll then be able to step through the code or inspect variables.

vscode-debug-statement

You can also see the status of the debug action in DataPower by searching for Debug Action Status to see any live debug sessions.

vscode-debug-statement

Connecting with Chrome DevTools

Connecting with Chrome DevTools is quite straight-forward. Navigate to chrome://inspect/#devices, and press Configure.. to add your debugging address/port (e.g. localhost:9229) to the list if it doesn’t already exist. When the GWS action is triggered, the debug session should show up and you can then click on inspect to show the debugger.

vscode-debug-statement

References

https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.7.0/com.ibm.dp.doc/debugger_remotedebugging.html

https://github.com/ibm-datapower/datapower-tutorials/blob/master/gatewayscript-remote-debugging/gateway-script-remote-debugging.md

https://code.visualstudio.com/docs/nodejs/nodejs-debugging

Commonly Used Commands

Here’s a short cheatsheet for some useful commands when working with Linux/Unix systems.

Get specific column using cut. Tab is the default delimiter in cut

1
cat content | cut -d 'delimiter' -f <field number>

Output specific column using awk

1
cat content | awk -F/ '{print $1}'

Base64 encode (-n for no newline)

1
echo -n 'admin:password' | base64

Create gzipped tar

1
tar -czvf archive.tgz file1 dir2

Extract gzipped tar

1
2
# Output dir has to exist
tar -xzvf archive.tgz -C targetdir

Listening ports

1
2
3
4
5
sudo netstat -plnt

# or

sudo lsof -i -P | grep -i 'listen'

Disk usage sorted by size

1
du -hs * | sort -h

Xargs to pass arguments from stdin

1
xargs brew install < list.txt

Heredoc Syntax to create multi-line file from command line

1
2
3
4
5
6
7
8
9
10
11
12
cat <<EOT > app-pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: my-app-ui
  labels:
    app: my-app
spec:
  containers:
    - name: nginx-server
      image: nginx
EOT

Working with Docker

1
2
3
4
5
6
7
8
# Remove stopped containers and all unused images
docker system prune -a

# Remove certain images 
docker images -a | grep "pattern" | awk '{print $3}' | xargs docker rmi

# Remove all images
docker rmi $(docker images -a -q)

Working with systemd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# List all services
systemctl list-units --type=service

# Restart
systemctl restart application.service

# Get logs by the service unit
journalctl -u nginx.service

# Get data from yesterday
journalctl --since yesterday

# Get data from specific timestamps
journalctl --since "2015-01-10" --until "2015-01-11 03:00"

# Get log data from previous boot
journalctl -b -1

Working with Openshift

1
2
3
4
5
6
7
# Get list of users with IAM in the name and put them in a group
# and bind the group with cluster-admin. Use space in awk split
oc adm groups new cloudiam

oc get users | grep IAM | awk -F '[[:space:]][[:space:]]+' '{print $1}' | xargs -I '{}' oc adm groups add-users cloudiam '{}'

oc adm policy add-cluster-role-to-group cluster-admin cloudiam

Virtual Environment Cheatsheet

For those who work with virual environments when developing, here’s a handy cheatsheet in case you need to switch between various virt-envs.

RVM for Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# List existing environments
rvm list

# Create new environment
rvm install 2.6.3

# Set default
rvm --default use 2.6.3

# Switch back to system's ruby
rvm use system

# Use a specific version
rvm use 2.1.1

anaconda for Python

1
2
3
4
5
6
7
8
9
10
11
# List existing environments
conda env list

# Create new environment
conda create --name py38 python=3.8

# Activate environment
conda activate py38

# Deactivate environment
conda deactivate

virtualenv for Python

1
2
3
4
5
6
7
8
9
10
11
# Create new environment (using the current Python version)
virtualenv my-env

# Create new environment (using a different Python version)
virtualenv -p /usr/bin/python2.7 my-env

# Activate new environment
source my-env/bin/activate

# Deactivate active environment
deactivate