-
-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #343 from netbox-community/DynamicVariables
Dynamic Configuration
- Loading branch information
Showing
9 changed files
with
302 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#### | ||
## This file contains extra configuration options that can't be configured | ||
## directly through environment variables. | ||
#### | ||
|
||
## Specify one or more name and email address tuples representing NetBox administrators. These people will be notified of | ||
## application errors (assuming correct email settings are provided). | ||
# ADMINS = [ | ||
# # ['John Doe', '[email protected]'], | ||
# ] | ||
|
||
|
||
## URL schemes that are allowed within links in NetBox | ||
# ALLOWED_URL_SCHEMES = ( | ||
# 'file', 'ftp', 'ftps', 'http', 'https', 'irc', 'mailto', 'sftp', 'ssh', 'tel', 'telnet', 'tftp', 'vnc', 'xmpp', | ||
# ) | ||
|
||
|
||
## NAPALM optional arguments (see http://napalm.readthedocs.io/en/latest/support/#optional-arguments). Arguments must | ||
## be provided as a dictionary. | ||
# NAPALM_ARGS = {} | ||
|
||
|
||
## Enable installed plugins. Add the name of each plugin to the list. | ||
# from netbox.configuration.configuration import PLUGINS | ||
# PLUGINS.append('my_plugin') | ||
|
||
## Plugins configuration settings. These settings are used by various plugins that the user may have installed. | ||
## Each key in the dictionary is the name of an installed plugin and its value is a dictionary of settings. | ||
# from netbox.configuration.configuration import PLUGINS_CONFIG | ||
# PLUGINS_CONFIG['my_plugin'] = { | ||
# 'foo': 'bar', | ||
# 'buzz': 'bazz' | ||
# } | ||
|
||
|
||
## Remote authentication support | ||
# REMOTE_AUTH_DEFAULT_PERMISSIONS = {} | ||
|
||
|
||
## By default uploaded media is stored on the local filesystem. Using Django-storages is also supported. Provide the | ||
## class path of the storage driver in STORAGE_BACKEND and any configuration options in STORAGE_CONFIG. For example: | ||
# STORAGE_BACKEND = 'storages.backends.s3boto3.S3Boto3Storage' | ||
# STORAGE_CONFIG = { | ||
# 'AWS_ACCESS_KEY_ID': 'Key ID', | ||
# 'AWS_SECRET_ACCESS_KEY': 'Secret', | ||
# 'AWS_STORAGE_BUCKET_NAME': 'netbox', | ||
# 'AWS_S3_REGION_NAME': 'eu-west-1', | ||
# } | ||
|
||
|
||
## This file can contain arbitrary Python code, e.g.: | ||
# from datetime import datetime | ||
# now = datetime.now().strftime("%d/%m/%Y %H:%M:%S") | ||
# BANNER_TOP = f'<marquee width="200px">This instance started on {now}.</marquee>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,79 @@ | ||
## Generic Parts | ||
# These functions are providing the functionality to load | ||
# arbitrary configuration files. | ||
# | ||
# They can be imported by other code (see `ldap_config.py` for an example). | ||
|
||
from os.path import abspath, isfile | ||
from os import scandir | ||
import importlib.util | ||
import sys | ||
|
||
try: | ||
spec = importlib.util.spec_from_file_location('configuration', '/etc/netbox/config/configuration.py') | ||
def _filename(f): | ||
return f.name | ||
|
||
|
||
def _import(module_name, path, loaded_configurations): | ||
spec = importlib.util.spec_from_file_location('', path) | ||
module = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(module) | ||
sys.modules['netbox.configuration'] = module | ||
except: | ||
raise ImportError('') | ||
sys.modules[module_name] = module | ||
|
||
loaded_configurations.insert(0, module) | ||
|
||
print(f"🧬 loaded config '{path}'") | ||
|
||
|
||
def read_configurations(config_module, config_dir, main_config): | ||
loaded_configurations = [] | ||
|
||
main_config_path = abspath(f'{config_dir}/{main_config}.py') | ||
if isfile(main_config_path): | ||
_import(f'{config_module}.{main_config}', main_config_path, loaded_configurations) | ||
else: | ||
print(f"⚠️ Main configuration '{main_config_path}' not found.") | ||
|
||
with scandir(config_dir) as it: | ||
for f in sorted(it, key=_filename): | ||
if not f.is_file(): | ||
continue | ||
|
||
if f.name.startswith('__'): | ||
continue | ||
|
||
if not f.name.endswith('.py'): | ||
continue | ||
|
||
if f.name == f'{config_dir}.py': | ||
continue | ||
|
||
module_name = f"{config_module}.{f.name[:-len('.py')]}".replace(".", "_") | ||
_import(module_name, f.path, loaded_configurations) | ||
|
||
if len(loaded_configurations) == 0: | ||
print(f"‼️ No configuration files found in '{config_dir}'.") | ||
raise ImportError(f"No configuration files found in '{config_dir}'.") | ||
|
||
return loaded_configurations | ||
|
||
|
||
## Specific Parts | ||
# This section's code actually loads the various configuration files | ||
# into the module with the given name. | ||
# It contains the logic to resolve arbitrary configuration options by | ||
# levaraging dynamic programming using `__getattr__`. | ||
|
||
|
||
_loaded_configurations = read_configurations( | ||
config_dir = '/etc/netbox/config/', | ||
config_module = 'netbox.configuration', | ||
main_config = 'configuration') | ||
|
||
|
||
def __getattr__(name): | ||
for config in _loaded_configurations: | ||
try: | ||
return getattr(config, name) | ||
except: | ||
pass | ||
raise AttributeError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
errorlog = '-' | ||
accesslog = '-' | ||
capture_output = False | ||
loglevel = 'debug' | ||
loglevel = 'info' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import importlib.util | ||
import sys | ||
from .configuration import read_configurations | ||
|
||
try: | ||
spec = importlib.util.spec_from_file_location('ldap_config', '/etc/netbox/config/ldap_config.py') | ||
module = importlib.util.module_from_spec(spec) | ||
spec.loader.exec_module(module) | ||
sys.modules['netbox.ldap_config'] = module | ||
except: | ||
raise ImportError('') | ||
_loaded_configurations = read_configurations( | ||
config_dir = '/etc/netbox/config/ldap/', | ||
config_module = 'netbox.configuration.ldap', | ||
main_config = 'ldap_config') | ||
|
||
|
||
def __getattr__(name): | ||
for config in _loaded_configurations: | ||
try: | ||
return getattr(config, name) | ||
except: | ||
pass | ||
raise AttributeError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters