7
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-05-29 12:20:49 +00:00

Migrate templating to jinja2

In the future, jinja2 templates will allow us to create more complex
configuration templates.
This commit is contained in:
Régis Behmo 2018-08-05 16:40:51 +02:00
parent ea2dd7c4fb
commit af6e62b069
13 changed files with 71 additions and 70 deletions

View File

@ -17,11 +17,13 @@ all: configure update migrate assets daemon
##################### Bootstrapping
configure:
docker build -t regis/openedx-configurator:latest configurator/
docker run --rm -it --volume="$(PWD)/config:/openedx/config" -e USERID=$(USERID) -e SILENT=$(SILENT) regis/openedx-configurator
docker run --rm -it --volume="$(PWD)/config:/openedx/config" \
-e USERID=$(USERID) -e SILENT=$(SILENT) \
regis/openedx-configurator:hawthorn
update:
docker-compose pull
docker pull regis/openedx-configurator:hawthorn
provision:
$(DOCKER_COMPOSE_RUN) lms bash -c "dockerize -wait tcp://mysql:3306 -timeout 20s && bash /openedx/config/provision.sh"
@ -103,20 +105,30 @@ android-push:
docker push regis/openedx-android:latest
android-dockerhub: android-build android-push
#################### Build images
build: build-forum build-xqueue build-openedx
#################### Deploying to docker hub
build:
# We need to build with docker, as long as docker-compose cannot push to dockerhub
build-openedx:
docker build -t regis/openedx:latest -t regis/openedx:hawthorn openedx/
build-configurator:
docker build -t regis/openedx-configurator:latest -t regis/openedx-configurator:hawthorn configurator/
build-forum:
docker build -t regis/openedx-forum:latest -t regis/openedx-forum:hawthorn forum/
build-xqueue:
docker build -t regis/openedx-xqueue:latest -t regis/openedx-xqueue:hawthorn xqueue/
push:
#################### Deploying to docker hub
push: push-openedx push-forum push-xqueue
push-openedx:
docker push regis/openedx:hawthorn
docker push regis/openedx:latest
push-configurator:
docker push regis/openedx-configurator:hawthorn
docker push regis/openedx-configurator:latest
push-forum:
docker push regis/openedx-forum:hawthorn
docker push regis/openedx-forum:latest
push-xqueue:
docker push regis/openedx-xqueue:hawthorn
docker push regis/openedx-xqueue:latest

View File

@ -1,5 +1,5 @@
API_HOST_URL: 'http://${LMS_HOST}'
API_HOST_URL: 'http://{{ LMS_HOST }}'
ENVIRONMENT_DISPLAY_NAME: 'universal'
PLATFORM_NAME: '${PLATFORM_NAME}'
PLATFORM_DESTINATION_NAME: '${LMS_HOST}'
FEEDBACK_EMAIL_ADDRESS: 'support@${LMS_HOST}'
PLATFORM_NAME: '{{ PLATFORM_NAME }}'
PLATFORM_DESTINATION_NAME: '{{ LMS_HOST }}'
FEEDBACK_EMAIL_ADDRESS: 'support@{{ LMS_HOST }}'

View File

@ -1 +1 @@
MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD={{ MYSQL_PASSWORD }}

View File

