From de0fdc5f3a0316e52a68591ed75f06f882734f0d Mon Sep 17 00:00:00 2001 From: Finn-Thorben Sell Date: Wed, 9 Dec 2020 18:54:35 +0100 Subject: [PATCH] add django system checks for path configuration --- imagetagger/imagetagger/base/__init__.py | 1 + imagetagger/imagetagger/base/checks.py | 40 ++++++++++++++++++++++ imagetagger/imagetagger/base/filesystem.py | 4 +-- imagetagger/imagetagger/settings.py | 2 +- 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 imagetagger/imagetagger/base/checks.py diff --git a/imagetagger/imagetagger/base/__init__.py b/imagetagger/imagetagger/base/__init__.py index e69de29b..f7930fdf 100644 --- a/imagetagger/imagetagger/base/__init__.py +++ b/imagetagger/imagetagger/base/__init__.py @@ -0,0 +1 @@ +from imagetagger.base.checks import * diff --git a/imagetagger/imagetagger/base/checks.py b/imagetagger/imagetagger/base/checks.py new file mode 100644 index 00000000..f74c5ea8 --- /dev/null +++ b/imagetagger/imagetagger/base/checks.py @@ -0,0 +1,40 @@ +from typing import List +from os.path import join, dirname +from django.core.checks import register, CheckMessage, Error +from django.conf import settings +from fs.base import FS +from . import filesystem + + +def _create_check_file(fs: FS, path: str): + fs.makedirs(dirname(path), recreate=True) + fs.create(path, wipe=True) + fs.writebytes(path, bytes('This is a demo file content', encoding='ASCII')) + fs.remove(path) + + +@register +def check_fs_root_config(app_configs, **kwargs) -> List[CheckMessage]: + try: + _create_check_file(filesystem.root(), join(settings.IMAGE_PATH, 'check.tmp.jpeg')) + _create_check_file(filesystem.root(), join(settings.TOOLS_PATH, 'check.tmp.py')) + return [] + except Exception as e: + return [Error( + 'Persistent filesystem is incorrectly configured. Could not create and delete a temporary check file', + hint=f'Check your FS_URL settings (currently {settings.FS_URL})', + obj=e, + )] + + +@register +def check_tmp_fs_config(app_configs, **kwargs) -> List[CheckMessage]: + try: + _create_check_file(filesystem.tmp(), join(settings.TMP_IMAGE_PATH, 'check.tmp.jpeg')) + return [] + except Exception as e: + return [Error( + 'Persistent filesystem is incorrectly configured. Could not create and delete a temporary check file', + hint=f'Check your TMP_FS_URL settings (currently {settings.TMP_FS_URL})', + obj=e, + )] diff --git a/imagetagger/imagetagger/base/filesystem.py b/imagetagger/imagetagger/base/filesystem.py index a99eabe5..eabc1c91 100644 --- a/imagetagger/imagetagger/base/filesystem.py +++ b/imagetagger/imagetagger/base/filesystem.py @@ -6,7 +6,7 @@ def root() -> fs.base.FS: """Get the root filesystem object or create one by opening `settings.FS_URL`.""" if root._instance is None: - root._instance = fs.open_fs(settings.FS_URL) + root._instance = fs.open_fs(settings.FS_URL, writeable=True) return root._instance root._instance = None @@ -15,7 +15,7 @@ def root() -> fs.base.FS: def tmp() -> fs.base.FS: """Get the tmp filesystem object or create one by opening `settings.TMP_FS_URL`.""" if tmp._instance is None: - tmp._instance = fs.open_fs(settings.TMP_FS_URL) + tmp._instance = fs.open_fs(settings.TMP_FS_URL, writeable=True) return tmp._instance tmp._instance = None \ No newline at end of file diff --git a/imagetagger/imagetagger/settings.py b/imagetagger/imagetagger/settings.py index c103d64b..90b7023c 100644 --- a/imagetagger/imagetagger/settings.py +++ b/imagetagger/imagetagger/settings.py @@ -216,7 +216,7 @@ def post_setup(cls): TOOL_UPLOAD_NOTICE = values.Value(environ_prefix='IT', default='') ENABLE_ZIP_DOWNLOAD = values.BooleanValue(environ_prefix='IT', default=is_in_docker()) USE_NGINX_IMAGE_PROVISION = values.BooleanValue(environ_prefix='IT', default=is_in_docker()) - FS_URL = values.Value(environ_prefix='IT', default=path_join(os.path.dirname(BASE_DIR), 'data')) + FS_URL = values.Value(environ_prefix='IT', default=path_join(BASE_DIR, 'data')) TMP_FS_URL = values.Value(environ_prefix='IT', default='temp://imagetagger') SENTRY_REPORTING_ENABLED = values.BooleanValue(environ_prefix='IT', default=False)