mirror of
https://github.com/ChristianLight/tutor.git
synced 2025-01-07 07:54:03 +00:00
refactor: job running methods for clarity and extensibility
It made little sense to create dedicated context classes for local/dev. Instead, we create local/dev compose methods which can be easily reused. Also, we renamed the "scripts" module to better reflect its function.
This commit is contained in:
parent
6ca863e04c
commit
1d4ab79863
@ -1,11 +1,10 @@
|
||||
import click
|
||||
|
||||
from .compose import ScriptRunner
|
||||
from .local import LocalContext
|
||||
from .compose import ComposeJobRunner
|
||||
from .local import docker_compose as local_docker_compose
|
||||
from .. import config as tutor_config
|
||||
from .. import env as tutor_env
|
||||
from .. import fmt
|
||||
from .. import utils
|
||||
|
||||
|
||||
@click.group(help="Build an Android app for your Open edX platform [BETA FEATURE]")
|
||||
@ -44,7 +43,7 @@ cp OpenEdXMobile/build/outputs/apk/prod/{apk_folder}/*.apk /openedx/data/"""
|
||||
|
||||
def docker_run(root, command):
|
||||
config = tutor_config.load(root)
|
||||
runner = ScriptRunner(root, config, LocalContext.docker_compose)
|
||||
runner = ComposeJobRunner(root, config, local_docker_compose)
|
||||
runner.run_job("android", command)
|
||||
|
||||
|
||||
|
@ -7,12 +7,12 @@ from .. import config as tutor_config
|
||||
from .. import env as tutor_env
|
||||
from ..exceptions import TutorError
|
||||
from .. import fmt
|
||||
from .. import scripts
|
||||
from .. import jobs
|
||||
from .. import serialize
|
||||
from .. import utils
|
||||
|
||||
|
||||
class ScriptRunner(scripts.BaseRunner):
|
||||
class ComposeJobRunner(jobs.BaseJobRunner):
|
||||
def __init__(self, root, config, docker_compose_func):
|
||||
super().__init__(root, config)
|
||||
self.docker_compose_func = docker_compose_func
|
||||
@ -138,8 +138,8 @@ def restart(context, services):
|
||||
@click.pass_obj
|
||||
def init(context, limit):
|
||||
config = tutor_config.load(context.root)
|
||||
runner = ScriptRunner(context.root, config, context.docker_compose)
|
||||
scripts.initialise(runner, limit_to=limit)
|
||||
runner = ComposeJobRunner(context.root, config, context.docker_compose)
|
||||
jobs.initialise(runner, limit_to=limit)
|
||||
|
||||
|
||||
@click.command(help="Create an Open edX user and interactively set their password")
|
||||
@ -155,10 +155,8 @@ def init(context, limit):
|
||||
@click.pass_obj
|
||||
def createuser(context, superuser, staff, password, name, email):
|
||||
config = tutor_config.load(context.root)
|
||||
runner = ScriptRunner(context.root, config, context.docker_compose)
|
||||
command = scripts.create_user_command(
|
||||
superuser, staff, name, email, password=password
|
||||
)
|
||||
runner = ComposeJobRunner(context.root, config, context.docker_compose)
|
||||
command = jobs.create_user_command(superuser, staff, name, email, password=password)
|
||||
runner.run_job("lms", command)
|
||||
|
||||
|
||||
@ -170,18 +168,18 @@ def createuser(context, superuser, staff, password, name, email):
|
||||
@click.pass_obj
|
||||
def settheme(context, theme_name, domain_names):
|
||||
config = tutor_config.load(context.root)
|
||||
runner = ScriptRunner(context.root, config, context.docker_compose)
|
||||
runner = ComposeJobRunner(context.root, config, context.docker_compose)
|
||||
for domain_name in domain_names:
|
||||
scripts.set_theme(theme_name, domain_name, runner)
|
||||
jobs.set_theme(theme_name, domain_name, runner)
|
||||
|
||||
|
||||
@click.command(help="Import the demo course")
|
||||
@click.pass_obj
|
||||
def importdemocourse(context):
|
||||
config = tutor_config.load(context.root)
|
||||
runner = ScriptRunner(context.root, config, context.docker_compose)
|
||||
runner = ComposeJobRunner(context.root, config, context.docker_compose)
|
||||
fmt.echo_info("Importing demo course")
|
||||
scripts.import_demo_course(runner)
|
||||
jobs.import_demo_course(runner)
|
||||
|
||||
|
||||
@click.command(
|
||||
|
@ -2,3 +2,7 @@
|
||||
class Context:
|
||||
def __init__(self, root):
|
||||
self.root = root
|
||||
|
||||
@staticmethod
|
||||
def docker_compose(root, config, *command):
|
||||
raise NotImplementedError
|
||||
|
@ -3,42 +3,39 @@ import os
|
||||
import click
|
||||
|
||||
from . import compose
|
||||
from .context import Context
|
||||
from .. import config as tutor_config
|
||||
from .. import env as tutor_env
|
||||
from .. import fmt
|
||||
from .. import utils
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class DevContext(Context):
|
||||
@staticmethod
|
||||
def docker_compose(root, config, *command):
|
||||
args = []
|
||||
for folder in ["local", "dev"]:
|
||||
# Add docker-compose.yml and docker-compose.override.yml (if it exists)
|
||||
# from "local" and "dev" folders (but not docker-compose.prod.yml)
|
||||
args += [
|
||||
"-f",
|
||||
tutor_env.pathjoin(root, folder, "docker-compose.yml"),
|
||||
]
|
||||
override_path = tutor_env.pathjoin(
|
||||
root, folder, "docker-compose.override.yml"
|
||||
)
|
||||
if os.path.exists(override_path):
|
||||
args += ["-f", override_path]
|
||||
return utils.docker_compose(
|
||||
*args,
|
||||
"--project-name",
|
||||
config["DEV_PROJECT_NAME"],
|
||||
*command,
|
||||
)
|
||||
def docker_compose(root, config, *command):
|
||||
"""
|
||||
Run docker-compose with dev arguments.
|
||||
"""
|
||||
args = []
|
||||
for folder in ["local", "dev"]:
|
||||
# Add docker-compose.yml and docker-compose.override.yml (if it exists)
|
||||
# from "local" and "dev" folders (but not docker-compose.prod.yml)
|
||||
args += [
|
||||
"-f",
|
||||
tutor_env.pathjoin(root, folder, "docker-compose.yml"),
|
||||
]
|
||||
override_path = tutor_env.pathjoin(root, folder, "docker-compose.override.yml")
|
||||
if os.path.exists(override_path):
|
||||
args += ["-f", override_path]
|
||||
return utils.docker_compose(
|
||||
*args,
|
||||
"--project-name",
|
||||
config["DEV_PROJECT_NAME"],
|
||||
*command,
|
||||
)
|
||||
|
||||
|
||||
@click.group(help="Run Open edX locally with development settings")
|
||||
@click.pass_context
|
||||
@click.pass_obj
|
||||
def dev(context):
|
||||
context.obj = DevContext(context.obj.root)
|
||||
context.docker_compose = docker_compose
|
||||
|
||||
|
||||
@click.command(
|
||||
|
@ -8,7 +8,7 @@ from .. import env as tutor_env
|
||||
from .. import exceptions
|
||||
from .. import fmt
|
||||
from .. import interactive as interactive_config
|
||||
from .. import scripts
|
||||
from .. import jobs
|
||||
from .. import serialize
|
||||
from .. import utils
|
||||
|
||||
@ -135,11 +135,11 @@ def delete(context, yes):
|
||||
@click.pass_obj
|
||||
def init(context, limit):
|
||||
config = tutor_config.load(context.root)
|
||||
runner = K8sScriptRunner(context.root, config)
|
||||
runner = K8sJobRunner(context.root, config)
|
||||
for service in ["mysql", "elasticsearch", "mongodb"]:
|
||||
if tutor_config.is_service_activated(config, service):
|
||||
wait_for_pod_ready(config, service)
|
||||
scripts.initialise(runner, limit_to=limit)
|
||||
jobs.initialise(runner, limit_to=limit)
|
||||
|
||||
|
||||
@click.command(help="Create an Open edX user and interactively set their password")
|
||||
@ -155,9 +155,7 @@ def init(context, limit):
|
||||
@click.pass_obj
|
||||
def createuser(context, superuser, staff, password, name, email):
|
||||
config = tutor_config.load(context.root)
|
||||
command = scripts.create_user_command(
|
||||
superuser, staff, name, email, password=password
|
||||
)
|
||||
command = jobs.create_user_command(superuser, staff, name, email, password=password)
|
||||
# This needs to be interactive in case the user needs to type a password
|
||||
kubectl_exec(config, "lms", command, attach=True)
|
||||
|
||||
@ -167,8 +165,8 @@ def createuser(context, superuser, staff, password, name, email):
|
||||
def importdemocourse(context):
|
||||
fmt.echo_info("Importing demo course")
|
||||
config = tutor_config.load(context.root)
|
||||
runner = K8sScriptRunner(context.root, config)
|
||||
scripts.import_demo_course(runner)
|
||||
runner = K8sJobRunner(context.root, config)
|
||||
jobs.import_demo_course(runner)
|
||||
|
||||
|
||||
@click.command(
|
||||
@ -179,9 +177,9 @@ def importdemocourse(context):
|
||||
@click.pass_obj
|
||||
def settheme(context, theme_name, domain_names):
|
||||
config = tutor_config.load(context.root)
|
||||
runner = K8sScriptRunner(context.root, config)
|
||||
runner = K8sJobRunner(context.root, config)
|
||||
for domain_name in domain_names:
|
||||
scripts.set_theme(theme_name, domain_name, runner)
|
||||
jobs.set_theme(theme_name, domain_name, runner)
|
||||
|
||||
|
||||
@click.command(name="exec", help="Execute a command in a pod of the given application")
|
||||
@ -320,7 +318,7 @@ class K8sClients:
|
||||
return self._core_api
|
||||
|
||||
|
||||
class K8sScriptRunner(scripts.BaseRunner):
|
||||
class K8sJobRunner(jobs.BaseJobRunner):
|
||||
def load_job(self, name):
|
||||
jobs = self.render("k8s", "jobs.yml")
|
||||
for job in serialize.load_all(jobs):
|
||||
|
@ -7,33 +7,32 @@ from .. import env as tutor_env
|
||||
from .. import fmt, utils
|
||||
from . import compose
|
||||
from .config import save as config_save_command
|
||||
from .context import Context
|
||||
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class LocalContext(Context):
|
||||
@staticmethod
|
||||
def docker_compose(root, config, *command):
|
||||
args = []
|
||||
override_path = tutor_env.pathjoin(root, "local", "docker-compose.override.yml")
|
||||
if os.path.exists(override_path):
|
||||
args += ["-f", override_path]
|
||||
return utils.docker_compose(
|
||||
"-f",
|
||||
tutor_env.pathjoin(root, "local", "docker-compose.yml"),
|
||||
"-f",
|
||||
tutor_env.pathjoin(root, "local", "docker-compose.prod.yml"),
|
||||
*args,
|
||||
"--project-name",
|
||||
config["LOCAL_PROJECT_NAME"],
|
||||
*command
|
||||
)
|
||||
def docker_compose(root, config, *command):
|
||||
"""
|
||||
Run docker-compose with local and production yml files.
|
||||
"""
|
||||
args = []
|
||||
override_path = tutor_env.pathjoin(root, "local", "docker-compose.override.yml")
|
||||
if os.path.exists(override_path):
|
||||
args += ["-f", override_path]
|
||||
return utils.docker_compose(
|
||||
"-f",
|
||||
tutor_env.pathjoin(root, "local", "docker-compose.yml"),
|
||||
"-f",
|
||||
tutor_env.pathjoin(root, "local", "docker-compose.prod.yml"),
|
||||
*args,
|
||||
"--project-name",
|
||||
config["LOCAL_PROJECT_NAME"],
|
||||
*command
|
||||
)
|
||||
|
||||
|
||||
@click.group(help="Run Open edX locally with docker-compose")
|
||||
@click.pass_context
|
||||
@click.pass_obj
|
||||
def local(context):
|
||||
context.obj = LocalContext(context.obj.root)
|
||||
context.docker_compose = docker_compose
|
||||
|
||||
|
||||
@click.command(help="Configure and run Open edX from scratch")
|
||||
|
@ -8,7 +8,7 @@ echo "Loading settings $DJANGO_SETTINGS_MODULE"
|
||||
"""
|
||||
|
||||
|
||||
class BaseRunner:
|
||||
class BaseJobRunner:
|
||||
def __init__(self, root, config):
|
||||
self.root = root
|
||||
self.config = config
|
Loading…
Reference in New Issue
Block a user