lifecycle: migrate to gunicorn instead of runserver

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-09-13 17:54:23 +02:00
parent a4b3519428
commit 8e59b06611
3 changed files with 12 additions and 10 deletions

View file

@ -68,4 +68,4 @@ migrate:
python -m lifecycle.migrate python -m lifecycle.migrate
run: run:
go run -v cmd/server/main.go WORKERS=1 go run -v cmd/server/main.go

View file

@ -5,7 +5,6 @@ import (
"os/exec" "os/exec"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"goauthentik.io/internal/config"
) )
type GoUnicorn struct { type GoUnicorn struct {
@ -29,10 +28,6 @@ func NewGoUnicorn() *GoUnicorn {
func (g *GoUnicorn) initCmd() { func (g *GoUnicorn) initCmd() {
command := "gunicorn" command := "gunicorn"
args := []string{"-c", "./lifecycle/gunicorn.conf.py", "authentik.root.asgi.app:application"} args := []string{"-c", "./lifecycle/gunicorn.conf.py", "authentik.root.asgi.app:application"}
if config.G.Debug {
command = "python"
args = []string{"manage.py", "runserver", "localhost:8000"}
}
g.log.WithField("args", args).WithField("cmd", command).Debug("Starting gunicorn") g.log.WithField("args", args).WithField("cmd", command).Debug("Starting gunicorn")
g.p = exec.Command(command, args...) g.p = exec.Command(command, args...)
g.p.Env = os.Environ() g.p.Env = os.Environ()

View file

@ -1,5 +1,6 @@
"""Gunicorn config""" """Gunicorn config"""
import os import os
import pwd
import warnings import warnings
from multiprocessing import cpu_count from multiprocessing import cpu_count
@ -7,13 +8,19 @@ import structlog
from kubernetes.config.incluster_config import SERVICE_HOST_ENV_NAME from kubernetes.config.incluster_config import SERVICE_HOST_ENV_NAME
bind = "127.0.0.1:8000" bind = "127.0.0.1:8000"
reload = True
user = "authentik" try:
group = "authentik" pwd.getpwnam("authentik")
user = "authentik"
group = "authentik"
except KeyError:
pass
worker_class = "uvicorn.workers.UvicornWorker" worker_class = "uvicorn.workers.UvicornWorker"
# Docker containers don't have /tmp as tmpfs # Docker containers don't have /tmp as tmpfs
worker_tmp_dir = "/dev/shm" # nosec if os.path.exists("/dev/shm"): # nosec
worker_tmp_dir = "/dev/shm" # nosec
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authentik.root.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authentik.root.settings")
@ -53,7 +60,7 @@ if SERVICE_HOST_ENV_NAME in os.environ:
else: else:
default_workers = max(cpu_count() * 0.25, 1) + 1 # Minimum of 2 workers default_workers = max(cpu_count() * 0.25, 1) + 1 # Minimum of 2 workers
workers = int(os.environ.get("WORKERS", default_workers)) workers = int(os.environ.get("WORKERS", default_workers))
threads = 4 threads = int(os.environ.get("THREADS", 4))
warnings.filterwarnings( warnings.filterwarnings(
"ignore", "ignore",