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

feat: [Improvement] Adds FORUM_ENV filter to manage forum environment vars #24

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ Configuration
- ``FORUM_REPOSITORY`` (default: ``"https://github.com/openedx/cs_comments_service.git"``)
- ``FORUM_REPOSITORY_VERSION`` (default: ``"{{ OPENEDX_COMMON_VERSION }}"``)

Customising Environment Variables
---------------------------------

To add, or modify environment variables that are supplied to the forum service,
you can use the ``FORUM_ENV`` hook.

To add or modify a environment variable, update the corresponding entry in the
``FORUM_ENV`` dict as follows:

.. code-block:: python

from tutorforum.hooks import FORUM_ENV

@FORUM_ENV.add()
def _add_forum_env_vars(env_vars):
env_vars.update({ "NEW_ENV_VAR": "VALUE" })
return env_vars

If the environment variable already exists, it will be overridden, otherwise it
will be added. Note that if multiple plugins override the same value, the last
override will apply.

It is posible to use templates when setting the above values.


Caveats for the `mongodb+srv://` syntax
---------------------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- [Improvement] Introduces the `FORUM_ENV` filter to which any additional forum
which simplifies management of environment variables for the forum service.
Additional environment variables can be added to this filter, and existing
values can be removed as needed by plugins. These are rendered into the new
`forum-k8s-env` and `forum-local-env` patches for the kubernetes and docker
configs respectively. (by @xitij2000)
11 changes: 11 additions & 0 deletions tutorforum/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
These hooks are stored in a separate module. If they were included in plugin.py, then
the tutor-forum hooks would be created in the context of some other plugin that imports
them.
"""
from __future__ import annotations

from tutor.core.hooks import Filter
xitij2000 marked this conversation as resolved.
Show resolved Hide resolved


FORUM_ENV: Filter[dict[str, str], []] = Filter()
18 changes: 1 addition & 17 deletions tutorforum/patches/k8s-deployments
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,4 @@ spec:
ports:
- containerPort: {{ FORUM_PORT }}
env:
- name: SEARCH_SERVER
value: "{{ ELASTICSEARCH_SCHEME }}://{{ ELASTICSEARCH_HOST }}:{{ ELASTICSEARCH_PORT }}"
- name: MONGODB_AUTH
value: "{% if MONGODB_USERNAME and MONGODB_PASSWORD %}{{ MONGODB_USERNAME}}:{{ MONGODB_PASSWORD }}@{% endif %}"
- name: MONGODB_HOST
value: "{{ MONGODB_HOST|forum_mongodb_host }}"
- name: MONGODB_PORT
value: "{{ MONGODB_PORT }}"
- name: MONGODB_DATABASE
value: "{{ FORUM_MONGODB_DATABASE }}"
- name: MONGOID_USE_SSL
value: "{{ 'true' if MONGODB_USE_SSL else 'false' }}"
- name: MONGOID_AUTH_SOURCE
value: "{{MONGODB_AUTH_SOURCE}}"
- name: MONGOID_AUTH_MECH
value: "{{ MONGODB_AUTH_MECHANISM|auth_mech_as_ruby }}"

{{ patch("forum-k8s-env")|indent(12) }}
17 changes: 1 addition & 16 deletions tutorforum/patches/k8s-jobs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,4 @@ spec:
- name: forum
image: {{ FORUM_DOCKER_IMAGE }}
env:
- name: SEARCH_SERVER
value: "{{ ELASTICSEARCH_SCHEME }}://{{ ELASTICSEARCH_HOST }}:{{ ELASTICSEARCH_PORT }}"
- name: MONGODB_AUTH
value: "{% if MONGODB_USERNAME and MONGODB_PASSWORD %}{{ MONGODB_USERNAME}}:{{ MONGODB_PASSWORD }}@{% endif %}"
- name: MONGODB_HOST
value: "{{ MONGODB_HOST|forum_mongodb_host }}"
- name: MONGODB_PORT
value: "{{ MONGODB_PORT }}"
- name: MONGODB_DATABASE
value: "{{ FORUM_MONGODB_DATABASE }}"
- name: MONGOID_USE_SSL
value: "{{ 'true' if MONGODB_USE_SSL else 'false' }}"
- name: MONGOID_AUTH_SOURCE
value: "{{MONGODB_AUTH_SOURCE}}"
- name: MONGOID_AUTH_MECH
value: "{{ MONGODB_AUTH_MECHANISM|auth_mech_as_ruby }}"
{{ patch("forum-k8s-env")|indent(10) }}
9 changes: 1 addition & 8 deletions tutorforum/patches/local-docker-compose-jobs-services
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
forum-job:
image: {{ FORUM_DOCKER_IMAGE }}
environment:
SEARCH_SERVER: "{{ ELASTICSEARCH_SCHEME }}://{{ ELASTICSEARCH_HOST }}:{{ ELASTICSEARCH_PORT }}"
MONGODB_AUTH: "{% if MONGODB_USERNAME and MONGODB_PASSWORD %}{{ MONGODB_USERNAME}}:{{ MONGODB_PASSWORD }}@{% endif %}"
MONGODB_HOST: "{{ MONGODB_HOST|forum_mongodb_host }}"
MONGODB_PORT: "{{ MONGODB_PORT }}"
MONGODB_DATABASE: "{{ FORUM_MONGODB_DATABASE }}"
MONGOID_AUTH_SOURCE: "{{ MONGODB_AUTH_SOURCE }}"
MONGOID_AUTH_MECH: "{{ MONGODB_AUTH_MECHANISM|auth_mech_as_ruby }}"
MONGOID_USE_SSL: "{{ 'true' if MONGODB_USE_SSL else 'false' }}"
{{ patch("forum-local-env")|indent(4) }}
depends_on: {{ [("elasticsearch", RUN_ELASTICSEARCH), ("mongodb", RUN_MONGODB)]|list_if }}
9 changes: 1 addition & 8 deletions tutorforum/patches/local-docker-compose-services
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
forum:
image: {{ FORUM_DOCKER_IMAGE }}
environment:
SEARCH_SERVER: "{{ ELASTICSEARCH_SCHEME }}://{{ ELASTICSEARCH_HOST }}:{{ ELASTICSEARCH_PORT }}"
MONGODB_AUTH: "{% if MONGODB_USERNAME and MONGODB_PASSWORD %}{{ MONGODB_USERNAME}}:{{ MONGODB_PASSWORD }}@{% endif %}"
MONGODB_HOST: "{{ MONGODB_HOST|forum_mongodb_host }}"
MONGODB_PORT: "{{ MONGODB_PORT }}"
MONGODB_DATABASE: "{{ FORUM_MONGODB_DATABASE }}"
MONGOID_AUTH_SOURCE: "{{ MONGODB_AUTH_SOURCE }}"
MONGOID_AUTH_MECH: "{{ MONGODB_AUTH_MECHANISM|auth_mech_as_ruby }}"
MONGOID_USE_SSL: "{{ 'true' if MONGODB_USE_SSL else 'false' }}"
{{ patch("forum-local-env")|indent(4) }}
restart: unless-stopped
depends_on: {{ [("elasticsearch", RUN_ELASTICSEARCH), ("mongodb", RUN_MONGODB)]|list_if }}
45 changes: 44 additions & 1 deletion tutorforum/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from glob import glob
import os
import urllib.parse
Expand All @@ -7,7 +9,7 @@
from tutor import hooks as tutor_hooks

from .__about__ import __version__

from .hooks import FORUM_ENV

config = {
"defaults": {
Expand All @@ -21,6 +23,18 @@
},
}

FORUM_ENV_BASE: dict[str,str] = {
"SEARCH_SERVER": "{{ ELASTICSEARCH_SCHEME }}://{{ ELASTICSEARCH_HOST }}:{{ ELASTICSEARCH_PORT }}",
xitij2000 marked this conversation as resolved.
Show resolved Hide resolved
"MONGODB_AUTH":
"{% if MONGODB_USERNAME and MONGODB_PASSWORD %}{{ MONGODB_USERNAME}}:{{ MONGODB_PASSWORD }}@{% endif %}",
"MONGODB_HOST": "{{ MONGODB_HOST|forum_mongodb_host }}",
"MONGODB_PORT": "{{ MONGODB_PORT }}",
"MONGODB_DATABASE": "{{ FORUM_MONGODB_DATABASE }}",
"MONGOID_AUTH_SOURCE": "{{ MONGODB_AUTH_SOURCE }}",
"MONGOID_AUTH_MECH": "{{ MONGODB_AUTH_MECHANISM|auth_mech_as_ruby }}",
"MONGOID_USE_SSL": "{{ 'true' if MONGODB_USE_SSL else 'false' }}",
}
regisb marked this conversation as resolved.
Show resolved Hide resolved

with open(
pkg_resources.resource_filename(
"tutorforum", os.path.join("templates", "forum", "tasks", "forum", "init")
Expand Down Expand Up @@ -112,6 +126,35 @@ def forum_mongodb_host(host: str) -> str:
return urllib.parse.urlunparse(parsed)


@FORUM_ENV.add(priority=tutor_hooks.priorities.HIGH)
def _add_base_forum_env(forum_env: dict) -> dict[str, str]:
"""
Add environment variables needed for standard build of forum service.
"""
forum_env.update(FORUM_ENV_BASE)
return forum_env


@tutor_hooks.Filters.ENV_PATCHES.add(priority=tutor_hooks.priorities.HIGH)
def _forum_env_patches(patches):
xitij2000 marked this conversation as resolved.
Show resolved Hide resolved
"""
Adds environment variables from FORUM_ENV filter to patches.
"""
# The forum service is configured entirely via environment variables. Docker
# Compose and Kubernetes use different syntax to specify environement
# variables. The following code reads environemnt variables from the
# `FORUM_ENV` filter and rendered in the appropriate format for both so they
# can be included as patches.
k8s_env_patch = ""
local_env_patch = ""
for key, value in FORUM_ENV.apply({}).items():
# Kubernetes
k8s_env_patch += f'- name: {key}\n value: "{value}"\n'
local_env_patch += f'{key}: "{value}"\n'
patches += [("forum-k8s-env", k8s_env_patch), ("forum-local-env", local_env_patch)]
return patches


tutor_hooks.Filters.ENV_TEMPLATE_FILTERS.add_items(
[
("auth_mech_as_ruby", auth_mech_as_ruby),
Expand Down