From 75099ca05eaaf0800dee67ec6ed4c5edd8625990 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 15 Aug 2020 02:40:03 -0300 Subject: [PATCH] Removed hardcoded paths, make workflow changes I removed some hardcoded paths for logging and where the default db should be located. I also organized deployment stuff a bit --- .../CalibreWebCompanion/settings.py | 68 ++++++++++--------- CalibreWebCompanion/gunicorn.conf.py | 17 ++++- .../library/context_processors.py | 2 +- CalibreWebCompanion/manage.py | 0 Dockerfile => deployment/Dockerfile | 6 +- deployment/nginx.conf | 10 +-- .../supervisord.conf | 0 7 files changed, 60 insertions(+), 43 deletions(-) mode change 100644 => 100755 CalibreWebCompanion/manage.py rename Dockerfile => deployment/Dockerfile (87%) rename supervisord.conf => deployment/supervisord.conf (100%) diff --git a/CalibreWebCompanion/CalibreWebCompanion/settings.py b/CalibreWebCompanion/CalibreWebCompanion/settings.py index 8648859..14e99e5 100644 --- a/CalibreWebCompanion/CalibreWebCompanion/settings.py +++ b/CalibreWebCompanion/CalibreWebCompanion/settings.py @@ -66,36 +66,37 @@ STATIC_ROOT = BASE_DIR + "/static/" -# logfile = usersettings["LOGFILE"] -# LOGGING = { -# "version": 1, -# "disable_existing_loggers": False, -# "root": {"level": "INFO", "handlers": ["file"]}, -# "handlers": { -# "file": { -# "level": "INFO", -# "class": "logging.FileHandler", -# "filename": usersettings["LOGFILE"], -# "formatter": "app", -# }, -# }, -# "loggers": { -# "django": { -# "handlers": ["file"], -# "level": "INFO", -# "propagate": True -# }, -# }, -# "formatters": { -# "app": { -# "format": ( -# u"%(asctime)s [%(levelname)-8s] " -# "(%(module)s.%(funcName)s) %(message)s" -# ), -# "datefmt": "%Y-%m-%d %H:%M:%S", -# }, -# }, -# } +logfile = usersettings["LOGFOLDER"] + "django.log" +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "root": {"level": "INFO", "handlers": ["file"]}, + "handlers": { + "file": { + "level": "INFO", + "class": "logging.FileHandler", + "filename": logfile, + "formatter": "app", + }, + }, + "loggers": { + "django": { + "handlers": ["file"], + "level": "INFO", + "propagate": True + }, + }, + "formatters": { + "app": { + "format": ( + u"%(asctime)s [%(levelname)-8s] " + "(%(module)s.%(funcName)s) %(message)s" + ), + "datefmt": "%Y-%m-%d %H:%M:%S", + }, + }, +} + ## ## ######################################################################## @@ -188,10 +189,15 @@ WSGI_APPLICATION = 'CalibreWebCompanion.wsgi.application' ## DATBASE ## # https://docs.djangoproject.com/en/3.0/ref/settings/#databases +if usersettings["ISDOCKER"]: + defaultdb_path = "/usr/src/app/data/" +else: + defaultdb_path = BASE_DIR + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'NAME': os.path.join(defaultdb_path, 'db.sqlite3'), }, 'calibre': { 'ENGINE': 'django.db.backends.sqlite3', diff --git a/CalibreWebCompanion/gunicorn.conf.py b/CalibreWebCompanion/gunicorn.conf.py index 5a3ca56..0e4ca5f 100644 --- a/CalibreWebCompanion/gunicorn.conf.py +++ b/CalibreWebCompanion/gunicorn.conf.py @@ -1,4 +1,6 @@ import multiprocessing +import os +import json bind = "127.0.0.1:8000" workers = multiprocessing.cpu_count() * 2 + 1 @@ -6,9 +8,20 @@ preload_app = True # By preloading an application you can save some RAM resource keepalive = 5 # daemon = True # Detaches the server from the controlling terminal and enters the background. disabled for now # logging -# errorlog = "/usr/src/app/data/logs/gunicorn_error.log" + +with open("settings.json", "r") as jfile: + settings = json.load(jfile) + +errorlog = settings["LOGFOLDER"] + "/gunicorn_error.log" loglevel = "warning" -# accesslog = "/usr/src/app/data/logs/gunicorn_access.log" +accesslog = settings["LOGFOLDER"] + "/gunicorn_access.log" + +if not os.path.isdir("/usr/src/app/data/logs"): + os.mkdir("/usr/src/app/data/logs") +if not os.path.isfile(errorlog): + os.system('touch {}'.format(errorlog)) +if not os.path.isfile(accesslog): + os.system('touch {}'.format(accesslog)) capture_output = True # debug settings which need to be commented out in prod diff --git a/CalibreWebCompanion/library/context_processors.py b/CalibreWebCompanion/library/context_processors.py index b2fb1af..148cbc3 100644 --- a/CalibreWebCompanion/library/context_processors.py +++ b/CalibreWebCompanion/library/context_processors.py @@ -15,7 +15,7 @@ def filters(request): unique_authors = Author.objects.only('name', "id").annotate(num_books=Count('book')).order_by('name') unique_tags = Tag.objects.annotate(num_books=Count('book')).order_by('name') unique_publishers = Publisher.objects.annotate(num_books=Count('book')).order_by('name') - unique_languages = Language.objects.annotate(num_books=Count('book')).order_by('rating') + unique_languages = Language.objects.annotate(num_books=Count('book')) unique_ratings = Rating.objects.annotate(num_books=Count('book')) unique_series = Series.objects.annotate(num_books=Count('book')).order_by('sort') diff --git a/CalibreWebCompanion/manage.py b/CalibreWebCompanion/manage.py old mode 100644 new mode 100755 diff --git a/Dockerfile b/deployment/Dockerfile similarity index 87% rename from Dockerfile rename to deployment/Dockerfile index bfc991f..3376854 100644 --- a/Dockerfile +++ b/deployment/Dockerfile @@ -11,7 +11,7 @@ RUN pip install -r requirements.txt RUN apk add nginx supervisor # do nginx stuff -RUN adduser -D -g 'www' www +# RUN adduser -D -g 'www' www RUN mkdir -p /run/nginx COPY ./deployment/nginx.conf /etc/nginx/ @@ -19,11 +19,9 @@ COPY ./deployment/nginx.conf /etc/nginx/ COPY ./CalibreWebCompanion ./CalibreWebCompanion ## gunicorn borks started with supervisord -COPY ./supervisord.conf /etc/ +COPY ./deployment/supervisord.conf /etc/ ENTRYPOINT /usr/bin/supervisord -c /etc/supervisord.conf -# create some dirs -RUN mkdir /usr/src/app/logs # docker run --publish 8000:80 \ # -v '/home/massiveatoms/Desktop/logs:/usr/src/app/data' \ diff --git a/deployment/nginx.conf b/deployment/nginx.conf index 2eb8176..df1c00d 100644 --- a/deployment/nginx.conf +++ b/deployment/nginx.conf @@ -3,7 +3,7 @@ worker_processes 1; # user nobody nogroup; # user massiveatoms; # TEMP disabled # user nobody nobody; # for systems with 'nobody' as a group instead -# error_log /home/massiveatoms/Desktop/logs/nginx.log warn; +error_log /usr/src/app/data/logs/nginx.log warn; # pid /var/run/nginx.pid; events { @@ -49,10 +49,10 @@ http { keepalive_timeout 5; # # MASSIVEATOMS - # location /download/ { - # alias "/run/media/massiveatoms/1AEEEA6EEEEA421D/Documents and Settings/MassiveAtoms/Documents/Calibre Library/"; - # # Never forget the fact that this little statement being root instead of alias caused us to lose more than a day troubleshooting - # } + location /download/ { + alias "/usr/src/app/calibredir"; + # Never forget the fact that this little statement being root instead of alias caused us to lose more than a day troubleshooting + } location /static/ { alias "/usr/src/app/CalibreWebCompanion/static/"; diff --git a/supervisord.conf b/deployment/supervisord.conf similarity index 100% rename from supervisord.conf rename to deployment/supervisord.conf