From 2939b4a5156838bbb5b399ebe40df536e6fbb052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 7 Jan 2023 21:09:47 +0100 Subject: [PATCH 1/7] Change to gunicorn --- .gitignore | 3 +- Caddyfile | 14 +++++ Dockerfile | 10 +++- Makefile | 17 +++++- entrypoint.sh | 6 +- poetry.lock | 75 ++++++++++++++++++++++++- pyproject.toml | 4 +- src/web/tracker/templatetags/version.py | 7 ++- src/web/web/settings.py | 4 +- 9 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 Caddyfile diff --git a/.gitignore b/.gitignore index ad2f128..cb91e09 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ __pycache__ .venv node_modules package-lock.json -db.sqlite3 \ No newline at end of file +db.sqlite3 +src/web/static \ No newline at end of file diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..158438a --- /dev/null +++ b/Caddyfile @@ -0,0 +1,14 @@ +{ + auto_https off + admin off +} + +:8000 { + handle_path /static/* { + root * src/web/static/ + file_server + } + handle { + reverse_proxy :8001 + } +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9948b9b..5838e0e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,12 +4,16 @@ COPY . /app RUN npm install && \ npx tailwindcss -i ./src/input.css -o ./src/web/tracker/static/base.css --minify -FROM python:3.10-slim-bullseye +FROM python:3.10.9-alpine -ENV VERSION_NUMBER 0.1.0-51-g9dead36 +ENV VERSION_NUMBER 0.1.1-3-gd029fda ENV PROD 1 -RUN useradd --create-home --uid 1000 timetracker +RUN apk add \ + vim \ + curl \ + caddy +RUN adduser -D -u 1000 timetracker WORKDIR /home/timetracker/app COPY . /home/timetracker/app/ RUN chown -R timetracker:timetracker /home/timetracker/app diff --git a/Makefile b/Makefile index 518a469..f678dcd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: createsuperuser shell +.PHONY: createsuperuser shell cleanstatic all: css migrate @@ -24,6 +24,9 @@ migrate: makemigrations dev: migrate sethookdir poetry run python src/web/manage.py runserver_plus +dev-prod: migrate collectstatic sethookdir + cd src/web/; poetry run python -m gunicorn --bind 0.0.0.0:8001 web.asgi:application -k uvicorn.workers.UvicornWorker + dumptracker: poetry run python src/web/manage.py dumpdata --format yaml tracker --output tracker_fixture.yaml @@ -39,6 +42,9 @@ createsuperuser: shell: poetry run python src/web/manage.py shell +collectstatic: + poetry run python src/web/manage.py collectstatic + poetry.lock: pyproject.toml poetry install @@ -48,5 +54,10 @@ test: poetry.lock sethookdir: git config core.hooksPath .githooks -make date: - python3 -c 'import datetime; from zoneinfo import ZoneInfo; print(datetime.datetime.isoformat(datetime.datetime.now(ZoneInfo("Europe/Prague")), timespec="minutes", sep=" "))' \ No newline at end of file +date: + python3 -c 'import datetime; from zoneinfo import ZoneInfo; print(datetime.datetime.isoformat(datetime.datetime.now(ZoneInfo("Europe/Prague")), timespec="minutes", sep=" "))' + +cleanstatic: + rm -r src/web/static/* + +clean: cleanstatic \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 14a6e39..1955a36 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,11 +1,13 @@ #!/bin/bash # Apply database migrations +set -euo pipefail echo "Apply database migrations" poetry run python src/web/manage.py migrate echo "Collect static files" poetry run python src/web/manage.py collectstatic -# Start server echo "Starting server" -poetry run python src/web/manage.py runserver 0.0.0.0:8000 \ No newline at end of file +caddy run +cd src/web || exit +poetry run python -m gunicorn --bind 0.0.0.0:8001 web.asgi:application -k uvicorn.workers.UvicornWorker diff --git a/poetry.lock b/poetry.lock index f2e96ea..f1f47b8 100644 --- a/poetry.lock +++ b/poetry.lock @@ -73,7 +73,7 @@ uvloop = ["uvloop (>=0.15.2)"] name = "click" version = "8.1.3" description = "Composable command line interface toolkit" -category = "dev" +category = "main" optional = false python-versions = ">=3.7" files = [ @@ -88,7 +88,7 @@ colorama = {version = "*", markers = "platform_system == \"Windows\""} name = "colorama" version = "0.4.6" description = "Cross-platform colored terminal text." -category = "dev" +category = "main" optional = false python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" files = [ @@ -215,6 +215,39 @@ files = [ [package.extras] test = ["pytest (>=6)"] +[[package]] +name = "gunicorn" +version = "20.1.0" +description = "WSGI HTTP Server for UNIX" +category = "main" +optional = false +python-versions = ">=3.5" +files = [ + {file = "gunicorn-20.1.0-py3-none-any.whl", hash = "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e"}, + {file = "gunicorn-20.1.0.tar.gz", hash = "sha256:e0a968b5ba15f8a328fdfd7ab1fcb5af4470c28aaf7e55df02a99bc13138e6e8"}, +] + +[package.dependencies] +setuptools = ">=3.0" + +[package.extras] +eventlet = ["eventlet (>=0.24.1)"] +gevent = ["gevent (>=1.4.0)"] +setproctitle = ["setproctitle"] +tornado = ["tornado (>=0.2)"] + +[[package]] +name = "h11" +version = "0.14.0" +description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1" +category = "main" +optional = false +python-versions = ">=3.7" +files = [ + {file = "h11-0.14.0-py3-none-any.whl", hash = "sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761"}, + {file = "h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d"}, +] + [[package]] name = "html-tag-names" version = "0.1.2" @@ -627,6 +660,23 @@ files = [ {file = "regex-2022.10.31.tar.gz", hash = "sha256:a3a98921da9a1bf8457aeee6a551948a83601689e5ecdd736894ea9bbec77e83"}, ] +[[package]] +name = "setuptools" +version = "65.6.3" +description = "Easily download, build, install, upgrade, and uninstall Python packages" +category = "main" +optional = false +python-versions = ">=3.7" +files = [ + {file = "setuptools-65.6.3-py3-none-any.whl", hash = "sha256:57f6f22bde4e042978bcd50176fdb381d7c21a9efa4041202288d3737a0c6a54"}, + {file = "setuptools-65.6.3.tar.gz", hash = "sha256:a7620757bf984b58deaf32fc8a4577a9bbc0850cf92c20e1ce41c38c19e5fb75"}, +] + +[package.extras] +docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "pygments-github-lexers (==0.0.5)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-favicon", "sphinx-hoverxref (<2)", "sphinx-inline-tabs", "sphinx-notfound-page (==0.8.3)", "sphinx-reredirects", "sphinxcontrib-towncrier"] +testing = ["build[virtualenv]", "filelock (>=3.4.0)", "flake8 (<5)", "flake8-2020", "ini2toml[lite] (>=0.9)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pip (>=19.1)", "pip-run (>=8.8)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)", "pytest-perf", "pytest-timeout", "pytest-xdist", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"] +testing-integration = ["build[virtualenv]", "filelock (>=3.4.0)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pytest", "pytest-enabler", "pytest-xdist", "tomli", "virtualenv (>=13.0.0)", "wheel"] + [[package]] name = "six" version = "1.16.0" @@ -708,6 +758,25 @@ files = [ {file = "tzdata-2022.7.tar.gz", hash = "sha256:fe5f866eddd8b96e9fcba978f8e503c909b19ea7efda11e52e39494bad3a7bfa"}, ] +[[package]] +name = "uvicorn" +version = "0.20.0" +description = "The lightning-fast ASGI server." +category = "main" +optional = false +python-versions = ">=3.7" +files = [ + {file = "uvicorn-0.20.0-py3-none-any.whl", hash = "sha256:c3ed1598a5668208723f2bb49336f4509424ad198d6ab2615b7783db58d919fd"}, + {file = "uvicorn-0.20.0.tar.gz", hash = "sha256:a4e12017b940247f836bc90b72e725d7dfd0c8ed1c51eb365f5ba30d9f5127d8"}, +] + +[package.dependencies] +click = ">=7.0" +h11 = ">=0.8" + +[package.extras] +standard = ["colorama (>=0.4)", "httptools (>=0.5.0)", "python-dotenv (>=0.13)", "pyyaml (>=5.1)", "uvloop (>=0.14.0,!=0.15.0,!=0.15.1)", "watchfiles (>=0.13)", "websockets (>=10.4)"] + [[package]] name = "werkzeug" version = "2.2.2" @@ -745,4 +814,4 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "4e2d507e5549e97f894ebcaf0781c1fa54acf3444583510c36d3807e6c4761d9" +content-hash = "fd85e51c8fb99824a433b451c9712b7418c13688b9eb0e8ca6c51768f544e48f" diff --git a/pyproject.toml b/pyproject.toml index 9ffc259..fffd1ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "timetracker" -version = "0.1.0" +version = "0.1.1" description = "A simple time tracker." authors = ["Lukáš Kucharczyk "] license = "GPL" @@ -9,6 +9,8 @@ readme = "README.md" [tool.poetry.dependencies] python = "^3.10" django = "^4.1.4" +gunicorn = "^20.1.0" +uvicorn = "^0.20.0" [tool.poetry.group.dev.dependencies] black = "^22.12.0" diff --git a/src/web/tracker/templatetags/version.py b/src/web/tracker/templatetags/version.py index 2bbfc25..be57455 100644 --- a/src/web/tracker/templatetags/version.py +++ b/src/web/tracker/templatetags/version.py @@ -1,4 +1,5 @@ from django import template +from django.conf import settings import time import os @@ -9,7 +10,11 @@ register = template.Library() def version_date(): return time.strftime( "%d-%b-%Y %H:%m", - time.gmtime(os.path.getmtime(os.path.abspath(os.path.join(".git")))), + time.gmtime( + os.path.getmtime( + os.path.abspath(os.path.join(settings.BASE_DIR, "..", "..", ".git")) + ) + ), ) diff --git a/src/web/web/settings.py b/src/web/web/settings.py index 9555f10..2010421 100644 --- a/src/web/web/settings.py +++ b/src/web/web/settings.py @@ -34,7 +34,6 @@ ALLOWED_HOSTS = ["*"] INSTALLED_APPS = [ "tracker.apps.TrackerConfig", - "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", @@ -44,6 +43,7 @@ INSTALLED_APPS = [ if DEBUG: INSTALLED_APPS.append("django_extensions") + INSTALLED_APPS.append("django.contrib.admin") MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", @@ -123,7 +123,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/4.1/howto/static-files/ STATIC_URL = "static/" -STATIC_ROOT = BASE_DIR / "/static" +STATIC_ROOT = BASE_DIR / "static" # Default primary key field type # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field -- 2.40.1 From 270d9f7296e5ca0eb2e757fa6b60edea115af1ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 7 Jan 2023 21:58:22 +0100 Subject: [PATCH 2/7] Ignore static folder when building container --- .dockerignore | 1 + Dockerfile | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..601036a --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +src/web/static/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5838e0e..c3fdea6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,10 +6,11 @@ RUN npm install && \ FROM python:3.10.9-alpine -ENV VERSION_NUMBER 0.1.1-3-gd029fda +ENV VERSION_NUMBER 0.1.1-4-g2939b4a ENV PROD 1 RUN apk add \ + bash \ vim \ curl \ caddy -- 2.40.1 From 25a58c273225756ede0fe12f45db3a15ee76d714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 7 Jan 2023 21:59:09 +0100 Subject: [PATCH 3/7] Be more explicit in docker-compose.yml --- Dockerfile | 2 +- docker-compose.yml | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index c3fdea6..b5c8a70 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN npm install && \ FROM python:3.10.9-alpine -ENV VERSION_NUMBER 0.1.1-4-g2939b4a +ENV VERSION_NUMBER 0.1.1-5-g270d9f7 ENV PROD 1 RUN apk add \ diff --git a/docker-compose.yml b/docker-compose.yml index 4f21b18..5934edb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,10 @@ --- -version: "2.1" services: timetracker: image: registry.kucharczyk.xyz/timetracker - build: Dockerfile + build: + context: . + dockerfile: Dockerfile container_name: timetracker environment: - TZ=Europe/Prague -- 2.40.1 From 1ccfdc321aaf98062db0b5d3d4d200f70a26b105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 7 Jan 2023 21:59:17 +0100 Subject: [PATCH 4/7] Start caddy in the background --- Dockerfile | 2 +- entrypoint.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index b5c8a70..1dfaabd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN npm install && \ FROM python:3.10.9-alpine -ENV VERSION_NUMBER 0.1.1-5-g270d9f7 +ENV VERSION_NUMBER 0.1.1-6-g25a58c2 ENV PROD 1 RUN apk add \ diff --git a/entrypoint.sh b/entrypoint.sh index 1955a36..e2a5970 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -8,6 +8,6 @@ echo "Collect static files" poetry run python src/web/manage.py collectstatic echo "Starting server" -caddy run +caddy start cd src/web || exit poetry run python -m gunicorn --bind 0.0.0.0:8001 web.asgi:application -k uvicorn.workers.UvicornWorker -- 2.40.1 From f7b69f770449c2a108b64661ab56099c9bafc4df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 7 Jan 2023 21:59:34 +0100 Subject: [PATCH 5/7] Add more utilities to Makefile --- Dockerfile | 2 +- Makefile | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1dfaabd..ac4c717 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN npm install && \ FROM python:3.10.9-alpine -ENV VERSION_NUMBER 0.1.1-6-g25a58c2 +ENV VERSION_NUMBER 0.1.1-7-g1ccfdc3 ENV PROD 1 RUN apk add \ diff --git a/Makefile b/Makefile index f678dcd..ca3c71d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,3 @@ -.PHONY: createsuperuser shell cleanstatic - all: css migrate initialize: npm css migrate sethookdir loadplatforms @@ -24,8 +22,11 @@ migrate: makemigrations dev: migrate sethookdir poetry run python src/web/manage.py runserver_plus +caddy: + caddy run --watch + dev-prod: migrate collectstatic sethookdir - cd src/web/; poetry run python -m gunicorn --bind 0.0.0.0:8001 web.asgi:application -k uvicorn.workers.UvicornWorker + cd src/web/; PROD=1 poetry run python -m gunicorn --bind 0.0.0.0:8001 web.asgi:application -k uvicorn.workers.UvicornWorker dumptracker: poetry run python src/web/manage.py dumpdata --format yaml tracker --output tracker_fixture.yaml @@ -60,4 +61,4 @@ date: cleanstatic: rm -r src/web/static/* -clean: cleanstatic \ No newline at end of file +clean: cleanstatic -- 2.40.1 From 0d20b543b0294dcf5ee160419ba780eaa9e3120e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 7 Jan 2023 21:59:54 +0100 Subject: [PATCH 6/7] Do not load the admin interface in prod --- Dockerfile | 2 +- src/web/web/urls.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index ac4c717..9ace161 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN npm install && \ FROM python:3.10.9-alpine -ENV VERSION_NUMBER 0.1.1-7-g1ccfdc3 +ENV VERSION_NUMBER 0.1.1-8-gf7b69f7 ENV PROD 1 RUN apk add \ diff --git a/src/web/web/urls.py b/src/web/web/urls.py index 33d8170..8e43fbf 100644 --- a/src/web/web/urls.py +++ b/src/web/web/urls.py @@ -16,10 +16,13 @@ Including another URLconf from django.contrib import admin from django.urls import include, path from django.views.generic import RedirectView +from django.conf import settings urlpatterns = [ - path("admin/", admin.site.urls), path("", RedirectView.as_view(url="/tracker/list-sessions")), path("tracker/", include("tracker.urls")), ] + +if settings.DEBUG: + urlpatterns.append(path("admin/", admin.site.urls)) -- 2.40.1 From c9b2d5bd8d709b39e7855e628524d357447567f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Sat, 7 Jan 2023 22:08:57 +0100 Subject: [PATCH 7/7] Update changelog --- CHANGELOG.md | 4 ++++ Dockerfile | 2 +- pyproject.toml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60fa0c2..f71930a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.2 / 2023-01-07 22:05+01:00 + +* Switch to Uvicorn/Gunicorn + Caddy (https://git.kucharczyk.xyz/lukas/timetracker/pulls/4) + ## 0.1.1 / 2023-01-05 23:26+01:00 * Order by timestamp_start by default * Add pre-commit hook to update version diff --git a/Dockerfile b/Dockerfile index 9ace161..3439f94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN npm install && \ FROM python:3.10.9-alpine -ENV VERSION_NUMBER 0.1.1-8-gf7b69f7 +ENV VERSION_NUMBER 0.1.2 ENV PROD 1 RUN apk add \ diff --git a/pyproject.toml b/pyproject.toml index fffd1ed..55795fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "timetracker" -version = "0.1.1" +version = "0.1.2" description = "A simple time tracker." authors = ["Lukáš Kucharczyk "] license = "GPL" -- 2.40.1