Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👷 Implement changes for flake based deployment #456

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ poe migrate
To create a new admin user, you can now run:

```shell
poe create_user --user admin --pass admin
poe admin create_user --user admin --pass admin
```

Now you can start the development server with
Expand Down
13 changes: 7 additions & 6 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ notebooks = [
"seaborn~=0.12.2",
]

[project.scripts]
transcribee-migrate = "transcribee_backend.db.run_migrations:main"
transcribee-admin = "transcribee_backend.admin_cli:main"

[tool.uv]
override-dependencies = [
"sqlalchemy==1.4.41"
Expand All @@ -58,16 +62,13 @@ start = "uvicorn transcribee_backend.main:app --ws websockets"
dev = "uvicorn transcribee_backend.main:app --reload --ws websockets"
migrate = "alembic upgrade head"
makemigrations = "alembic revision --autogenerate -m"
create_user = "scripts/create_user.py"
create_worker = "scripts/create_worker.py"
create_api_token = "scripts/create_api_token.py"
reset_task = "scripts/reset_task.py"
admin = "transcribee-admin"
generate_openapi = "python -m scripts.generate_openapi"
test = "pytest tests/"
pyright = "pyright transcribee_backend/"

[tool.setuptools]
packages = ["transcribee_backend"]
[tool.setuptools.packages.find]
include = ["transcribee_backend*"]

[build-system]
requires = ["setuptools", "setuptools-scm"]
Expand Down
14 changes: 0 additions & 14 deletions backend/scripts/create_api_token.py

This file was deleted.

40 changes: 0 additions & 40 deletions backend/scripts/create_user.py

This file was deleted.

38 changes: 0 additions & 38 deletions backend/scripts/create_user_token.py

This file was deleted.

28 changes: 0 additions & 28 deletions backend/scripts/create_worker.py

This file was deleted.

33 changes: 0 additions & 33 deletions backend/scripts/reset_task.py

This file was deleted.

22 changes: 0 additions & 22 deletions backend/scripts/set_password.py

This file was deleted.

40 changes: 40 additions & 0 deletions backend/transcribee_backend/admin_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import argparse

from .command import Command
from .commands.create_api_token import CreateApiTokenCmd
from .commands.create_user import CreateUserCmd
from .commands.create_user_token import CreateUserTokenCmd
from .commands.create_worker import CreateWorkerCmd
from .commands.reset_task import ResetTaskCmd
from .commands.set_password import SetPasswordCmd

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(metavar="COMMAND")


def add_command(name: str, description: str, command: Command):
subparser = subparsers.add_parser(name, help=description, description=description)
command.configure_parser(subparser)
subparser.set_defaults(func=command.run)


# all commands belong here
add_command("create_api_token", "Create an API token", CreateApiTokenCmd())
add_command("create_user_token", "Create an user token", CreateUserTokenCmd())
add_command("create_user", "Create a new user", CreateUserCmd())
add_command("create_worker", "Register a new worker", CreateWorkerCmd())
add_command("reset_task", "Reset a task", ResetTaskCmd())
add_command("set_password", "Set the password of a user", SetPasswordCmd())


def main():
args = parser.parse_args()

if "func" in args:
args.func(args)
else:
parser.print_help()


if __name__ == "__main__":
main()
15 changes: 15 additions & 0 deletions backend/transcribee_backend/admin_cli/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from abc import ABC, abstractmethod
from argparse import ArgumentParser


class Command(ABC):
def __init__(self):
pass

@abstractmethod
def configure_parser(self, parser: ArgumentParser):
pass

@abstractmethod
def run(self, args):
pass
15 changes: 15 additions & 0 deletions backend/transcribee_backend/admin_cli/commands/create_api_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from transcribee_backend.admin_cli.command import Command
from transcribee_backend.auth import create_api_token
from transcribee_backend.db import SessionContextManager


class CreateApiTokenCmd(Command):
def configure_parser(self, parser):
parser.add_argument("--name", required=True)

def run(self, args):
with SessionContextManager(
path="management_command:create_api_token"
) as session:
token = create_api_token(session=session, name=args.name)
print(f"Token created: {token.token}")
38 changes: 38 additions & 0 deletions backend/transcribee_backend/admin_cli/commands/create_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import random
import string

from transcribee_backend.admin_cli.command import Command
from transcribee_backend.auth import create_user
from transcribee_backend.db import SessionContextManager
from transcribee_backend.exceptions import UserAlreadyExists


def random_password():
chars = string.ascii_lowercase + string.digits
return "".join(random.choice(chars) for _ in range(20))


class CreateUserCmd(Command):
def configure_parser(self, parser):
parser.add_argument("--user", required=True)
parser.add_argument("--password", required=False)

def run(self, args):
password = args.password

if not password:
password = random_password()

print("Auto-generated password.")
print("Infos to send to user:")
print()
print(f"Username: {args.user}")
print(f"Password: {password} (Please change)")
print()

with SessionContextManager(path="management_command:create_user") as session:
try:
create_user(session=session, username=args.user, password=password)
print("User created")
except UserAlreadyExists:
print("Could not create user. A user with that name already exists.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import datetime

from sqlmodel import select
from transcribee_backend.admin_cli.command import Command
from transcribee_backend.auth import generate_user_token
from transcribee_backend.db import SessionContextManager
from transcribee_backend.helpers.time import now_tz_aware
from transcribee_backend.models.user import User


class CreateUserTokenCmd(Command):
def configure_parser(self, parser):
parser.add_argument("--username", required=True)
parser.add_argument("--valid-days", required=True)

def run(self, args):
with SessionContextManager(
path="management_command:create_user_token"
) as session:
valid_days = int(args.valid_days)
if valid_days < 0:
print("Valid days must be positive")
exit(1)

valid_until = now_tz_aware() + datetime.timedelta(days=valid_days)

user = session.exec(
select(User).where(User.username == args.username)
).one_or_none()

if user is None:
print(f"User {args.user} not found")
exit(1)

key, user_token = generate_user_token(user, valid_until=valid_until)
session.add(user_token)
session.commit()

print(f"User token created and valid until {valid_until}")
print(f"Secret: {key}")
Loading
Loading