This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
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.
Merge pull request #21 from match4everyone/easier-deploy
Optimize docker deployment and reduce error surfaces
- Loading branch information
Showing
34 changed files
with
623 additions
and
123 deletions.
There are no files selected for viewing
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,3 @@ | ||
# Use backend_user to specify a user to run the backend container as. | ||
# If none is specified root will be used | ||
BACKEND_USER=user:group |
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,5 @@ | ||
**/__pycache__ | ||
**/.gitkeep | ||
**/.gitignore | ||
run/* | ||
!run/log |
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,11 +1,23 @@ | ||
FROM ubuntu:18.04 | ||
|
||
RUN apt-get update && apt-get install -y python3 python3-pip libpq-dev gettext | ||
WORKDIR /match4everyone-backend | ||
COPY requirements.txt /match4everyone-backend/requirements.txt | ||
|
||
WORKDIR /backend | ||
COPY requirements.txt /backend/requirements.txt | ||
RUN pip3 install -r requirements.txt | ||
COPY requirements.prod.txt /match4everyone-backend/requirements.prod.txt | ||
COPY requirements.prod.txt /backend/requirements.prod.txt | ||
RUN pip3 install -r requirements.prod.txt | ||
|
||
COPY . . | ||
|
||
RUN python3 manage.py makemessages --no-location | ||
RUN python3 manage.py compilemessages | ||
RUN python3 manage.py collectstatic --no-input | ||
# Change permissions on run/ in case app is later run by non-root user | ||
# and delete log files as these are created during the above commands when django loads the configuration | ||
# and will be created non writeable by others than root | ||
RUN rm -v run/log/* && chmod a+rwX run/log && chmod a+rwX backups | ||
|
||
EXPOSE 8000 | ||
ENTRYPOINT ["./entrypoint.sh"] | ||
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"] |
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,23 @@ | ||
import fileinput | ||
import re | ||
|
||
from django.core.management.commands.makemessages import Command as MakeMessageCommand | ||
|
||
|
||
class Command(MakeMessageCommand): | ||
|
||
help = ( # noqa | ||
MakeMessageCommand.help | ||
+ "Modified behaviour: Will reset creation date in all po files to 1900-01-01 00:00+0000" | ||
"to prevent diffs from being created when the only change would be a rerun of makemessages" | ||
) | ||
|
||
def build_potfiles(self): | ||
potfiles = super().build_potfiles() | ||
print("Using constant POT-Creation-Date: 1900-01-01 00:00+0000") | ||
pattern = re.compile(r"POT-Creation-Date: .*\\n") | ||
for line in fileinput.input(files=potfiles, inplace=True): | ||
line = pattern.sub(r"POT-Creation-Date: 1900-01-01 00:00+0000\\n", line) | ||
print(line, end="") | ||
|
||
return potfiles |
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,19 @@ | ||
#!/usr/bin/env bash | ||
set -e -o pipefail | ||
|
||
if ! [[ -z "${PRODUCTION}" ]]; then | ||
echo Running in production mode, waiting for database to come up | ||
# Moved from docker-compose, as this is needed for migrate | ||
echo -n "Waiting for database" | ||
while ! (< /dev/tcp/database/5432) &> /dev/null; do | ||
echo -n . | ||
sleep .2 | ||
done | ||
echo " OK" | ||
fi | ||
|
||
echo PYTHONPATH: $PYTHONPATH | ||
django-admin migrate | ||
django-admin check | ||
|
||
exec "$@" |
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
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 |
---|---|---|
|
@@ -3,5 +3,3 @@ gunicorn==20.0.4 | |
psutil==5.7.0 | ||
psycopg2==2.8.5 | ||
psycopg2==2.8.5 | ||
whitenoise==5.0.1 | ||
whitenoise[brotli] |
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 |
---|---|---|
|
@@ -31,3 +31,4 @@ sendgrid==6.2.0 | |
six==1.15.0 | ||
tqdm==4.46.1 | ||
whitenoise==5.0.1 | ||
whitenoise[brotli] |
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
Empty file.
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,4 +1,21 @@ | ||
#!/usr/bin/env bash | ||
source database.prod.env | ||
docker-compose -f docker-compose.dev.yml -f docker-compose.prod.yml exec backend sh -c 'python3 manage.py dumpdata > /match4everyone-backend/backups/fixture-$(date +%F_%H%M%S).json' | ||
docker-compose -f docker-compose.dev.yml -f docker-compose.prod.yml exec database sh -c "pg_dumpall -U $POSTGRES_USER> /backups/pg_backup-$(date +%F_%H%M%S).sql" | ||
# This backup script will create a database dump from the postgres container | ||
# the created backup will then be moved to the current directory | ||
set -o errexit | ||
|
||
WORKINGDIR=$(pwd) | ||
|
||
echo "Creating PostgreSQL Dump" | ||
docker-compose -f docker-compose.yml -f docker-compose.prod.yml exec database sh -c 'pg_dumpall -U $POSTGRES_USER> /backups/pg_backup-$(date +%F_%H%M%S).sql' | ||
|
||
# Create a throwaway container (--rm removes after running the command) to move the backups from the DB-container volume to the local directory for further processing | ||
# (Mounts the current working dir as /backup-bind-mount inside the container and moves the backups there) | ||
echo "Moving the backups to $WORKINGDIR" | ||
DB_CONTAINER="$(docker-compose -f docker-compose.yml -f docker-compose.prod.yml ps -q database)" | ||
docker run --rm --volumes-from "$DB_CONTAINER" -v "${WORKINGDIR}:/host" alpine sh -c "mv -v backups/* /host/database/backups" | ||
|
||
echo -e "\nCreating Django fixtures" | ||
docker-compose -f docker-compose.yml -f docker-compose.prod.yml exec backend sh -c 'django-admin dumpdata > /backend/backups/fixture-$(date +%F_%H%M%S).json' | ||
echo "Moving the backups to $WORKINGDIR" | ||
BACKEND_CONTAINER="$(docker-compose -f docker-compose.yml -f docker-compose.prod.yml ps -q backend)" | ||
docker run --rm --volumes-from "$BACKEND_CONTAINER" -v "${WORKINGDIR}:/host" alpine sh -c "mv -v /backend/backups/*.json /host/backend/backups" |
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
# Ignore database files | ||
data/* | ||
# Ignore backups | ||
backups/ | ||
backups/* | ||
!backups/.gitkeep |
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,3 +1,4 @@ | ||
FROM postgres | ||
FROM postgres:12 | ||
|
||
COPY init-db.sh /docker-entrypoint-initdb.d/ | ||
COPY postgresql.conf /etc/postgresql.conf |
Empty file.
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
Oops, something went wrong.