Skip to content

Commit

Permalink
add django system checks for path configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lilioid committed Dec 9, 2020
1 parent 884bf48 commit bee1917
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions imagetagger/imagetagger/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from imagetagger.base.checks import *
40 changes: 40 additions & 0 deletions imagetagger/imagetagger/base/checks.py
Original file line number Diff line number Diff line change
@@ -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,
)]
4 changes: 2 additions & 2 deletions imagetagger/imagetagger/base/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 1 addition & 2 deletions imagetagger/imagetagger/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import typing
from os.path import join as path_join
from configurations import Configuration, values
from django.contrib import messages
Expand Down Expand Up @@ -216,7 +215,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)
Expand Down

0 comments on commit bee1917

Please sign in to comment.