def generate_ddc_project_compose(project: Project) -> Path:
    """This function is called every time ddc-project is run.
    It assembles a docker-compose file from the given configuration.
    It should execute as fast as possible.
    """
    project_compose_path = project.private_filepath("docker-compose.yml")
    template_path = DDC_PROJECT_TEMPLATE_PATH
    final_image = None
    if image_exists(project.image_name):
        final_image = project.image_name
    if not image_exists(project.requirements_image_name):
        logger.warning(f"Image {project.requirements_image_name} not found\n"
                       "Run\nderex build requirements\n to build it")

    openedx_customizations = project.get_openedx_customizations()

    tmpl = Template(template_path.read_text())
    text = tmpl.render(
        project=project,
        final_image=final_image,
        wsgi_py_path=WSGI_PY_PATH,
        derex_django_path=DEREX_DJANGO_PATH,
        openedx_customizations=openedx_customizations,
    )
    project_compose_path.write_text(text)
    return project_compose_path
Exemple #2
0
def build_requirements_image(project: Project):
    """Build the docker image the includes project requirements for the given project.
    The requirements are installed in a container based on the dev image, and assets
    are compiled there.
    """
    if project.requirements_dir is None:
        return
    paths_to_copy = [str(project.requirements_dir)]
    dockerfile_contents = [f"FROM {project.base_image}"]
    dockerfile_contents.extend(
        docker_commands_to_install_requirements(project))

    openedx_customizations = project.get_openedx_customizations()
    if openedx_customizations:
        openedx_customizations_paths = [DEREX_OPENEDX_CUSTOMIZATIONS_PATH]
        if project.openedx_customizations_dir:
            openedx_customizations_paths.append(
                project.openedx_customizations_dir)

        for openedx_customization_path in openedx_customizations_paths:
            paths_to_copy.append(openedx_customization_path)

        for destination, source in openedx_customizations.items():
            docker_build_context_source = source
            for openedx_customization_path in openedx_customizations_paths:
                docker_build_context_source = docker_build_context_source.replace(
                    str(openedx_customization_path), "openedx_customizations")
            dockerfile_contents.append(
                f"COPY {docker_build_context_source} {destination}")

    compile_command = ("; \\\n").join((
        # Remove files from the previous image
        "rm -rf /openedx/staticfiles",
        "derex_update_assets",
        "derex_cleanup_assets",
    ))
    if project.config.get("update_assets", False):
        dockerfile_contents.append(f"RUN sh -c '{compile_command}'")
    dockerfile_text = "\n".join(dockerfile_contents)
    build_image(dockerfile_text,
                paths_to_copy,
                tag=project.requirements_image_name)