Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- [Feature] `tutor` now treats a missing plugin as an error and will now return
a non-zero exit code if a plugin fails to load. This ensures that a missing
or broken plugin stops deployment instead of potentially leading to a broken
deployment (by @xitij2000)
62 changes: 61 additions & 1 deletion tests/commands/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import unittest

import click

from tests.helpers import PluginsTestCase
from tutor import hooks
from tutor.__about__ import __version__
from tutor.commands.cli import cli
from tutor.commands.context import ignore_plugin_errors

from .base import TestCommandMixin


class CliTests(unittest.TestCase, TestCommandMixin):
def test_help(self) -> None:
result = self.invoke(["help"])
self.assertEqual(0, result.exit_code)
self.assertIsNone(result.exception)
self.assertEqual(0, result.exit_code)

def test_cli_help(self) -> None:
result = self.invoke(["--help"])
Expand All @@ -21,3 +27,57 @@ def test_cli_version(self) -> None:
self.assertEqual(0, result.exit_code)
self.assertIsNone(result.exception)
self.assertRegex(result.stdout, rf"cli, version {__version__}\n")

def test_ignore_plugin_errors_flag_in_help(self) -> None:
"""The --ignore-plugin-errors flag should appear in help output."""
result = self.invoke(["--help"])
self.assertEqual(0, result.exit_code)
self.assertIn("ignore-plugin-errors", result.output)


class PluginErrorExitCodeTests(PluginsTestCase, TestCommandMixin):
"""Tests for plugin error exit code behavior."""

def setUp(self) -> None:
super().setUp()

@ignore_plugin_errors
@click.command(name="exempt")
def exempt_command() -> None:
click.echo("exempt_command ran")

@click.command(name="non-exempt")
def non_exempt_command() -> None:
click.echo("non_exempt_command ran")

cli.add_command(exempt_command)
self.addCleanup(cli.commands.pop, "exempt")
cli.add_command(non_exempt_command)
self.addCleanup(cli.commands.pop, "non-exempt")

def _add_plugin_error(self) -> None:
"""Add a test plugin error."""
with hooks.Contexts.PLUGINS.enter():
hooks.Filters.PLUGIN_ERRORS.add_item(("badplugin", "test error"))

def test_check_plugin_errors_exits_with_code_1(self) -> None:
"""Pugin errors prevent command from running if errors exist."""
self._add_plugin_error()
result = self.invoke(["non-exempt"])
self.assertEqual(result.exit_code, 1)
self.assertNotIn("non_exempt_command ran", result.output)
self.assertIn("badplugin", result.output)

def test_check_plugin_errors_ignore(self) -> None:
"""Command should run when ignore-plugin-errors is set."""
self._add_plugin_error()
result = self.invoke(["--ignore-plugin-errors", "non-exempt"])
self.assertEqual(result.exit_code, 0)
self.assertIn("non_exempt_command ran", result.output)

def test_check_plugin_exempt_command(self) -> None:
"""Command should run if they are exempt."""
self._add_plugin_error()
result = self.invoke(["exempt"])
self.assertEqual(result.exit_code, 0)
self.assertIn("exempt_command ran", result.output)
54 changes: 51 additions & 3 deletions tutor/commands/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from tutor import exceptions, fmt, hooks, utils
from tutor.__about__ import __app__, __version__
from tutor.commands.config import config_command
from tutor.commands.context import Context
from tutor.commands.context import Context, ignore_plugin_errors
from tutor.commands.dev import dev
from tutor.commands.images import images_command
from tutor.commands.k8s import k8s
Expand All @@ -32,6 +32,41 @@ def main() -> None:
sys.exit(1)


def check_plugin_errors(context: click.Context) -> None:
"""
Check for plugin loading errors and exit with code 1 if any were found,
unless --ignore-plugin-errors is set.
"""
errors: list[tuple[str, str]] = list(hooks.Filters.PLUGIN_ERRORS.iterate())
if not errors or context.invoked_subcommand is None:
return
# This is done to narrow the type. This function is only called from a one
# place which is always a group.
assert isinstance(context.command, click.Group)
subcommand = context.command.get_command(context, context.invoked_subcommand)
ignore = context.obj.ignore_plugin_errors or getattr(
subcommand, "ignore_plugin_errors", False
)

for plugin, error in errors:
if ignore:
fmt.echo_alert(f"Failed to enable plugin '{plugin}': {error}")
else:
fmt.echo_error(f"Plugin '{plugin}' failed to load: {error}")
# Only exit with an error code if:
# - errors are not being ignored
# - no exception has already been raised that would return its own error code
if ignore:
return
match sys.exc_info()[1]:
case None:
sys.exit(1)
case SystemExit(code=code) | click.exceptions.Exit(exit_code=code) if code == 0:
sys.exit(1)
case _:
return


