From 5a71dbca87dcd1b2a37ff5d2709fa4dba772ea27 Mon Sep 17 00:00:00 2001 From: John Nagro Date: Wed, 6 Sep 2023 18:09:15 +0000 Subject: [PATCH] feat: docker-based local work without lms --- Dockerfile | 98 ++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 5 +++ README.rst | 6 ++- docker-compose.yml | 24 ++++++++++++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..777d676 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,98 @@ +# Docker in this repo is only supported for running tests locally +# as an alternative to virtualenv natively - johnnagro 2023-09-06 +FROM ubuntu:focal as app +LABEL org.opencontainers.image.authors="sre@edx.org" + + +# Packages installed: +# git; Used to pull in particular requirements from github rather than pypi, +# and to check the sha of the code checkout. + +# build-essentials; so we can use make with the docker container + +# language-pack-en locales; ubuntu locale support so that system utilities have a consistent +# language and time zone. + +# python; ubuntu doesnt ship with python, so this is the python we will use to run the application + +# python3-pip; install pip to install application requirements.txt files + +# pkg-config +# mysqlclient>=2.2.0 requires this (https://github.com/PyMySQL/mysqlclient/issues/620) + +# libmysqlclient-dev; to install header files needed to use native C implementation for +# MySQL-python for performance gains. + +# libssl-dev; # mysqlclient wont install without this. + +# python3-dev; to install header files for python extensions; much wheel-building depends on this + +# gcc; for compiling python extensions distributed with python packages like mysql-client + +# If you add a package here please include a comment above describing what it is used for +RUN apt-get update && apt-get -qy install --no-install-recommends \ + language-pack-en \ + locales \ + python3.8 \ + python3-pip \ + python3.8-venv \ + pkg-config \ + libmysqlclient-dev \ + libssl-dev \ + python3-dev \ + gcc \ + build-essential \ + git \ + curl + + +RUN pip install --upgrade pip setuptools +# delete apt package lists because we do not need them inflating our image +RUN rm -rf /var/lib/apt/lists/* + +RUN ln -s /usr/bin/python3 /usr/bin/python + +RUN locale-gen en_US.UTF-8 +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 +ENV DJANGO_SETTINGS_MODULE test_settings + +# Env vars: path +ENV VIRTUAL_ENV='/edx/app/venvs/edx-arch-experiments' +ENV PATH="$VIRTUAL_ENV/bin:$PATH" +ENV PATH="/edx/app/edx-arch-experiments/node_modules/.bin:${PATH}" +ENV PATH="/edx/app/edx-arch-experiments/bin:${PATH}" +ENV PATH="/edx/app/nodeenv/bin:${PATH}" + +RUN useradd -m --shell /bin/false app + +WORKDIR /edx/app/edx-arch-experiments + +RUN python3.8 -m venv $VIRTUAL_ENV +ENV PATH="$VIRTUAL_ENV/bin:$PATH" + +# Copy the requirements explicitly even though we copy everything below +# this prevents the image cache from busting unless the dependencies have changed. +COPY requirements/ /edx/app/edx-arch-experiments/requirements/ + +# Dependencies are installed as root so they cannot be modified by the application user. +RUN pip install -r requirements/dev.txt +RUN pip install nodeenv + +# Set up a Node environment and install Node requirements. +# Must be done after Python requirements, since nodeenv is installed +# via pip. +# The node environment is already 'activated' because its .../bin was put on $PATH. +RUN nodeenv /edx/app/nodeenv --node=18.15.0 --prebuilt + +RUN mkdir -p /edx/var/log + +# Code is owned by root so it cannot be modified by the application user. +# So we copy it before changing users. +USER app + +# This line is after the requirements so that changes to the code will not +# bust the image cache +COPY . /edx/app/edx-arch-experiments + diff --git a/Makefile b/Makefile index 8bf0844..f831242 100644 --- a/Makefile +++ b/Makefile @@ -105,3 +105,8 @@ dummy_translations: ## generate dummy translation (.po) files build_dummy_translations: extract_translations dummy_translations compile_translations ## generate and compile dummy translation files validate_translations: build_dummy_translations detect_changed_source_translations ## validate translations + +## Docker in this repo is only supported for running tests locally +## as an alternative to virtualenv natively - johnnagro 2023-09-06 +test-shell: ## Run a shell, as root, on the specified service container + docker-compose run -u 0 test-shell env TERM=$(TERM) /bin/bash diff --git a/README.rst b/README.rst index 33cf226..bb56fa0 100644 --- a/README.rst +++ b/README.rst @@ -28,7 +28,11 @@ One Time Setup Local testing ~~~~~~~~~~~~~ -To test your changes locally, you will need to install the package from your local branch into edx-platform. For example, if using devstack, copy or clone your branch into /src/edx-arch-experiments. Then, in an lms or cms shell, run ``pip install -e /edx/src/edx-arch-experiments``. The plug-in configuration will automatically be picked up once installed, and changes will be hot reloaded. +Two options are available for testing changes locally: + +First, via `make test-shell` a dockerized development envrionment can be launched to run `pytest` and do basic migration work. + +Second, a local copy of the package can be installed into edx-platform. For example, if using devstack, copy or clone your branch into /src/edx-arch-experiments. Then, in an lms or cms shell, run ``pip install -e /edx/src/edx-arch-experiments``. The plug-in configuration will automatically be picked up once installed, and changes will be hot reloaded. Every time you develop something in this repo diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4e0e434 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +# Docker in this repo is only supported for running tests locally +# as an alternative to virtualenv natively - johnnagro 2023-09-06 +version: "2.1" +services: + test-shell: + build: + context: . + dockerfile: Dockerfile + container_name: arch-experiments.test.app + hostname: app.test.arch-experiments + volumes: + - .:/edx/app/edx-arch-experiments + + networks: + - devstack_default + # Allows attachment to this container using 'docker attach '. + stdin_open: true + tty: true + environment: + DJANGO_SETTINGS_MODULE: test_settings + +networks: + devstack_default: + external: true