Difference between revisions of "VSCode"

From ScientificComputing
Jump to: navigation, search
(Workflow)
 
(36 intermediate revisions by the same user not shown)
Line 3: Line 3:
  
 
==Solution for using VSCode on an HPC cluster==
 
==Solution for using VSCode on an HPC cluster==
The main (closed source) branch of VSCode cannot be integrated with the batch system and the developers refuse to implement such features (https://github.com/microsoft/vscode-remote-release/issues/1722). But there is also an opensource version of VSCode. A fork of the opensource version called code-server allows users to run VSCode in a browser window, which resolves most issues related to running it on an HPC cluster.
+
The main branch of VSCode cannot be integrated nicely with a batch system and the developers refuse to implement such features. But there is also an opensource version of VSCode. A fork of the opensource version called code-server allows users to run VSCode in a browser window, which resolves most issues related to running it on an HPC cluster. With this solution you can run the code-server in a batch job, then create an SSH tunnel to the compute node and finally you can connect your local browser to the code-server instance running in the batch job.
  
 
==Preparation==
 
==Preparation==
The preparation steps only needs to be done once.
+
The preparation steps only need to be executed once. You need to carry out those steps to set up the basic configuration for your ETH account with regards to the code-server.
  
* Login to the cluster
+
* Login to the Euler cluster
* Switch to the new software stack, either using
+
* Start and interactive job with
 +
srun --ntasks=1 --time=00:10:00 --mem-per-cpu=2048 --pty bash
 +
* When using Euler, switch to the new software stack (in case you haven't set it as default yet), either using
 
  env2lmod
 
  env2lmod
 
for the current shell, or
 
for the current shell, or
 
  set_software_stack.sh new
 
  set_software_stack.sh new
to set it as permanent default
+
to set it as permanent default (when using this command, you need to logout and login again to make the change becoming active). This step can be omitted for Leonhard Open
* Load the modules required for code-server
+
* Load the modules for one of the code-server installations (use 3.12.0 if you use the script that automates creation of the SSH tunnel):
 
  module load gcc/6.3.0 code-server/3.9.3
 
  module load gcc/6.3.0 code-server/3.9.3
 +
module load gcc/6.3.0 code-server/3.11.1
 +
module load gcc/6.3.0 code-server/3.12.0
 
* Start the code-server once with the command <tt>code-server</tt>
 
* Start the code-server once with the command <tt>code-server</tt>
  [sfux@eu-g1-043-1 ~]$ code-server
+
 
  [2021-04-21T12:27:29.229Z] info  code-server 3.9.3 fe2dc2deb08e378069891b622bb62ad1d261d1b1
+
  [sfux@eu-ms-001-01 ~]$ code-server
  [2021-04-21T12:27:29.235Z] info  Using user-data-dir ~/.local/share/code-server
+
  [2021-11-02T10:01:45.407Z] info  code-server 3.12.0 4cd55f94c0a72f05c18cea070e10b969996614d2
  [2021-04-21T12:27:29.249Z] info  Using config file ~/.config/code-server/config.yaml
+
  [2021-11-02T10:01:45.409Z] info  Using user-data-dir ~/.local/share/code-server
  [2021-04-21T12:27:29.249Z] info  HTTP server listening on http://127.0.0.1:8080
+
  [2021-11-02T10:01:45.433Z] info  Using config file ~/.config/code-server/config.yaml
  [2021-04-21T12:27:29.249Z] info    - Authentication is enabled
+
  [2021-11-02T10:01:45.433Z] info  HTTP server listening on http://127.0.0.1:8080
  [2021-04-21T12:27:29.249Z] info      - Using password from ~/.config/code-server/config.yaml
+
  [2021-11-02T10:01:45.433Z] info    - Authentication is enabled
  [2021-04-21T12:27:29.249Z] info    - Not serving HTTPS
+
  [2021-11-02T10:01:45.433Z] info      - Using password from ~/.config/code-server/config.yaml
This will setup the local configuration (including a password for you) and store it in your home directory in $HOME/.config/code-server/config.yaml
+
  [2021-11-02T10:01:45.433Z] info    - Not serving HTTPS
* After the server started, terminate it with ctrl+c
+
[sfux@eu-ms-001-01 ~]$
 +
 
 +
This will setup the local configuration (including a password for you) and store it in your home directory in <tt>$HOME/.config/code-server/config.yaml</tt>
 +
* After the server started, terminate it with <tt>ctrl+c</tt>
  
 
==Workflow==
 
==Workflow==
* Login to the Euler cluster
+
The code-server workflow to use VSCode on the Euler cluster contains 3 parts
* Switch to the new software stack, either using
+
 
  env2lmod
+
# Starting the code-server in a batch job
for the current shell, or
+
# Creating an SSH tunnel between the local computer and the compute node, where the code-server is running
  set_software_stack.sh new
+
# Using the SSH tunnel to connect a browser running on your local computer with the code server running on the compute node
to set it as permanent default
+
 
* Load the modules required for code-server
+
Points 2. and 3. in the workflow are automated by the script start_vscode.sh shown below.
  module load gcc/6.3.0 code-server/3.9.3
+
 
* Start a batch job running the following shell script
+
===Installation of the script===
  #!/bin/bash
+
Download the repository with the command
 +
  git clone https://gitlab.ethz.ch/sfux/VSCode_remote_HPC.git
 +
 
 +
===Starting the code-server in a batch job===
 +
The start_vscode.sh script needs to be executed on your local computer. Please find below the list of options that can be used with the script:
 +
 
 +
$ ./start_vscode.sh --help
 +
./start_vscode.sh: Script to start a VSCode on Euler from a local computer
 +
 +
  Usage: start_vscode.sh [options]
 +
 +
Options:
 +
 +
        -u | --username      USERNAME        ETH username for SSH connection to Euler
 +
        -n | --numcores      NUM_CPU          Number of CPU cores to be used on the cluster
 +
        -W | --runtime        RUN_TIME        Run time limit for the code-server in hours and minutes HH:MM
 +
        -m | --memory        MEM_PER_CORE    Memory limit in MB per core
 +
        -b | --batchsys      BATCH_SYS        Batch system to use (LSF or SLURM)
 +
 +
Optional arguments:
 +
 +
        -c | --config        CONFIG_FILE      Configuration file for specifying options
 +
        -g | --numgpu        NUM_GPU          Number of GPUs to be used on the cluster
 +
        -h | --help                            Display help for this script and quit
 +
        -i | --interval      INTERVAL        Time interval for checking if the job on the cluster already started
 +
        -k | --key            SSH_KEY_PATH    Path to SSH key with non-standard name
 +
        -v | --version                        Display version of the script and exit
 +
 +
 +
  Examples:
 +
 +
        ./start_vscode.sh -u sfux -b SLURM -n 4 -W 04:00 -m 2048
 +
 +
        ./start_vscode.sh --username sfux --batchsys SLURM --numcores 2 --runtime 01:30 --memory 2048
 +
 +
        ./start_vscode.sh -c /c/Users/sfux/.vsc_config
 +
 +
  Format of configuration file:
 
   
 
   
  port=$((20000 + $RANDOM % 45000))
+
  VSC_USERNAME=""            # ETH username for SSH connection to Euler
  local_port=8899
+
  VSC_NUM_CPU=1              # Number of CPU cores to be used on the cluster
  ip=$(hostname -i)
+
  VSC_NUM_GPU=0              # Number of GPUs to be used on the cluster
  code-server --bind-addr=$(hostname -i):${port}
+
  VSC_RUN_TIME="01:00"        # Run time limit for the code-server in hours and minutes HH:MM
  echo "ssh -N -f -L localhost:${local_port}:${ip}:${port} ${USER}@euler.ethz.ch" > $HOME/VSCode_tunnel
+
VSC_MEM_PER_CPU_CORE=1024  # Memory limit in MB per core
* Setup an SSH tunnel using the command stored in the file $HOME/VSCode_tunnel in your home directory on the cluster. The command for setting up the SSH tunnel needs to be executed on your local computer
+
VSC_WAITING_INTERVAL=60    # Time interval to check if the job on the cluster already started
* Login with the password stored in your home directory  ~/.config/code-server/config.yaml
+
VSC_SSH_KEY_PATH=""        # Path to SSH key with non-standard name
 +
VSC_BATCH_SYSTEM="SLURM"    # Batch system to use (SLURM or LSF)
 +
 
 +
===Reconnect to a code-server session===
 +
When running the script, it creates a local file called reconnect_info in the installation directory, which contains all information regarding the used ports, the remote ip address, the command for the SSH tunnel and the URL for the browser. This information should be sufficient to reconnect to a code-server session if connection was lost.
 +
 
 +
===Cleanup after the job===
 +
Please note that after finishing your work with VSCode, you need to do some manual cleanup for terminating the SSH tunnel. For finding the process id of the SSH tunnel on your local computer, you can use the command
 +
 
 +
  ps -u | grep -m1 -- "-L" | grep -- "-N"
 +
 
 +
Once you know the process id of the SSH tunnel, you can terminate it with the command
 +
 
 +
kill PID
 +
 
 +
with <tt>PID</tt> being the process id of the SSH tunnel. Please also don't forget to terminate the batch job where the server is running, as it would otherwise continue to run until the runtime limit of the job is reached.
 +
 
 +
This example is from a Linux computer. If you are using git bash on Windows, then you can find the SSH process with the ps kommand and use kill to stop it.
 +
 
 +
==Extensions==
 +
Due to Microsoft's terms of service, extensions from their marketplace can not directly be used with code-server:
 +
 
 +
https://coder.com/docs/code-server/latest/FAQ#why-cant-code-server-use-microsofts-extension-marketplace
 +
 
 +
Instead one can use extensions from Open-VSX, where most extensions (those that are open source) from the Microsoft's extension marketplace are available:
 +
 
 +
https://open-vsx.org/
 +
 
 +
You can install extensions either via command line
 +
 
 +
https://coder.com/docs/code-server/latest/FAQ#how-do-i-install-an-extension
 +
https://coder.com/docs/code-server/latest/FAQ#how-do-i-install-an-extension-manually
 +
 
 +
Extensions are stored in <tt>$HOME/.local/share/code-server/extensions</tt> unless you set the environment variable <tt>$XDG_DATA_HOME</tt>, then they are stored in <tt>$XDG_DATA_HOME/code-server/extensions</tt>

Latest revision as of 07:54, 10 November 2022

Introduction

Visual Studio Code (VSCode) is a popular editor for developers. It has some plugins that allow users to run the editor on their local computer and connect via SSH to a remote system. In the case of an HPC cluster, this is suboptimal, as VSCode will just connect to one of the login nodes and start a large number of threads there. Our system administrators are regularly checking the login nodes and are warning users that are overloading a login node. Users that repeatedly overload login nodes will temporarily be banned from accessing the cluster.

Solution for using VSCode on an HPC cluster

The main branch of VSCode cannot be integrated nicely with a batch system and the developers refuse to implement such features. But there is also an opensource version of VSCode. A fork of the opensource version called code-server allows users to run VSCode in a browser window, which resolves most issues related to running it on an HPC cluster. With this solution you can run the code-server in a batch job, then create an SSH tunnel to the compute node and finally you can connect your local browser to the code-server instance running in the batch job.

Preparation

The preparation steps only need to be executed once. You need to carry out those steps to set up the basic configuration for your ETH account with regards to the code-server.

  • Login to the Euler cluster
  • Start and interactive job with
srun --ntasks=1 --time=00:10:00 --mem-per-cpu=2048 --pty bash
  • When using Euler, switch to the new software stack (in case you haven't set it as default yet), either using
env2lmod

for the current shell, or

set_software_stack.sh new

to set it as permanent default (when using this command, you need to logout and login again to make the change becoming active). This step can be omitted for Leonhard Open

  • Load the modules for one of the code-server installations (use 3.12.0 if you use the script that automates creation of the SSH tunnel):
module load gcc/6.3.0 code-server/3.9.3
module load gcc/6.3.0 code-server/3.11.1
module load gcc/6.3.0 code-server/3.12.0
  • Start the code-server once with the command code-server
[sfux@eu-ms-001-01 ~]$ code-server
[2021-11-02T10:01:45.407Z] info  code-server 3.12.0 4cd55f94c0a72f05c18cea070e10b969996614d2
[2021-11-02T10:01:45.409Z] info  Using user-data-dir ~/.local/share/code-server
[2021-11-02T10:01:45.433Z] info  Using config file ~/.config/code-server/config.yaml
[2021-11-02T10:01:45.433Z] info  HTTP server listening on http://127.0.0.1:8080
[2021-11-02T10:01:45.433Z] info    - Authentication is enabled
[2021-11-02T10:01:45.433Z] info      - Using password from ~/.config/code-server/config.yaml
[2021-11-02T10:01:45.433Z] info    - Not serving HTTPS
[sfux@eu-ms-001-01 ~]$

This will setup the local configuration (including a password for you) and store it in your home directory in $HOME/.config/code-server/config.yaml

  • After the server started, terminate it with ctrl+c

Workflow

The code-server workflow to use VSCode on the Euler cluster contains 3 parts

  1. Starting the code-server in a batch job
  2. Creating an SSH tunnel between the local computer and the compute node, where the code-server is running
  3. Using the SSH tunnel to connect a browser running on your local computer with the code server running on the compute node

Points 2. and 3. in the workflow are automated by the script start_vscode.sh shown below.

Installation of the script

Download the repository with the command

git clone https://gitlab.ethz.ch/sfux/VSCode_remote_HPC.git

Starting the code-server in a batch job

The start_vscode.sh script needs to be executed on your local computer. Please find below the list of options that can be used with the script:

$ ./start_vscode.sh --help
./start_vscode.sh: Script to start a VSCode on Euler from a local computer

Usage: start_vscode.sh [options]

Options:

        -u | --username       USERNAME         ETH username for SSH connection to Euler
        -n | --numcores       NUM_CPU          Number of CPU cores to be used on the cluster
        -W | --runtime        RUN_TIME         Run time limit for the code-server in hours and minutes HH:MM
        -m | --memory         MEM_PER_CORE     Memory limit in MB per core
        -b | --batchsys       BATCH_SYS        Batch system to use (LSF or SLURM)

Optional arguments:

        -c | --config         CONFIG_FILE      Configuration file for specifying options
        -g | --numgpu         NUM_GPU          Number of GPUs to be used on the cluster
        -h | --help                            Display help for this script and quit
        -i | --interval       INTERVAL         Time interval for checking if the job on the cluster already started
        -k | --key            SSH_KEY_PATH     Path to SSH key with non-standard name
        -v | --version                         Display version of the script and exit


Examples:

        ./start_vscode.sh -u sfux -b SLURM -n 4 -W 04:00 -m 2048

        ./start_vscode.sh --username sfux --batchsys SLURM --numcores 2 --runtime 01:30 --memory 2048

        ./start_vscode.sh -c /c/Users/sfux/.vsc_config

Format of configuration file:

VSC_USERNAME=""             # ETH username for SSH connection to Euler
VSC_NUM_CPU=1               # Number of CPU cores to be used on the cluster
VSC_NUM_GPU=0               # Number of GPUs to be used on the cluster
VSC_RUN_TIME="01:00"        # Run time limit for the code-server in hours and minutes HH:MM
VSC_MEM_PER_CPU_CORE=1024   # Memory limit in MB per core
VSC_WAITING_INTERVAL=60     # Time interval to check if the job on the cluster already started
VSC_SSH_KEY_PATH=""         # Path to SSH key with non-standard name
VSC_BATCH_SYSTEM="SLURM"    # Batch system to use (SLURM or LSF)

Reconnect to a code-server session

When running the script, it creates a local file called reconnect_info in the installation directory, which contains all information regarding the used ports, the remote ip address, the command for the SSH tunnel and the URL for the browser. This information should be sufficient to reconnect to a code-server session if connection was lost.

Cleanup after the job

Please note that after finishing your work with VSCode, you need to do some manual cleanup for terminating the SSH tunnel. For finding the process id of the SSH tunnel on your local computer, you can use the command

ps -u | grep -m1 -- "-L" | grep -- "-N"

Once you know the process id of the SSH tunnel, you can terminate it with the command

kill PID

with PID being the process id of the SSH tunnel. Please also don't forget to terminate the batch job where the server is running, as it would otherwise continue to run until the runtime limit of the job is reached.

This example is from a Linux computer. If you are using git bash on Windows, then you can find the SSH process with the ps kommand and use kill to stop it.

Extensions

Due to Microsoft's terms of service, extensions from their marketplace can not directly be used with code-server:

https://coder.com/docs/code-server/latest/FAQ#why-cant-code-server-use-microsofts-extension-marketplace

Instead one can use extensions from Open-VSX, where most extensions (those that are open source) from the Microsoft's extension marketplace are available:

https://open-vsx.org/

You can install extensions either via command line

https://coder.com/docs/code-server/latest/FAQ#how-do-i-install-an-extension
https://coder.com/docs/code-server/latest/FAQ#how-do-i-install-an-extension-manually

Extensions are stored in $HOME/.local/share/code-server/extensions unless you set the environment variable $XDG_DATA_HOME, then they are stored in $XDG_DATA_HOME/code-server/extensions