class TutorCli(click.Group):
"""
Dynamically load subcommands at runtime.
Expand Down Expand Up @@ -99,20 +134,33 @@ def ensure_plugins_enabled(self, ctx: click.Context) -> None:
is_flag=True,
help="Print this help",
)
@click.option(
"--ignore-plugin-errors",
"ignore_plugin_errors",
is_flag=True,
default=False,
help="Continue running even if plugin loading fails",
)
@click.pass_context
def cli(context: click.Context, root: str, show_help: bool) -> None:
def cli(
context: click.Context, root: str, show_help: bool, ignore_plugin_errors: bool
) -> None:
if utils.is_root():
fmt.echo_alert(
"You are running Tutor as root. This is strongly not recommended. If you are doing this in order to access"
" the Docker daemon, you should instead add your user to the 'docker' group. (see https://docs.docker.com"
"/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user)"
)
context.obj = Context(root)
context.obj = Context(root, ignore_plugin_errors)
context.help_option_names = ["-h", "--help"]

check_plugin_errors(context)

if context.invoked_subcommand is None or show_help:
click.echo(context.get_help())


@ignore_plugin_errors
@click.command(help="Print this help", name="help")
@click.pass_context
def help_command(context: click.Context) -> None:
Expand Down
3 changes: 2 additions & 1 deletion tutor/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
from tutor import config as tutor_config
from tutor import env, exceptions, fmt, hooks, serialize, utils
from tutor import interactive as interactive_config
from tutor.commands.context import Context
from tutor.commands.context import Context, ignore_plugin_errors
from tutor.commands.params import ConfigLoaderParam, ParamValue
from tutor.types import Config, ConfigValue


@ignore_plugin_errors
@click.group(
name="config",
short_help="Configure Open edX",
Expand Down
22 changes: 21 additions & 1 deletion tutor/commands/context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from typing import TypeVar

import click

from tutor.tasks import BaseTaskRunner
from tutor.types import Config

Expand All @@ -12,8 +16,9 @@ class Context:
$ tutor --root=... local run ...
"""

def __init__(self, root: str) -> None:
def __init__(self, root: str, ignore_plugin_errors: bool = False) -> None:
self.root = root
self.ignore_plugin_errors = ignore_plugin_errors


class BaseTaskContext(Context):
Expand All @@ -28,3 +33,18 @@ def job_runner(self, config: Config) -> BaseTaskRunner:
Return a runner capable of running docker-compose/kubectl commands.
"""
raise NotImplementedError


CMD = TypeVar("CMD", bound=click.Command)


def ignore_plugin_errors(command: CMD) -> CMD:
"""
Ignore plugins errors for commands marked with this.

Commands decorated with this will run even if there were errors when loading
plugins. This needs to be the first decorator and only works for top-level
command, exempting all subcommands.
"""
setattr(command, "ignore_plugin_errors", True)
return command
3 changes: 2 additions & 1 deletion tutor/commands/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from tutor.plugins.base import PLUGINS_ROOT, PLUGINS_ROOT_ENV_VAR_NAME
from tutor.types import Config

from .context import Context
from .context import Context, ignore_plugin_errors


class PluginName(click.ParamType[str]):
Expand Down Expand Up @@ -74,6 +74,7 @@ def shell_complete(
return autocompleted


@ignore_plugin_errors
@click.group(
name="plugins",
short_help="Manage Tutor plugins",
Expand Down
9 changes: 9 additions & 0 deletions tutor/hooks/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,15 @@ def your_filter_callback(some_data):
#: regular expression. For instance: ``("openedx", r".*xblock.*")``.
MOUNTED_DIRECTORIES: Filter[list[tuple[str, str]], []] = Filter()

#: List of plugins that failed to load, along with the corresponding error
#: message. This filter is populated by `tutor.plugins.load_all` when a
#: plugin raises an exception during loading. It is consumed by the CLI to
#: report errors after the subcommand has finished.
#:
#: :parameter list[tuple[str, str]] errors: list of ``(plugin_name, error_message)``
#: tuples.
PLUGIN_ERRORS: Filter[list[tuple[str, str]], []] = Filter()

#: List of plugin indexes that are loaded when we run ``tutor plugins update``. By
#: default, the plugin indexes are stored in the user configuration. This filter makes
#: it possible to extend and modify this list with plugins.
Expand Down
3 changes: 2 additions & 1 deletion tutor/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def load_all(names: t.Iterable[str]) -> None:
try:
load(name)
except Exception as e:
fmt.echo_alert(f"Failed to enable plugin '{name}': {e}")
with hooks.Contexts.PLUGINS.enter():
hooks.Filters.PLUGIN_ERRORS.add_item((name, str(e)))
hooks.Actions.PLUGINS_LOADED.do()


Expand Down
Loading