-
-
Save zeryx/5d4f89029c15b92b127bcb82808d412a to your computer and use it in GitHub Desktop.
Automates reservation scheduling and decomissioning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
from time import sleep | |
import argparse | |
def signin(url, username, password): | |
full_url = f"{url}/webapi/signin" | |
payload = {"username": username, "password": password} | |
response = requests.post(full_url, json=payload) | |
cookies = response.cookies | |
return cookies | |
def set_reservation(url, cookie, algorithm_name, caller_name, version_type="LATEST_PRIVATE", num_reservations=1): | |
num_reservations = str(num_reservations) | |
full_url = f"{url}/webapi/stickyslots" | |
algorithm_username = algorithm_name.split("/")[0] | |
algorithm_algoname = algorithm_name.split("/")[1] | |
print(f"username: {algorithm_username} algoname: {algorithm_algoname}") | |
payload = {"username": algorithm_username, "algoname": algorithm_algoname, "calling_user": caller_name, "version_type": version_type, "num_slots": num_reservations, "version_identifier": ""} | |
response = requests.post(url=full_url, json=payload, cookies=cookie) | |
if response.status_code != 200: | |
raise Exception(response.text) | |
print("reservation set") | |
return response | |
def delete_reservation(url, cookie, algorithm_name, caller_name, num_reservations=1): | |
get_url = f"{url}/webapi/stickyslots" | |
response = requests.get(url=get_url, cookies=cookie) | |
if response.status_code != 200: | |
raise Exception(response.text) | |
result = response.json() | |
algorithm_algoname = algorithm_name.split("/")[1].lower() | |
print(f"found {len(result)} stickyslots") | |
for slot in result: | |
id = slot['reservation_id'] | |
# s_username = slot['billable_to'] | |
# s_algorithm_algoname = slot['algoname'].lower() | |
# s_num_reservations = slot['num_slots'] | |
delete_url = f"{get_url}/{id}" | |
result = requests.delete(url=delete_url, cookies=cookie) | |
if result.status_code != 200: | |
raise Exception(result.text) | |
print("deleted reservation") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description="Creates reservations for an Algorithm, and after X amount of time - deactivates it." | |
"Use this for benchmarking or for Supercomputer style usage." | |
) | |
parser.add_argument( | |
"-u", | |
"--username", | |
dest="username", | |
type=str, | |
help="Your Algorithmia Admin username", | |
required=True | |
) | |
parser.add_argument( | |
"-p", | |
"--password", | |
dest="password", | |
type=str, | |
help="Your Algorithmia Admin password", | |
required=True | |
) | |
parser.add_argument( | |
"-n", | |
"--algorithm-name", | |
dest="algorithm_name", | |
type=str, | |
help="The algorithm name you wish to set a reservation for", | |
default="demo/hello" | |
) | |
parser.add_argument( | |
"-c", | |
"--caller-username", | |
dest="caller", | |
type=str, | |
help="The user account that will be calling this Algorithm", | |
default="demo" | |
) | |
parser.add_argument( | |
"-r", | |
"--num-reservations", | |
dest="num_reservations", | |
type=int, | |
help="The number of reservations to allocate for your Algorithm", | |
default=1 | |
) | |
parser.add_argument( | |
"--lifetime", | |
dest="lifetime", | |
type=int, | |
help="The amount of time (in seconds) that you'd like to hold this reservation for", | |
default=60 | |
) | |
args = parser.parse_args() | |
base_url = "https://algorithmia.ecd-ai.com" | |
username = args.username | |
password = args.password | |
algorithm_name = args.algorithm_name | |
caller = args.caller | |
num_reservations = args.num_reservations | |
lifetime = args.lifetime | |
print(f"lifetime {lifetime} num_reservations: {num_reservations} algorithm_name {algorithm_name}") | |
cookie = signin(base_url, username, password) | |
res = set_reservation(base_url, cookie, algorithm_name, caller, num_reservations=num_reservations) | |
sleep(lifetime) | |
cookie = signin(base_url, username, password) | |
delete_reservation(base_url, cookie, algorithm_name, caller) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment