0

I am working remotely via the Remote-SSH plugin on a code which is located on an NFS filesystem on a GPU cluster. The project uses CMake. For debugging, I'd like to be able to work out of any machine on the cluster, and I'd like to use machine-specific build directories.

Is there a variable that stores the hostname so I can edit the following line in settings.json appropriately?

"cmake.buildDirectory": "${workspaceFolder}/build-${whatever_variable_stores_hostname}"
Jindra Helcl
  • 3,457
  • 1
  • 20
  • 26

2 Answers2

3

Since the Remote-SSH plugin makes use of your ssh config file, you can do the following in your ssh config:

Host CLUSTERA
 User USERNAME
 HostName IP.ADDRESS.HERE
 IdentityFile ~/.ssh/YOUR.pem
 RequestTTY yes
 RemoteCommand export HOSTNAME=export HOSTNAME=`cat /proc/sys/kernel/hostname` && bash
  1. The RemoteCommand option execute a command remotely on the server
  2. RequestTTY yes force ssh to allocate a PTY for the session If you don't you will get a non-interactive session.
  3. cat /proc/sys/kernel/hostname get's the host name
  4. export HOSTNAME=`cat /proc/sys/kernel/hostname` && bash and pipe the hostname into an environment variable call HOSTNAME, (you can name it whatever you want) and opens a shell.
  5. You then can put this in your setting.json in VSCode
"cmake.buildDirectory": "${workspaceFolder}/build-${HOSTNAME}"
Algo7
  • 2,122
  • 1
  • 8
  • 19
  • Thanks, I ended up doing a different thing, but this post helped me getting it through. I set the variable in ~/.bashrc and it helped. The problem might have been in that the vscode server runs on the node after disconnect, so bashrc might not have been reloaded. Or, it helped to specify RequestTTY. I need to figure this out before I write up my solution. – Jindra Helcl Jan 19 '21 at 12:21
  • Ok. Post your solution here later so we can all benefit from that. – Algo7 Jan 21 '21 at 17:22
1

It turns out you can use the environment variables in settings.json as expected using ${env:your_var}.

However, once the VS Code server is started, it keeps running even after closing the client VS Code remote connection. All subsequent connections only check for existing VS Code server installation and connect to it. Therefore, when you update your ~/.bashrc to set additional environment variables, such as:

export HOST=`hostname`

The VS Code server environment will remain unchanged.

The solution is to invoke the Remote-SSH Kill VS Code Server on Host command in your VS Code client to force-restart the server on your next login. You will notice that after your next login, the VS Code server installation log will show the output of the printenv command, which should now reflect the updates to the ~/.bashrc file.

Note on the $HOSTNAME variable

For some reason, the $HOSTNAME variable which is usually set when you log in to a machine with an interactive session is not set in the environment of the VS Code server instance - that's why I ended up using a custom variable set in ~/.bashrc.

Jindra Helcl
  • 3,457
  • 1
  • 20
  • 26