Difference between revisions of "VSCode"

From ScientificComputing
Jump to: navigation, search
(Preparation)
Line 6: Line 6:
  
 
==Preparation==
 
==Preparation==
The preparation steps only need to be executed once. It needs to be carried out to setup the basic configuration for this particular user with regards to the code-server.
+
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 cluster
Line 31: Line 31:
  
 
==Workflow==
 
==Workflow==
 +
The code-server workflow to use VSCode on the Euler cluster contains 3 parts
 +
 +
# Starting the code-server in a batch job
 +
# Creating an SSH tunnel between the local computer and the compute node, where the code-server is running
 +
# Using the SSH tunnel to connect a browser running on your local computer with the code server running on the compute node
 +
 +
===Starting the code-server in a batch job===
 
* Login to the Euler cluster
 
* Login to the Euler cluster
 
* Switch to the new software stack, either using
 
* Switch to the new software stack, either using
Line 39: Line 46:
 
* Load the modules required for code-server
 
* Load the modules required for code-server
 
  module load gcc/6.3.0 code-server/3.9.3
 
  module load gcc/6.3.0 code-server/3.9.3
* Start a batch job running the following shell script
+
* Create a shell script (run_code_server.sh) with the following content:
 
  #!/bin/bash
 
  #!/bin/bash
 
   
 
   
Line 47: Line 54:
 
  echo "ssh -N -f -L localhost:${local_port}:${ip}:${port} ${USER}@euler.ethz.ch" > $HOME/VSCode_tunnel
 
  echo "ssh -N -f -L localhost:${local_port}:${ip}:${port} ${USER}@euler.ethz.ch" > $HOME/VSCode_tunnel
 
  code-server --bind-addr=${ip}:${port}
 
  code-server --bind-addr=${ip}:${port}
* Setup an SSH tunnel using the command stored in the file <tt>$HOME/VSCode_tunnel</tt> in your home directory on the cluster. The command for setting up the SSH tunnel needs to be executed on your local computer
+
 
 +
This script will determine the port and the ip address of the compute node (required for the SSH tunnel) and put together the command for setting up the SSH tunnel and store it in a file $HOME/VSCode_tunnel in your home directory on the cluster. The script will also start the code server, which is then running in a batch job.
 +
 
 +
* Run the script in a batch job. For example (the example command assumes that you run the bsub command in the directory where the run_code_server.sh script is stored):
 +
 
 +
bsub -n 1 -W 1:00 -R "rusage[mem=2800]" ./run_code_server.sh
 +
 
 +
===Setting up the SSH tunnel===
 +
The SSH tunnel is required to connect your local computer with the compute node, that is running the code-server.
 +
* Login to the cluster and run the command <tt>cat $HOME/VSCode_tunnel
 +
[sfux@eu-login-12 ~]$ cat $HOME/VSCode_tunnel
 +
ssh -N -f -L localhost:8899:10.205.176.16:26078 sfux@euler.ethz.ch
 +
[sfux@eu-login-12 ~]$
 +
 
 +
Now you need to run this command in a terminal/shell on your local computer (works for macOS and Linux) to establish the SSH tunnel
 +
 
 +
===Using the local browser to display the code-server GUI===
 
* Open a browser on your local computer and connect to the URL
 
* Open a browser on your local computer and connect to the URL
 
  http://localhost:8899
 
  http://localhost:8899
* Login with the password stored in your home directory <tt>$HOME/.config/code-server/config.yaml</tt>
+
* Login with the password stored in your home directory <tt>$HOME/.config/code-server/config.yaml</tt> on the cluster
  
 
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, you can use the command
 
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, you can use the command

Revision as of 12:58, 26 April 2021

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 (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. 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 cluster
  • Start and interactive job with
bsub -Is -W 0:10 -n 1 -R "rusage[mem=2048]" bash
  • 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)

  • Load the modules required for code-server
module load gcc/6.3.0 code-server/3.9.3
  • Start the code-server once with the command code-server
[sfux@eu-g1-043-1 ~]$ code-server
[2021-04-21T12:27:29.229Z] info  code-server 3.9.3 fe2dc2deb08e378069891b622bb62ad1d261d1b1
[2021-04-21T12:27:29.235Z] info  Using user-data-dir ~/.local/share/code-server
[2021-04-21T12:27:29.249Z] info  Using config file ~/.config/code-server/config.yaml
[2021-04-21T12:27:29.249Z] info  HTTP server listening on http://127.0.0.1:8080
[2021-04-21T12:27:29.249Z] info    - Authentication is enabled
[2021-04-21T12:27:29.249Z] info      - Using password from ~/.config/code-server/config.yaml
[2021-04-21T12:27:29.249Z] info    - Not serving HTTPS

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

Starting the code-server in a batch job

  • Login to the Euler cluster
  • Switch to the new software stack, 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)

  • Load the modules required for code-server
module load gcc/6.3.0 code-server/3.9.3
  • Create a shell script (run_code_server.sh) with the following content:
#!/bin/bash

port=$((20000 + $RANDOM % 45000))
local_port=8899
ip=$(hostname -i)
echo "ssh -N -f -L localhost:${local_port}:${ip}:${port} ${USER}@euler.ethz.ch" > $HOME/VSCode_tunnel
code-server --bind-addr=${ip}:${port}

This script will determine the port and the ip address of the compute node (required for the SSH tunnel) and put together the command for setting up the SSH tunnel and store it in a file $HOME/VSCode_tunnel in your home directory on the cluster. The script will also start the code server, which is then running in a batch job.

  • Run the script in a batch job. For example (the example command assumes that you run the bsub command in the directory where the run_code_server.sh script is stored):

bsub -n 1 -W 1:00 -R "rusage[mem=2800]" ./run_code_server.sh

Setting up the SSH tunnel

The SSH tunnel is required to connect your local computer with the compute node, that is running the code-server.

  • Login to the cluster and run the command cat $HOME/VSCode_tunnel
[sfux@eu-login-12 ~]$ cat $HOME/VSCode_tunnel
ssh -N -f -L localhost:8899:10.205.176.16:26078 sfux@euler.ethz.ch
[sfux@eu-login-12 ~]$

Now you need to run this command in a terminal/shell on your local computer (works for macOS and Linux) to establish the SSH tunnel

Using the local browser to display the code-server GUI

  • Open a browser on your local computer and connect to the URL
http://localhost:8899
  • Login with the password stored in your home directory <tt>$HOME/.config/code-server/config.yaml on the cluster

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, 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.