Skip to content

Commit f44159a

Browse files
committed
feat: allow multiple pod instances to be ran
- docker initialization marker is the creation of a .initialized file instead of detecting if the database has been created or not - make createDb file cleaning is error-proof and can be executed multiple times in parallel - collectstatic logs are quieter - the default documentation advise to rely on the POD_INSTANCE environment var to discriminate ES_INDEX, caches and redis session prefix - docker pod entrypoints rely on the POD_INSTANCE en var to detect the right .initialized file - docker pod entrypoints rely on the POD_PORT en var to run the app on dedicated port - the 'Site' object is not statically initialized with initial_data.json, but dynamically in main/apps.py - the celery queues use the POD_INSTANCE var as keyprefixes - the default database is named after POD_INSTANCE if available
1 parent dd08c8e commit f44159a

File tree

15 files changed

+83
-34
lines changed

15 files changed

+83
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pod/main/static/custom/img
5050
!pod/custom/settings_local.py.example
5151
settings_local.py
5252
transcription/*
53+
.*initialized
5354

5455
# Unit test utilities #
5556
#######################

Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ upgrade:
4040

4141
# Création des données initiales dans la BDD SQLite intégrée
4242
createDB:
43-
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
44-
find . -path "*/migrations/*.pyc" -delete
43+
find . -path "*/migrations/*.py" -not -name "__init__.py" -exec rm --force "{}" +
44+
find . -path "*/migrations/*.pyc" -exec rm --force "{}" +
4545
make updatedb
4646
make migrate
47-
python3 manage.py loaddata initial_data
4847

4948
# Mise à jour des fichiers de langue
5049
lang:
@@ -78,7 +77,7 @@ pystyle:
7877
statics:
7978
cd pod; yarn install; yarn upgrade
8079
# --clear Clear the existing files before trying to copy or link the original file.
81-
python3 manage.py collectstatic --clear
80+
python3 manage.py collectstatic --clear --verbosity 0
8281

8382
# Generate configuration docs in .MD format
8483
createconfigs:
@@ -171,6 +170,6 @@ endif
171170
sudo rm -rf ./pod/static
172171
sudo rm -rf ./pod/node_modules
173172
sudo rm -rf ./pod/db_migrations
174-
sudo rm -rf ./pod/db.sqlite3
175-
sudo rm -rf ./pod/db_remote.sqlite3
173+
sudo rm -rf ./pod/*.sqlite3
174+
sudo rm -rf ./pod/.*initialized
176175
sudo rm -rf ./pod/media

docker-compose-dev-with-volumes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ services:
3131
ports:
3232
- 9200:9200
3333
environment:
34+
- logger.level=WARN
3435
- discovery.type=single-node
3536
- bootstrap.memory_lock=true
3637
- "ES_JAVA_OPTS=-Xms2g -Xmx2g"

docker-compose-full-dev-with-volumes-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ services:
7070
ports:
7171
- 9200:9200
7272
environment:
73+
- logger.level=WARN
7374
- discovery.type=single-node
7475
- bootstrap.memory_lock=true
7576
- "ES_JAVA_OPTS=-Xms2g -Xmx2g"

docker-compose-full-dev-with-volumes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ services:
7070
ports:
7171
- 9200:9200
7272
environment:
73+
- logger.level=WARN
7374
- discovery.type=single-node
7475
- bootstrap.memory_lock=true
7576
- "ES_JAVA_OPTS=-Xms2g -Xmx2g"

dockerfile-dev-with-volumes/README.adoc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ DOCKER_ENV=light
6161
Renseignez le fichier pod/custom/settings_local.py comme ceci :
6262
[source,python]
6363
----
64+
import os
65+
INSTANCE = os.getenv("POD_INSTANCE", "pod")
6466
6567
USE_PODFILE = True
6668
EMAIL_ON_ENCODING_COMPLETION = False
@@ -69,6 +71,7 @@ DEBUG = True
6971
# on précise ici qu'on utilise ES version 8
7072
ES_VERSION = 8
7173
ES_URL = ['http://elasticsearch.localhost:9200/']
74+
ES_INDEX = INSTANCE
7275
7376
7477
CACHES = {
@@ -78,22 +81,23 @@ CACHES = {
7881
"OPTIONS": {
7982
"CLIENT_CLASS": "django_redis.client.DefaultClient",
8083
},
81-
"KEY_PREFIX": "pod"
84+
"KEY_PREFIX": INSTANCE
8285
},
8386
"select2": {
8487
"BACKEND": "django_redis.cache.RedisCache",
8588
"LOCATION": "redis://redis.localhost:6379/2",
8689
"OPTIONS": {
8790
"CLIENT_CLASS": "django_redis.client.DefaultClient",
8891
},
92+
'KEY_PREFIX': INSTANCE
8993
},
9094
}
9195
SESSION_ENGINE = "redis_sessions.session"
9296
SESSION_REDIS = {
9397
"host": "redis.localhost",
9498
"port": 6379,
9599
"db": 4,
96-
"prefix": "session",
100+
"prefix": f"session-{INSTANCE}",
97101
"socket_timeout": 1,
98102
"retry_on_timeout": False,
99103
}
@@ -148,7 +152,7 @@ Vous devriez obtenir ce message une fois esup-pod lancé
148152
----
149153
$ pod-dev-with-volumes | Superuser created successfully.
150154
----
151-
L'application esup-pod est dès lors disponible via cette URL : localhost:8000
155+
L'application esup-pod est dès lors disponible via cette URL : pod.localhost:8000
152156

153157
=== Arrêt de la stack
154158
$ CTRL+C dans la fenetre depuis laquelle l'application esup-pod a été lancée
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
#!/bin/sh
2-
echo "Launching commands into pod-dev"
2+
INSTANCE="${POD_INSTANCE:-pod}"
3+
PORT="${POD_PORT:-8000}"
4+
echo "Launching commands into ${INSTANCE}-dev"
35
mkdir -p pod/node_modules
46
mkdir -p pod/db_migrations && touch pod/db_migrations/__init__.py
57
ln -fs /tmp/node_modules/* pod/node_modules
68
# Mise en route
79
# Base de données SQLite intégrée
8-
BDD_FILE=/usr/src/app/pod/db.sqlite3
9-
if test ! -f "$BDD_FILE"; then
10-
echo "$BDD_FILE does not exist."
10+
INIT_FILE="/usr/src/app/pod/.${INSTANCE}.initialized"
11+
if test ! -f "$INIT_FILE"; then
12+
echo "$INIT_FILE does not exist."
1113
python3 manage.py create_pod_index
12-
curl -XGET "elasticsearch:9200/pod/_search"
14+
curl -XGET "elasticsearch.localhost:9200/${INSTANCE}/_search"
1315
# Deployez les fichiers statiques
14-
python3 manage.py collectstatic --no-input --clear
16+
python3 manage.py collectstatic --no-input --clear --verbosity 0
1517
# Lancez le script présent à la racine afin de créer les fichiers de migration, puis de les lancer pour créer la base de données SQLite intégrée.
1618
make createDB
1719
# SuperUtilisateur
1820
# Il faut créer un premier utilisateur qui aura tous les pouvoirs sur votre instance.
1921
python3 manage.py createsuperuser --noinput
22+
touch "$INIT_FILE"
2023
else
21-
echo "$BDD_FILE exist."
24+
echo "$INIT_FILE exist."
2225
fi
2326
# Serveur de développement
2427
# Le serveur de développement permet de tester vos futures modifications facilement.
2528
# N'hésitez pas à lancer le serveur de développement pour vérifier vos modifications au fur et à mesure.
2629
# À ce niveau, vous devriez avoir le site en français et en anglais et voir l'ensemble de la page d'accueil.
27-
python3 manage.py runserver 0.0.0.0:8000 --insecure
30+
python3 manage.py runserver "0.0.0.0:${PORT}" --insecure
2831
sleep infinity
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
#!/bin/sh
2-
echo "Launching commands into pod-dev"
2+
INSTANCE="${POD_INSTANCE:-pod}"
3+
PORT="${POD_PORT:-8000}"
4+
echo "Launching commands into ${INSTANCE}-dev"
35
mkdir -p pod/node_modules
46
mkdir -p pod/db_migrations && touch pod/db_migrations/__init__.py
57
ln -fs /tmp/node_modules/* pod/node_modules
68
# Mise en route
79
# Base de données SQLite intégrée
8-
BDD_FILE=/usr/src/app/pod/db.sqlite3
9-
if test ! -f "$BDD_FILE"; then
10-
echo "$BDD_FILE does not exist."
10+
INIT_FILE="/usr/src/app/pod/.${INSTANCE}.initialized"
11+
if test ! -f "$INIT_FILE"; then
12+
echo "$INIT_FILE does not exist."
1113
python3 manage.py create_pod_index
12-
curl -XGET "elasticsearch.localhost:9200/pod/_search"
14+
curl -XGET "elasticsearch.localhost:9200/${INSTANCE}/_search"
1315
# Deployez les fichiers statiques
14-
python3 manage.py collectstatic --no-input --clear
16+
python3 manage.py collectstatic --no-input --clear --verbosity 0
1517
# Lancez le script présent à la racine afin de créer les fichiers de migration, puis de les lancer pour créer la base de données SQLite intégrée.
1618
make createDB
1719
# SuperUtilisateur
1820
# Il faut créer un premier utilisateur qui aura tous les pouvoirs sur votre instance.
1921
python3 manage.py createsuperuser --noinput
22+
touch "$INIT_FILE"
2023
else
21-
echo "$BDD_FILE exist."
24+
echo "$INIT_FILE exist."
2225
fi
2326
# Serveur de développement
2427
# Le serveur de développement permet de tester vos futures modifications facilement.
2528
# N'hésitez pas à lancer le serveur de développement pour vérifier vos modifications au fur et à mesure.
2629
# À ce niveau, vous devriez avoir le site en français et en anglais et voir l'ensemble de la page d'accueil.
27-
python3 manage.py runserver 0.0.0.0:8000 --insecure
30+
python3 manage.py runserver "0.0.0.0:${PORT}" --insecure
2831
sleep infinity

pod/main/apps.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
"""Esup-pod Main applications."""
22

3+
import os
4+
35
from django.apps import AppConfig
46
from django.db.models.signals import post_migrate
57
from django.utils.translation import gettext_lazy as _
8+
from django.conf import settings
69
from django.core.exceptions import ObjectDoesNotExist
10+
from django.core.management import call_command
711

812
import json
913

14+
SITE_ID = getattr(settings, "SITE_ID", 1)
15+
INSTANCE = os.getenv("POD_INSTANCE", "pod")
16+
PORT = os.getenv("POD_PORT", 8000)
17+
18+
19+
def load_data(sender, **kwargs):
20+
from django.contrib.sites.models import Site
21+
22+
call_command("loaddata", "initial_data.json")
23+
24+
domain = f"{INSTANCE}.localhost:{PORT}"
25+
site, _ = Site.objects.update_or_create(id=settings.SITE_ID)
26+
site.domain = domain
27+
site.name = domain
28+
site.save()
29+
1030

1131
def create_missing_pages(sender, **kwargs) -> None:
1232
"""Create missing flat pages from json fixtures."""
@@ -72,6 +92,7 @@ def create_missing_conf(sender, **kwargs) -> None:
7292

7393
print("---> Creating missing configurations...")
7494
json_data = []
95+
7596
with open("./pod/main/fixtures/initial_data.json", encoding="utf-8") as data_file:
7697
json_data = json.loads(data_file.read())
7798

@@ -157,3 +178,4 @@ def ready(self):
157178
post_migrate.connect(create_missing_conf, sender=self)
158179
post_migrate.connect(create_missing_pages, sender=self)
159180
post_migrate.connect(create_first_block, sender=self)
181+
post_migrate.connect(load_data, sender=self)

pod/main/celery.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
# set the default Django settings module for the 'celery' program.
77
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pod.settings")
88

9+
try:
10+
from pod.custom import settings_local
11+
except ImportError:
12+
from pod import settings as settings_local
13+
14+
INSTANCE = getattr(settings_local, "INSTANCE", None)
15+
916
app = Celery("pod_project")
1017

1118
# Using a string here means the worker doesn't have to serialize
@@ -20,6 +27,8 @@
2027
"pod.main.tasks.*": {"queue": "celery"},
2128
"pod.main.celery.*": {"queue": "celery"},
2229
}
30+
if INSTANCE:
31+
app.conf.broker_transport_options = {"global_keyprefix": INSTANCE}
2332

2433

2534
@app.task(bind=True)

0 commit comments

Comments
 (0)