Skip to content

Commit

Permalink
Merge pull request #770 from MetaCell/feature/ch-59
Browse files Browse the repository at this point in the history
CH-59 Update/Refactor the django application template
  • Loading branch information
filippomc authored Sep 27, 2024
2 parents e76fc02 + e11608f commit 5943858
Show file tree
Hide file tree
Showing 36 changed files with 438 additions and 129 deletions.
12 changes: 7 additions & 5 deletions application-templates/django-app/api/templates/main.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ from fastapi.staticfiles import StaticFiles

{{imports | replace(".","openapi.")}}

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "__APP_NAME__.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_baseapp.settings")
apps.populate(settings.INSTALLED_APPS)

# migrate the Django models
os.system("python manage.py migrate")

from api.controllers import *
from __APP_NAME__.controllers import *

app = FastAPI(
{% if info %}
Expand Down Expand Up @@ -66,10 +66,12 @@ async def add_process_time_header(request: Request, call_next):

if os.environ.get('KUBERNETES_SERVICE_HOST', None):
# init the auth service when running in/for k8s
from cloudharness_django.services import init_services, get_auth_service
init_services()
from cloudharness_django.services import init_services_in_background, get_auth_service
init_services_in_background()

# start the kafka event listener when running in/for k8s
import cloudharness_django.services.events
from cloudharness_django.services.events import init_listener_in_background
init_listener_in_background()

async def has_access():
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'
name = '__APP_NAME__'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import __APP_NAME__.controllers.test as test_controller
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import os

from django.db import migrations


def create_kc_client_and_roles(apps, schema_editor):
if os.environ.get("KUBERNETES_SERVICE_HOST", None):
# running in K8S so create the KC client and roles
from cloudharness_django.services import get_auth_service, get_user_service, init_services

init_services()
get_auth_service().create_client()
get_user_service().sync_kc_users_groups()


class Migration(migrations.Migration):

dependencies = [
("cloudharness_django", "0001_initial"),
("django_baseapp", "0001_initial"),
]

operations = [
migrations.RunPython(create_kc_client_and_roles),
]
operations = []
25 changes: 2 additions & 23 deletions application-templates/django-app/backend/__APP_NAME__/views.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
import mimetypes
from pathlib import Path
from django.shortcuts import render

from django.conf import settings
from django.http import FileResponse, HttpResponseRedirect
from django.urls import reverse
from django.utils._os import safe_join


def view_404(request, exception=None):
return HttpResponseRedirect(reverse("index"))


def index(request, path=""):
if path == "":
path = "index.html"
fullpath = Path(safe_join(settings.STATIC_ROOT, "www", path))
content_type, encoding = mimetypes.guess_type(str(fullpath))
content_type = content_type or "application/octet-stream"
try:
fullpath.open("rb")
except FileNotFoundError:
return index(request, "") # index.html
return FileResponse(fullpath.open("rb"), content_type=content_type)
# Create your views here.

This file was deleted.

3 changes: 0 additions & 3 deletions application-templates/django-app/backend/api/views.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@
init_services()

# start the kafka event listener
import cloudharness_django.services.events # noqa E402
from cloudharness_django.services.events import init_listner # noqa E402

init_listner()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from django.db import migrations


def create_kc_client_and_roles(apps, schema_editor):
if os.environ.get("KUBERNETES_SERVICE_HOST", None):
# running in K8S so create the KC client and roles
from cloudharness_django.services import get_auth_service, get_user_service, init_services

init_services()
get_auth_service().create_client()
get_user_service().sync_kc_users_groups()


class Migration(migrations.Migration):

dependencies = [
("cloudharness_django", "0001_initial"),
]

operations = [
migrations.RunPython(create_kc_client_and_roles),
]
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
]


ROOT_URLCONF = "__APP_NAME__.urls"
ROOT_URLCONF = "django_baseapp.urls"

TEMPLATES = [
{
Expand All @@ -71,7 +71,7 @@
},
]

WSGI_APPLICATION = "__APP_NAME__.wsgi.application"
WSGI_APPLICATION = "django_baseapp.wsgi.application"


# Password validation
Expand Down Expand Up @@ -130,8 +130,8 @@

# add the local apps
INSTALLED_APPS += [
"api",
"__APP_NAME__"
"__APP_NAME__",
"django_baseapp"
]

# override django admin base template with a local template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from django.contrib import admin
from django.urls import path, re_path

from __APP_NAME__.views import index
from django_baseapp.views import index


urlpatterns = [path("admin/", admin.site.urls)]
Expand Down
24 changes: 24 additions & 0 deletions application-templates/django-app/backend/django_baseapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mimetypes
from pathlib import Path

from django.conf import settings
from django.http import FileResponse, HttpResponseRedirect
from django.urls import reverse
from django.utils._os import safe_join


def view_404(request, exception=None):
return HttpResponseRedirect(reverse("index"))


