From f77ff7536253b5f70176df7075327069e356aebc Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 24 Jan 2022 18:04:23 -0700 Subject: [PATCH] Creating main.py argument which checks if the media directory is correct. --- main.py | 15 +++++++++++++-- wrolpi/common.py | 19 ++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 1c258266..d19abc2a 100755 --- a/main.py +++ b/main.py @@ -47,6 +47,9 @@ async def main(loop): parser = argparse.ArgumentParser() parser.add_argument('-v', '--verbose', action='count') parser.add_argument('--version', action='store_true', default=False) + parser.add_argument('-c', '--check-media', action='store_true', default=False, + help='Check that the media directory is mounted and has the correct permissions.' + ) sub_commands = parser.add_subparsers(title='sub-commands', dest='sub_commands') @@ -65,6 +68,14 @@ async def main(loop): print(get_version_string()) return 0 + if args.check_media: + # Run the media directory check. Exit with informative return code. + result = check_media_directory() + if result is False: + return 1 + print('Media directory is correct.') + return 0 + logger.warning(f'Starting with: {sys.argv}') await set_log_level(args) logger.debug(get_version_string()) @@ -144,5 +155,5 @@ def periodic_downloads(app: Sanic, loop): if __name__ == '__main__': loop_ = asyncio.get_event_loop() - result = loop_.run_until_complete(main(loop_)) - sys.exit(result) + result_ = loop_.run_until_complete(main(loop_)) + sys.exit(result_) diff --git a/wrolpi/common.py b/wrolpi/common.py index bf12bbf0..8f332987 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -406,14 +406,28 @@ def get_media_directory() -> Path: return MEDIA_DIRECTORY +MEDIA_DIRECTORY_PERMISSIONS = 0o40755 + + def check_media_directory(): """ - Check that the media directory exists and is readable/writeable. Log an error if the directory + Check that the media directory exists and has the correct permissions. Log an error if the directory is unusable. """ + result = True media_directory = get_media_directory() if not media_directory.is_dir(): logger.error(f'Media directory does not exist: {media_directory}') + result = False + + permissions = media_directory.stat().st_mode + if permissions != MEDIA_DIRECTORY_PERMISSIONS: + logger.error(f'Media directory has the wrong permissions: {permissions}') + result = False + + if not media_directory.is_mount(): + logger.warning('Media directory is not a mount.') + try: # Write a file into the media directory, log an error if this is not possible. This file should be cleaned up # by tempfile. @@ -421,6 +435,9 @@ def check_media_directory(): tf.write(b'test') except Exception as e: logger.error(f'Could not write to media directory: {media_directory}', exc_info=e) + result = False + + return result def get_absolute_media_path(path: str) -> Path: