Skip to content

Commit 43c05d8

Browse files
committed
Dynamically load configuration files
1 parent fd3d3d1 commit 43c05d8

File tree

5 files changed

+117
-14
lines changed

5 files changed

+117
-14
lines changed

Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ ARG NETBOX_PATH
6161
COPY ${NETBOX_PATH} /opt/netbox
6262

6363
COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py
64-
COPY configuration/gunicorn_config.py /etc/netbox/config/
64+
COPY docker/gunicorn_config.py /etc/netbox/
6565
COPY docker/nginx.conf /etc/netbox-nginx/nginx.conf
6666
COPY docker/docker-entrypoint.sh /opt/netbox/docker-entrypoint.sh
6767
COPY startup_scripts/ /opt/netbox/startup_scripts/
6868
COPY initializers/ /opt/netbox/initializers/
69-
COPY configuration/configuration.py /etc/netbox/config/configuration.py
69+
COPY configuration/ /etc/netbox/config/
7070

7171
WORKDIR /opt/netbox/netbox
7272

@@ -79,7 +79,7 @@ RUN mkdir static && chmod -R g+w static media
7979

8080
ENTRYPOINT [ "/opt/netbox/docker-entrypoint.sh" ]
8181

82-
CMD ["gunicorn", "-c /etc/netbox/config/gunicorn_config.py", "netbox.wsgi"]
82+
CMD ["gunicorn", "-c /etc/netbox/gunicorn_config.py", "netbox.wsgi"]
8383

8484
LABEL ORIGINAL_TAG="" \
8585
NETBOX_GIT_BRANCH="" \
@@ -122,4 +122,3 @@ RUN apk add --no-cache \
122122
util-linux
123123

124124
COPY docker/ldap_config.docker.py /opt/netbox/netbox/netbox/ldap_config.py
125-
COPY configuration/ldap_config.py /etc/netbox/config/ldap_config.py
File renamed without changes.

docker/configuration.docker.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,62 @@
1+
from os.path import abspath, isfile
2+
from os import scandir
13
import importlib.util
24
import sys
35

4-
try:
5-
spec = importlib.util.spec_from_file_location('configuration', '/etc/netbox/config/configuration.py')
6+
_CONFIG_DIR = '/etc/netbox/config/'
7+
_MAIN_CONFIG = 'configuration'
8+
_MODULE = 'netbox.configuration'
9+
_loaded_configurations = []
10+
11+
12+
def __getattr__(name):
13+
for config in _loaded_configurations:
14+
try:
15+
return getattr(config, name)
16+
except:
17+
pass
18+
raise AttributeError
19+
20+
21+
def _filename(f):
22+
return f.name
23+
24+
25+
def _import(module_name, path):
26+
spec = importlib.util.spec_from_file_location('', path)
627
module = importlib.util.module_from_spec(spec)
728
spec.loader.exec_module(module)
8-
sys.modules['netbox.configuration'] = module
9-
except:
10-
raise ImportError('')
29+
sys.modules[module_name] = module
30+
31+
_loaded_configurations.insert(0, module)
32+
33+
print(f"🧬 loaded config '{path}'")
34+
35+
36+
_main_config_path = abspath(f'{_CONFIG_DIR}/{_MAIN_CONFIG}.py')
37+
if isfile(_main_config_path):
38+
_import(f'{_MODULE}.configuration', _main_config_path)
39+
else:
40+
print(f"⚠️ Main configuration '{_main_config_path}' not found.")
41+
42+
with scandir(_CONFIG_DIR) as it:
43+
for f in sorted(it, key=_filename):
44+
if not f.is_file():
45+
continue
46+
47+
if f.name.startswith('__'):
48+
continue
49+
50+
if not f.name.endswith('.py'):
51+
continue
52+
53+
if f.name == f'{_MAIN_CONFIG}.py':
54+
continue
55+
56+
module_name = f"{_MODULE}.{f.name[:-len('.py')]}"
57+
58+
_import(module_name, f.path)
59+
60+
if len(_loaded_configurations) == 0:
61+
print(f"‼️ No configuration files found in '{_CONFIG_DIR}'.")
62+
raise ImportError(f"No configuration files found in '{_CONFIG_DIR}'.")
File renamed without changes.

docker/ldap_config.docker.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,62 @@
1+
from os.path import abspath, isfile
2+
from os import scandir
13
import importlib.util
24
import sys
35

4-
try:
5-
spec = importlib.util.spec_from_file_location('ldap_config', '/etc/netbox/config/ldap_config.py')
6+
_CONFIG_DIR = '/etc/netbox/config/ldap/'
7+
_MAIN_CONFIG = 'ldap_config'
8+
_MODULE = 'netbox.configuration.ldap'
9+
_loaded_configurations = []
10+
11+
12+
def __getattr__(name):
13+
for config in _loaded_configurations:
14+
try:
15+
return getattr(config, name)
16+
except:
17+
pass
18+
raise AttributeError
19+
20+
21+
def _filename(f):
22+
return f.name
23+
24+
25+
def _import(module_name, path):
26+
spec = importlib.util.spec_from_file_location('', path)
627
module = importlib.util.module_from_spec(spec)
728
spec.loader.exec_module(module)
8-
sys.modules['netbox.ldap_config'] = module
9-
except:
10-
raise ImportError('')
29+
sys.modules[module_name] = module
30+
31+
_loaded_configurations.insert(0, module)
32+
33+
print(f"🧬 loaded config '{path}'")
34+
35+
36+
_main_config_path = abspath(f'{_CONFIG_DIR}/{_MAIN_CONFIG}.py')
37+
if isfile(_main_config_path):
38+
_import(f'{_MODULE}.configuration', _main_config_path)
39+
else:
40+
print(f"⚠️ Main configuration '{_main_config_path}' not found.")
41+
42+
with scandir(_CONFIG_DIR) as it:
43+
for f in sorted(it, key=_filename):
44+
if not f.is_file():
45+
continue
46+
47+
if f.name.startswith('__'):
48+
continue
49+
50+
if not f.name.endswith('.py'):
51+
continue
52+
53+
if f.name == f'{_MAIN_CONFIG}.py':
54+
continue
55+
56+
module_name = f"{_MODULE}.{f.name[:-len('.py')]}"
57+
58+
_import(module_name, f.path)
59+
60+
if len(_loaded_configurations) == 0:
61+
print(f"‼️ No configuration files found in '{_CONFIG_DIR}'.")
62+
raise ImportError(f"No configuration files found in '{_CONFIG_DIR}'.")

0 commit comments

Comments
 (0)