From 62d7fd00b95fef0198f23c53b3263c5c2e021197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Tue, 21 Jan 2020 16:30:16 +0100 Subject: [PATCH] Serve static assets with whitenoise instead of nginx This drastically simplifies volume management, as it is no longer necessary to manually copy static assets from the docker image to the bind-mounted volume. This deprecates the "k8s-deployments-nginx-init-containers" patch, as we no longer need to init the nginx container. Plugins are encouraged to start using whitenoise as well for serving static assets. TODO: - test media serving: DOES NOT WORK. Whitenoise was designed to serve a fixed list of static files. Godammit. - compare performances --- CHANGELOG.md | 1 + tutor/templates/apps/nginx/cms.conf | 24 --------------- tutor/templates/apps/nginx/lms.conf | 24 --------------- .../apps/openedx/settings/cms/production.py | 2 ++ .../apps/openedx/settings/lms/production.py | 2 ++ tutor/templates/build/openedx/Dockerfile | 3 ++ tutor/templates/k8s/deployments.yml | 29 ------------------- tutor/templates/local/docker-compose.prod.yml | 7 ----- 8 files changed, 8 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0536e23..6bba96a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Note: Breaking changes between versions are indicated by "💥". ## Unreleased +- 💥[Improvement] Serve openedx static assets with [whitenoise](http://whitenoise.evans.io/en/stable/) instead of nginx. This removes the `k8s-deployments-nginx-init-containers` patch. Plugins are encouraged to implement static asset serving with Whitenoise as well. - [Bugfix] Fix dependency on mysql service when mysql is not activated - [Improvement] Improve openedx Docker image build time and size with multi-stage build - [Feature] Get rid of outdated sysadmin dashboard in LMS at /sysadmin diff --git a/tutor/templates/apps/nginx/cms.conf b/tutor/templates/apps/nginx/cms.conf index 4b36d66..864f549 100644 --- a/tutor/templates/apps/nginx/cms.conf +++ b/tutor/templates/apps/nginx/cms.conf @@ -47,29 +47,5 @@ server { try_files /$file =404; expires 31536000s; } - - location ~ ^/static/(?P.*) { - root /var/www/openedx; - try_files /staticfiles/$file /course_static/$file =404; - - # return a 403 for static files that shouldn't be - # in the staticfiles directory - location ~ ^/static/(?:.*)(?:\.xml|\.json|README.TXT) { - return 403; - } - - # Set django-pipelined files to maximum cache time - location ~ "/static/(?P.*\.[0-9a-f]{12}\..*)" { - expires max; - try_files /staticfiles/$collected /course_static/$collected =404; - } - location ~ "/static/(?P[0-9a-f]{7}/.*)" { - expires max; - try_files /staticfiles/$collected /course_static/$collected =404; - } - - # Expire other static files immediately (there should be very few / none of these) - expires epoch; - } } {% endif %} diff --git a/tutor/templates/apps/nginx/lms.conf b/tutor/templates/apps/nginx/lms.conf index 5a31878..a415a67 100644 --- a/tutor/templates/apps/nginx/lms.conf +++ b/tutor/templates/apps/nginx/lms.conf @@ -64,29 +64,5 @@ server { try_files /$file =404; expires 31536000s; } - - location ~ ^/static/(?P.*) { - root /var/www/openedx; - try_files /staticfiles/$file /course_static/$file =404; - - # return a 403 for static files that shouldn't be - # in the staticfiles directory - location ~ ^/static/(?:.*)(?:\.xml|\.json|README.TXT) { - return 403; - } - - # Set django-pipelined files to maximum cache time - location ~ "/static/(?P.*\.[0-9a-f]{12}\..*)" { - expires max; - try_files /staticfiles/$collected /course_static/$collected =404; - } - location ~ "/static/(?P[0-9a-f]{7}/.*)" { - expires max; - try_files /staticfiles/$collected /course_static/$collected =404; - } - - # Expire other static files immediately (there should be very few / none of these) - expires epoch; - } } {% endif %} diff --git a/tutor/templates/apps/openedx/settings/cms/production.py b/tutor/templates/apps/openedx/settings/cms/production.py index 6b04a88..1549e32 100644 --- a/tutor/templates/apps/openedx/settings/cms/production.py +++ b/tutor/templates/apps/openedx/settings/cms/production.py @@ -9,4 +9,6 @@ ALLOWED_HOSTS = [ "cms", ] +MIDDLEWARE.insert(0, "whitenoise.middleware.WhiteNoiseMiddleware") + {{ patch("openedx-cms-production-settings") }} diff --git a/tutor/templates/apps/openedx/settings/lms/production.py b/tutor/templates/apps/openedx/settings/lms/production.py index 79d568f..2976862 100644 --- a/tutor/templates/apps/openedx/settings/lms/production.py +++ b/tutor/templates/apps/openedx/settings/lms/production.py @@ -10,6 +10,8 @@ ALLOWED_HOSTS = [ "lms", ] +MIDDLEWARE.insert(0, "whitenoise.middleware.WhiteNoiseMiddleware") + # Required to display all courses on start page SEARCH_SKIP_ENROLLMENT_START_DATE_FILTERING = True diff --git a/tutor/templates/build/openedx/Dockerfile b/tutor/templates/build/openedx/Dockerfile index b053727..8c35290 100644 --- a/tutor/templates/build/openedx/Dockerfile +++ b/tutor/templates/build/openedx/Dockerfile @@ -84,6 +84,9 @@ RUN pip install https://github.com/overhangio/edx-ora2/archive/overhangio/boto2t # Install scorm xblock RUN pip install "openedx-scorm-xblock<11.0.0,>=10.0.0" +# Install whitenoise, for serving static assets +RUN pip install "whitenoise==5.1.0" + # Install private requirements: this is useful for installing custom xblocks. COPY ./requirements/ /openedx/requirements RUN cd /openedx/requirements/ \ diff --git a/tutor/templates/k8s/deployments.yml b/tutor/templates/k8s/deployments.yml index 3cb1175..15e8072 100644 --- a/tutor/templates/k8s/deployments.yml +++ b/tutor/templates/k8s/deployments.yml @@ -370,35 +370,12 @@ spec: labels: app.kubernetes.io/name: nginx spec: - initContainers: - - name: clean-openedx-staticfiles - image: {{ DOCKER_IMAGE_OPENEDX }} - args: ['rm', '-rf', '/var/www/openedx/staticfiles'] - volumeMounts: - - mountPath: /var/www/openedx/ - name: openedx-staticfiles - - name: init-openedx-staticfiles - image: {{ DOCKER_IMAGE_OPENEDX }} - args: ['cp', '-r', '/openedx/staticfiles', '/var/www/openedx/'] - volumeMounts: - - mountPath: /var/www/openedx/ - name: openedx-staticfiles - {{ patch("k8s-deployments-nginx-init-containers")|indent(8) }} containers: - name: nginx image: {{ DOCKER_IMAGE_NGINX }} volumeMounts: - mountPath: /etc/nginx/conf.d/ name: config - - mountPath: /var/www/openedx/ - name: openedx-staticfiles - readOnly: true - - mountPath: /openedx/data/cms - name: data-cms - readOnly: true - - mountPath: /openedx/data/lms - name: data-lms - readOnly: true {{ patch("k8s-deployments-nginx-volume-mounts")|indent(12) }} ports: - containerPort: 80 @@ -407,12 +384,6 @@ spec: - name: config configMap: name: nginx-config - - name: openedx-staticfiles - emptyDir: {} - - name: data-cms - emptyDir: {} - - name: data-lms - emptyDir: {} {{ patch("k8s-deployments-nginx-volumes")|indent(8) }} {% if ACTIVATE_RABBITMQ %} --- diff --git a/tutor/templates/local/docker-compose.prod.yml b/tutor/templates/local/docker-compose.prod.yml index 8cc6584..e3a453b 100644 --- a/tutor/templates/local/docker-compose.prod.yml +++ b/tutor/templates/local/docker-compose.prod.yml @@ -1,11 +1,5 @@ version: "3.7" services: - openedx-assets: - image: {{ DOCKER_IMAGE_OPENEDX }} - volumes: - - ../../data/openedx:/var/www/openedx - command: sh -c "rm -rf /var/www/openedx/staticfiles && cp -r /openedx/staticfiles/ /var/www/openedx/" - nginx: image: {{ DOCKER_IMAGE_NGINX }} restart: unless-stopped @@ -21,7 +15,6 @@ services: {% endif %} volumes: - ../apps/nginx:/etc/nginx/conf.d/:ro - - ../../data/openedx:/var/www/openedx:ro - ../../data/openedx-media:/var/www/openedx-media:ro {% if ACTIVATE_HTTPS %}- ../../data/letsencrypt:/etc/letsencrypt/:ro{% endif %} {{ patch("local-docker-compose-nginx-volumes")|indent(6) }}