From a0c19252df129da0b6c223dbc6d91c4f6a010ce5 Mon Sep 17 00:00:00 2001 From: Dave Page Date: Tue, 2 Jun 2020 11:11:53 +0530 Subject: [PATCH] Improve error messaging if the storage and log directories cannot be created. Fixes #5576 --- docs/en_US/release_notes_4_23.rst | 1 + web/pgadmin/setup/data_directory.py | 74 ++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/docs/en_US/release_notes_4_23.rst b/docs/en_US/release_notes_4_23.rst index 7e3e15b6b..4504f9ea8 100644 --- a/docs/en_US/release_notes_4_23.rst +++ b/docs/en_US/release_notes_4_23.rst @@ -9,6 +9,7 @@ This release contains a number of bug fixes and new features since the release o New features ************ +| `Issue #5576 `_ - Improve error messaging if the storage and log directories cannot be created. Housekeeping ************ diff --git a/web/pgadmin/setup/data_directory.py b/web/pgadmin/setup/data_directory.py index 581643d1b..943e7ebfc 100644 --- a/web/pgadmin/setup/data_directory.py +++ b/web/pgadmin/setup/data_directory.py @@ -8,7 +8,7 @@ ########################################################################## import os -from flask import current_app +import pwd def _create_directory_if_not_exists(_path): @@ -21,7 +21,22 @@ def create_app_data_directory(config): Create the required directories (if not present). """ # Create the directory containing the configuration file (if not present). - _create_directory_if_not_exists(os.path.dirname(config.SQLITE_PATH)) + try: + _create_directory_if_not_exists(os.path.dirname(config.SQLITE_PATH)) + except PermissionError as e: + print("ERROR : Failed to create the directory {}:\n {}". + format(os.path.dirname(config.SQLITE_PATH), e)) + print( + "HINT : Create the directory {}, ensure it is writeable by\n" + " '{}', and try again, or, create a config_local.py file\n" + " and override the SQLITE_PATH setting per\n" + " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html". + format( + os.path.dirname(config.SQLITE_PATH), + pwd.getpwuid(os.getuid()).pw_name, + config.APP_VERSION)) + exit(1) + # Try to set the permissions on the directory, but don't complain # if we can't. This may be the case on a mounted directory, e.g. in # OpenShift. We'll still secure the config database anyway. @@ -31,15 +46,62 @@ def create_app_data_directory(config): except Exception as e: # The flask app isn't setup yet, so we can't use the logger print('WARNING: Failed to set ACL on the directory containing the ' - 'configuration database: {}'.format(e)) + 'configuration database:\n {}'.format(e)) + print("HINT : You may need to manually set the permissions on\n" + " {} to allow {} to write to it.". + format(os.path.dirname(config.SQLITE_PATH), + pwd.getpwuid(os.getuid()).pw_name)) # Create the directory containing the log file (if not present). - _create_directory_if_not_exists(os.path.dirname(config.LOG_FILE)) + try: + _create_directory_if_not_exists(os.path.dirname(config.LOG_FILE)) + except PermissionError as e: + print("ERROR : Failed to create the directory {}:\n {}". + format(os.path.dirname(config.LOG_FILE), e)) + print( + "HINT : Create the directory {}, ensure it is writeable by\n" + " '{}', and try again, or, create a config_local.py file\n" + " and override the LOG_FILE setting per\n" + " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html". + format( + os.path.dirname(config.LOG_FILE), + pwd.getpwuid(os.getuid()).pw_name, + config.APP_VERSION)) + exit(1) # Create the session directory (if not present). - _create_directory_if_not_exists(config.SESSION_DB_PATH) + try: + _create_directory_if_not_exists(config.SESSION_DB_PATH) + except PermissionError as e: + print("ERROR : Failed to create the directory {}:\n {}". + format(config.SESSION_DB_PATH, e)) + print( + "HINT : Create the directory {}, ensure it is writeable by\n" + " '{}', and try again, or, create a config_local.py file\n" + " and override the SESSION_DB_PATH setting per\n" + " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html". + format( + config.SESSION_DB_PATH, + pwd.getpwuid(os.getuid()).pw_name, + config.APP_VERSION)) + exit(1) + if os.name != 'nt': os.chmod(config.SESSION_DB_PATH, 0o700) # Create the storage directory (if not present). - _create_directory_if_not_exists(config.STORAGE_DIR) + try: + _create_directory_if_not_exists(config.STORAGE_DIR) + except PermissionError as e: + print("ERROR : Failed to create the directory {}\n {}:". + format(config.STORAGE_DIR, e)) + print( + "HINT : Create the directory {}, ensure it is writable by\n" + " '{}', and try again, or, create a config_local.py file\n" + " and override the STORAGE_DIR setting per\n" + " https://www.pgadmin.org/docs/pgadmin4/{}/config_py.html". + format( + config.STORAGE_DIR, + pwd.getpwuid(os.getuid()).pw_name, + config.APP_VERSION)) + exit(1)