Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

VSCode on O2

It is now possible to run VS Code Remote Development (https://code.visualstudio.com/docs/remote/ssh) from your local computer directly against the resources allocated for your O2 Slurm job.

Before starting make sure to have all the requirements as described in https://code.visualstudio.com/docs/remote/ssh

Note for Windows users: depending on how you have VSCode installed, you may need to follow a slightly different procedure than what is outlined below. If you are using WSL, and VSCode is installed into WSL, you can follow all instructions in this section as if you are a Linux/Mac OS machine. However, if your VSCode installation is installed “normally” (e.g., it exists as a standard Windows application that you’d normally double click, etc. to start), you will need to access a different .ssh directory, which will usually live at %UserProfile% (C:\Users\{Username}). If you also have WSL installed, you can navigate to this directory by typing cd /mnt/c/Users/{Username}/.ssh. If not, you should still be able to navigate to your %UserProfile%\.ssh directory in Powershell, where you can also generate your SSH keys. You can read more about this arrangement in the VSCode documentation/blog.

SSH Keys

The first step is to set up SSH keys from your local machine to O2. This step is optional as VSCode should support password-based authentication. An example of how to set up SSH keys would be:

  • To create keys for connecting from your desktop to O2, run the following ssh-keygen command (or equivalent) on your desktop.

Code Block
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/USERHOME/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /USERHOME/.ssh/id_rsa.
Your public key has been saved in /USERHOME/.ssh/id_rsa.pub.
The key fingerprint is:
a5:b5:38:73:b7:3c:a6:8a:1d:a8:bd:87:4e:be:33:21 

You will then need to add the content of ~/.ssh/id_rsa.pub from your local computer to the O2 file ~/.ssh/authorized_keys

On a Linux or Mac computer, you can use the command

ssh-copy-id -i $HOME/.ssh/id_rsa.pub user@o2.hms.harvard.edu

or alternatively copy your local file .ssh/id_rsa.pub to O2 (!!! do not replace your O2 ~/.ssh/id_rsa.pub place the temporary copy somewhere else !!!) and then add the content of the copied id_rsa.pub file to your O2 ~/.ssh/authorized_keys.

For example:

$ cat ~/temp_copy_of_my_id_rsa.pub >> ~/.ssh/authorized_keys

and finally remember to delete that temporary copy from O2 ( rm ~/temp_copy_of_my_id_rsa.pub )

Make sure that your O2 $HOME and .ssh permissions are set correctly by executing

Code Block
$ chmod 0600 ~/.ssh/authorized_keys
$ chmod 0700 ~/.ssh

and

Code Block

$ chmod g-w /home/$USER

You can also set up SSH keys to be used within the O2 cluster following the instruction from How to Generate SSH Keys

VSCode Remote Extension

You will need to install the VSCode Remote Extension https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh and if you want the extension pack https://aka.ms/vscode-remote/download/extension

Local SSH Configuration

The final step is to create an entry in your local SSH configuration file ~/.ssh/configto be used with VSCode. This configuration needs to be done in your computer not O2. Windows users should see the above note

...

We strongly recommend using the VS Code application on the O2 Portal. For more information, please visit O2Portal - Visual Studio Code AppandO2Portal

...

for configuration nuances.

Inside the file ~/.ssh/config you need to add the lines:

Code Block
Host o2jump
  HostName o2.hms.harvard.edu
  User your_username
  ForwardAgent yes
  ForwardX11 yes
  ForwardX11Trusted yes

Host o2job
  HostName compute_node_of_job
  User your_username
  ProxyJump o2jump
  ForwardAgent yes

Where compute_node_of_job is the hostname where the O2 job was dispatched.

How to start a VSCode Remote Extension using an O2 job

After completing the steps above to use the VSCode remote extension you first need to configure and submit a job on the O2 cluster allocating the resources you foresee necessary for your VSCode work.

Any sbatch job that can work as a placeholder would work, we recommend something like the following submission script:

Code Block
#!/bin/bash

#SBATCH -p priority
#SBATCH -c 1
#SBATCH --mem=4G
#SBATCH -t 6:00:00
#SBATCH --job-name="vscodetunnel"

sleep 6h

and adjust the resources as needed making sure to match the sleep time with your desired WallTime. Submit this job via standard sbatch procedures.
If you used the above template, you can then check the job status using the command

squeue -u $USER --name=vscodetunnel

Once the job start running the last output column NODELIST(REASON) will return the hostname where the job was dispatched, which you need to manually enter in your local machine ~/.ssh/config to replace compute_node_of_job.

At this point you can open your local installation of VSCode, select the remote extension and connect to the o2job option (do not use the o2jump).

Once you are done using VSCode remember to cancel the O2 job as that job will remain running for the duration of the sleep command.

O2 Portal VSCode Application

We also provide an Open OnDemand application to run VSCode entirely on O2 via the O2 Portal interface.

For more information please check O2Portal and O2Portal - Visual Studio Code App

Code Server

An alternative to the VSCode Remote Extension and the O2 Portal is Code Server.

Code Server is an open-source tool similar to VS Code, and it brings the functionality of VS Code to your browser. Important -As an open-source tool, code-server is not displaying or allowing the installation of some extensions (e.g., Copilot, Copilot Chat, other GitHub-related extensions), due to proprietary restrictions.

This page shows you how to install and run VS Code using your local browser. Let us know if you have any questions.

Connect to O2

Please review the How to Login to O2 and the Using Slurm Basic wiki pages for more details. If using a Windows computer, we recommend to download/install https://mobaxterm.mobatek.net/and open a new terminal. If using a Mac computer, you can open the ‘Terminal’ app and use the ssh command as follow:

...

Info

Newer versions of code-server aren't compatible with CentOS 7. Therefore, a container is necessary to circumvent the compatibility issue.

Run the latest version (4.20.0) using a container

Code Block
# Export the port number
export PORT=51234 

# Launch an interactive job (resources: 2 hours and 3 GiB of RAM) 
srun --pty -p priority -t 0-2 --mem=3G --tunnel $PORT:$PORT bash

# Start code-server using the singularity container
singularity exec /n/app/singularity/containers/code-server-v4.20.0.sif /app/code-server/bin/code-server

# In theory, you should see something similar to:
singularity exec /n/app/singularity/containers/code-server-v4.20.0.sif /app/code-server/bin/code-server
[2024-01-11T17:15:54.233Z] info  code-server 4.20.0 
[2024-01-11T17:15:54.237Z] info  Using user-data-dir /home/<HMS_ID>/.local/share/code-server
[2024-01-11T17:15:54.264Z] info  Using config file /home/<HMS_ID/.config/code-server/config.yaml
[2024-01-11T17:15:54.265Z] info  HTTP server listening on http://127.0.0.1:50003/
[2024-01-11T17:15:54.265Z] info    - Authentication is enabled
[2024-01-11T17:15:54.266Z] info      - Using password from /home/<HMS_ID>/.config/code-server/config.yaml
[2024-01-11T17:15:54.266Z] info    - Not serving HTTPS
[2024-01-11T17:15:54.267Z] info  Session server listening on /home/<HMS_ID/.local/share/code-server/code-server-ipc.sock

# To see the password on /home/<HMS_ID>/.config/code-server/config.yaml
# You'll need to open a new terminal, connect to O2, and paste the following command:
cat ~/.config/code-server/config.yaml

Installing and Testing an Older Version Locally

Code Block
export PORT=51234 
srun --pty -p priority -t 8:0:0 --tunnel $PORT:$PORT bash 

mkdir -p ~/.local/lib ~/.local/bin
curl -fL https://github.com/cdr/code-server/releases/download/v4.7.0/code-server-4.7.0-linux-amd64.tar.gz | tar -C ~/.local/lib -xz
mv ~/.local/lib/code-server-4.7.0-linux-amd64 ~/.local/lib/code-server-4.7.0

# Start code server
~/.local/lib/code-server-4.7.0/bin/code-server 

# You should see output: 
$ [2021-10-05T13:58:56.961Z] info  code-server 4.7.0 4cd55f94c0a72f05c18cea070e10b969996614d2
[2021-10-05T13:58:56.962Z] info  Using user-data-dir ~/.local/share/code-server
[2021-10-05T13:58:56.995Z] info  Using config file ~/.config/code-server/config.yaml
[2021-10-05T13:58:56.995Z] info  HTTP server listening on http://127.0.0.1:51234
[2021-10-05T13:58:56.995Z] info    - Authentication is enabled
[2021-10-05T13:58:56.995Z] info      - Using password from ~/.config/code-server/config.yaml
[2021-10-05T13:58:56.995Z] info    - Not serving HTTPS

# Command + click the "http://127.0.0.1:51234" to open the link
# Then copy and paste the password to browser
cat ~/.config/code-server/config.yaml

Create an Alias to login, start an interactive job, and launch code-server from a local machine

Code Block
# Set DUO default to push if you didn’t do it yet. 
# Run this command on O2:  
echo export DUO_PASSCODE=push >> $HOME/.bashrc

# On your local machine
# set up alias. You could add one of the following two lines to your ~/.bash_profile

# If using an older version:
alias vscode_old='PORT=51234; CLUSTER_USER=YourHMSID; ssh -L $PORT:127.0.0.1:$PORT $CLUSTER_USER@o2.hms.harvard.edu -t "hostname; echo port is: $PORT; kill -9 $(/usr/sbin/lsof -t -i:$PORT) 2>/dev/null; srun --pty -p priority -t 8:0:0 --tunnel $PORT:$PORT bash -c \"hostname; echo port is: $PORT; kill -9 $(/usr/sbin/lsof -t -i:$PORT) 2>/dev/null; export PORT=$PORT; ~/.local/lib/code-server-4.7.0/bin/code-server & bash;\""'

# If using the latest version (4.20.0)
alias vscode='PORT=50234; CLUSTER_USER=YourHMSID; ssh -L $PORT:127.0.0.1:$PORT $CLUSTER_USER@o2.hms.harvard.edu -t "hostname; echo port is: $PORT; kill -9 $(/usr/sbin/lsof -t -i:$PORT) 2>/dev/null; srun --pty -p priority -t 8:0:0 --tunnel $PORT:$PORT bash -c \"hostname; echo port is: $PORT; kill -9 $(/usr/sbin/lsof -t -i:$PORT) 2>/dev/null; export PORT=$PORT; singularity exec -B '/n' /n/app/singularity/containers/code-server-v4.20.0.sif /app/code-server/bin/code-server & bash;\""'

# login to O2, start interative job and run vs code server
vscode

...

Note: You don't have to use the number 51234 for the PORT; you can choose any number within the range of 50,000 for the PORT value.

Frequently Asked Questions (FAQ)

Why I see “port 51234 is in use or unavailable" when launching code-server?

Please select a different port number in the 50,000 range.

Why am I encountering the message “channel #: open failed: connect failed: Connection refused" in the terminal?

You have a code-server tab open in your browser. The solution is to simply close the tab or tabs.