Slurm API

From HPC Guide
Revision as of 09:24, 30 November 2023 by Levk (talk | contribs) (Created page with "This page describes how to connect and use slurm API in order to submit a job in powerslurm cluster, Including a job originating from a web site Official Documentation: https...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page describes how to connect and use slurm API in order to submit a job in powerslurm cluster, Including a job originating from a web site

Official Documentation: https://slurm.schedmd.com/rest_api.html

Authentication

In-order to authenticate against the API, you need to use your TAU username and a JWT token.

Tokens can only be created on Login nodes, such as powerslurm-login.

Python example for token creation

#!/usr/bin/env python3

import requests
from jwt import JWT
from jwt.jwa import HS256
from jwt.jwk import jwk_from_dict
from jwt.utils import b64encode
import time
import getpass

def generate_jwt_token(expiration_time=60):
    # Get the currently logged in user
    current_user = getpass.getuser()
    with open("/var/spool/slurm/statesave/jwt_hs256.key", "rb") as f:
        priv_key = f.read()
    signing_key = jwk_from_dict({
        'kty': 'oct',
        'k': b64encode(priv_key)
    })
    message = {
        "exp": int(time.time() + expiration_time),
        "iat": int(time.time()),
        "sun": current_user
    }
    jwt_instance = JWT()
    compact_jws = jwt_instance.encode(message, signing_key, alg='HS256')
    return compact_jws



# example output:
print(generate_jwt_token())
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOiAxNjk5NTMyNTkwLCAiaWF0IjogMTY5OTUzMjUzMCwgInN1biI6ICJsZXZrIn0.YCxJohapkovR16TQ75DsO3G9ODcisoSeOVbAYwA4Q7E

Submit Job:

The base url for the API is: https://slurmtron.tau.ac.il

Job submissions url is: https://slurmtron.tau.ac.il/slurm/v0.0.39/job/submit

Python Example:

#!/usr/bin/python3

import requests
from jwt import JWT
from jwt.jwa import HS256
from jwt.jwk import jwk_from_dict
from jwt.utils import b64encode
import time
import getpass

current_user = getpass.getuser()
def generate_jwt_token(expiration_time=60):
    with open("/var/spool/slurm/statesave/jwt_hs256.key", "rb") as f:
        priv_key = f.read()
    signing_key = jwk_from_dict({
        'kty': 'oct',
        'k': b64encode(priv_key)
    })
    message = {
        "exp": int(time.time() + expiration_time),
        "iat": int(time.time()),
        "sun": current_user
    }
    jwt_instance = JWT()
    compact_jws = jwt_instance.encode(message, signing_key, alg='HS256')
    return compact_jws


# api url
base_url = "https://slurmtron.tau.ac.il"
# auth token
jwt_token = generate_jwt_token()

# job submission url
job_url = f'{base_url}/slurm/v0.0.39/job/submit'
# Auth Headers
headers = {
    'X-SLURM-USER-NAME': current_user,
    'X-SLURM-USER-TOKEN': jwt_token
}

# the job request
jobs_request = requests.post(
    job_url,
    headers=headers,
    json={
        "script": "#!/bin/bash\n\n"
                  "srun hostname\n"
                  "echo \"hello world444\"\n"
                  "sleep 30",
        "job": {

            "partition": "Partition_Name",
            "tasks": 1,
            "name": "test",
            "account": "Slurm_Account_Name",
            "nodes": "1",
			# how much CPU you need
            "cpus_per_task": 2,
			# How much Memory you need per node, in MB
            "memory_per_node": {
                "number": 2048,
                "set": False,
                "infinite": True
              },
			# List of nodes where the job must be allocated (uncomment the below 3 lines, and specify node name)
            # "required_nodes": [  
            #     "Node_Name"
            # ],
            "standard_input": "/dev/null",
            "standard_output": "FULL_PATH_TO_OUTPUT_FILE",
            "standard_error": "FULL_PATH_TO_INPUT_FILE",
            "environment": [
                "PATH=/bin:/usr/bin/:/usr/local/bin/",
                "LD_LIBRARY_PATH=/lib/:/lib64/:/usr/local/lib"
            ],
        },

    })
jobs_result = jobs_request.json()

More Examples:

https://docs.lxp.lu/cloud/slurmrestd/