Commit c30d7a0022ba865fea11aa77e23be0e396b8c576
1 parent
fac44a85
Exists in
master
and in
30 other branches
Added logging for settings initialization
[skip ci]
Showing
2 changed files
with
51 additions
and
31 deletions
Show diff stats
colab/settings.py
| @@ -253,8 +253,8 @@ from .utils import conf | @@ -253,8 +253,8 @@ from .utils import conf | ||
| 253 | 253 | ||
| 254 | SOCIAL_NETWORK_ENABLED = locals().get('SOCIAL_NETWORK_ENABLED') or False | 254 | SOCIAL_NETWORK_ENABLED = locals().get('SOCIAL_NETWORK_ENABLED') or False |
| 255 | 255 | ||
| 256 | -locals().update(conf.load_colab_apps()) | ||
| 257 | locals().update(conf.load_py_settings()) | 256 | locals().update(conf.load_py_settings()) |
| 257 | +locals().update(conf.load_colab_apps()) | ||
| 258 | 258 | ||
| 259 | COLAB_APPS = locals().get('COLAB_APPS') or {} | 259 | COLAB_APPS = locals().get('COLAB_APPS') or {} |
| 260 | PROXIED_APPS = {} | 260 | PROXIED_APPS = {} |
colab/utils/conf.py
| 1 | 1 | ||
| 2 | import os | 2 | import os |
| 3 | import sys | 3 | import sys |
| 4 | +import logging | ||
| 4 | import importlib | 5 | import importlib |
| 5 | import warnings | 6 | import warnings |
| 6 | 7 | ||
| 7 | from django.core.exceptions import ImproperlyConfigured | 8 | from django.core.exceptions import ImproperlyConfigured |
| 8 | 9 | ||
| 10 | +logger = logging.getLogger('colab.init') | ||
| 11 | +if os.environ.get('COLAB_DEBUG'): | ||
| 12 | + logger.addHandler(logging.StreamHandler()) | ||
| 13 | + logger.setLevel(logging.INFO) | ||
| 14 | + | ||
| 9 | 15 | ||
| 10 | class InaccessibleSettings(ImproperlyConfigured): | 16 | class InaccessibleSettings(ImproperlyConfigured): |
| 11 | """Settings.py is Inaccessible. | 17 | """Settings.py is Inaccessible. |
| @@ -48,57 +54,71 @@ def _load_py_file(py_path, path): | @@ -48,57 +54,71 @@ def _load_py_file(py_path, path): | ||
| 48 | 54 | ||
| 49 | 55 | ||
| 50 | def load_py_settings(): | 56 | def load_py_settings(): |
| 51 | - settings_dir = '/etc/colab/settings.d' | ||
| 52 | settings_file = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.py') | 57 | settings_file = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.py') |
| 53 | settings_module = settings_file.split('.')[-2].split('/')[-1] | 58 | settings_module = settings_file.split('.')[-2].split('/')[-1] |
| 54 | py_path = "/".join(settings_file.split('/')[:-1]) | 59 | py_path = "/".join(settings_file.split('/')[:-1]) |
| 55 | 60 | ||
| 61 | + logger.info('Settings file: %s', settings_file) | ||
| 62 | + | ||
| 56 | if not os.path.exists(py_path): | 63 | if not os.path.exists(py_path): |
| 57 | msg = "The py file {} does not exist".format(py_path) | 64 | msg = "The py file {} does not exist".format(py_path) |
| 58 | raise InaccessibleSettings(msg) | 65 | raise InaccessibleSettings(msg) |
| 59 | 66 | ||
| 60 | py_settings = _load_py_file(settings_module, py_path) | 67 | py_settings = _load_py_file(settings_module, py_path) |
| 61 | 68 | ||
| 62 | - # Try to read settings from settings.d | 69 | + # Read settings from settings.d |
| 70 | + settings_dir = '/etc/colab/settings.d' | ||
| 71 | + logger.info('Settings directory: %s', settings_dir) | ||
| 72 | + | ||
| 73 | + if not os.path.exists(settings_dir): | ||
| 74 | + return py_settings | ||
| 75 | + | ||
| 76 | + for file_name in os.listdir(settings_dir): | ||
| 77 | + if not file_name.endswith('.py'): | ||
| 78 | + continue | ||
| 63 | 79 | ||
| 64 | - if os.path.exists(settings_dir): | ||
| 65 | - for file_name in os.listdir(settings_dir): | ||
| 66 | - if file_name.endswith('.py'): | ||
| 67 | - file_module = file_name.split('.')[0] | ||
| 68 | - py_settings_d = _load_py_file(file_module, settings_dir) | ||
| 69 | - py_settings.update(py_settings_d) | 80 | + file_module = file_name.split('.')[0] |
| 81 | + py_settings_d = _load_py_file(file_module, settings_dir) | ||
| 82 | + py_settings.update(py_settings_d) | ||
| 83 | + logger.info('Loaded %s/%s', settings_dir, file_name) | ||
| 70 | 84 | ||
| 71 | return py_settings | 85 | return py_settings |
| 72 | 86 | ||
| 73 | 87 | ||
| 74 | def load_colab_apps(): | 88 | def load_colab_apps(): |
| 75 | plugins_dir = os.getenv('COLAB_PLUGINS', '/etc/colab/plugins.d/') | 89 | plugins_dir = os.getenv('COLAB_PLUGINS', '/etc/colab/plugins.d/') |
| 90 | + logger.info('Plugin settings directory: %s', plugins_dir) | ||
| 76 | 91 | ||
| 77 | COLAB_APPS = {} | 92 | COLAB_APPS = {} |
| 78 | 93 | ||
| 79 | # Try to read settings from plugins.d | 94 | # Try to read settings from plugins.d |
| 80 | - if os.path.exists(plugins_dir): | ||
| 81 | - for file_name in os.listdir(plugins_dir): | ||
| 82 | - if file_name.endswith('.py'): | ||
| 83 | - file_module = file_name.split('.')[0] | ||
| 84 | - py_settings_d = _load_py_file(file_module, plugins_dir) | ||
| 85 | - fields = ['verbose_name', 'upstream', 'urls', | ||
| 86 | - 'menu_urls', 'middlewares', 'dependencies', | ||
| 87 | - 'context_processors', 'private_token'] | ||
| 88 | - | ||
| 89 | - app_name = py_settings_d.get('name') | ||
| 90 | - if not app_name: | ||
| 91 | - warnings.warn("Plugin missing name variable") | ||
| 92 | - continue | ||
| 93 | - | ||
| 94 | - COLAB_APPS[app_name] = {} | ||
| 95 | - COLAB_APPS[app_name]['menu_title'] = \ | ||
| 96 | - py_settings_d.get('menu_title') | ||
| 97 | - | ||
| 98 | - for key in fields: | ||
| 99 | - value = py_settings_d.get(key) | ||
| 100 | - if value: | ||
| 101 | - COLAB_APPS[app_name][key] = value | 95 | + if not os.path.exists(plugins_dir): |
| 96 | + return {'COLAB_APPS': COLAB_APPS} | ||
| 97 | + | ||
| 98 | + for file_name in os.listdir(plugins_dir): | ||
| 99 | + if not file_name.endswith('.py'): | ||
| 100 | + continue | ||
| 101 | + | ||
| 102 | + file_module = file_name.split('.')[0] | ||
| 103 | + py_settings_d = _load_py_file(file_module, plugins_dir) | ||
| 104 | + logger.info('Loaded plugin settings: %s/%s', plugins_dir, file_name) | ||
| 105 | + | ||
| 106 | + app_name = py_settings_d.get('name') | ||
| 107 | + if not app_name: | ||
| 108 | + warnings.warn("Plugin missing name variable") | ||
| 109 | + continue | ||
| 110 | + | ||
| 111 | + COLAB_APPS[app_name] = {} | ||
| 112 | + COLAB_APPS[app_name]['menu_title'] = py_settings_d.get('menu_title') | ||
| 113 | + | ||
| 114 | + fields = ['verbose_name', 'upstream', 'urls', | ||
| 115 | + 'menu_urls', 'middlewares', 'dependencies', | ||
| 116 | + 'context_processors', 'private_token'] | ||
| 117 | + | ||
| 118 | + for key in fields: | ||
| 119 | + value = py_settings_d.get(key) | ||
| 120 | + if value: | ||
| 121 | + COLAB_APPS[app_name][key] = value | ||
| 102 | 122 | ||
| 103 | return {'COLAB_APPS': COLAB_APPS} | 123 | return {'COLAB_APPS': COLAB_APPS} |
| 104 | 124 |