VS Code Remote Tunnel using sbatch

VS Code Remote Tunnel using sbatch

You can now use the Visual Studio (VS) Code Remote Development to connect from your local computer to O2. Once the connection is established, you will be able to use the resources allocated for your O2 Slurm job.

Before starting, please make sure:

1. Recent version of VS Code installed for your local machine.

2 Remote Explorer and Remote SSH extensions installed, if not already present by default

For details, please visit: System Requirements.

Note: The method described here will launch a single VS Code session at a time for each user on the cluster.

The following are the steps you need to follow to establish a connection between your local VSCode program and an O2 job/compute node.

1 Log in to O2 from your local machine:

$ ssh yourID@o2.hms.harvard.edu

2 Create a job script such as the vscode.sh example below (you only need to create the job script once)

#!/bin/bash #SBATCH -p priority # Change to desired partition #SBATCH --mem=4g # Change to desired RAM #SBATCH --time=06:00:00 # Change to desired wall-time #SBATCH -c 1 # Change to desired CPU cores #SBATCH -o vscode.out # Output file name set -o errexit -o nounset -o pipefail MY_SCRATCH="/n/scratch/users/${USER:0:1}/${USER}/vscode" mkdir -p $MY_SCRATCH echo $MY_SCRATCH #If not done yet, obtain the tarball and untar it in $MY_SCRATCH location to obtain the #executable, code if [ -e "$MY_SCRATCH/code" ]; then echo "using $MY_SCRATCH/code" else echo "install a new version of code" curl -Lk 'https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64' | tar -C $MY_SCRATCH -xzf - fi #And run it using the provider of your choice $MY_SCRATCH/code tunnel user login --provider microsoft #$MY_SCRATCH/code tunnel user login --provider github #Accept the license terms & launch the tunnel $MY_SCRATCH/code tunnel --accept-server-license-terms --name o2tunnel

 

NOTE:

The script above requires for you to have a scratch space. Please create one if you don’t have yet: https://harvardmed.atlassian.net/wiki/spaces/O2/pages/2652045313 Or you can also change the path for “MY_SCRATCH” to your desired location.

You will also need a Microsoft account to authenticate the tunnel. Your Harvard / HMS account will work and is the recommended option.

The Slurm job paratemers in the above script are an example. You can customize the job according to your needs.

3 Submit the job

$ sbatch vscode.sh Submitted batch job 4613516

4 Look at the output file created from the job (in this example “vscode.out”) and follow the instructions you will see “use a web browser to open the page https://microsoft.com/devicelogin and enter the code <some_code> to authenticate

$ tail -f vscode.out ... To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code ABCDEFGH to authenticate. Open a web browser, enter the URL, and the code. After authentication, wait a few seconds to a minute, and print the output file again: $ tail -f vscode.out * Visual Studio Code Server * * By using the software, you agree to * the Visual Studio Code Server License Terms (https://aka.ms/vscode-server-license) and * the Microsoft Privacy Statement (https://privacy.microsoft.com/en-US/privacystatement). * Open this link in your browser https://vscode.dev/tunnel/cannon/n/home01/jharvard/vscode

 

5 Connect local VS Code with the O2 job:

Once you have authenticated the tunnel to your VSCode job, on your local machine open VSCode and follow the steps below:

  1. In your local vscode (in your own laptop/desktop), add the Remote Tunnel extension (ms-vscode.remote-server only needed once)

    1. On the local VSCode, install Remote Tunnel extension

    2. Click on VS Code Account menu, choose “Turn on Remote Tunnel Access

  2. Connect to the cluster:

    1. Click on the bottom left corner

    2. Options will appear on the top text bar

    3. Select “Connect to Tunnel…”

image-20250626-173005.png

Then choose the authentication method that you used in vscode.job, microsoft or github

image-20250626-173043.png

 

  1. Click on the Remote Explorer icon and pull up the Remote Tunnel drop-down menu

  2. Click on o2tunnel to get connected to the remote machine either in the same VS Code window (indicated by ->) or a new one (icon besides ->).
    Prior to clicking, make sure you see: Remote -> Tunnels -> o2tunnel running

  3. Finally, when you get vscode connected, you can also open a terminal on vscode that will be running on the compute node where your submitted job is running.

 

6 Troubleshoot:

Why am I seeing warn error access singleton, retrying: the process holding the singleton lock file (pid=…) exited?

Please try deleting or renaming /home/$USER/.vscode/cli/tunnel-stable.lock and starting a new session.

 

Enjoy your work using your local VSCode on the compute node.