From a1e7e4f30eba63f37a2363240b516f20e4772e99 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 17 Aug 2020 08:23:00 +0200 Subject: [PATCH] Prepare for deploy --- .dockerignore | 2 ++ .gitignore | 1 + Dockerfile | 19 +++++++++++++++++++ articles/static/style.css | 4 ++++ blog/settings.py | 26 +++++++++++++++++++++----- docker-compose.yml | 15 +++++++++++++++ docker/run.sh | 5 +++++ requirements-dev.txt | 1 + requirements.txt | 2 +- 9 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100755 docker/run.sh create mode 100644 requirements-dev.txt diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b818462 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +*.db +*.sqlite3 diff --git a/.gitignore b/.gitignore index 3c6ae3e..0256955 100644 --- a/.gitignore +++ b/.gitignore @@ -272,3 +272,4 @@ dmypy.json # End of https://www.toptal.com/developers/gitignore/api/osx,pycharm,python .idea +db/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d42e59d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.8.5-slim + +RUN mkdir /app && mkdir /db +WORKDIR /app +COPY requirements.txt ./ +RUN pip install -r requirements.txt +COPY manage.py ./ +COPY articles ./articles/ +COPY blog ./blog/ +COPY docker ./docker/ + +ENV SECRET_KEY "changeme" +ENV DEBUG "false" +ENV HOST "" +ENV DB_BASE_DIR "/db" + +HEALTHCHECK --start-period=30s CMD curl -f http://localhost:8000 + +CMD ["/app/docker/run.sh"] diff --git a/articles/static/style.css b/articles/static/style.css index e12dd07..9a67b9e 100644 --- a/articles/static/style.css +++ b/articles/static/style.css @@ -31,6 +31,10 @@ a { transition: text-decoration .12s ease; } +/*.article-list a {*/ +/* text-decoration: underline var(--accent) 1px;*/ +/*}*/ + a:hover, a:focus { text-decoration: none; } diff --git a/blog/settings.py b/blog/settings.py index a635866..7385a6b 100644 --- a/blog/settings.py +++ b/blog/settings.py @@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/3.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.1/ref/settings/ """ - +import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -20,12 +20,20 @@ BASE_DIR = Path(__file__).resolve(strict=True).parent.parent # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "s#!83!8e$3s89m)r$1ghsgxbndf8=#^qt(_*o%xbq0j2t8#db5" +SECRET_KEY = os.getenv( + "SECRET_KEY", "s#!83!8e$3s89m)r$1ghsgxbndf8=#^qt(_*o%xbq0j2t8#db5" +) # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.getenv("DEBUG", "true").lower() == "true" -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = [ + "localhost", + "127.0.0.1", +] +HOST = os.getenv("HOST") +if HOST: + ALLOWED_HOSTS.append(HOST) # Application definition @@ -74,10 +82,17 @@ WSGI_APPLICATION = "blog.wsgi.application" # Database # https://docs.djangoproject.com/en/3.1/ref/settings/#databases +DB_BASE_DIR = os.getenv("DB_BASE_DIR", BASE_DIR) +if not DB_BASE_DIR: + # Protect against empty strings + DB_BASE_DIR = BASE_DIR +else: + DB_BASE_DIR = Path(DB_BASE_DIR).resolve(strict=True) + DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", - "NAME": BASE_DIR / "db.sqlite3", + "NAME": DB_BASE_DIR / "db.sqlite3", } } @@ -113,6 +128,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/3.1/howto/static-files/ STATIC_URL = "/static/" +STATIC_ROOT = BASE_DIR / "static" AUTH_USER_MODEL = "articles.User" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a8a6763 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +version: '2.4' +services: + django: + image: crocmagnon/blog + build: . + ports: + - 8000:8000 + env_file: + - .env + volumes: + - ./db:/db +# - /srv/blog/db:/db + - ./static:/app/static +# - /srv/blog/static:/app/static + restart: on-failure diff --git a/docker/run.sh b/docker/run.sh new file mode 100755 index 0000000..d1c1466 --- /dev/null +++ b/docker/run.sh @@ -0,0 +1,5 @@ +#!/bin/sh +yes yes | python manage.py migrate && \ +#yes yes | pipenv run python manage.py createcachetable && \ +python manage.py collectstatic --noinput && \ +gunicorn blog.wsgi -b 0.0.0.0:8000 --log-file - diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..e69b5a0 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1 @@ +pre-commit==2.6.0 diff --git a/requirements.txt b/requirements.txt index 957549f..0546b15 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ django==3.1 -pre-commit==2.6.0 markdown==3.2.2 +gunicorn==20.0.4