From 095494f96f00b33dc9e956b094105c49b5bc558b Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 18 Jan 2020 01:20:32 +0000 Subject: [PATCH] Improve Docker configurations --- .dockerignore | 2 ++ Dockerfile | 11 +++++++---- app/__init__.py | 2 +- app/blueprints/thumbnails/__init__.py | 15 ++++++++++----- app/tasks/importtasks.py | 4 ++-- app/utils.py | 4 +++- config.example.cfg | 3 +++ docker-compose.yml | 6 ++---- 8 files changed, 30 insertions(+), 17 deletions(-) diff --git a/.dockerignore b/.dockerignore index db3fdd0d..0c8c6cab 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,5 @@ .git data uploads +*.pyc +__pycache__ diff --git a/Dockerfile b/Dockerfile index c88d0a31..00f683c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,15 +5,18 @@ RUN groupadd -g 5123 cdb && \ WORKDIR /home/cdb +RUN mkdir /var/cdb +RUN chown -R cdb:cdb /var/cdb + COPY requirements.txt requirements.txt -RUN pip install -r ./requirements.txt +RUN pip install -r requirements.txt RUN pip install gunicorn COPY utils utils -COPY config.cfg ./config.cfg +COPY config.cfg config.cfg COPY migrations migrations COPY app app -RUN mkdir /home/cdb/app/public/uploads/ -RUN chown cdb:cdb /home/cdb -R +RUN chown -R cdb:cdb /home/cdb + USER cdb diff --git a/app/__init__.py b/app/__init__.py index 4c1512aa..478f0d86 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -72,7 +72,7 @@ from flask_login import logout_user @app.route("/uploads/") def send_upload(path): - return send_from_directory("public/uploads", path) + return send_from_directory(app.config['UPLOAD_DIR'], path) @menu.register_menu(app, ".help", "Help", order=19, endpoint_arguments_constructor=lambda: { 'path': 'help' }) @app.route('//') diff --git a/app/blueprints/thumbnails/__init__.py b/app/blueprints/thumbnails/__init__.py index 1f461022..820d2281 100644 --- a/app/blueprints/thumbnails/__init__.py +++ b/app/blueprints/thumbnails/__init__.py @@ -25,10 +25,10 @@ from PIL import Image ALLOWED_RESOLUTIONS=[(100,67), (270,180), (350,233)] def mkdir(path): + assert(path != "" and path is not None) if not os.path.isdir(path): os.mkdir(path) -mkdir("app/public/thumbnails/") def resize_and_crop(img_path, modified_path, size): img = Image.open(img_path) @@ -65,10 +65,15 @@ def make_thumbnail(img, level): w, h = ALLOWED_RESOLUTIONS[level - 1] - mkdir("app/public/thumbnails/{:d}/".format(level)) + upload_dir = current_app.config["UPLOAD_DIR"] + thumbnail_dir = current_app.config["THUMBNAIL_DIR"] + mkdir(thumbnail_dir) - cache_filepath = "public/thumbnails/{:d}/{}".format(level, img) - source_filepath = "public/uploads/" + img + output_dir = os.path.join(thumbnail_dir, str(level)) + mkdir(output_dir) - resize_and_crop("app/" + source_filepath, "app/" + cache_filepath, (w, h)) + cache_filepath = os.path.join(output_dir, img) + source_filepath = os.path.join(upload_dir, img) + + resize_and_crop(source_filepath, cache_filepath, (w, h)) return send_file(cache_filepath) diff --git a/app/tasks/importtasks.py b/app/tasks/importtasks.py index ed435849..ebe8e1e2 100644 --- a/app/tasks/importtasks.py +++ b/app/tasks/importtasks.py @@ -389,7 +389,7 @@ def makeVCSRelease(id, branch): try: filename = randomString(10) + ".zip" - destPath = os.path.join("app/public/uploads", filename) + destPath = os.path.join(app.config["UPLOAD_DIR"], filename) with open(destPath, "wb") as fp: repo.archive(fp, format="zip") @@ -424,7 +424,7 @@ def importRepoScreenshot(id): sourcePath = gitDir + "/screenshot." + ext if os.path.isfile(sourcePath): filename = randomString(10) + "." + ext - destPath = os.path.join("app/public/uploads", filename) + destPath = os.path.join(app.config["UPLOAD_DIR"], filename) shutil.copyfile(sourcePath, destPath) ss = PackageScreenshot() diff --git a/app/utils.py b/app/utils.py index 102219de..5d1c8da2 100644 --- a/app/utils.py +++ b/app/utils.py @@ -46,6 +46,8 @@ def randomString(n): return ''.join(random.choice(string.ascii_lowercase + \ string.ascii_uppercase + string.digits) for _ in range(n)) +assert(os.path.isdir(app.config["UPLOAD_DIR"]), "UPLOAD_DIR must exist") + def doFileUpload(file, fileType, fileTypeDesc): if not file or file is None or file.filename == "": flash("No selected file", "error") @@ -73,7 +75,7 @@ def doFileUpload(file, fileType, fileTypeDesc): file.stream.seek(0) filename = randomString(10) + "." + ext - file.save(os.path.join("app/public/uploads", filename)) + file.save(os.path.join(app.config["UPLOAD_DIR"], filename)) return "/uploads/" + filename def make_flask_user_password(plaintext_str): diff --git a/config.example.cfg b/config.example.cfg index 51e3f505..60f69282 100644 --- a/config.example.cfg +++ b/config.example.cfg @@ -25,6 +25,9 @@ MAIL_PORT=587 MAIL_USE_TLS=True MAIL_UTILS_ERROR_SEND_TO=[""] +UPLOAD_DIR="/var/cdb/uploads/" +THUMBNAIL_DIR="/var/cdb/thumbnails/" + LANGUAGES = { 'en': 'English', } diff --git a/docker-compose.yml b/docker-compose.yml index 4f8ccfe7..40b345f5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,9 +21,7 @@ services: ports: - 5123:5123 volumes: - - "./data/uploads:/home/cdb/app/public/uploads" - - "./app:/home/cdb/appsrc" - - "./migrations:/home/cdb/migrations" + - "./data/uploads:/var/cdb/uploads" depends_on: - db - redis @@ -36,7 +34,7 @@ services: environment: - FLASK_CONFIG=../config.cfg volumes: - - "./data/uploads:/home/cdb/app/public/uploads" + - "./data/uploads:/var/cdb/uploads" depends_on: - redis