Skip to content

Commit 67aab85

Browse files
committed
lava_callback: migrate to fastapi
It will be easier to maintain API and Pipeline, as both will be powered by FastAPI framework. Signed-off-by: Denys Fedoryshchenko <[email protected]>
1 parent ba258b3 commit 67aab85

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

Diff for: docker-compose-production.yaml

+10-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ services:
99

1010
lava-callback:
1111
# With uWSGI in socket mode, to use with reverse proxy e.g. Nginx and SSL
12-
command:
13-
- '/usr/local/bin/uwsgi'
14-
- '--master'
15-
- '--socket=:8000'
16-
- '--buffer-size=32768'
17-
- '-p${NPROC:-4}'
18-
- '--enable-threads'
19-
- '--wsgi-file=/home/kernelci/pipeline/lava_callback.py'
20-
- '--callable=app'
12+
command: uvicorn lava_callback:app --host 0.0.0.0 --port 8000 --app-dir /home/kernelci/pipeline/
13+
#command:
14+
# - '/usr/local/bin/uwsgi'
15+
# - '--master'
16+
# - '--socket=:8000'
17+
# - '--buffer-size=32768'
18+
# - '-p${NPROC:-4}'
19+
# - '--enable-threads'
20+
# - '--wsgi-file=/home/kernelci/pipeline/lava_callback.py'
21+
# - '--callable=app'
2122

2223
# With uWSGI HTTP server, suitable for a public instance but no SSL
2324
# command:

Diff for: docker/lava-callback/requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
flask==2.3.2
21
uwsgi==2.0.22
2+
uvicorn==0.30.1
3+
fastapi==0.111.0

Diff for: src/lava_callback.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
import gzip
1010
import json
1111
import requests
12-
from flask import Flask, request
1312
import toml
1413
import threading
14+
import uvicorn
15+
from fastapi import FastAPI, HTTPException, Request
1516

1617
import kernelci.api.helper
1718
import kernelci.config
@@ -26,7 +27,7 @@
2627
)
2728
SETTINGS_PREFIX = 'runtime'
2829

29-
app = Flask(__name__)
30+
app = FastAPI()
3031
executor = ThreadPoolExecutor(max_workers=16)
3132

3233

@@ -86,15 +87,20 @@ def _upload_log(log_parser, job_node, storage):
8687
return _upload_file(storage, job_node, src, 'log.txt.gz')
8788

8889

89-
@app.errorhandler(requests.exceptions.HTTPError)
90-
def handle_http_error(ex):
91-
detail = ex.response.json().get('detail') or str(ex)
92-
return detail, ex.response.status_code
93-
94-
95-
@app.route('/')
96-
def hello():
97-
return "KernelCI API & Pipeline LAVA callback handler"
90+
@app.get('/')
91+
async def read_root():
92+
page = '''
93+
<html>
94+
<head>
95+
<title>KernelCI Pipeline Callback</title>
96+
</head>
97+
<body>
98+
<h1>KernelCI Pipeline Callback</h1>
99+
<p>This is a callback endpoint for the KernelCI pipeline.</p>
100+
</body>
101+
</html>
102+
'''
103+
return page
98104

99105

100106
def async_job_submit(api_helper, node_id, job_callback):
@@ -146,8 +152,9 @@ def submit_job(api_helper, node_id, job_callback):
146152
executor.submit(async_job_submit, api_helper, node_id, job_callback)
147153

148154

149-
@app.post('/node/<node_id>')
150-
def callback(node_id):
155+
# POST /node/<node_id>
156+
@app.post('/node/{node_id}')
157+
async def callback(node_id: str, request: Request):
151158
tokens = SETTINGS.get(SETTINGS_PREFIX)
152159
if not tokens:
153160
return 'Unauthorized', 401
@@ -169,7 +176,7 @@ def callback(node_id):
169176
if not lab_name:
170177
return 'Unauthorized', 401
171178

172-
data = request.get_json()
179+
data = await request.json()
173180
job_callback = kernelci.runtime.lava.Callback(data)
174181
api_config_name = job_callback.get_meta('api_config_name')
175182
api_token = os.getenv('KCI_API_TOKEN')
@@ -185,4 +192,4 @@ def callback(node_id):
185192
tokens = SETTINGS.get(SETTINGS_PREFIX)
186193
if not tokens:
187194
print('No tokens configured in toml file')
188-
app.run(host='0.0.0.0', port=8000)
195+
uvicorn.run(app, host='0.0.0.0', port=8000)

0 commit comments

Comments
 (0)