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

CH-59 Update/Refactor the django application template #770

Merged
merged 22 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
29f6dd3
CH-59 Rename __APP_NAME__ directory to ch_django and api directory to…
condar-metacell Sep 12, 2024
9127e92
CH-59 Move settings back to __APP_NAME__ directory
condar-metacell Sep 12, 2024
301b8ed
CH-59 Add initial api migration depending on ch_django migration
condar-metacell Sep 12, 2024
15b4470
CH-59 Update api to __APP_NAME__ in django templates
condar-metacell Sep 13, 2024
21c6167
CH-59 Rename ch_django directory to django_baseapp directory in the d…
condar-metacell Sep 16, 2024
ae198ee
CH-59 Fix linting issues
condar-metacell Sep 16, 2024
9e62160
Merge branch 'develop' into feature/ch-59
condar-metacell Sep 24, 2024
6201a14
CH-59 Move settings back to the django_baseapp in the django-app temp…
condar-metacell Sep 24, 2024
21a21ca
CH-59 Add back in the removed shutil import
condar-metacell Sep 24, 2024
1a604bd
CH-59 Update django settings location
condar-metacell Sep 24, 2024
fcf609c
CH-59 Update views import to import from django_baseapp
condar-metacell Sep 24, 2024
37a628e
CH-149 Refactor harness-application script for better maintainability
condar-metacell Sep 26, 2024
c8ce970
CH-149 Add dev setup script for django applications
condar-metacell Sep 26, 2024
63a4e18
CH-149 Change cloudharness-django depdencies from fixed versions to m…
condar-metacell Sep 26, 2024
795b7d4
CH-149 Add debug configuration to vscode launch settings when creatin…
condar-metacell Sep 26, 2024
d4937fb
CH-67 Make events listener initialization an explicit action instead …
condar-metacell Sep 26, 2024
2f7b474
CH-67 Change listener initialization to use a singleton and the setup…
condar-metacell Sep 26, 2024
2a1824a
CH-67 Add background initialization for auth and events to main.py te…
condar-metacell Sep 26, 2024
b291842
CH-149 Update command in generate_fastapi_server
condar-metacell Sep 27, 2024
0dfd8c8
CH-149 Add unit tests for the added utility
condar-metacell Sep 27, 2024
09d9842
CH-59 Fix linting errors
condar-metacell Sep 27, 2024
e11608f
CH-67 Fix incorrect assignment of singleton
condar-metacell Sep 27, 2024
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
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
Loading