Use caddy with uWSGI in docker-compose usage

pull/11/head
JensDiemer 2020-10-25 19:01:31 +01:00
rodzic 021bd59c23
commit 9ddc703e20
14 zmienionych plików z 167 dodań i 89 usunięć

3
.gitignore vendored
Wyświetl plik

@ -20,6 +20,9 @@
# docker-compose usage:
volumes
# Django
secret.txt
# Coverage HTML Report files:
htmlcov

Wyświetl plik

@ -96,7 +96,6 @@ create-starter: ## Create starter file.
##############################################################################
# docker-compose usage
check-compose:
@if [[ "$(shell poetry run docker-compose --version 2>/dev/null)" = *"docker-compose version"* ]] ; \
then \
@ -137,6 +136,14 @@ shell_inventory: ## Go into bash shell in inventory container
shell_postgres: ## Go into bash shell in postgres container
./compose.sh exec postgres /bin/bash
shell_caddy: ## Go into bash shell in caddy container
./compose.sh exec caddy /bin/ash
##############################################################################
caddy_environ: ## Prints the caddy environment
./compose.sh exec caddy /usr/bin/caddy environ
##############################################################################
logs: ## Display docker logs from all containers
@ -145,9 +152,12 @@ logs: ## Display docker logs from all containers
logs_postgres: ## Display docker logs from postgres container
./compose.sh logs --tail=500 --follow postgres
logs_inventory: ## Display docker logs from postgres container
logs_inventory: ## Display docker logs from inventory container
./compose.sh logs --tail=500 --follow inventory
logs_caddy: ## Display docker logs from caddy container
./compose.sh logs --tail=500 --follow caddy
##############################################################################
dbbackup: ## Backup database
@ -175,4 +185,8 @@ reload_inventory: ## Reload server in "inventory" container
./compose.sh exec inventory ./docker/kill_python.sh
./compose.sh logs --tail=500 --follow inventory
restart_caddy: ## Restart caddy container
./compose.sh stop caddy
$(MAKE) up
.PHONY: help install lint fix test publish

Wyświetl plik

