cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
264
Views
1
Helpful
1
Replies

CML - Users who leave Labs on, but logoff?

paul-fletcher
Level 1
Level 1

We have a full CML cluster installed, and as an admin.. I am seeing users who leave their Labs on, even though they have logged off.

Is there anyway to A) see if the user is inactive, and has been offline for a while? Or B) have Labs STOP on their own after a certain amount of time being inactive?

The reason for this would be to save power, and return nodes back to the pool for others.

If nodes aren't released, others may find they don't have all the nodes available for their Labs.

1 Reply 1

Torbjørn
VIP
VIP

Hi @paul-fletcher,

I run the following script from cron each night to shut down any running labs. It's not an ideal solution, but it works:

import requests, json

import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

username = "your username"
password = "your password"
baseurl = "https://your.host.name/api/v0"

def authenticate(baseurl, username, password):
  payload = {
    "username": username,
    "password": password
  }

  token = response = requests.post(url=f"{baseurl}/authenticate", data=json.dumps(payload), verify=False).json()
  return(token)

def main():
  token = authenticate(baseurl=baseurl, username=username, password=password)

  lab_list = requests.get(
    url=f"{baseurl}/labs",
    headers={"Authorization": f"Bearer {token}"},
    params={"show_all": True},
    verify=False).json()

  for lab in lab_list:
    response = requests.put(url=f"{baseurl}/labs/{lab}/stop", headers={"Authorization": f"Bearer {token}"}, verify = False)
    if response.status_code == 204:
      print("Lab stopped successfully: " + str(lab))

if __name__ == "__main__":
  main()

Add this to an appropriate folder, I have put it under "/home/sysadmin/stopall.py" on my CML server.

You can then add the following line to cron(cron can be edited by running "crontab -e"):

0 2 * * * python3 /home/sysadmin/stopall.py
Happy to help! Please mark as helpful/solution if applicable.
Get in touch: https://torbjorn.dev