Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚚 add production deployment capabilities #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ RUN ./install-packages.sh
FROM common-base AS dependencies
ENV PATH="/opt/venv/bin:$PATH"

# apt-get install build-essential -y
COPY requirements.txt /app/

RUN pip install --target /opt/packages -r requirements.txt
Expand All @@ -44,7 +43,7 @@ RUN pip install --target /opt/packages -r requirements.txt
FROM common-base AS app-run
COPY --from=dependencies /opt/packages /opt/packages
ENV PYTHONPATH "${PYTHONPATH}:/opt/packages"
# ENV PYTHONPATH="$PYTHONPATH:/app/lemarche:/app/config"
ENV PATH "${PATH}:/opt/packages/bin"
COPY ./ark ./ark
COPY ./ark_import ./ark_import
COPY ./arklet ./arklet
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ Happy minting, binding, and resolving!
See arklet/settings.py for the full list of options to put in your config file.

## Deploying
:warning: Django being Django, you have to test the _production_ build locally.
Why ? In _development_ build (ie: with `debug` turned on) Django servers the static content, while in
_production_ mode it doesn't. [Whitenoise](http://whitenoise.evans.io/en/stable/)
is used to serve the static content in _production_.

To simulate _production_ mode, just set `ARK_DEBUG` to `False` in your local environment's
configuration (or build the _prod_ build target).

### With docker
Using the provided Dockerfile (is you wish to set a build target, use `prod`,
but being the default target you can skip this), provide the following values
Expand Down
21 changes: 20 additions & 1 deletion arklet/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
ARKLET_SENTRY_TRANSACTIONS_PER_TRACE=(int, 1),
ARKLET_STATIC_ROOT=(str, "static"),
ARKLET_MEDIA_ROOT=(str, "media"),
COMPRESSION_ENABLED=(bool, True),

)
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -52,6 +54,9 @@
"ark.archive.org",
]

CSRF_TRUSTED_ORIGINS=[
'https://%s' % env("ARKLET_HOST")
]

# Application definition

Expand All @@ -62,11 +67,13 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"compressor",
"ark.apps.ArkConfig",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
Expand Down Expand Up @@ -168,9 +175,21 @@
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = "/static/"

STATIC_ROOT = env.str("ARKLET_STATIC_ROOT")
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"compressor.finders.CompressorFinder",
]

STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATICFILES_FINDERS += ["compressor.finders.CompressorFinder"]

COMPRESS_ENABLED = env.bool("COMPRESSION_ENABLED")
COMPRESS_OFFLINE = True
COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage"

# Media files
MEDIA_ROOT = env.str("ARKLET_MEDIA_ROOT")

# Default primary key field type
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ services:
context: .
target: dev
dockerfile: ./Dockerfile
#command: tail -F /var/log/lastlog
command: /app/entrypoint.sh
volumes:
- ./ark:/app/ark
Expand Down
20 changes: 16 additions & 4 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Getting static files for Admin panel hosting!
set -e

# White while DB is spinning up
# Wait a bit while DB is spinning up
echo "pg_isready -h $ARKLET_POSTGRES_HOST -p $ARKLET_POSTGRES_PORT"
while ! pg_isready -h $ARKLET_POSTGRES_HOST -p $ARKLET_POSTGRES_PORT; do
>&2 echo "Postgres is unavailable - sleeping"
Expand All @@ -14,7 +14,19 @@ done
# ./manage.py compress --force

./manage.py migrate
./manage.py createsuperuser

./manage.py runserver 0.0.0.0:$ARKLET_PORT
# gunicorn config.wsgi:application -w 2 -b :8880 --reload
case "$ENV" in
"dev")
# Run the Django development server
./manage.py runserver 0.0.0.0:$ARKLET_PORT
;;
"prod")
# Run the Gunicorn production server
./manage.py collectstatic --noinput
gunicorn arklet.wsgi:application -w 2 -b :$ARKLET_PORT --reload
;;
*)
echo "Wrong environment defined, check ENV value (prod|dev)"
exit 1
;;
esac
7 changes: 6 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ black==21.9b0
certifi==2021.10.8
click==8.0.3
Django==4.0.4
django-appconf==1.0.5
django-compressor==4.0
django-environ==0.8.0
gunicorn==20.1.0
Jinja2==3.0.2
MarkupSafe==2.0.1
Expand All @@ -14,10 +17,12 @@ platformdirs==2.4.0
psycopg2-binary==2.9.1
pytz==2021.3
PyYAML==6.0
rcssmin==1.1.0
regex==2021.10.23
rjsmin==1.2.0
sentry-sdk==1.4.3
sqlparse==0.4.2
tomli==1.2.2
typing-extensions==3.10.0.2
urllib3==1.26.7
django-environ==0.8.0
whitenoise==6.0.0