Skip to content

Commit

Permalink
refactor(init): added superuser creation and periodic tasks to init
Browse files Browse the repository at this point in the history
  • Loading branch information
esteban-puerta-rs committed Oct 27, 2024
1 parent 502a45c commit 6b51f8f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
32 changes: 31 additions & 1 deletion kitchenai/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing_extensions import Annotated
from rich.console import Console
from rich.spinner import Spinner
import os

console = Console()

Expand All @@ -22,7 +23,12 @@ def add(module: str = typer.Argument("app.kitchen:kitchen")):
execute_from_command_line(["manage", "add_module", module])

@app.command()
def init(verbose: Annotated[int, typer.Option(help="verbosity level. default 0")] = 0):
def init(
verbose: Annotated[int, typer.Option(help="verbosity level. default 0")] = 0,
email: Annotated[str, typer.Option(help="superuser email")] = os.environ.get("DJANGO_SUPERUSER_EMAIL", "admin@localhost"),
password: Annotated[str, typer.Option(help="superuser email")] = os.environ.get("DJANGO_SUPERUSER_PASSWORD", "admin"),

):
django.setup()
from django.core.management import execute_from_command_line
from kitchenai.core.models import KitchenAIManagement
Expand All @@ -33,8 +39,32 @@ def init(verbose: Annotated[int, typer.Option(help="verbosity level. default 0")
if not verbose == 1:
with console.status("Applying migrations...", spinner="dots"):
execute_from_command_line(cmd)

with console.status("Setting up periodic tasks", spinner="dots"):
execute_from_command_line(["manage", "setup_periodic_tasks"])

with console.status("Creating superuser...", spinner="dots"):
execute_from_command_line(["manage", "setup_periodic_tasks"])

username = os.environ.get("DJANGO_SUPERUSER_USERNAME", email.split("@")[0])

if password == "admin":
os.environ["DJANGO_SUPERUSER_PASSWORD"] = "admin"

execute_from_command_line(
["manage", "createsuperuser", "--noinput", "--traceback", "--email", email, "--username", username]
)
else:
execute_from_command_line(cmd)
execute_from_command_line(["manage", "setup_periodic_tasks"])
username = os.environ.get("DJANGO_SUPERUSER_USERNAME", email.split("@")[0])

if password == "admin":
os.environ["DJANGO_SUPERUSER_PASSWORD"] = "admin"

execute_from_command_line(
["manage", "createsuperuser", "--noinput", "--traceback", "--email", email, "--username", username]
)


KitchenAIManagement.objects.all().delete()
Expand Down
41 changes: 31 additions & 10 deletions kitchenai/templates/build_templates/Dockerfile.tmpl
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
# Stage 1: Use the kitchenai base image
FROM epuerta18/kitchenai:latest

# Stage 1: General enviroment
FROM kitchenai:latest
# Set up environment variables
ENV PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv" \
PYTHONPATH="/app" \
PATH="$VENV_PATH/bin:$PATH"

# Install dependencies
WORKDIR $PYSETUP_PATH
COPY ./requirements.txt ./
RUN pip install --upgrade pip uv \
&& python -m uv venv $VENV_PATH && uv pip install -r requirements.txt
# Copy additional dependencies (e.g., wheel files) into the container
COPY dist/ /tmp

# Install additional dependencies into the existing virtual environment
WORKDIR /tmp
#RUN python -m ensurepip && \
python -m pip install /tmp/*.whl

COPY requirements.txt requirements.txt

RUN python -m ensurepip && \
python -m pip install -r requirements.txt

# Build static files
COPY . /app
# Set the working directory
WORKDIR /app

# Run migrations and setup tasks using the existing manage.py script
RUN . $VENV_PATH/bin/activate && \
python manage.py migrate && \
python manage.py setup_periodic_tasks

COPY . /app/

# Initialize the application
RUN . $VENV_PATH/bin/activate && python manage.py init

# Expose the application port
EXPOSE 8000

# Use the init system defined in the base image
ENTRYPOINT ["/init"]
# HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl -f http://0.0.0.0:8000/health

0 comments on commit 6b51f8f

Please sign in to comment.