From 7b033454b3f4ca38d79fad2fca21128c35a0df90 Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Fri, 5 Mar 2021 10:32:42 -0300 Subject: [PATCH] nginx unit works --- .gitignore | 2 +- .../CalibreWebCompanion/settings.py | 78 ++++++++++--------- .../templates/library/book_detail.html | 2 +- CalibreWebCompanion/settings.json.bak | 12 ++- deployment/Dockerfile | 15 ++-- deployment/entrypoints/config.json | 32 ++++++-- deployment/entrypoints/start.sh | 25 +++++- deployment/instructions.md | 6 +- test.sh | 17 ++++ 9 files changed, 124 insertions(+), 65 deletions(-) create mode 100755 test.sh diff --git a/.gitignore b/.gitignore index aed35f7..546aa27 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ settings.json db.sqlite3 dummyusers.json *.prof - +statictest.txt # IDE .vscode diff --git a/CalibreWebCompanion/CalibreWebCompanion/settings.py b/CalibreWebCompanion/CalibreWebCompanion/settings.py index 8041696..9d4119c 100644 --- a/CalibreWebCompanion/CalibreWebCompanion/settings.py +++ b/CalibreWebCompanion/CalibreWebCompanion/settings.py @@ -23,9 +23,10 @@ with open(BASE_DIR + "/settings.json", "r") as userfile: CALIBRE_DIR = os.path.abspath(usersettings["CALIBRE_DIR"]) SECRET_KEY = usersettings["SECRET_KEY"] ALLOWED_HOSTS = usersettings["ALLOWED_HOSTS"] - INTERNAL_IPS = usersettings["INTERNAL_IPS"] DEBUG = usersettings["DEBUG"] +DOCKER = os.environ.get('AM_DOCKER_INSTANCE', False) + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) @@ -64,38 +65,41 @@ STATIC_ROOT = BASE_DIR + "/static/" ######################################################################### # LOGGING - - -# 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", +# TODO: think about the issue for a bit. no write access to file on first run +# but startscript doesn't run to give access if it can't start django +# if not DOCKER: +# logfile = "/app//data//django.log" +# if not os.path.isfile(logfile): +# os.mknod(logfile) +# 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 +# "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", +# "formatters": { +# "app": { +# "format": ( +# u"%(asctime)s [%(levelname)-8s] " +# "(%(module)s.%(funcName)s) %(message)s" +# ), +# "datefmt": "%Y-%m-%d %H:%M:%S", +# }, # }, -# }, -# } +# } ## ## @@ -190,19 +194,23 @@ 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/" + +if DOCKER: + djangodb_path = "/app/CalibreWebCompanion" + calibredb_path = "/app/content/" + else: - defaultdb_path = BASE_DIR + djangodb_path = BASE_DIR + calibredb_path = CALIBRE_DIR DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(defaultdb_path, 'db.sqlite3'), + 'NAME': os.path.join(djangodb_path, 'db.sqlite3'), }, 'calibre': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(CALIBRE_DIR, 'metadata.db'), + 'NAME': os.path.join(calibredb_path, 'metadata.db'), } } diff --git a/CalibreWebCompanion/library/templates/library/book_detail.html b/CalibreWebCompanion/library/templates/library/book_detail.html index 475db15..577436d 100644 --- a/CalibreWebCompanion/library/templates/library/book_detail.html +++ b/CalibreWebCompanion/library/templates/library/book_detail.html @@ -6,7 +6,7 @@
- download
diff --git a/CalibreWebCompanion/settings.json.bak b/CalibreWebCompanion/settings.json.bak index befb87d..8caa744 100644 --- a/CalibreWebCompanion/settings.json.bak +++ b/CalibreWebCompanion/settings.json.bak @@ -1,15 +1,13 @@ { - "CALIBRE_DIR": "/usr/src/app/calibredir", + "CALIBRE_DIR": "/app/content", + "LOGFOLDER" : "/usr/src/app/data/logs/", "SECRET_KEY": "u(8^+rb%rz5hsx4v^^y(ul7g(4n7a8!db@s*9(m5cs*2_ppy8+", "ALLOWED_HOSTS": [ - "127.0.0.1" + "*" ], "INTERNAL_IPS": [ - "127.0.0.1" + "127.0.0.1", "localhost" ], - "DEBUG" : true, - "LOGFOLDER" : "/usr/src/app/data/logs/", - "ISDOCKER" : true - + "DEBUG" : false } \ No newline at end of file diff --git a/deployment/Dockerfile b/deployment/Dockerfile index 743d67b..77a0e1a 100644 --- a/deployment/Dockerfile +++ b/deployment/Dockerfile @@ -2,23 +2,22 @@ ## pull official base image FROM nginx/unit:1.22.0-python3.9 EXPOSE 80 - +ENV AM_DOCKER_INSTANCE Yes ## set work directory -WORKDIR /usr/src/app +WORKDIR /app ## install dependencies COPY ./requirements.txt . RUN pip install -r requirements.txt - ## copy project COPY ./CalibreWebCompanion ./CalibreWebCompanion -COPY ./deployment/entrypoints/* /docker-entrypoint.d/ + # perms -RUN chown -R unit:unit /usr/src/app +RUN chown -R unit:unit /app +COPY ./deployment/entrypoints/* /docker-entrypoint.d/ RUN chmod +x /docker-entrypoint.d/start.sh - # docker run --publish 80:80 \ -# -v '/home/MassiveAtoms/Desktop/logs:/usr/src/app/data' \ -# -v '/home/MassiveAtoms/windows/Users/MassiveAtoms/Documents/Calibre Library/:/usr/src/app/calibredir' \ +# -v '/home/MassiveAtoms/Desktop/logs:/app/data' \ +# -v '/home/MassiveAtoms/windows/Users/MassiveAtoms/Documents/Calibre Library/:/app/calibredir' \ # --name cw calibreweb:1.0 diff --git a/deployment/entrypoints/config.json b/deployment/entrypoints/config.json index 2910604..0225d34 100644 --- a/deployment/entrypoints/config.json +++ b/deployment/entrypoints/config.json @@ -6,14 +6,30 @@ }, "routes": [ { - "match": {"uri": "/static/*"}, - "action": {"share": "/usr/src/app/CalibreWebCompanion/static"} + "match": { + "uri": [ + "/content/*", + "!~\\.db" + ] + }, + "action": { + "share": "/app/" + } }, { - "match": {"uri": "/download/*"}, - "action": {"share": "/usr/src/app/calibredir"} + "match": { + "uri": "/static/*" + }, + "action": { + "share": "/app/CalibreWebCompanion/" + } }, { + "match": { + "uri": [ + "!~\\.db", "*" + ] + }, "action": { "pass": "applications/django" } @@ -22,12 +38,16 @@ "applications": { "django": { "type": "python 3", - "path": "/usr/src/app/CalibreWebCompanion/", + "path": "/app/CalibreWebCompanion/", "module": "CalibreWebCompanion.wsgi", "environment": { "DJANGO_SETTINGS_MODULE": "CalibreWebCompanion.settings", "DB_ENGINE": "django.db.backends.sqlite3" - + }, + "processes" : { + "max": 25, + "spare": 1, + "idle_timeout": 20 } } } diff --git a/deployment/entrypoints/start.sh b/deployment/entrypoints/start.sh index 17b4326..5c6fc91 100644 --- a/deployment/entrypoints/start.sh +++ b/deployment/entrypoints/start.sh @@ -1,5 +1,22 @@ -path=/usr/src/app/CalibreWebCompanion -chown -R unit:unit /usr/src/app +CAL_DIR="/app/content/" +DATA_DIR="/app/data/" +CWC_PATH="/app/CalibreWebCompanion" +if [ ! -d "$CAL_DIR" ]; then + echo "Calibre Library not mounted at the correct location." + echo "Mount it at /app/content/" + echo "Exiting..." + exit 1 +fi -python "${path}/manage.py" makemigrations -python "${path}/manage.py" migrate +if [ ! -d "$DATA_DIR" ]; then + echo "A data directory not mounted at the correct location, exiting" + echo "This is used to store the database and logs" + echo "mount something at /app/data/" + echo "exiting" + exit 1 +fi + +cp -R -u -p "/app/CalibreWebCompanion/db.sqlite3" "/app/data/" +ls -l /app +python "${CWC_PATH}/manage.py" makemigrations +python "${CWC_PATH}/manage.py" migrate diff --git a/deployment/instructions.md b/deployment/instructions.md index 2cfad25..257871a 100644 --- a/deployment/instructions.md +++ b/deployment/instructions.md @@ -8,12 +8,12 @@ Here's an example of step 5 ``` docker run --publish 80:80 \ --v '/home/MassiveAtoms/Desktop/logs:/usr/src/app/data' \ --v '/home/MassiveAtoms/windows/Users/MassiveAtoms/Documents/Calibre\ Library/:/usr/src/app/calibredir' \ +-v '/home/MassiveAtoms/Desktop/logs:/app/data' \ +-v '/home/MassiveAtoms/windows/Users/MassiveAtoms/Documents/Calibre\ Library/:/app/calibredir' \ --name cw calibreweb:1.0 ``` -your Calibre path/volume/whatever needs to be mounted at `/usr/src/app/calibredir`, and you need to mount a volume for the db and logs at `/usr/src/app/data` +your Calibre path/volume/whatever needs to be mounted at `/app/calibredir`, and you need to mount a volume for the db and logs at `/app/data` Issues with it at the moment: 1. we still need to do something to create a random secret key. Atm, this would still diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..3ca1537 --- /dev/null +++ b/test.sh @@ -0,0 +1,17 @@ +docker build --tag calibreweb:1.0 . -f ./deployment/Dockerfile +docker stop cw +docker rm cw +docker run --publish 80:80 \ +-v '/home/MassiveAtoms/Desktop/logs:/app/data' \ +-v '/home/MassiveAtoms/windows/Users/MassiveAtoms/Documents/Calibre Library/:/app/content' \ +--name cw calibreweb:1.0 +sleep 8 +echo "download/test" +curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/download/statictest.txt +echo " " +echo "/test" +curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/statictest.txt +echo " " +echo "/static/test" +curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/static/statictest.txt +echo " " \ No newline at end of file