Skip to content

Commit 62c7f23

Browse files
author
Kairo de Araujo
committed
Implement the report using the Tern
This commit implements the report using Tern The reports use the ``concurrent.futures``, but instead of building all the implementation for it, we use flask-executer that makes the implementation on top of Flask. All the report requests go to the ``tern-tasks`` (Futures) and are handled such as a Celery implementation. All tasks start with the status 'UNKNOWN', which means that the task has no defined status until the ``tern-tasks`` start processing it. Once the task is in picked up by the ``tern-tasks`` it is moved to 'RUNNING'. If the processing finishes, the status is moved to 'FINISH' independently of the task content/result. Here the status is for the task itself. The FAIL status is given when the task cannot finish its routine. The Tern, for now, is called using ``subprocess``, and the output is handled by the stdout and stderr. The initial API specification was changed a bit for more clearness. How to run as Docker container was added to the README.md. Signed-off-by: Kairo de Araujo <[email protected]>
1 parent 4086cba commit 62c7f23

16 files changed

+456
-167
lines changed

.dockerignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/
2+
docs/
3+
cached/
4+
.github
5+
.gitignore
6+
Dockerfile
7+
Makefile
8+
Pipfile
9+
Pipfile.lock
10+
README.md
11+
requirements-dev.txt
12+
setup.py
13+
tox.ini
14+
.coverage
15+
.dockerignore
16+
.git
17+
.mypy_cache
18+
.pytest_cache
19+
.tox
20+
__pycache__
21+
tern_rest_api.egg-info

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# tern-rest-api specifics
2+
cached
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) 2022 VMware, Inc. All Rights Reserved.
2+
# SPDX-License-Identifier: BSD-2-Clause
3+
4+
FROM python:3.9-slim-buster as base
5+
6+
RUN echo "deb http://deb.debian.org/debian bullseye main" > /etc/apt/sources.list.d/bullseye.list \
7+
&& echo "Package: *\nPin: release n=bullseye\nPin-Priority: 50" > /etc/apt/preferences.d/bullseye \
8+
&& apt-get update \
9+
&& apt-get install -y --no-install-recommends \
10+
attr \
11+
findutils \
12+
fuse-overlayfs/bullseye \
13+
fuse3/bullseye \
14+
git \
15+
jq \
16+
skopeo \
17+
&& rm -rf /var/lib/apt/lists/*
18+
19+
RUN mkdir /opt/tern-rest-api
20+
21+
ADD . /opt/tern-rest-api
22+
WORKDIR /opt/tern-rest-api
23+
RUN pip install --no-cache -r ./requirements.txt
24+
25+
ENV TERN_API_CACHE_DIR=/var/opt/tern-rest-api/cached
26+
ENV TERN_DEFAULT_REGISTRY="registry.hub.docker.com"
27+
28+
ENV FLASK_APP=/opt/tern-rest-api/app.py
29+
CMD ["bash", "docker_start.sh"]

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
build-dev:
2+
docker build -t tern-rest-api:dev .
3+
4+
5+
serve-dev: build-dev
6+
docker run --rm --name tern-rest-api -e ENVIRONMENT=DEVELOPMENT --privileged -v /var/run/docker.sock:/var/run/docker.sock -v $(PWD):/opt/tern-rest-api -p 5001:80 tern-rest-api:dev

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ name = "pypi"
77
flask = "*"
88
flask-restx = "*"
99
tern = "*"
10+
flask-executor = "*"
11+
gunicorn = "*"
1012

1113
[dev-packages]
1214
black = "*"

Pipfile.lock

Lines changed: 120 additions & 133 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ $ pipenv install -d
5959

6060
### Running the development Tern REST API
6161

62+
## As a Docker Container
63+
```shell
64+
$ make server-dev
65+
```
66+
Open http://localhost/ in your browser.
6267

68+
## On your local machine
6369
Runing the API locally
6470

6571
```shell

app.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from flask_restx import Api
1010

11-
from tern_api import __version__, tern_api
11+
from tern_api import __version__, tern_app
1212
from tern_api.api.v1.common_models import api_models_namespace
1313
from tern_api.api.v1.reports import ns as report_v1
1414
from tern_api.api.v1.version import ns as version_v1
@@ -26,20 +26,21 @@
2626

2727

2828
api = Api(
29-
tern_api,
29+
tern_app,
3030
version=__version__.version,
3131
title="Tern REST API",
3232
description="Tern Project REST API",
3333
)
3434

35+
3536
api.add_namespace(api_models_namespace)
3637
api.add_namespace(version_v1, path="/api/v1/version")
3738
api.add_namespace(report_v1, path="/api/v1/report")
3839

3940

4041
def export_swagger_json(filepath):
41-
tern_api.config["SERVER_NAME"] = "localhost"
42-
with tern_api.app_context().__enter__():
42+
tern_app.config["SERVER_NAME"] = "localhost"
43+
with tern_app.app_context().__enter__():
4344
with open(filepath, "w") as f:
4445
swagger_json = json.dumps(api.__schema__, indent=4)
4546
f.write(swagger_json)

docker_start.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
if [[ ${ENVIRONMENT^^} == "DEVELOPMENT" ]]; then
4+
flask run --reload --debugger -h 0.0.0.0 -p 80
5+
else
6+
gunicorn --workers=1 -b 0.0.0.0:80 app:tern_api
7+
fi

requirements-dev.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,34 @@ black==22.1.0
1515
certifi==2021.10.8
1616
chardet==4.0.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
1717
charset-normalizer==2.0.10; python_version >= '3'
18-
click==8.0.3; python_version >= '3.6'
19-
coverage==6.3.1
18+
click==8.0.4; python_version >= '3.6'
19+
coverage==6.3.2
2020
debian-inspector==30.0.0; python_version >= '3.6' and python_version < '4'
2121
distlib==0.3.4
2222
docker==5.0.3; python_version >= '3.6'
2323
dockerfile-parse==1.2.0
24-
filelock==3.5.1; python_version >= '3.7'
24+
filelock==3.6.0; python_version >= '3.7'
2525
flake8==4.0.1
26+
flask-executor==0.10.0
2627
flask-restx==0.5.1
2728
flask==2.0.3
2829
gitdb==4.0.9; python_version >= '3.6'
2930
gitpython==3.1.26; python_version >= '3.7'
31+
gunicorn==20.1.0
3032
idna==3.3; python_version >= '3'
3133
iniconfig==1.1.1
3234
isort==5.10.1
33-
itsdangerous==2.0.1; python_version >= '3.6'
35+
itsdangerous==2.1.0; python_version >= '3.7'
3436
jinja2==3.0.3; python_version >= '3.6'
3537
jsonschema==4.4.0; python_version >= '3.7'
36-
markupsafe==2.0.1; python_version >= '3.6'
38+
markupsafe==2.1.0; python_version >= '3.7'
3739
mccabe==0.6.1
3840
mypy-extensions==0.4.3
3941
packageurl-python==0.9.6; python_version >= '3.6'
4042
packaging==21.3; python_version >= '3.6'
4143
pathspec==0.9.0
4244
pbr==5.8.0; python_version >= '2.6'
43-
platformdirs==2.5.0; python_version >= '3.7'
45+
platformdirs==2.5.1; python_version >= '3.7'
4446
pluggy==1.0.0; python_version >= '3.6'
4547
prettytable==3.0.0; python_version >= '3.7'
4648
py==1.11.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
@@ -62,7 +64,7 @@ tomli==2.0.1; python_version >= '3.7'
6264
tox==3.24.5
6365
typing-extensions==4.1.1; python_version < '3.10'
6466
urllib3==1.26.8; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'
65-
virtualenv==20.13.1; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
67+
virtualenv==20.13.3; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
6668
wcwidth==0.2.5
6769
websocket-client==1.2.3; python_version >= '3.6'
6870
werkzeug==2.0.3; python_version >= '3.6'

0 commit comments

Comments
 (0)