@ -42,7 +42,7 @@ any many more... ;)
There exists two kind of installation/usage:
* local virtualenv (without docker)
* docker-compose
* production use with docker-compose
see below
@ -84,7 +84,6 @@ restart Restart all containers
}}}
=== local install without docker
{{{
@ -115,15 +114,33 @@ Install docker, e.g.: https://docs.docker.com/engine/install/ubuntu/
{{{
# Install "docker-compose" via poetry extras:
~/PyInventory$ make install-compose
}}}
# Start containers via docker-compose:
Create a {{{.env}}} file with these content, e.g.:
{{{
# Public domain or "localhost" for local testing:
HOSTNAME=localhost
# eMail address for Let's encrypt (Use "internal" for self signed https certificates):
LETSENCRYPT_EMAIL=internal
}}}
e.g. in production:
{{{
HOSTNAME=domain.tld
LETSENCRYPT_EMAIL=webmaster@domain.tld
}}}
Start containers via docker-compose:
{{{
~/PyInventory$ make up
}}}
Notes:
Notes: At the first start it takes a little while until the database is created
* at the first start it takes a little while until the database is created
* The web page is available via: {{{http://localhost:8000/}}}
Create first super user:
{{{
~/PyInventory$ make docker_createsuperuser
}}}
== Screenshots

Wyświetl plik

@ -77,7 +77,7 @@ There exists two kind of installation/usage:
* local virtualenv (without docker)
* docker-compose
* production use with docker-compose
see below
@ -119,16 +119,6 @@ prepare
dbrestore Restore a database backup
restart Restart all containers
.env
====
Create a ``.env`` file, for some settings, e.g.:
::
# enable Django-Debug-Toolbar:
ENABLE_DJDT=1
local install without docker
============================
@ -161,15 +151,37 @@ Install docker, e.g.: `https://docs.docker.com/engine/install/ubuntu/ <https://d
# Install "docker-compose" via poetry extras:
~/PyInventory$ make install-compose
Create a ``.env`` file with these content, e.g.:
::
# Public domain or "localhost" for local testing:
HOSTNAME=localhost
# Start containers via docker-compose:
# eMail address for Let's encrypt (Use "internal" for self signed https certificates):
LETSENCRYPT_EMAIL=internal
e.g. in production:
::
HOSTNAME=domain.tld
LETSENCRYPT_EMAIL=webmaster@domain.tld
Start containers via docker-compose:
::
~/PyInventory$ make up
Notes:
Notes: At the first start it takes a little while until the database is created
* at the first start it takes a little while until the database is created
Create first super user:
* The web page is available via: ``http://localhost:8000/``
::
~/PyInventory$ make docker_createsuperuser
-----------
Screenshots
@ -233,6 +245,10 @@ history
* `compare v0.2.0...master <https://github.com/jedie/PyInventory/compare/v0.2.0...master>`_ **dev**
* Remove usage of ``.env`` file
* split settings for local development and production use
* tbc
* `v0.2.0 - 24.10.2020 <https://github.com/jedie/PyInventory/compare/v0.1.0...v0.2.0>`_
@ -288,4 +304,4 @@ donation
------------
``Note: this file is generated from README.creole 2020-10-24 19:34:25 with "python-creole"``
``Note: this file is generated from README.creole 2020-10-25 19:34:37 with "python-creole"``

Wyświetl plik

@ -1,5 +1,12 @@
#!/bin/sh
#!/bin/bash
set -ex
set -e
if [[ -f .env ]]; then
echo "Read '.env' file..."
source .env
fi
set -x
exec poetry run docker-compose "$@"

Wyświetl plik

@ -1,21 +1,36 @@
version: "3.7"
services:
caddy: # https://hub.docker.com/_/caddy
image: caddy:2-alpine
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./docker/Caddyfile:/etc/caddy/Caddyfile
- ./volumes/static/:/srv/:ro
environment:
- HOSTNAME=${HOSTNAME:-localhost}
- LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL:-internal}
inventory:
build:
context: .
dockerfile: Dockerfile
restart: "no"
restart: unless-stopped
hostname: inventory
ports:
- "8000:8000"
- "8000"
env_file: ./docker/common.env
environment:
- DJANGO_SETTINGS_MODULE=inventory_project.settings.local_docker
- DJANGO_SETTINGS_MODULE=inventory_project.settings.docker
- HOSTNAME=${HOSTNAME:-localhost}
links:
- postgres:postgres
depends_on:
- postgres
- caddy
volumes:
- .:/PyInventory
- ./volumes/static/:/PyInventory/static/:rw
@ -26,10 +41,10 @@ services:
postgres:
# https://hub.docker.com/_/postgres
image: postgres:11-alpine
restart: "no"
restart: unless-stopped
hostname: postgres
ports:
- "5432:5432"
- "5432"
env_file: ./docker/common.env
environment:
- POSTGRES_HOST_AUTH_METHOD=trust

9
docker/Caddyfile 100644
Wyświetl plik

@ -0,0 +1,9 @@
# https://caddyserver.com/docs/caddyfile
{
admin off
}
{$HOSTNAME} {
tls {$LETSENCRYPT_EMAIL}
reverse_proxy inventory:8000
}

Wyświetl plik

@ -27,8 +27,20 @@ echo "$(date +%c) - ${0}"
./manage.sh makemigrations
./manage.sh migrate
./manage.sh runserver 0.0.0.0:8000
echo "runserver terminated with exit code: $?"
exec poetry run uwsgi \
--http inventory:8000 \
--chdir /PyInventory \
--wsgi-file /PyInventory/inventory_project/wsgi.py \
--static-map /static=/PyInventory/static \
--master \
--processes 2 \
--threads 2 \
--ignore-sigpipe \
--ignore-write-errors \
--disable-write-exception \
--http-auto-chunked \
--http-keepalive
echo "uwsgi terminated with exit code: $?"
sleep 3
exit 1
)

Wyświetl plik

@ -12,6 +12,16 @@ from django.utils.translation import ugettext_lazy as _
BASE_PATH = __Path(__file__).resolve().parent.parent.parent
# SECURITY WARNING: keep the secret key used in production secret!
__SECRET_FILE = __Path(BASE_PATH, 'secret.txt').resolve()
if not __SECRET_FILE.is_file():
print(f'Generate {__SECRET_FILE}')
from secrets import token_urlsafe as __token_urlsafe
__SECRET_FILE.open('w').write(__token_urlsafe(128))
SECRET_KEY = __SECRET_FILE.open('r').read().strip()
# Application definition
INSTALLED_APPS = [

Wyświetl plik

@ -1,21 +1,25 @@
"""
Django settings for production usage
Django settings for docker usage (local and production)
"""
import os as __os
from pathlib import Path as __Path
from inventory_project.settings.base import * # noqa
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
HOSTNAME = __os.environ['HOSTNAME']
INTERNAL_IPS = ()
if HOSTNAME != 'localhost':
print(f'Production mode on domain: {HOSTNAME!r}')
DEBUG = False
INTERNAL_IPS = ()
else:
print('Local development mode')
DEBUG = True
INTERNAL_IPS = ('127.0.0.1', '0.0.0.0', 'localhost')
ALLOWED_HOSTS = ('domain.tld',) # TODO
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = __Path('secret.txt').resolve().open('r').read().strip()
ALLOWED_HOSTS = (HOSTNAME,)
DATABASES = {
'default': {

Wyświetl plik

@ -18,8 +18,6 @@ INTERNAL_IPS = ('127.0.0.1', '0.0.0.0', 'localhost')
ALLOWED_HOSTS = INTERNAL_IPS
SECRET_KEY = 'no secret for local development'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',

Wyświetl plik

@ -1,20 +0,0 @@
"""
Django settings for local development with docker-compose usage
"""
import os as __os
from inventory_project.settings.local import * # noqa
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': __os.environ['DB_NAME'],
'USER': __os.environ['DB_USER'],
'PASSWORD': __os.environ['DB_PASS'],
'HOST': __os.environ['DB_HOST'],
'PORT': __os.environ['DB_PORT'],
'DEBUG_NAME': 'default',
'CONN_MAX_AGE': 600,
},
}

43
poetry.lock wygenerowano
Wyświetl plik

@ -471,20 +471,6 @@ python-versions = ">=3.6"
[package.dependencies]
astor = "*"
[[package]]
name = "gunicorn"
version = "20.0.4"
description = "WSGI HTTP Server for UNIX"
category = "main"
optional = false
python-versions = ">=3.4"
[package.extras]
eventlet = ["eventlet (>=0.9.7)"]
gevent = ["gevent (>=0.13)"]
setproctitle = ["setproctitle"]
tornado = ["tornado (>=0.2)"]
[[package]]
name = "icdiff"
version = "1.9.1"
@ -749,7 +735,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
[[package]]
name = "pygments"
version = "2.7.1"
version = "2.7.2"
description = "Pygments is a syntax highlighting package written in Python."
category = "dev"
optional = false
@ -1118,9 +1104,17 @@ brotli = ["brotlipy (>=0.6.0)"]
secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"]
socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7,<2.0)"]
[[package]]
name = "uwsgi"
version = "2.0.19.1"
description = "The uWSGI server"
category = "main"
optional = false
python-versions = "*"
[[package]]
name = "virtualenv"
version = "20.0.35"
version = "20.1.0"
description = "Virtual Python Environment builder"
category = "dev"
optional = false
@ -1191,7 +1185,7 @@ postgres = ["psycopg2-binary"]
[metadata]
lock-version = "1.1"
python-versions = ">=3.7,<4.0.0"
content-hash = "d07f05b3cb200bb30974923869838af5e96a1935fdcbaa2e536f03f046583d90"
content-hash = "0ab17efa6bba93be2850b753353a4dcf990d0b89cec16a508701a42011fe5862"
[metadata.files]
appdirs = [
@ -1441,10 +1435,6 @@ flynt = [
{file = "flynt-0.55-py3-none-any.whl", hash = "sha256:df62c89d34e1318fa40b865343a70dcdc970be849f610c9b79af43cdbc40877b"},
{file = "flynt-0.55.tar.gz", hash = "sha256:2e24775b15b8cc4965703e1f9b21e4658533a8bfb57a4782125dff2faa124e83"},
]
gunicorn = [
{file = "gunicorn-20.0.4-py2.py3-none-any.whl", hash = "sha256:cd4a810dd51bf497552cf3f863b575dabd73d6ad6a91075b65936b151cbf4f9c"},
{file = "gunicorn-20.0.4.tar.gz", hash = "sha256:1904bb2b8a43658807108d59c3f3d56c2b6121a701161de0ddf9ad140073c626"},
]
icdiff = [
{file = "icdiff-1.9.1.tar.gz", hash = "sha256:66972dd03318da55280991db375d3ef6b66d948c67af96c1ebdb21587e86655e"},
]
@ -1570,8 +1560,8 @@ pyflakes = [
{file = "pyflakes-2.2.0.tar.gz", hash = "sha256:35b2d75ee967ea93b55750aa9edbbf72813e06a66ba54438df2cfac9e3c27fc8"},
]
pygments = [
{file = "Pygments-2.7.1-py3-none-any.whl", hash = "sha256:307543fe65c0947b126e83dd5a61bd8acbd84abec11f43caebaf5534cbc17998"},
{file = "Pygments-2.7.1.tar.gz", hash = "sha256:926c3f319eda178d1bd90851e4317e6d8cdb5e292a3386aac9bd75eca29cf9c7"},
{file = "Pygments-2.7.2-py3-none-any.whl", hash = "sha256:88a0bbcd659fcb9573703957c6b9cff9fab7295e6e76db54c9d00ae42df32773"},
{file = "Pygments-2.7.2.tar.gz", hash = "sha256:381985fcc551eb9d37c52088a32914e00517e57f4a21609f48141ba08e193fa0"},
]
pynacl = [
{file = "PyNaCl-1.4.0-cp27-cp27m-macosx_10_10_x86_64.whl", hash = "sha256:ea6841bc3a76fa4942ce00f3bda7d436fda21e2d91602b9e21b7ca9ecab8f3ff"},
@ -1723,9 +1713,12 @@ urllib3 = [
{file = "urllib3-1.25.11-py2.py3-none-any.whl", hash = "sha256:f5321fbe4bf3fefa0efd0bfe7fb14e90909eb62a48ccda331726b4319897dd5e"},
{file = "urllib3-1.25.11.tar.gz", hash = "sha256:8d7eaa5a82a1cac232164990f04874c594c9453ec55eef02eab885aa02fc17a2"},
]
uwsgi = [
{file = "uWSGI-2.0.19.1.tar.gz", hash = "sha256:faa85e053c0b1be4d5585b0858d3a511d2cd10201802e8676060fd0a109e5869"},
]
virtualenv = [
{file = "virtualenv-20.0.35-py2.py3-none-any.whl", hash = "sha256:0ebc633426d7468664067309842c81edab11ae97fcaf27e8ad7f5748c89b431b"},
{file = "virtualenv-20.0.35.tar.gz", hash = "sha256:2a72c80fa2ad8f4e2985c06e6fc12c3d60d060e410572f553c90619b0f6efaf3"},
{file = "virtualenv-20.1.0-py2.py3-none-any.whl", hash = "sha256:b0011228208944ce71052987437d3843e05690b2f23d1c7da4263fde104c97a2"},
{file = "virtualenv-20.1.0.tar.gz", hash = "sha256:b8d6110f493af256a40d65e29846c69340a947669eec8ce784fcf3dd3af28380"},
]
webencodings = [
{file = "webencodings-0.5.1-py2.py3-none-any.whl", hash = "sha256:a0af1213f3c2226497a97e2b3aa01a7e4bee4f403f95be16fc9acd2947514a78"},

Wyświetl plik

@ -38,7 +38,7 @@ readme='README.rst'
python = ">=3.7,<4.0.0"
colorama = "*" # Console colors under windows: https://pypi.org/project/colorama/
colorlog = "*" # https://github.com/borntyping/python-colorlog
gunicorn = "*" # https://gunicorn.org/
uWSGI = "*"
#
# https://www.djangoproject.com/download/#supported-versions
# v2.2 LTS - extended support until April 2022