151 lines
4.2 KiB
Python
151 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
from glob import glob
|
|
import os
|
|
import pkg_resources
|
|
import typing as t
|
|
|
|
from tutor import hooks as tutor_hooks
|
|
from tutor.__about__ import __version_suffix__
|
|
|
|
from .__about__ import __version__
|
|
|
|
# Handle version suffix in nightly mode, just like tutor core
|
|
if __version_suffix__:
|
|
__version__ += "-" + __version_suffix__
|
|
|
|
HERE = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
config = {
|
|
"unique": {
|
|
"MYSQL_PASSWORD": "{{ 8|random_string }}",
|
|
"SECRET_KEY": "{{ 20|random_string }}",
|
|
"OAUTH2_SECRET": "{{ 8|random_string }}",
|
|
"OAUTH2_SECRET_SSO": "{{ 8|random_string }}",
|
|
},
|
|
"defaults": {
|
|
"VERSION": __version__,
|
|
"DOCKER_IMAGE": "{{ DOCKER_REGISTRY}}overhangio/openedx-discovery:{{ DISCOVERY_VERSION }}",
|
|
"HOST": "discovery.{{ LMS_HOST }}",
|
|
"INDEX_OVERRIDES": {},
|
|
"MYSQL_DATABASE": "discovery",
|
|
"MYSQL_USERNAME": "discovery",
|
|
"OAUTH2_KEY": "discovery",
|
|
"OAUTH2_KEY_DEV": "discovery-dev",
|
|
"OAUTH2_KEY_SSO": "discovery-sso",
|
|
"OAUTH2_KEY_SSO_DEV": "discovery-sso-dev",
|
|
"CACHE_REDIS_DB": "{{ OPENEDX_CACHE_REDIS_DB }}",
|
|
"ATLAS_PULL": False,
|
|
"EXTRA_PIP_REQUIREMENTS": [],
|
|
},
|
|
}
|
|
|
|
# Initialization tasks
|
|
init_tasks = ("mysql", "lms", "discovery")
|
|
for service in init_tasks:
|
|
with open(
|
|
os.path.join(
|
|
pkg_resources.resource_filename("tutordiscovery", "templates"),
|
|
"discovery",
|
|
"tasks",
|
|
service,
|
|
"init",
|
|
),
|
|
encoding="utf8",
|
|
) as fi:
|
|
tutor_hooks.Filters.CLI_DO_INIT_TASKS.add_item(
|
|
(
|
|
service,
|
|
fi.read(),
|
|
)
|
|
)
|
|
|
|
# Image management
|
|
tutor_hooks.Filters.IMAGES_BUILD.add_item(
|
|
(
|
|
"discovery",
|
|
("plugins", "discovery", "build", "discovery"),
|
|
"{{ DISCOVERY_DOCKER_IMAGE }}",
|
|
(),
|
|
)
|
|
)
|
|
tutor_hooks.Filters.IMAGES_PULL.add_item(
|
|
(
|
|
"discovery",
|
|
"{{ DISCOVERY_DOCKER_IMAGE }}",
|
|
)
|
|
)
|
|
tutor_hooks.Filters.IMAGES_PUSH.add_item(
|
|
(
|
|
"discovery",
|
|
"{{ DISCOVERY_DOCKER_IMAGE }}",
|
|
)
|
|
)
|
|
|
|
|
|
REPO_NAME = "course-discovery"
|
|
APP_NAME = "discovery"
|
|
|
|
|
|
# Automount /openedx/discovery folder from the container
|
|
@tutor_hooks.Filters.COMPOSE_MOUNTS.add()
|
|
def _mount_course_discovery(mounts, name):
|
|
if name == REPO_NAME:
|
|
mounts.append((APP_NAME, "/openedx/discovery"))
|
|
return mounts
|
|
|
|
|
|
# Bind-mount repo at build-time, both for prod and dev images
|
|
@tutor_hooks.Filters.IMAGES_BUILD_MOUNTS.add()
|
|
def _mount_course_discovery_on_build(mounts: list[tuple[str, str]], host_path: str) -> list[tuple[str, str]]:
|
|
path_basename = os.path.basename(host_path)
|
|
if path_basename == REPO_NAME:
|
|
mounts.append((APP_NAME, f"{APP_NAME}-src"))
|
|
mounts.append((f"{APP_NAME}-dev", f"{APP_NAME}-src"))
|
|
return mounts
|
|
|
|
|
|
####### Boilerplate code
|
|
# Add the "templates" folder as a template root
|
|
tutor_hooks.Filters.ENV_TEMPLATE_ROOTS.add_item(
|
|
pkg_resources.resource_filename("tutordiscovery", "templates")
|
|
)
|
|
# Render the "build" and "apps" folders
|
|
tutor_hooks.Filters.ENV_TEMPLATE_TARGETS.add_items(
|
|
[
|
|
("discovery/build", "plugins"),
|
|
("discovery/apps", "plugins"),
|
|
],
|
|
)
|
|
# Load patches from files
|
|
for path in glob(
|
|
os.path.join(
|
|
pkg_resources.resource_filename("tutordiscovery", "patches"),
|
|
"*",
|
|
)
|
|
):
|
|
with open(path, encoding="utf-8") as patch_file:
|
|
tutor_hooks.Filters.ENV_PATCHES.add_item(
|
|
(os.path.basename(path), patch_file.read())
|
|
)
|
|
# Add configuration entries
|
|
tutor_hooks.Filters.CONFIG_DEFAULTS.add_items(
|
|
[(f"DISCOVERY_{key}", value) for key, value in config.get("defaults", {}).items()]
|
|
)
|
|
tutor_hooks.Filters.CONFIG_UNIQUE.add_items(
|
|
[(f"DISCOVERY_{key}", value) for key, value in config.get("unique", {}).items()]
|
|
)
|
|
tutor_hooks.Filters.CONFIG_OVERRIDES.add_items(
|
|
list(config.get("overrides", {}).items())
|
|
)
|
|
|
|
|
|
@tutor_hooks.Filters.APP_PUBLIC_HOSTS.add()
|
|
def _print_discovery_public_hosts(hosts: list[str], context_name: t.Literal["local", "dev"]) -> list[str]:
|
|
if context_name == "dev":
|
|
hosts += ["{{ DISCOVERY_HOST }}:8381"]
|
|
else:
|
|
hosts += ["{{ DISCOVERY_HOST }}"]
|
|
return hosts
|