@ -1,6 +1,6 @@
server {
listen 80;
server_name studio.localhost £{CMS_HOST};
server_name studio.localhost {{ CMS_HOST }};
# Prevent invalid display courseware in IE 10+ with high privacy settings
add_header P3P 'CP="Open edX does not have a P3P policy."';

View File

@ -1,6 +1,6 @@
server {
listen 80;
server_name localhost £{LMS_HOST} preview.£{LMS_HOST};
server_name localhost {{ LMS_HOST }} preview.{{ LMS_HOST }};
# Prevent invalid display courseware in IE 10+ with high privacy settings
add_header P3P 'CP="Open edX does not have a P3P policy."';

View File

@ -1,18 +1,18 @@
{
"SECRET_KEY": "${SECRET_KEY}",
"SECRET_KEY": "{{ SECRET_KEY }}",
"AWS_ACCESS_KEY_ID": "",
"AWS_SECRET_ACCESS_KEY": "",
"XQUEUE_INTERFACE": {
"django_auth": {
"username": "${XQUEUE_AUTH_USERNAME}",
"password": "${XQUEUE_AUTH_PASSWORD}"
"username": "{{ XQUEUE_AUTH_USERNAME }}",
"password": "{{ XQUEUE_AUTH_PASSWORD }}"
},
"url": "http://xqueue:8040"
},
"CONTENTSTORE": {
"ENGINE": "xmodule.contentstore.mongo.MongoContentStore",
"DOC_STORE_CONFIG": {
"db": "${MONGODB_DATABASE}",
"db": "{{ MONGODB_DATABASE }}",
"host": "mongodb"
}
},
@ -23,9 +23,9 @@
"DATABASES": {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "${MYSQL_DATABASE}",
"USER": "${MYSQL_USERNAME}",
"PASSWORD": "${MYSQL_PASSWORD}",
"NAME": "{{ MYSQL_DATABASE }}",
"USER": "{{ MYSQL_USERNAME }}",
"PASSWORD": "{{ MYSQL_PASSWORD }}",
"HOST": "mysql",
"PORT": "3306",
"ATOMIC_REQUESTS": true

View File

@ -1,17 +1,17 @@
{
"SITE_NAME": "${CMS_HOST}",
"SITE_NAME": "{{ CMS_HOST }}",
"BOOK_URL": "",
"LOG_DIR": "/openedx/data/logs",
"LOGGING_ENV": "sandbox",
"OAUTH_OIDC_ISSUER": "http://localhost:8000/oauth2",
"PLATFORM_NAME": "${PLATFORM_NAME}",
"PLATFORM_NAME": "{{ PLATFORM_NAME }}",
"FEATURES": {
"PREVIEW_LMS_BASE": "preview.${LMS_HOST}"
"PREVIEW_LMS_BASE": "preview.{{ LMS_HOST }}"
},
"LMS_ROOT_URL": "http://${LMS_HOST}",
"CMS_ROOT_URL": "http://${CMS_HOST}",
"CMS_BASE": "${CMS_HOST}",
"LMS_BASE": "${LMS_HOST}",
"LMS_ROOT_URL": "http://{{ LMS_HOST }}",
"CMS_ROOT_URL": "http://{{ CMS_HOST }}",
"CMS_BASE": "{{ CMS_HOST }}",
"LMS_BASE": "{{ LMS_HOST }}",
"CELERY_BROKER_HOSTNAME": "rabbitmq",
"CELERY_BROKER_TRANSPORT": "amqp",
"MEDIA_ROOT": "/openedx/data/uploads/",

View File

@ -1,31 +1,31 @@
{
"SECRET_KEY": "${SECRET_KEY}",
"SECRET_KEY": "{{ SECRET_KEY }}",
"AWS_ACCESS_KEY_ID": "",
"AWS_SECRET_ACCESS_KEY": "",
"XQUEUE_INTERFACE": {
"django_auth": {
"username": "${XQUEUE_AUTH_USERNAME}",
"password": "${XQUEUE_AUTH_PASSWORD}"
"username": "{{ XQUEUE_AUTH_USERNAME }}",
"password": "{{ XQUEUE_AUTH_PASSWORD }}"
},
"url": "http://xqueue:8040"
},
"CONTENTSTORE": {
"ENGINE": "xmodule.contentstore.mongo.MongoContentStore",
"DOC_STORE_CONFIG": {
"db": "${MONGODB_DATABASE}",
"db": "{{ MONGODB_DATABASE }}",
"host": "mongodb"
}
},
"DOC_STORE_CONFIG": {
"db": "${MONGODB_DATABASE}",
"db": "{{ MONGODB_DATABASE }}",
"host": "mongodb"
},
"DATABASES": {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "${MYSQL_DATABASE}",
"USER": "${MYSQL_USERNAME}",
"PASSWORD": "${MYSQL_PASSWORD}",
"NAME": "{{ MYSQL_DATABASE }}",
"USER": "{{ MYSQL_USERNAME }}",
"PASSWORD": "{{ MYSQL_PASSWORD }}",
"HOST": "mysql",
"PORT": "3306",
"ATOMIC_REQUESTS": true

View File

@ -1,17 +1,17 @@
{
"SITE_NAME": "${LMS_HOST}",
"SITE_NAME": "{{ LMS_HOST }}",
"BOOK_URL": "",
"LOG_DIR": "/openedx/data/logs",
"LOGGING_ENV": "sandbox",
"OAUTH_OIDC_ISSUER": "http://localhost:8000/oauth2",
"PLATFORM_NAME": "${PLATFORM_NAME}",
"PLATFORM_NAME": "{{ PLATFORM_NAME }}",
"FEATURES": {
"PREVIEW_LMS_BASE": "preview.${LMS_HOST}"
"PREVIEW_LMS_BASE": "preview.{{ LMS_HOST }}"
},
"LMS_ROOT_URL": "http://${LMS_HOST}",
"CMS_ROOT_URL": "http://${CMS_HOST}",
"CMS_BASE": "${CMS_HOST}",
"LMS_BASE": "${LMS_HOST}",
"LMS_ROOT_URL": "http://{{ LMS_HOST }}",
"CMS_ROOT_URL": "http://{{ CMS_HOST }}",
"CMS_BASE": "{{ CMS_HOST }}",
"LMS_BASE": "{{ LMS_HOST }}",
"CELERY_BROKER_HOSTNAME": "rabbitmq",
"CELERY_BROKER_TRANSPORT": "amqp",
"COMMENTS_SERVICE_URL": "http://forum:4567",

View File

@ -1,5 +1,5 @@
mysql -u root --password="${MYSQL_PASSWORD}" --host "mysql" -e 'CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE};'
mysql -u root --password="${MYSQL_PASSWORD}" --host "mysql" -e 'GRANT ALL ON ${MYSQL_DATABASE}.* TO "${MYSQL_USERNAME}"@"%" IDENTIFIED BY "${MYSQL_PASSWORD}";'
mysql -u root --password="{{ MYSQL_PASSWORD }}" --host "mysql" -e 'CREATE DATABASE IF NOT EXISTS {{ MYSQL_DATABASE }};'
mysql -u root --password="{{ MYSQL_PASSWORD }}" --host "mysql" -e 'GRANT ALL ON {{ MYSQL_DATABASE }}.* TO "{{ MYSQL_USERNAME }}"@"%" IDENTIFIED BY "{{ MYSQL_PASSWORD }}";'
mysql -u root --password="${MYSQL_PASSWORD}" --host "mysql" -e 'CREATE DATABASE IF NOT EXISTS ${XQUEUE_MYSQL_DATABASE};'
mysql -u root --password="${MYSQL_PASSWORD}" --host "mysql" -e 'GRANT ALL ON ${XQUEUE_MYSQL_DATABASE}.* TO "${XQUEUE_MYSQL_USERNAME}"@"%" IDENTIFIED BY "${XQUEUE_MYSQL_PASSWORD}";'
mysql -u root --password="{{ MYSQL_PASSWORD }}" --host "mysql" -e 'CREATE DATABASE IF NOT EXISTS {{ XQUEUE_MYSQL_DATABASE }};'
mysql -u root --password="{{ MYSQL_PASSWORD }}" --host "mysql" -e 'GRANT ALL ON {{ XQUEUE_MYSQL_DATABASE }}.* TO "{{ XQUEUE_MYSQL_USERNAME }}"@"%" IDENTIFIED BY "{{ XQUEUE_MYSQL_PASSWORD }}";'

View File

@ -3,9 +3,9 @@ from .settings import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '${XQUEUE_MYSQL_DATABASE}',
'USER': '${XQUEUE_MYSQL_USERNAME}',
'PASSWORD': '${XQUEUE_MYSQL_PASSWORD}',
'NAME': '{{ XQUEUE_MYSQL_DATABASE }}',
'USER': '{{ XQUEUE_MYSQL_USERNAME }}',
'PASSWORD': '{{ XQUEUE_MYSQL_PASSWORD }}',
'HOST': 'mysql',
'PORT': '3306',
}
@ -19,8 +19,8 @@ LOGGING = get_logger_config(
RABBIT_HOST = 'rabbitmq'
RABBIT_PORT = 5672
SECRET_KEY = '${XQUEUE_SECRET_KEY}'
SECRET_KEY = '{{ XQUEUE_SECRET_KEY }}'
XQUEUE_USERS = {
'${XQUEUE_AUTH_USERNAME}': '${XQUEUE_AUTH_PASSWORD}'
'{{ XQUEUE_AUTH_USERNAME }}': '{{ XQUEUE_AUTH_PASSWORD}}'
}

View File

@ -20,6 +20,6 @@ CMD ./configure.py interactive ${SILENT:+--silent} && \
./configure.py substitute ./config/openedx/templates/provision.sh.templ ./config/openedx/provision.sh && \
./configure.py substitute ./config/xqueue/templates/universal.py.templ ./config/xqueue/universal.py && \
./configure.py substitute ./config/mysql/templates/auth.env.templ ./config/mysql/auth.env && \
./configure.py substitute --delimiter '£' ./config/nginx/templates/lms.conf.templ ./config/nginx/lms.conf && \
./configure.py substitute --delimiter '£' ./config/nginx/templates/cms.conf.templ ./config/nginx/cms.conf && \
./configure.py substitute ./config/nginx/templates/lms.conf.templ ./config/nginx/lms.conf && \
./configure.py substitute ./config/nginx/templates/cms.conf.templ ./config/nginx/cms.conf && \
./configure.py substitute ./config/android/templates/universal.yaml.templ ./config/android/universal.yaml

View File

@ -10,6 +10,8 @@ import sys
from collections import OrderedDict
import jinja2
class Configurator:
@ -82,7 +84,6 @@ def main():
parser_interactive.set_defaults(func=interactive)
parser_substitute = subparsers.add_parser('substitute')
parser_substitute.add_argument('--delimiter', default='$', help="Template file delimiter")
parser_substitute.add_argument('src', help="Template source file")
parser_substitute.add_argument('dst', help="Destination configuration file")
parser_substitute.set_defaults(func=substitute)
@ -140,10 +141,10 @@ def interactive(configurator, args):
def substitute(configurator, args):
with codecs.open(args.src, encoding='utf-8') as fi:
template = template_class(args.delimiter)(fi.read())
template = jinja2.Template(fi.read(), undefined=jinja2.StrictUndefined)
try:
substituted = template.substitute(**configurator.as_dict())
except KeyError as e:
substituted = template.render(**configurator.as_dict())
except jinja2.exceptions.UndefinedError as e:
sys.stderr.write("ERROR Missing config value '{}' for template {}\n".format(e.args[0], args.src))
sys.exit(1)
@ -153,18 +154,6 @@ def substitute(configurator, args):
print("Generated file {} from template {}".format(args.dst, args.src))
def template_class(user_delimiter='$'):
"""
The default delimiter of the python Template class is '$'. Here, we
generate a Template class with a custom delimiter. This cannot be done
after the class creation because the Template metaclass uses the delimiter
value.
"""
class Template(string.Template):
delimiter = user_delimiter
return Template
def random_string(length):
return "".join([random.choice(string.ascii_letters + string.digits) for _ in range(length)])