def index(request, path=""):
if path == "":
path = "index.html"
fullpath = Path(safe_join(settings.STATIC_ROOT, "www", path))
content_type, encoding = mimetypes.guess_type(str(fullpath))
content_type = content_type or "application/octet-stream"
try:
fullpath.open("rb")
except FileNotFoundError:
return index(request, "") # index.html
return FileResponse(fullpath.open("rb"), content_type=content_type)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "__APP_NAME__.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_baseapp.settings")

application = get_wsgi_application()

Expand All @@ -21,4 +21,6 @@
init_services()

# start the kafka event listener
import cloudharness_django.services.events # noqa E402
from cloudharness_django.services.events import init_listner # noqa E402

init_listner()
2 changes: 1 addition & 1 deletion application-templates/django-app/backend/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "__APP_NAME__.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_baseapp.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
51 changes: 51 additions & 0 deletions application-templates/django-app/dev-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

CURRENT_PATH=$(pwd)
CH_DIRECTORY="../../cloud-harness"
INSTALL_PYTEST=false
CURRENT_DIRECTORY="$(pwd)"
APP_NAME="__APP_NAME__"

pip_upgrade_error() {
echo "Unable to upgrade pip"
exit 1
}

install_error () {
echo "Unable to install $1" 1>&2
exit 1
}

while getopts ch_directory:pytest arg;
do
case "$arg" in
ch_directory) CH_DIRECTORY=${OPTARG};;
pytest) INSTALL_PYTEST=true;;
esac
done

pip install --upgrade pip || pip_upgrade_error

# Install pip dependencies from cloudharness-base-debian image

if $INSTALL_PYTEST; then
pip install pytest || install_error pytest
fi

pip install -r "$CH_DIRECTORY/libraries/models/requirements.txt" || install_error "models requirements"
pip install -r "$CH_DIRECTORY/libraries/cloudharness-common/requirements.txt" || install_error "cloudharness-common requirements"
pip install -r "$CH_DIRECTORY/libraries/client/cloudharness_cli/requirements.txt" || install_error "cloudharness_cli requirements"

pip install -e "$CH_DIRECTORY/libraries/models" || install_error models
pip install -e "$CH_DIRECTORY/libraries/cloudharness-common" || install_error cloudharness-common
pip install -e "$CH_DIRECTORY/libraries/client/cloudharness_cli" || install_error cloudharness_cli

# Install pip dependencies from cloudharness-django image

pip install -r "$CH_DIRECTORY/infrastructure/common-images/cloudharness-django/libraries/fastapi/requirements.txt" || install_error "cloudharness-django fastapi requirements"
pip install -e "$CH_DIRECTORY/infrastructure/common-images/cloudharness-django/libraries/cloudharness-django" || install_error cloudharness-django

# Install application

pip install -r "$CURRENT_DIRECTORY/backend/requirements.txt" || install_error "$APP_NAME dependencies"
pip install -e "$CURRENT_DIRECTORY/backend" || install_error "$APP_NAME"
24 changes: 24 additions & 0 deletions deployment-configuration/vscode-django-app-debug-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"args": [
"--host",
"0.0.0.0",
"--port",
"8000",
"main:app"
],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/applications/__APP_NAME__/backend",
"env": {
"ACCOUNTS_ADMIN_PASSWORD": "metacell",
"ACCOUNTS_ADMIN_USERNAME": "admin",
"CH_CURRENT_APP_NAME": "__APP_NAME__",
"CH_VALUES_PATH": "${workspaceFolder}/deployment/helm/values.yaml",
"DJANGO_SETTINGS_MODULE": "django_baseapp.settings",
"KUBERNETES_SERVICE_HOST": "ssdds"
},
"justMyCode": false,
"module": "uvicorn",
"name": "__APP_NAME__ backend",
"request": "launch",
"type": "debugpy"
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ Quick start
init_services()
# start the kafka event listener
import cloudharness_django.services.events
from cloudharness_django.services.events import init_listner # noqa E402
init_listner()
```
4. Start the development server and visit http://127.0.0.1:8000/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,28 @@ def init_services(
admin_role=admin_role)
_user_service = UserService(_auth_service)
return _auth_service


def init_services_in_background(
client_name: str = settings.KC_CLIENT_NAME,
client_roles: List[str] = settings.KC_ALL_ROLES,
privileged_roles: List[str] = settings.KC_PRIVILEGED_ROLES,
admin_role: str = settings.KC_ADMIN_ROLE,
default_user_role: str = settings.KC_DEFAULT_USER_ROLE
):
import threading
import time
from cloudharness import log

def background_operation():
services_initialized = False

while not services_initialized:
try:
init_services(client_name, client_roles, privileged_roles, admin_role, default_user_role)
services_initialized = True
except:
log.exception("Error initializing services. Retrying in 5 seconds...")
time.sleep(5)

threading.Thread(target=background_operation).start()
Loading

0 comments on commit 5943858

Please sign in to comment.