Using VS Code with the HPC-VCL image

Using VS Code directly with HPC login nodes puts significant strain on them. Users are encouraged to use the HPC-VCL image when using VS Code to edit code run in HPC. HPC documentation explains the process well. This page explains how to use a script and configure VS Code to use whatever HPC-VCL reservation you currently have without having to reconfigure VS Code each time you make a new VCL reservation or having to click the Connect button for the reservation when you change locations. NOTE: The script assumes you only have one reservation for the HPC-VCL image, and the image is named “HPC (RHEL 9 64 bit VM)”.

First, you’ll need to get a bearer token from the VCL site to use with the script. Select Manage->User Preferences->Manage Tokens. Specify a name for the token and click Create Token. The new token will be displayed. Note: This is the only time the token value will be displayed. You will need to save a copy of it now.

Create a script named getResIP.py with the following contents:

#!/usr/bin/python
import xmlrpc.client
import requests

token = 'Bearer aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
imagename = 'HPC (RHEL 9 64 bit VM)'

def remote_vcl_call(method, args, token):
    """
    Make an XML-RPC call to the VCL server.
    
    :param method: The XML-RPC method name.
    :param args: A list of arguments for the XML-RPC call.
    :param token: The token for authentication.
    :return: The response data from the XML-RPC call.
    """
    headers = {
        "Content-Type": "text/xml",
        "X-Authorization": token,
        "X-APIVERSION": "2",
    }
    request_body = xmlrpc.client.dumps(tuple(args), methodname=method)
    try:
        response = requests.post(
            "https://vcl.ncsu.edu/scheduling/index.php?mode=xmlrpccall",
            headers=headers,
            data=request_body,
            verify=True
        )

        if response.status_code != 200:
            print(f"HTTP error: {response.status_code}")
            return None

        response_data, methodname = xmlrpc.client.loads(response.content)

        return response_data

    except xmlrpc.client.Fault as e:
        if(e.faultCode == 3):
            print("Received access denied error from server")
            exit()
        print(response.content)
        print(f"XML-RPC Fault occurred: {e.faultCode} - {e.faultString}")

    except Exception as e:
        print(response.content)
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    rc = remote_vcl_call("XMLRPCgetRequestIds", [], token)

    for item in rc[0]['requests']:
        if(item['imagename'] == imagename and item['serverowner'] == 1):
            requestid = item['requestid']
            rc = remote_vcl_call( "XMLRPCgetIP", [], token)
            remoteIP = rc[0]['ip']
            rc = remote_vcl_call( "XMLRPCgetRequestConnectData", [requestid, remoteIP], token)
            print(rc[0]['serverIP'], end='')
            exit()

Replace the long string of “aaaaaaaaaaa” at the top of the file after Bearer with your newly created token.

It is a good idea to run the script manually to ensure it is working properly. It will return the IP address of your reservation for the HPC-VCL image.

Once it is working correctly when run manually, the next step is to modify your ssh config file to use it. On Mac and Linux, the ssh config file is in your home directory as .ssh/config. You’ll need to create the file if it’s not already there. You’ll also need to have the netcat package installed on your system. You’ll need to add the following lines to the file (replace the string /path/to/getResIP.py with the actual path to where you have getResIP.py saved.

Host vscodenode
     ProxyCommand bash -c "nc $(/path/to/getResIP.py) %p"

For Windows, the configuration will depend on the ssh client you are using. The AI of your choice should be able to explain how to configure it.

You should now be able to run ssh vscodenode to connect to whatever HPC-VCL reservation you have. Configure VS Code to use vscodenode as the host to connect to when remotely editing code, and it will automatically connect to whatever HPC-VCL reservation you have.