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: upgrade to v1 plugin API #44

Merged
merged 1 commit into from
Apr 19, 2022
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def load_about():
include_package_data=True,
python_requires=">=3.5",
install_requires=["tutor>=13.0.0,<14.0.0"],
entry_points={"tutor.plugin.v0": ["mfe = tutormfe.plugin"]},
entry_points={"tutor.plugin.v1": ["mfe = tutormfe.plugin"]},
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
Expand Down
88 changes: 67 additions & 21 deletions tutormfe/plugin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from glob import glob
import os
import pkg_resources

from .__about__ import __version__

HERE = os.path.abspath(os.path.dirname(__file__))
from tutor import hooks

templates = os.path.join(HERE, "templates")
from .__about__ import __version__

config = {
"defaults": {
Expand Down Expand Up @@ -43,22 +42,69 @@
},
}

hooks = {
"build-image": {
"mfe": "{{ MFE_DOCKER_IMAGE }}",
},
"remote-image": {
"mfe": "{{ MFE_DOCKER_IMAGE }}",
},
"init": ["lms"],
}
hooks.Filters.COMMANDS_INIT.add_item(
(
"lms",
("mfe", "tasks", "lms", "init"),
)
)
hooks.Filters.IMAGES_BUILD.add_item(
(
"mfe",
("plugins", "mfe", "build", "mfe"),
"{{ MFE_DOCKER_IMAGE }}",
(),
)
)


@hooks.Filters.IMAGES_PULL.add()
@hooks.Filters.IMAGES_PUSH.add()
def _add_remote_mfe_image_iff_customized(images, user_config):
"""
Register MFE image for pushing & pulling if and only if it has
been set to something other than the default.

This is work-around to an upstream issue with MFE config. Briefly:
User config is baked into MFE builds, so Tutor cannot host a generic
pre-built MFE image. Howevever, individual Tutor users may want/need to
build and host their own MFE image. So, as a compromise, we tell Tutor
to push/pull the MFE image if the user has customized it to anything
other than the default image URL.
"""
image_tag = user_config["MFE_DOCKER_IMAGE"]
if not image_tag.startswith("docker.io/overhangio/openedx-mfe:"):
# Image has been customized. Add to list for pulling/pushing.
images.append(("mfe", image_tag))
return images

####### Boilerplate code
regisb marked this conversation as resolved.
Show resolved Hide resolved
# Add the "templates" folder as a template root
hooks.Filters.ENV_TEMPLATE_ROOTS.add_item(
pkg_resources.resource_filename("tutormfe", "templates")
)
# Render the "build" and "apps" folders
hooks.Filters.ENV_TEMPLATE_TARGETS.add_items(
[
("mfe/build", "plugins"),
("mfe/apps", "plugins"),
],
)
# Load patches from files
for path in glob(
os.path.join(
pkg_resources.resource_filename("tutormfe", "patches"),
"*",
)
):
with open(path, encoding="utf-8") as patch_file:
hooks.Filters.ENV_PATCHES.add_item((os.path.basename(path), patch_file.read()))

def patches():
all_patches = {}
for path in glob(os.path.join(HERE, "patches", "*")):
with open(path) as patch_file:
name = os.path.basename(path)
content = patch_file.read()
all_patches[name] = content
return all_patches
# Add configuration entries
hooks.Filters.CONFIG_DEFAULTS.add_items(
[(f"MFE_{key}", value) for key, value in config.get("defaults", {}).items()]
)
hooks.Filters.CONFIG_UNIQUE.add_items(
[(f"MFE_{key}", value) for key, value in config.get("unique", {}).items()]
)
hooks.Filters.CONFIG_OVERRIDES.add_items(list(config.get("overrides", {}).items()))