-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Database creation now happens inside docker entrypoint
- Loading branch information
1 parent
6d3d0e4
commit a3ced1e
Showing
11 changed files
with
127 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
.docker_container_id | ||
.docker_postgres_data_directory | ||
.docker_django_secret_key | ||
.docker_postgres_password | ||
.docker_secret_key | ||
.docker_volume_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,26 +9,27 @@ FROM debian:12 | |
# also in the shell scripts stored in this directory. | ||
MAINTAINER Francesco Ballarin <[email protected]> | ||
|
||
ARG SECRET_KEY | ||
ARG DJANGO_SECRET_KEY | ||
ARG POSTGRES_PASSWORD | ||
|
||
WORKDIR /root | ||
|
||
RUN apt update -y -q && \ | ||
apt install -y -qq curl net-tools python3-pip sudo unzip vim wget | ||
|
||
COPY . . | ||
|
||
# Part 1: turing and its runtime dependencies | ||
COPY patches patches | ||
COPY turing turing | ||
|
||
RUN apt install -y -qq postgresql postgresql-client postgresql-contrib && \ | ||
echo "$POSTGRES_PASSWORD\n$POSTGRES_PASSWORD" | passwd postgres | ||
|
||
RUN bash patches/turing/apply_patches.sh && \ | ||
python3 -m pip install --break-system-packages -r turing/requirements.txt | ||
|
||
RUN cat <<EOF >> /root/turing/Turing/settings.ini | ||
RUN cat <<EOF > /root/turing/Turing/settings.ini | ||
[settings] | ||
SECRET_KEY=$SECRET_KEY | ||
SECRET_KEY=$DJANGO_SECRET_KEY | ||
DEBUG=False | ||
DEV_MODE=False | ||
ALLOWED_HOSTS=* | ||
|
@@ -42,7 +43,7 @@ EOF | |
RUN cd turing && \ | ||
python3 manage.py collectstatic | ||
|
||
# Part 2: test dependencies | ||
# Part 2: turing and its test dependencies | ||
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \ | ||
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \ | ||
apt update -y -q && \ | ||
|
@@ -56,5 +57,11 @@ RUN LATEST_STABLE_RELEASE=$(curl -sS https://googlechromelabs.github.io/chrome-f | |
|
||
RUN python3 -m pip install --break-system-packages -r turing/requirements-dev.txt | ||
|
||
# The end: run server on docker container start | ||
CMD service postgresql start && cd turing && python3 manage.py runserver 0.0.0.0:8080 | ||
# Part 3: mathrace-interaction | ||
COPY mathrace_interaction mathrace_interaction | ||
|
||
# Part 4: set up entrypoint that: | ||
# * creates and initialize databases on first run, | ||
# * starts the server | ||
COPY docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh | ||
ENTRYPOINT ["docker-entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/bin/bash | ||
# Copyright (C) 2024 by the Turing @ DMF authors | ||
# | ||
# This file is part of Turing @ DMF. | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
set -e | ||
|
||
# Installed postgres version | ||
POSTGRES_VERSION=15 | ||
|
||
# Get the value of docker build args from the settings file | ||
POSTGRES_DATABASE_NAME=$(sed -n -e "s/^RDS_DB_NAME=//p" /root/turing/Turing/settings.ini) | ||
if [[ "${POSTGRES_DATABASE_NAME}" != *"-db" ]]; then | ||
echo "Expected database name ${POSTGRES_DATABASE_NAME} to end with -db" | ||
exit 1 | ||
fi | ||
POSTGRES_PASSWORD=$(sed -n -e "s/^RDS_PASSWORD=//p" /root/turing/Turing/settings.ini) | ||
|
||
# Hardcode values of docker create args | ||
POSTGRES_CLUSTER_NAME=${POSTGRES_DATABASE_NAME/-db/-cluster} | ||
if [[ "${POSTGRES_CLUSTER_NAME}" != *"-cluster" ]]; then | ||
echo "Expected cluster name ${POSTGRES_CLUSTER_NAME} to end with -cluster" | ||
exit 1 | ||
fi | ||
POSTGRES_CLUSTER_DATA_DIRECTORY=/mnt/postgres_data_directory | ||
|
||
# Create a new postgres cluster with data directory that matches the volume mounted in docker_create_container.sh, | ||
# if not already done previously | ||
# Note that the marker file .postgres_cluster_created cannot be put in ${POSTGRES_CLUSTER_DATA_DIRECTORY}, | ||
# because the cluster needs to be re-created in every container. This is safe upon container destruction because | ||
# postgres data direcory will not be cleared out when creating the cluster in a new container. | ||
POSTGRES_CLUSTER_CREATED_FILE=/root/turing/.postgres_cluster_created | ||
if [[ ! -f ${POSTGRES_CLUSTER_CREATED_FILE} ]]; then | ||
echo "Creating a new postgres cluster" | ||
pg_dropcluster ${POSTGRES_VERSION} main | ||
pg_createcluster ${POSTGRES_VERSION} --datadir=${POSTGRES_CLUSTER_DATA_DIRECTORY} ${POSTGRES_CLUSTER_NAME} -- -E UTF8 --locale=C.utf8 --lc-messages=C | ||
cp /etc/postgresql/${POSTGRES_VERSION}/${POSTGRES_CLUSTER_NAME}/*.conf ${POSTGRES_CLUSTER_DATA_DIRECTORY}/ | ||
touch ${POSTGRES_CLUSTER_CREATED_FILE} | ||
else | ||
echo "Reusing existing postgres cluster" | ||
fi | ||
|
||
# Start postgresql service | ||
echo "Starting postgresql service" | ||
service postgresql start | ||
|
||
# Initialize an empty postgres database, if not already done previously | ||
POSTGRES_DATABASE_INITIALIZED_FILE=${POSTGRES_CLUSTER_DATA_DIRECTORY}/.postgres_database_initialized | ||
if [[ ! -f ${POSTGRES_DATABASE_INITIALIZED_FILE} ]]; then | ||
echo "Initializing an empty postgres database" | ||
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD '${POSTGRES_PASSWORD}';" | ||
sudo -u postgres createdb ${POSTGRES_DATABASE_NAME} | ||
touch ${POSTGRES_DATABASE_INITIALIZED_FILE} | ||
else | ||
echo "Reusing existing postgres database" | ||
fi | ||
|
||
# Ask turing to initialize the django database, if not already done previously | ||
DJANGO_DATABASE_MIGRATED_FILE=${POSTGRES_CLUSTER_DATA_DIRECTORY}/.django_database_migrated | ||
if [[ ! -f ${DJANGO_DATABASE_MIGRATED_FILE} ]]; then | ||
echo "Initializing django database" | ||
cd /root/turing | ||
python3 manage.py makemigrations | ||
python3 manage.py makemigrations engine | ||
python3 manage.py migrate | ||
touch ${DJANGO_DATABASE_MIGRATED_FILE} | ||
else | ||
echo "Not initializing again django database" | ||
fi | ||
|
||
# Add a default administration user to the django database, if not already done previously | ||
DJANGO_ADMIN_INITIALIZED_FILE=${POSTGRES_CLUSTER_DATA_DIRECTORY}/.django_admin_initialized | ||
if [[ ! -f ${DJANGO_ADMIN_INITIALIZED_FILE} ]]; then | ||
echo "Initialize the default django administrator user with username admin and password admin. Make sure to change both the username and the password as soon as possible!" | ||
cd /root/turing | ||
[email protected] DJANGO_SUPERUSER_USERNAME=admin DJANGO_SUPERUSER_PASSWORD=admin python3 manage.py createsuperuser --no-input | ||
touch ${DJANGO_ADMIN_INITIALIZED_FILE} | ||
else | ||
echo "Not initializing again default django administrator" | ||
fi | ||
|
||
# Start the server | ||
echo "Starting the server" | ||
cd /root/turing | ||
python3 manage.py runserver 0.0.0.0:8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters