예제 #1
0
def reset_mysql_cmd(context, force):
    """Reset MySQL database for the current project"""

    if context.obj is None:
        click.echo("This command needs to be run inside a derex project")
        return 1
    project = context.obj

    from derex.runner.mysql import reset_mysql_openedx

    if project.runmode is not ProjectRunMode.debug and not force:
        # Safety belt: we don't want people to run this in production
        context.fail(
            "The command mysql reset can only be run in `debug` runmode.\n"
            "Use --force to override"
        )

    if not force:
        if not click.confirm(
            "Are you sure you want to delete all data on MySQL database "
            f'"{project.mysql_db_name}" and restore it to the project '
            f'"{project.name}" default state ?'
        ):
            return 1
    reset_mysql_openedx(DebugBaseImageProject())
    return 0
예제 #2
0
def run_django_script(project: Optional[Project],
                      script_text: str,
                      context: str = "lms") -> Any:
    """Run a script in a django shell, decode its stdout
    with JSON and return it.
    If the script does not output a parsable JSON None is returned.
    """
    script_fp, script_path = mkstemp(".py", "derex-run-script-")
    result_fp, result_path = mkstemp(".json", "derex-run-script-result")
    os.write(script_fp, script_text.encode("utf-8"))
    os.close(script_fp)
    args = [
        "run",
        "--rm",
        "-v",
        f"{result_path}:/result.json",
        "-v",
        f"{script_path}:/script.py",
        context,
        "sh",
        "-c",
        f"echo \"exec(open('/script.py').read())\" | ./manage.py {context} shell > /result.json",
    ]

    try:
        run_ddc_project(args, project=DebugBaseImageProject())
    finally:
        result_json = open(result_path).read()
        try:
            os.close(result_fp)
        except OSError:
            pass
        try:
            os.close(script_fp)
        except OSError:
            pass
        os.unlink(result_path)
        os.unlink(script_path)
    try:
        return json.loads(result_json)
    except json.decoder.JSONDecodeError:
        return None
예제 #3
0
def compile_theme(project):
    """Compile theme sass files"""
    from derex.runner.ddc import run_ddc_project

    if project.themes_dir is None:
        click.echo("No theme directory present in this project")
        return
    themes = ",".join(el.name for el in project.themes_dir.iterdir())
    uid = os.getuid()
    args = [
        "run",
        "--rm",
        "lms",
        "sh",
        "-c",
        f"""set -ex
            export PATH=/openedx/edx-platform/node_modules/.bin:$PATH  # FIXME: this should not be necessary
            paver compile_sass --theme-dirs /openedx/themes --themes {themes}
            chown {uid}:{uid} /openedx/themes/* -R""",
    ]
    run_ddc_project(args, DebugBaseImageProject(), exit_afterwards=True)
예제 #4
0
def reindex_courses(project, course_ids):
    """Reindex all courses on elasticsearch.
    Course ids may be specified as arguemnts in order
    to reindex specific courses.

    e.g. `derex reindex_courses course-v1:edX+DemoX+Demo_Course`"""

    from derex.runner.ddc import run_ddc_project

    django_cmd = ["python", "manage.py", "cms", "reindex_course"]

    if course_ids:
        for course_id in course_ids:
            django_cmd.append(course_id)
    else:
        # Here we use the "--setup" option instead of "--all" to avoid
        # the confirmation prompt
        django_cmd.append("--setup")

    args = ["run", "--rm", "cms", "sh", "-c", " ".join(django_cmd)]
    run_ddc_project(args, DebugBaseImageProject(), exit_afterwards=True)