felicity-lims/felicity/cli.py

110 lines
2.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import asyncio
2024-08-08 22:59:54 +08:00
import subprocess
2024-09-20 20:35:58 +08:00
import typer
from uvicorn import Config, Server
from felicity.almigrator import FelicityMigrator
from felicity.logcong import LOGGING_CONFIG
alembic_service = FelicityMigrator()
app = typer.Typer()
@app.command()
2024-09-22 23:15:27 +08:00
def runserver(
host: str = "0.0.0.0",
port: int = 8000,
workers: int = 1,
reload: bool = False,
colors: bool = True,
2024-09-28 15:29:22 +08:00
) -> None:
2024-09-22 23:15:27 +08:00
"""Felicity LIMS Server"""
config = Config(
app="felicity.main:felicity",
host=host,
port=port,
workers=workers,
reload=reload,
use_colors=colors,
2024-09-22 23:15:27 +08:00
log_config=LOGGING_CONFIG,
)
Server(config).run()
2024-08-08 22:59:54 +08:00
@app.command()
def gunicorn(
2024-09-22 23:15:27 +08:00
host: str = "0.0.0.0",
port: int = 8000,
workers: int = 1,
reload: bool = False,
colors: bool = True,
2024-09-28 15:29:22 +08:00
) -> None:
2024-09-22 23:15:27 +08:00
"""Felicity LIMS Server"""
2024-08-08 22:59:54 +08:00
# Build the command to run gunicorn with uvicorn workers
command = [
"gunicorn",
f"--bind={host}:{port}",
f"--workers={workers}",
2024-09-22 23:04:34 +08:00
"--reload" if reload else "",
"--log-level=info",
"--access-logfile=-" if colors else "",
2024-08-08 22:59:54 +08:00
"felicity.main:felicity",
2024-09-22 23:15:27 +08:00
"--worker-class=uvicorn.workers.UvicornWorker",
2024-08-08 22:59:54 +08:00
]
# Join the command list into a single string and print it for debugging
cmd_str = " ".join(command)
print(f"Running command: {cmd_str}")
# Execute the command
subprocess.run(cmd_str, shell=True, check=True)
@app.command()
2024-09-28 15:29:22 +08:00
def upgrade(revision: str = typer.Option("head", help="Target revision to upgrade to")) -> None:
"""Upgrade to a specified revision."""
alembic_service.upgrade(revision)
typer.echo(f"Upgraded to revision: {revision}")
@app.command()
2024-09-22 23:15:27 +08:00
def downgrade(
revision: str = typer.Argument(..., help="Target revision to downgrade to"),
2024-09-28 15:29:22 +08:00
) -> None:
"""Downgrade to a specified revision."""
alembic_service.downgrade(revision)
typer.echo(f"Downgraded to revision: {revision}")
@app.command()
2024-09-28 15:29:22 +08:00
def revision(message: str = typer.Argument(..., help="Message for the new revision")) -> None:
2024-09-20 20:35:58 +08:00
"""Not working --- no idea why yet
Create a new Alembic revision with a message."""
alembic_service.create_revision(message)
typer.echo(f"Created new revision with message: {message}")
@app.command()
2024-09-28 15:29:22 +08:00
def current() -> None:
"""Show the current database revision."""
alembic_service.current()
@app.command()
2024-09-28 15:29:22 +08:00
def history() -> None:
"""Show the revision history."""
alembic_service.history()
@app.command()
2024-09-28 15:29:22 +08:00
def updates() -> None:
"""Check for updates"""
asyncio.run(alembic_service.check_for_updates())
2024-09-28 15:29:22 +08:00
def main() -> None:
2024-09-22 23:15:27 +08:00
app()