Skip to content

Commit

Permalink
feat: apply git release version to OpenAPI spec (livepeer#164)
Browse files Browse the repository at this point in the history
This commit ensures that the latest git release flag is applied to the OpenAPI spec.
  • Loading branch information
rickstaa authored Aug 20, 2024
1 parent 557631c commit 78b5813
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion runner/gateway.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Livepeer AI Runner",
"description": "An application to run AI pipelines",
"version": "0.1.0"
"version": "v0.1.2"
},
"servers": [
{
Expand Down
54 changes: 47 additions & 7 deletions runner/gen_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
upscale,
)
from fastapi.openapi.utils import get_openapi
import subprocess
import logging

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)

# Specify Endpoints for OpenAPI schema generation.
SERVERS = [
Expand All @@ -23,6 +32,28 @@
]


def get_latest_git_release_tag():
"""
Get the latest Git release tag that follows semantic versioning.
Returns:
str: The latest Git release tag, or None if an error occurred.
"""
try:
command = (
"git tag -l 'v*' | grep -E '^v[0-9]+\\.[0-9]+\\.[0-9]+$' | sort -V | "
"tail -n 1"
)
latest_tag = subprocess.check_output(command, shell=True, text=True)
return latest_tag.strip()
except subprocess.CalledProcessError as e:
logger.error("Error occurred while getting the latest git tag: %s", e)
return None
except Exception as e:
logger.error("Unexpected error: %s", e)
return None


def translate_to_gateway(openapi):
"""Translate the OpenAPI schema from the 'runner' entrypoint to the 'gateway'
entrypoint created by the https://github.com/livepeer/go-livepeer package.
Expand Down Expand Up @@ -70,14 +101,15 @@ def translate_to_gateway(openapi):
return openapi


def write_openapi(fname, entrypoint="runner"):
def write_openapi(fname, entrypoint="runner", version="0.0.0"):
"""Write OpenAPI schema to file.
Args:
fname (str): The file name to write to. The file extension determines the file
type. Either 'json' or 'yaml'.
entrypoint (str): The entrypoint to generate the OpenAPI schema for, either
'gateway' or 'runner'. Default is 'runner'.
version (str): The version to set in the OpenAPI schema. Default is '0.0.0'.
"""
app.include_router(health.router)
app.include_router(text_to_image.router)
Expand All @@ -88,10 +120,10 @@ def write_openapi(fname, entrypoint="runner"):

use_route_names_as_operation_ids(app)

print(f"Generating OpenAPI schema for '{entrypoint}' entrypoint...")
logger.info(f"Generating OpenAPI schema for '{entrypoint}' entrypoint...")
openapi = get_openapi(
title="Livepeer AI Runner",
version="0.1.0",
version=version,
openapi_version=app.openapi_version,
description="An application to run AI pipelines",
routes=app.routes,
Expand All @@ -100,13 +132,15 @@ def write_openapi(fname, entrypoint="runner"):

# Translate OpenAPI schema to 'gateway' side entrypoint if requested.
if entrypoint == "gateway":
print("Translating OpenAPI schema from 'runner' to 'gateway' entrypoint...")
logger.info(
"Translating OpenAPI schema from 'runner' to 'gateway' entrypoint..."
)
openapi = translate_to_gateway(openapi)
fname = f"gateway.{fname}"

# Write OpenAPI schema to file.
with open(fname, "w") as f:
print(f"Writing OpenAPI schema to '{fname}'...")
logger.info(f"Writing OpenAPI schema to '{fname}'...")
if fname.endswith(".yaml"):
yaml.dump(
openapi,
Expand All @@ -119,7 +153,7 @@ def write_openapi(fname, entrypoint="runner"):
f,
indent=4, # Make human readable.
)
print("OpenAPI schema generated and saved.")
logger.info("OpenAPI schema generated and saved.")


if __name__ == "__main__":
Expand All @@ -144,6 +178,12 @@ def write_openapi(fname, entrypoint="runner"):
)
args = parser.parse_args()

# Set the 'version' to the latest Git release tag.
latest_tag = get_latest_git_release_tag()

# Generate orchestrator and Gateway facing OpenAPI schemas.
logger.info("Generating OpenAPI schema version: $latest_tag")
for entrypoint in args.entrypoint:
write_openapi(f"openapi.{args.type.lower()}", entrypoint)
write_openapi(
f"openapi.{args.type.lower()}", entrypoint=entrypoint, version=latest_tag
)
2 changes: 1 addition & 1 deletion runner/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Livepeer AI Runner",
"description": "An application to run AI pipelines",
"version": "0.1.0"
"version": "v0.1.2"
},
"servers": [
{
Expand Down

0 comments on commit 78b5813

Please sign in to comment.