2020-09-02 22:04:12 +00:00
|
|
|
"""Gunicorn config"""
|
2020-11-11 13:48:19 +00:00
|
|
|
import os
|
2022-02-01 14:01:39 +00:00
|
|
|
from hashlib import sha512
|
2020-09-11 21:21:11 +00:00
|
|
|
from multiprocessing import cpu_count
|
2022-05-29 19:45:25 +00:00
|
|
|
from os import makedirs
|
|
|
|
from pathlib import Path
|
2022-04-28 19:50:03 +00:00
|
|
|
from tempfile import gettempdir
|
2022-05-29 19:45:25 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2020-09-11 21:21:11 +00:00
|
|
|
|
2021-04-07 14:16:17 +00:00
|
|
|
from kubernetes.config.incluster_config import SERVICE_HOST_ENV_NAME
|
2022-05-29 19:45:25 +00:00
|
|
|
from prometheus_client.values import MultiProcessValue
|
2020-09-02 22:04:12 +00:00
|
|
|
|
2022-02-01 14:01:39 +00:00
|
|
|
from authentik import get_full_version
|
|
|
|
from authentik.lib.config import CONFIG
|
2023-09-02 15:58:37 +00:00
|
|
|
from authentik.lib.logging import get_logger_config
|
2022-02-01 14:01:39 +00:00
|
|
|
from authentik.lib.utils.http import get_http_session
|
|
|
|
from authentik.lib.utils.reflection import get_env
|
2023-05-25 16:52:21 +00:00
|
|
|
from authentik.root.install_id import get_install_id_raw
|
2022-05-29 19:45:25 +00:00
|
|
|
from lifecycle.worker import DjangoUvicornWorker
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from gunicorn.arbiter import Arbiter
|
2022-02-01 14:01:39 +00:00
|
|
|
|
2022-05-29 19:45:25 +00:00
|
|
|
_tmp = Path(gettempdir())
|
2021-12-11 18:55:09 +00:00
|
|
|
worker_class = "lifecycle.worker.DjangoUvicornWorker"
|
2022-05-29 19:45:25 +00:00
|
|
|
worker_tmp_dir = str(_tmp.joinpath("authentik_worker_tmp"))
|
|
|
|
prometheus_tmp_dir = str(_tmp.joinpath("authentik_prometheus_tmp"))
|
2020-09-02 22:04:12 +00:00
|
|
|
|
2022-05-29 19:45:25 +00:00
|
|
|
makedirs(worker_tmp_dir, exist_ok=True)
|
|
|
|
makedirs(prometheus_tmp_dir, exist_ok=True)
|
2020-11-11 13:48:19 +00:00
|
|
|
|
2023-09-02 15:58:37 +00:00
|
|
|
bind = f"unix://{str(_tmp.joinpath('authentik-core.sock'))}"
|
|
|
|
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authentik.root.settings")
|
|
|
|
os.environ.setdefault("PROMETHEUS_MULTIPROC_DIR", prometheus_tmp_dir)
|
|
|
|
|
2022-01-26 09:04:58 +00:00
|
|
|
max_requests = 1000
|
|
|
|
max_requests_jitter = 50
|
|
|
|
|
2023-09-02 15:58:37 +00:00
|
|
|
logconfig_dict = get_logger_config()
|
2020-09-11 21:21:11 +00:00
|
|
|
|
|
|
|
# if we're running in kubernetes, use fixed workers because we can scale with more pods
|
|
|
|
# otherwise (assume docker-compose), use as much as we can
|
2021-04-07 14:16:17 +00:00
|
|
|
if SERVICE_HOST_ENV_NAME in os.environ:
|
2021-11-29 13:27:55 +00:00
|
|
|
default_workers = 2
|
2020-09-11 21:21:11 +00:00
|
|
|
else:
|
2021-08-05 08:15:31 +00:00
|
|
|
default_workers = max(cpu_count() * 0.25, 1) + 1 # Minimum of 2 workers
|
2021-11-29 13:27:55 +00:00
|
|
|
|
2023-07-31 17:34:59 +00:00
|
|
|
workers = CONFIG.get_int("web.workers", default_workers)
|
|
|
|
threads = CONFIG.get_int("web.threads", 4)
|
2022-01-16 12:57:07 +00:00
|
|
|
|
2023-01-15 16:02:31 +00:00
|
|
|
|
2022-05-29 19:45:25 +00:00
|
|
|
def post_fork(server: "Arbiter", worker: DjangoUvicornWorker):
|
|
|
|
"""Tell prometheus to use worker number instead of process ID for multiprocess"""
|
|
|
|
from prometheus_client import values
|
|
|
|
|
|
|
|
values.ValueClass = MultiProcessValue(lambda: worker._worker_id)
|
|
|
|
|
|
|
|
|
|
|
|
def worker_exit(server: "Arbiter", worker: DjangoUvicornWorker):
|
2022-01-16 12:57:07 +00:00
|
|
|
"""Remove pid dbs when worker is shutdown"""
|
|
|
|
from prometheus_client import multiprocess
|
|
|
|
|
2022-05-29 19:45:25 +00:00
|
|
|
multiprocess.mark_process_dead(worker._worker_id)
|
|
|
|
|
|
|
|
|
|
|
|
def on_starting(server: "Arbiter"):
|
|
|
|
"""Attach a set of IDs that can be temporarily re-used.
|
|
|
|
Used on reloads when each worker exists twice."""
|
|
|
|
server._worker_id_overload = set()
|
|
|
|
|
|
|
|
|
|
|
|
def nworkers_changed(server: "Arbiter", new_value, old_value):
|
|
|
|
"""Gets called on startup too.
|
|
|
|
Set the current number of workers. Required if we raise the worker count
|
|
|
|
temporarily using TTIN because server.cfg.workers won't be updated and if
|
|
|
|
one of those workers dies, we wouldn't know the ids go that far."""
|
|
|
|
server._worker_id_current_workers = new_value
|
|
|
|
|
|
|
|
|
|
|
|
def _next_worker_id(server: "Arbiter"):
|
|
|
|
"""If there are IDs open for re-use, take one. Else look for a free one."""
|
|
|
|
if server._worker_id_overload:
|
|
|
|
return server._worker_id_overload.pop()
|
|
|
|
|
|
|
|
in_use = set(w._worker_id for w in tuple(server.WORKERS.values()) if w.alive)
|
|
|
|
free = set(range(1, server._worker_id_current_workers + 1)) - in_use
|
|
|
|
|
|
|
|
return free.pop()
|
|
|
|
|
|
|
|
|
|
|
|
def on_reload(server: "Arbiter"):
|
|
|
|
"""Add a full set of ids into overload so it can be re-used once."""
|
|
|
|
server._worker_id_overload = set(range(1, server.cfg.workers + 1))
|
|
|
|
|
|
|
|
|
|
|
|
def pre_fork(server: "Arbiter", worker: DjangoUvicornWorker):
|
|
|
|
"""Attach the next free worker_id before forking off."""
|
|
|
|
worker._worker_id = _next_worker_id(server)
|
2022-02-01 14:01:39 +00:00
|
|
|
|
|
|
|
|
2023-07-19 21:13:22 +00:00
|
|
|
if not CONFIG.get_bool("disable_startup_analytics", False):
|
2022-02-01 14:01:39 +00:00
|
|
|
env = get_env()
|
|
|
|
should_send = env not in ["dev", "ci"]
|
|
|
|
if should_send:
|
|
|
|
try:
|
|
|
|
get_http_session().post(
|
|
|
|
"https://goauthentik.io/api/event",
|
|
|
|
json={
|
|
|
|
"domain": "authentik",
|
|
|
|
"name": "pageview",
|
|
|
|
"referrer": get_full_version(),
|
|
|
|
"url": (
|
|
|
|
f"http://localhost/{env}?utm_source={get_full_version()}&utm_medium={env}"
|
|
|
|
),
|
|
|
|
},
|
|
|
|
headers={
|
2023-05-25 16:52:21 +00:00
|
|
|
"User-Agent": sha512(get_install_id_raw().encode("ascii")).hexdigest()[:16],
|
2022-02-01 14:01:39 +00:00
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
timeout=5,
|
|
|
|
)
|
2023-04-18 11:28:19 +00:00
|
|
|
# pylint: disable=broad-exception-caught
|
|
|
|
except Exception: # nosec
|
2022-02-01 14:01:39 +00:00
|
|
|
pass
|
2023-06-28 14:44:50 +00:00
|
|
|
|
2023-07-19 21:13:22 +00:00
|
|
|
if CONFIG.get_bool("remote_debug"):
|
2023-06-28 14:44:50 +00:00
|
|
|
import debugpy
|
|
|
|
|
|
|
|
debugpy.listen(("0.0.0.0", 6800)) # nosec
|