From 961252ef26dcaee38edc6ac65fce4f8e8d49e43f Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Tue, 18 Oct 2022 00:23:10 +0000 Subject: [PATCH] rework uwsgi conf --- .../management/commands/create_uwsgi_conf.py | 60 +++++++++++++++++++ docker/containers/tactical/entrypoint.sh | 34 +---------- install.sh | 31 +--------- restore.sh | 30 +--------- update.sh | 39 +----------- 5 files changed, 65 insertions(+), 129 deletions(-) create mode 100644 api/tacticalrmm/core/management/commands/create_uwsgi_conf.py diff --git a/api/tacticalrmm/core/management/commands/create_uwsgi_conf.py b/api/tacticalrmm/core/management/commands/create_uwsgi_conf.py new file mode 100644 index 00000000..159983aa --- /dev/null +++ b/api/tacticalrmm/core/management/commands/create_uwsgi_conf.py @@ -0,0 +1,60 @@ +import configparser +import os +from pathlib import Path + +from django.conf import settings +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = "Generate conf for uwsgi" + + def handle(self, *args, **kwargs): + + self.stdout.write("Creating uwsgi conf...") + + config = configparser.ConfigParser() + + if getattr(settings, "DOCKER_BUILD", False): + home = str(Path(os.getenv("VIRTUAL_ENV"))) # type: ignore + socket = "0.0.0.0:8080" + else: + home = str(settings.BASE_DIR.parents[0] / "env") + socket = str(settings.BASE_DIR / "tacticalrmm.sock") + + config["uwsgi"] = { + "chdir": str(settings.BASE_DIR), + "module": "tacticalrmm.wsgi", + "home": home, + "master": str(getattr(settings, "UWSGI_MASTER", True)).lower(), + "enable-threads": str( + getattr(settings, "UWSGI_ENABLE_THREADS", True) + ).lower(), + "socket": socket, + "harakiri": str(getattr(settings, "UWSGI_HARAKIRI", 300)), + "chmod-socket": str(getattr(settings, "UWSGI_CHMOD_SOCKET", 660)), + "buffer-size": str(getattr(settings, "UWSGI_BUFFER_SIZE", 65535)), + "vacuum": str(getattr(settings, "UWSGI_VACUUM", True)).lower(), + "die-on-term": str(getattr(settings, "UWSGI_DIE_ON_TERM", True)).lower(), + "max-requests": str(getattr(settings, "UWSGI_MAX_REQUESTS", 500)), + "disable-logging": str( + getattr(settings, "UWSGI_DISABLE_LOGGING", True) + ).lower(), + "cheaper-algo": "busyness", + "cheaper": str(getattr(settings, "UWSGI_CHEAPER", 4)), + "cheaper-initial": str(getattr(settings, "UWSGI_CHEAPER_INITIAL", 4)), + "workers": str(getattr(settings, "UWSGI_MAX_WORKERS", 40)), + "cheaper-step": str(getattr(settings, "UWSGI_CHEAPER_STEP", 2)), + "cheaper-overload": str(getattr(settings, "UWSGI_CHEAPER_OVERLOAD", 3)), + "cheaper-busyness-min": str(getattr(settings, "UWSGI_BUSYNESS_MIN", 5)), + "cheaper-busyness-max": str(getattr(settings, "UWSGI_BUSYNESS_MAX", 10)), + } + + if getattr(settings, "UWSGI_DEBUG", False): + config["uwsgi"]["stats"] = "/tmp/stats.socket" + config["uwsgi"]["cheaper-busyness-verbose"] = str(True).lower() + + with open(settings.BASE_DIR / "app.ini", "w") as fp: + config.write(fp) + + self.stdout.write("Created uwsgi conf") diff --git a/docker/containers/tactical/entrypoint.sh b/docker/containers/tactical/entrypoint.sh index 22fd1a29..4bc830e3 100644 --- a/docker/containers/tactical/entrypoint.sh +++ b/docker/containers/tactical/entrypoint.sh @@ -111,37 +111,6 @@ EOF echo "${localvars}" > ${TACTICAL_DIR}/api/tacticalrmm/local_settings.py -uwsgiconf="$(cat << EOF -[uwsgi] -chdir = /opt/tactical/api -module = tacticalrmm.wsgi -home = /opt/venv -master = true -enable-threads = true -socket = 0.0.0.0:8080 -harakiri = 300 -chmod-socket = 660 -buffer-size = 65535 -vacuum = true -die-on-term = true -max-requests = 500 -disable-logging = true -cheaper-algo = busyness -cheaper = 4 -cheaper-initial = 4 -workers = 20 -cheaper-step = 2 -cheaper-overload = 3 -cheaper-busyness-min = 5 -cheaper-busyness-max = 10 -# stats = /tmp/stats.socket # uncomment when debugging -# cheaper-busyness-verbose = true # uncomment when debugging -EOF -)" - - echo "${uwsgiconf}" > ${TACTICAL_DIR}/api/uwsgi.ini - - # run migrations and init scripts python manage.py pre_update_tasks python manage.py migrate --no-input @@ -152,6 +121,7 @@ EOF python manage.py load_community_scripts python manage.py reload_nats python manage.py create_natsapi_conf + python manage.py create_uwsgi_conf python manage.py create_installer_user python manage.py post_update_tasks @@ -173,7 +143,7 @@ fi if [ "$1" = 'tactical-backend' ]; then check_tactical_ready - uwsgi ${TACTICAL_DIR}/api/uwsgi.ini + uwsgi ${TACTICAL_DIR}/api/app.ini fi if [ "$1" = 'tactical-celery' ]; then diff --git a/install.sh b/install.sh index 160e17cc..7f9f6b2e 100644 --- a/install.sh +++ b/install.sh @@ -411,6 +411,7 @@ pip install --no-cache-dir -r /rmm/api/tacticalrmm/requirements.txt python manage.py migrate python manage.py collectstatic --no-input python manage.py create_natsapi_conf +python manage.py create_uwsgi_conf python manage.py load_chocos python manage.py load_community_scripts WEB_VERSION=$(python manage.py get_config webversion) @@ -429,36 +430,6 @@ python manage.py generate_barcode ${RANDBASE} ${djangousername} ${frontenddomain deactivate read -n 1 -s -r -p "Press any key to continue..." -uwsgini="$(cat << EOF -[uwsgi] -chdir = /rmm/api/tacticalrmm -module = tacticalrmm.wsgi -home = /rmm/api/env -master = true -enable-threads = true -socket = /rmm/api/tacticalrmm/tacticalrmm.sock -harakiri = 300 -chmod-socket = 660 -buffer-size = 65535 -vacuum = true -die-on-term = true -max-requests = 500 -disable-logging = true -cheaper-algo = busyness -cheaper = 4 -cheaper-initial = 4 -workers = 20 -cheaper-step = 2 -cheaper-overload = 3 -cheaper-busyness-min = 5 -cheaper-busyness-max = 10 -# stats = /tmp/stats.socket # uncomment when debugging -# cheaper-busyness-verbose = true # uncomment when debugging -EOF -)" -echo "${uwsgini}" > /rmm/api/tacticalrmm/app.ini - - rmmservice="$(cat << EOF [Unit] Description=tacticalrmm uwsgi daemon diff --git a/restore.sh b/restore.sh index 4dda15c6..c7305bfd 100755 --- a/restore.sh +++ b/restore.sh @@ -258,35 +258,6 @@ npm install meshcentral@${MESH_VER} print_green 'Restoring the backend' -uwsgini="$(cat << EOF -[uwsgi] -chdir = /rmm/api/tacticalrmm -module = tacticalrmm.wsgi -home = /rmm/api/env -master = true -enable-threads = true -socket = /rmm/api/tacticalrmm/tacticalrmm.sock -harakiri = 300 -chmod-socket = 660 -buffer-size = 65535 -vacuum = true -die-on-term = true -max-requests = 500 -disable-logging = true -cheaper-algo = busyness -cheaper = 4 -cheaper-initial = 4 -workers = 20 -cheaper-step = 2 -cheaper-overload = 3 -cheaper-busyness-min = 5 -cheaper-busyness-max = 10 -# stats = /tmp/stats.socket # uncomment when debugging -# cheaper-busyness-verbose = true # uncomment when debugging -EOF -)" -echo "${uwsgini}" > /rmm/api/tacticalrmm/app.ini - cp $tmp_dir/rmm/local_settings.py /rmm/api/tacticalrmm/tacticalrmm/ cp $tmp_dir/rmm/env /rmm/web/.env gzip -d $tmp_dir/rmm/debug.log.gz @@ -325,6 +296,7 @@ pip install --no-cache-dir -r /rmm/api/tacticalrmm/requirements.txt python manage.py migrate python manage.py collectstatic --no-input python manage.py create_natsapi_conf +python manage.py create_uwsgi_conf python manage.py reload_nats python manage.py post_update_tasks API=$(python manage.py get_config api) diff --git a/update.sh b/update.sh index 080cca43..de7d05ac 100644 --- a/update.sh +++ b/update.sh @@ -156,38 +156,6 @@ echo "${daphneservice}" | sudo tee /etc/systemd/system/daphne.service > /dev/nul sudo systemctl daemon-reload fi -rm -f /rmm/api/tacticalrmm/app.ini - -uwsgini="$(cat << EOF -[uwsgi] -chdir = /rmm/api/tacticalrmm -module = tacticalrmm.wsgi -home = /rmm/api/env -master = true -enable-threads = true -socket = /rmm/api/tacticalrmm/tacticalrmm.sock -harakiri = 300 -chmod-socket = 660 -buffer-size = 65535 -vacuum = true -die-on-term = true -max-requests = 500 -disable-logging = true -cheaper-algo = busyness -cheaper = 4 -cheaper-initial = 4 -workers = 20 -cheaper-step = 2 -cheaper-overload = 3 -cheaper-busyness-min = 5 -cheaper-busyness-max = 10 -# stats = /tmp/stats.socket # uncomment when debugging -# cheaper-busyness-verbose = true # uncomment when debugging -EOF -)" -echo "${uwsgini}" > /rmm/api/tacticalrmm/app.ini - - if [ ! -f /etc/apt/sources.list.d/nginx.list ]; then osname=$(lsb_release -si); osname=${osname^} osname=$(echo "$osname" | tr '[A-Z]' '[a-z]') @@ -377,17 +345,12 @@ python manage.py reload_nats python manage.py load_chocos python manage.py create_installer_user python manage.py create_natsapi_conf +python manage.py create_uwsgi_conf python manage.py post_update_tasks API=$(python manage.py get_config api) WEB_VERSION=$(python manage.py get_config webversion) deactivate -printf >&2 "${GREEN}Turning off redis aof${NC}\n" -sudo redis-cli config set appendonly no -sudo redis-cli config rewrite -sudo rm -f /var/lib/redis/appendonly.aof - - if [ -d /rmm/web ]; then rm -rf /rmm/web fi