From 75ac34d8d6d6a7d89c48c7d60ea8b0183250a6a3 Mon Sep 17 00:00:00 2001 From: Newnius Date: Thu, 9 Jul 2020 10:12:44 +0800 Subject: [PATCH] add api.py --- api.py | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 api.py diff --git a/api.py b/api.py new file mode 100644 index 0000000..a5fbd3e --- /dev/null +++ b/api.py @@ -0,0 +1,113 @@ +import requests +import time +import os +import json + +'''Configuration''' +BASE_URL = 'http://yao.pasalab.jluapp.com' +'''''' + +sess = requests.Session() +sess.headers.update({'Referer': BASE_URL}) + +status_map = [ + 'Created', # 0 + 'Starting', # 1 + 'Running', # 2 + 'Stopped', # 3 + 'Finished', # 4 + 'Failed', # 5 +] + + +def login(user='', pwd=''): + # Get CSRF Token + r = sess.get(BASE_URL) + # print(r.content) + + # Login + url = BASE_URL + '/service?action=user_login' + r = sess.post(url, data={}) + # print(r.content) + return + + +def get_sys_status(): + # Retrieve Status + r = sess.get(BASE_URL + '/service?action=summary_get') + print(r.content) + # b'{"jobs":{"finished":1,"running":0,"pending":0},"gpu":{"free":20,"using":0},"errno":0,"msg":"Success !"}' + + # Get pool Util history + r = sess.get(BASE_URL + '/service?action=summary_get_pool_history') + # print(r.content) + return + + +def submit_job(job): + r = sess.post(BASE_URL + '/service?action=job_submit', data=job) + data = str(r.content, 'utf-8') + msg = json.loads(data) + print(msg) + return msg + + +def job_list(): + print("\nList of jobs:") + r = sess.get(BASE_URL + '/service?action=job_list&who=self&sort=nobody&order=desc&offset=0&limit=10') + data = str(r.content, 'utf-8') + msg = json.loads(data) + + if len(msg['jobs']) > 0: + for job in msg['jobs']: + name = job['name'] + status = status_map[job['status']] + print("Status of job: {} is {}".format(name, status)) + print("\n") + + +def job_status(job_name): + r = sess.get(BASE_URL + '/service?action=job_status&name=' + job_name) + data = str(r.content, 'utf-8') + msg = json.loads(data) + print("Status of tasks in {}:".format(job_name)) + if len(msg['tasks']) > 0: + for task in msg['tasks']: + print("{} ({}) is/was running on {}".format(task['hostname'], task['status'], task['node'])) + + +if __name__ == '__main__': + os.environ["TZ"] = 'Asia/Shanghai' + if hasattr(time, 'tzset'): + time.tzset() + + login() + get_sys_status() + + tasks = [{ + "name": "node1", + "image": "quickdeploy/yao-tensorflow:1.14-gpu", + "cmd": "python /workspace/scripts/tf_cnn_benchmarks/tf_cnn_benchmarks.py \\ --num_gpus=1 \\ --batch_size=32 \\ --model=resnet50 \\ --num_batches=200 \\ --train_dir=/tmp \\ --variable_update=parameter_server", + "cpu_number": "4", + "memory": "4096", + "gpu_number": "1", + "gpu_memory": "8192", + "is_ps": "0", + "gpu_model": "k80", + }] + # print(json.dumps(tasks)) + job = { + 'name': 'test', + 'workspace': 'http://code.pasalab.jluapp.com/newnius/yao-job-benchmarks.git', + 'cluster': 'default', + 'priority': '25', + 'run_before': '', + 'locality': '0', + 'tasks': json.dumps(tasks), + } + + msg = submit_job(job) + if msg['errno'] == 0: + job_status(msg['job_name']) + + job_list()