Added git commit and build date env and API
This commit is contained in:
@@ -197,6 +197,8 @@ class Info(BaseModel):
|
||||
data_dir: str = Field(example="/home/app/.local/share/spoolman")
|
||||
backups_dir: str = Field(example="/home/app/.local/share/spoolman/backups")
|
||||
db_type: str = Field(example="sqlite")
|
||||
git_commit: Optional[str] = Field(example="a1b2c3d")
|
||||
build_date: Optional[datetime] = Field(example="2021-01-01T00:00:00Z")
|
||||
|
||||
|
||||
class HealthCheck(BaseModel):
|
||||
|
||||
@@ -43,6 +43,8 @@ async def info() -> models.Info:
|
||||
data_dir=str(env.get_data_dir().resolve()),
|
||||
backups_dir=str(env.get_backups_dir().resolve()),
|
||||
db_type=str(env.get_database_type() or "sqlite"),
|
||||
git_commit=env.get_commit_hash(),
|
||||
build_date=env.get_build_date(),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
@@ -245,3 +246,29 @@ def get_version() -> str:
|
||||
str: The version.
|
||||
"""
|
||||
return pkg_resources.get_distribution("spoolman").version
|
||||
|
||||
|
||||
def get_commit_hash() -> Optional[str]:
|
||||
"""Get the latest commit hash of the package.
|
||||
|
||||
Can end with "-dirty" if there are uncommitted changes.
|
||||
|
||||
Returns:
|
||||
Optional[str]: The commit hash.
|
||||
"""
|
||||
commit_hash = os.getenv("GIT_COMMIT", "unknown")
|
||||
if commit_hash == "unknown":
|
||||
return None
|
||||
return commit_hash
|
||||
|
||||
|
||||
def get_build_date() -> Optional[datetime]:
|
||||
"""Get the build date of the package.
|
||||
|
||||
Returns:
|
||||
Optional[datetime.datetime]: The build date.
|
||||
"""
|
||||
build_date = os.getenv("BUILD_DATE", "unknown")
|
||||
if build_date == "unknown":
|
||||
return None
|
||||
return datetime.fromisoformat(build_date)
|
||||
|
||||
@@ -63,7 +63,12 @@ if env.is_debug_mode():
|
||||
@app.on_event("startup")
|
||||
async def startup() -> None:
|
||||
"""Run the service's startup sequence."""
|
||||
logger.info("Starting Spoolman v%s...", app.version)
|
||||
logger.info(
|
||||
"Starting Spoolman v%s (commit: %s) (built: %s)",
|
||||
app.version,
|
||||
env.get_commit_hash(),
|
||||
env.get_build_date(),
|
||||
)
|
||||
|
||||
logger.info("Setting up database...")
|
||||
database.setup_db(database.get_connection_url())
|
||||
|
||||
Reference in New Issue
Block a user