Skip to content

Commit 632d2fc

Browse files
committed
Introduce FastAPI healthz and system endpoints
1 parent e969d3e commit 632d2fc

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed
+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Copyright Contributors to the Packit project.
22
# SPDX-License-Identifier: MIT
33

4-
routers = []
4+
from .healthz import router as healthz_router
5+
from .system import router as system_router
6+
7+
routers = [healthz_router, system_router]
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
4+
from fastapi import APIRouter
5+
6+
router = APIRouter()
7+
8+
9+
@router.get("/healthz")
10+
async def health_check() -> dict:
11+
"""Health check"""
12+
return {"status": "ok"}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
4+
import re
5+
from logging import getLogger
6+
from typing import Optional
7+
8+
import ogr
9+
import packit
10+
import specfile
11+
from fastapi import APIRouter
12+
from pydantic import BaseModel
13+
from setuptools_scm import get_version
14+
15+
import packit_service
16+
17+
logger = getLogger("packit_service")
18+
19+
router = APIRouter()
20+
21+
22+
def get_commit_from_version(version) -> Optional[str]:
23+
"""
24+
Version can look like this:
25+
0.76.0.post18+g116edc5
26+
0.1.dev1+gc03b1bd.d20230615
27+
0.18.0.post4+g28cb117
28+
0.45.1.dev2+g3b0fc3b
29+
30+
The 7 characters after the "+g" is the short version of the git commit hash.
31+
"""
32+
if matches := re.search(r"\+g([A-Za-z0-9]{7})", version):
33+
return matches.groups()[0]
34+
return None
35+
36+
37+
class PackageVersionResponse(BaseModel):
38+
commit: Optional[str]
39+
version: str
40+
41+
42+
@router.get("/system")
43+
async def get_system_information() -> dict[str, PackageVersionResponse]:
44+
"""System information"""
45+
packages_and_versions = {project: project.__version__ for project in [ogr, specfile, packit]}
46+
# packit_service might not be installed (i.e. when running locally)
47+
# so it's treated differently
48+
packages_and_versions[packit_service] = packit_service.__version__ or get_version(
49+
root="..",
50+
relative_to=packit_service.__file__,
51+
)
52+
53+
return {
54+
project.__name__: PackageVersionResponse(
55+
commit=get_commit_from_version(version),
56+
version=version,
57+
)
58+
for project, version in packages_and_versions.items()
59+
if version
60+
}

0 commit comments

Comments
 (0)