Submitting vscode job on slurm

From HPC Guide
Revision as of 12:31, 3 April 2025 by Dvory (talk | contribs) (Created page with "2 options hereby presented to use vscode 1. vscode via using an interactive job, and loading vscode module 2. vscode via ssh proxyjump =vscode module= This method may be to...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

2 options hereby presented to use vscode

1. vscode via using an interactive job, and loading vscode module

2. vscode via ssh proxyjump

vscode module

This method may be too slow for the users, as noted by user luxembourg, however possible.

User needs to:

Login to powerslurm with '-X' Request for interactive job with GUI,

srun --partition power-general --x11 --pty bash

Then on the compute node need to load module

module load vscode/vscode-1.98.2

Also need to define a cache dir, it may be done in one of the scratches, e.g.

export XDG_RUNTIME_DIR=/scratch200/dvory

So within the interactive job one may activate vscode by typing

code

vscode via ssh proxyjump

Both vscode and cursor behave similarly, and this approach is supposed to be good for both of them.

With this setup, a windows machine will be able to activate vscode/cursor in a compute node in the cluster, having the same slurm environment variables as defined in a batch script.

  • Every user may configure this, no need for root


In powerslurm-login node

One time setup

Install dropbear in userspace.

dropbear is used to launch a private SSH server inside the job

cd ~
mkdir -p dropbear/install

# Build it from source
curl -LO https://matt.ucc.asn.au/dropbear/releases/dropbear-2022.83.tar.bz2
tar -xjf dropbear-2022.83.tar.bz2
cd dropbear-2022.83
./configure --prefix=$HOME/dropbear/install
make && make install

# Generate host key
$HOME/dropbear/install/bin/dropbearkey -t rsa -f $HOME/dropbear/install/server-key

Build a script

Name of the script may be for example vscode_slurm_job.sh

#!/bin/bash
#SBATCH --job-name=vscode
#SBATCH --partition=power-general
#SBATCH --cpus-per-task=8
#SBATCH --mem=64G
#SBATCH --output=slurm_vscode.log

DROPBEAR="$HOME/dropbear/install"
SLURM_ENV_FILE="$HOME/.slurm-env.bash"

# 🧠 Derive unique port from SLURM_JOB_ID
PORT=$(( 40000 + SLURM_JOB_ID % 10000 ))

# Save SLURM-related env vars to a file
env | awk -F= '$1~/^(SLURM|CUDA|NVIDIA_)/ { print "export " $0 }' > "$SLURM_ENV_FILE"

# Write VS Code shell script
cat <<EOF > "$HOME/.vscode-shell"
#!/bin/bash
source $SLURM_ENV_FILE
export XDG_RUNTIME_DIR=/tmp/\$USER-vscode-runtime
mkdir -p \$XDG_RUNTIME_DIR
chmod 700 \$XDG_RUNTIME_DIR
exec bash
EOF

chmod +x "$HOME/.vscode-shell"

# 📝 Write SSH connection instructions to a file
HOSTNAME=$(hostname -f)
echo "VS Code SSH setup info for this job:" > $HOME/vscode-connection-info.txt
echo "--------------------------------------" >> $HOME/vscode-connection-info.txt
echo "HostName: $HOSTNAME" >> $HOME/vscode-connection-info.txt
echo "Port: $PORT" >> $HOME/vscode-connection-info.txt
echo "Username: $USER" >> $HOME/vscode-connection-info.txt
echo "RemoteCommand: ~/.vscode-shell" >> $HOME/vscode-connection-info.txt
echo "ProxyJump: $USER@powerslurm-login.tau.ac.il" >> $HOME/vscode-connection-info.txt
echo "" >> $HOME/vscode-connection-info.txt
echo "Suggested ~/.ssh/config entry:" >> $HOME/vscode-connection-info.txt
cat <<EOF >> $HOME/vscode-connection-info.txt

Host slurm-vscode
  HostName $HOSTNAME
  User $USER
  Port $PORT
  ProxyJump $USER@powerslurm-login.tau.ac.il
  RemoteCommand ~/.vscode-shell
  IdentityFile ~/.ssh/your_key
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
EOF

# Cleanup on exit
cleanup() {
    rm -f "$SLURM_ENV_FILE"
}
trap 'cleanup' SIGTERM SIGINT EXIT

# Launch dropbear
$DROPBEAR/sbin/dropbear -r $DROPBEAR/server-key -F -E -w -s -p $PORT

In windows

Generate an SSH key

Open PowerShell or Git Bash on Windows, and run:

ssh-keygen -t rsa -b 4096
   Accept the default path (C:\Users\YourName\.ssh\id_rsa)
   Choose a passphrase or leave empty

You now have:

   Private key: C:\Users\YourName\.ssh\id_rsa ← Keep this secure!
   Public key: C:\Users\YourName\.ssh\id_rsa.pub

Copy the content of id_rsa.pub to powerslurm-login

vim ~/.ssh/authorized_keys
# Paste the key and save

This key may also be copied to the compute node that is allocated for the job

Create ~\.ssh\config file

Set up ~/.ssh/config:

Create or edit C:\Users\<YourUsername>\.ssh\config, containing:

Host slurm-vscode
  HostName compute-0-xxx     # Replace with the compute node
  User dvory                 # Replace with actual username
  ProxyJump dvory@powerslurm-login.tau.ac.il
  Port 64321
  IdentityFile ~/.ssh/id_rsa      # Or correct private key
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

Set permissions of .ssh/config

Need to open cmd, and then run:

icacls "%USERPROFILE%\.ssh\id_rsa" /reset
icacls "%USERPROFILE%\.ssh\id_rsa" /inheritance:r
icacls "%USERPROFILE%\.ssh\id_rsa" /grant:r "%USERNAME%:F"
icacls "%USERPROFILE%\.ssh\config" /reset
icacls "%USERPROFILE%\.ssh\config" /inheritance:r
icacls "%USERPROFILE%\.ssh\config" /grant:r "%USERNAME%:F"

Download VS Code (https://code.visualstudio.com/) or Cursor (https://www.cursor.com/)


Open the application and install there, or make sure that thie application Remote-SSH is installed

Installing Remote-SSH extension

Launch your VS Code / Cursor app. 🔍 Open the Extensions Sidebar

   Click the square icon on the left sidebar (or press Ctrl+Shift+X)
   In the Search box, type:

Remote - SSH

📦 Install the Extension

   Find: "Remote - SSH by Microsoft"
   Click Install

Remote-SSH Extension in Marketplace

You’ll see a green install bar. Once done, you’ll see a new green "><" icon in the bottom-left — that’s the remote connection control.

Test connection

In VS Code or Cursor:

   Hit F1 → “Remote-SSH: Connect to Host”
   Select slurm-vscode

You should get connected to the compute node inside the running Slurm job.

Running vscode

On powerslurm-login

Submit the script:

sbatch vscode_slurm_job.sh

Check that the job is running, and find the allocated node:

squeue --me

The script that was submitted to run is supposed to generate a file named ~/vscode-connection-info.txt, containing all information needed for the proxyjump procedure.

Check compute node and port when the job is running, by running:

cat ~/vscode-connection-info.txt

You will see an output like:

VS Code SSH setup info for this job:
--------------------------------------
HostName: compute-0-499.power5
Port: 43104
Username: dvory
RemoteCommand: ~/.vscode-shell
ProxyJump: dvory@powerslurm-login.tau.ac.il

Suggested ~/.ssh/config entry:

Host slurm-vscode
  HostName compute-0-499.power5
  User dvory
  Port 43104
  ProxyJump dvory@powerslurm-login.tau.ac.il
  RemoteCommand ~/.vscode-shell
  IdentityFile ~/.ssh/your_key
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

And would need to change accordingly the ~/.ssh/config file on windows

Let us assume it is running on node compute-0-499, port 43104 as was returned in the above example

On windows

Change file ~/.ssh/config according to the actual compute node that was allocated to the job

Host slurm-vscode
  HostName compute-0-499     # Replace with the compute node
  User dvory                 # Replace with actual username
  ProxyJump dvory@powerslurm-login.tau.ac.il
  Port 43104                 # Replace with actual port
  IdentityFile ~/.ssh/id_rsa # Or correct private key
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null

In vscode ot cursor

Press F1 → type and select:

Remote-SSH: Connect to Host...

Or click the green "><" icon (bottom-left)

Choose a host defined in your ~/.ssh/config (e.g., slurm-vscode)