forked from VallariAg/teuthology-api
-
Notifications
You must be signed in to change notification settings - Fork 10
/
helpers.py
94 lines (77 loc) · 2.53 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from multiprocessing import Process
import logging
import os
import uuid
from fastapi import HTTPException, Request
from teuthology_api.config import settings
import teuthology
import requests # Note: import requests after teuthology
from requests.exceptions import HTTPError
PADDLES_URL = settings.paddles_url
log = logging.getLogger(__name__)
def logs_run(func, args):
"""
Run the command function in a seperate process (to isolate logs),
and return logs printed during the execution of the function.
"""
_id = str(uuid.uuid4())
log_file = f"/archive_dir/{_id}.log"
teuthology_process = Process(target=_execute_with_logs, args=(func, args, log_file))
teuthology_process.start()
teuthology_process.join()
logs = ""
with open(log_file, encoding="utf-8") as file:
logs = file.readlines()
if os.path.isfile(log_file):
os.remove(log_file)
return logs
def _execute_with_logs(func, args, log_file):
"""
To store logs, set a new FileHandler for teuthology root logger
and then execute the command function.
"""
teuthology.setup_log_file(log_file)
func(args)
def get_run_details(run_name: str):
"""
Queries paddles to look if run is created.
"""
url = f"{PADDLES_URL}/runs/{run_name}/"
try:
run_info = requests.get(url)
run_info.raise_for_status()
return run_info.json()
except HTTPError as http_err:
log.error(http_err)
raise HTTPException(
status_code=http_err.response.status_code, detail=repr(http_err)
) from http_err
except Exception as err:
log.error(err)
raise HTTPException(status_code=500, detail=repr(err)) from err
def get_username(request: Request):
"""
Get username from request.session
"""
username = request.session.get("user", {}).get("username")
if username:
return username
log.error("username empty, user probably is not logged in.")
raise HTTPException(
status_code=401,
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)
def get_token(request: Request):
"""
Get access token from request.session
"""
token = request.session.get("user", {}).get("access_token")
if token:
return {"access_token": token, "token_type": "bearer"}
log.error("access_token empty, user probably is not logged in.")
raise HTTPException(
status_code=401,
detail="You need to be logged in",
headers={"WWW-Authenticate": "Bearer"},
)