-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
253 lines (207 loc) · 8.67 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import logging
import coloredlogs
import yaml
import os
import argparse
import sys
from typing import NamedTuple, Dict, Any
from types import SimpleNamespace
# Temporary logging configuration
coloredlogs.install(level="WARN")
class TLSCertificateConfig(NamedTuple):
"""A path to a certificate required by TLS."""
file: str
class DockerTLSConfig(NamedTuple):
"""The container representing the TLS config information needed by Docker"""
verify: TLSCertificateConfig
cert: TLSCertificateConfig
key: TLSCertificateConfig
enabled: bool
class DockerConfig(NamedTuple):
"""
Configuration for Docker.
Attributes:
host (str): The Docker host URL.
tls (DockerTLSConfig): The container that holds the Docker host URL and TLS configurations that go with it
"""
host: str
tls: DockerTLSConfig
class LogLevelConfig(NamedTuple):
"""
Configuration for logging levels.
Attributes:
general (str): The general logging level.
application (str): The application-specific logging level.
"""
general: str
application: str
class TraefikConfig(NamedTuple):
"""
Configuration for Traefik.
Attributes:
containerName (str): The name of the Traefik container.
monitoredLabel (str): The label used to identify monitored services.
networkLabel (str): The label used to identify the network.
"""
containerName: str
monitoredLabel: str
networkLabel: str
class Config(NamedTuple):
"""
The main configuration class.
Attributes:
docker (DockerConfig): The Docker configuration.
logLevel (LogLevelConfig): The logging level configuration.
traefik (TraefikConfig): The Traefik configuration.
"""
docker: DockerConfig
logLevel: LogLevelConfig
traefik: TraefikConfig
def dict_to_simplenamespace(dict_obj):
"""
Recursively converts a nested dictionary into a SimpleNamespace object.
Args:
dict_obj (dict): The input dictionary to be converted.
Returns:
SimpleNamespace: The resulting SimpleNamespace object.
"""
logging.info("Converting dictionary to SimpleNamespace")
logging.debug(f"Input dictionary: {dict_obj}")
if isinstance(dict_obj, dict):
for key, value in dict_obj.items():
logging.debug(f"Processing key: {key}")
dict_obj[key] = dict_to_simplenamespace(value)
elif isinstance(dict_obj, list):
logging.debug(f"Processing list with {len(dict_obj)} items")
return [dict_to_simplenamespace(item) for item in dict_obj]
result = SimpleNamespace(**dict_obj) if isinstance(dict_obj, dict) else dict_obj
logging.debug(f"Resulting SimpleNamespace or value: {result}")
return result
def flatten_keys(d, parent_key='', sep='.'):
"""
Flattens a nested dictionary into a flat dictionary with concatenated keys.
Args:
d (dict): The input nested dictionary to be flattened.
parent_key (str): The parent key for the current level of recursion.
sep (str): The separator used for concatenating keys.
Returns:
dict: The flattened dictionary.
"""
items = []
for k, v in d.items():
new_key = f"{parent_key}{sep}{k}" if parent_key else k
if isinstance(v, dict):
items.extend(flatten_keys(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def parse_args(config):
"""
Parses command line arguments based on the configuration.
Args:
config (dict): The configuration dictionary.
Returns:
argparse.Namespace: The parsed command line arguments.
"""
parser = argparse.ArgumentParser(
description='Command Line Interface for the Application')
parser.add_argument('-c', '--config', default=os.path.join(os.path.dirname(__file__), 'config.yaml'), type=str, help='Path to the config.yaml file')
config_flat = flatten_keys(config)
# Dynamically add arguments based on the config.yaml structure
for key, value in config_flat.items():
cli_key = key.replace('..', '.').lower() # Ensure CLI arguments are lowercase
parser.add_argument(f'--{cli_key}',
type=type(value),
default=value,
help=f'Specify the {cli_key} value')
args, unknown = parser.parse_known_args()
if unknown:
logging.critical(f"Unknown arguments provided: {unknown}. Exiting program.")
sys.exit(1)
return args
def load_config() -> Config:
"""
Loads configuration settings from a YAML file and applies overrides.
Returns:
Config: The final configuration as a Config object.
"""
logging.info("Loading configuration from file")
with open('config.yaml', "r") as file:
config_data = yaml.safe_load(file)
logging.debug("Loaded initial configuration: %s", config_data)
args = parse_args(config_data)
if args.config:
with open(args.config, "r") as file:
config_data = yaml.safe_load(file)
logging.info("Loaded configuration from file: %s", config_data)
apply_overrides_from_env_and_cli(config_data, args)
tls_cert_verify: TLSCertificateConfig = TLSCertificateConfig(
file=config_data["docker"]["tls"]["verify"])
tls_cert_container_cert: TLSCertificateConfig = TLSCertificateConfig(
file=config_data["docker"]["tls"]["cert"])
tls_cert_container_key: TLSCertificateConfig = TLSCertificateConfig(
file=config_data["docker"]["tls"]["key"])
docker_tls: DockerTLSConfig = DockerTLSConfig(
enabled=config_data["docker"]["tls"]["enabled"],
verify=tls_cert_verify,
cert=tls_cert_container_cert,
key=tls_cert_container_key)
# Traefik has it's own parameter for network, so we have to use it !
if 'networkLabel' not in config_data['traefik']:
config_data['traefik']['networkLabel'] = 'traefik.docker.network'
docker: DockerConfig = DockerConfig(host=config_data["docker"]["host"], tls=docker_tls)
log_level: LogLevelConfig = LogLevelConfig(
general=config_data["logLevel"]["general"],
application=config_data["logLevel"]["application"])
traefik: TraefikConfig = TraefikConfig(
containerName=config_data["traefik"]["containerName"],
monitoredLabel=config_data["traefik"]["monitoredLabel"],
networkLabel=config_data["traefik"]["networkLabel"])
config: Config = Config(docker=docker, logLevel=log_level, traefik=traefik)
logging.debug("Final configuration as Config: %s", config)
return config
def apply_overrides_from_env_and_cli(config, args):
"""
Applies environment variables and command line arguments as overrides to the configuration.
Args:
config (dict): The configuration dictionary to be modified.
args (argparse.Namespace): The parsed command line arguments.
"""
def apply_overrides(config, prefix=''):
env_vars = {k.upper(): v for k, v in os.environ.items()}
cli_args = vars(args) # Convert Namespace to dict for easier access
for key, value in config.items():
full_key = f"{prefix}{key}".upper()
env_key = full_key.replace('.', '_')
arg_key = full_key.lower()
if isinstance(value, dict):
apply_overrides(value, prefix=f"{full_key}.")
else:
if env_key in env_vars:
config[key] = type(value)(env_vars[env_key]) # Convert env var to correct type
if arg_key in cli_args and cli_args[arg_key] is not None:
config[key] = cli_args[arg_key] # Directly use the value from CLI args
apply_overrides(config)
def init_loggers(config: Config) -> logging.Logger:
"""
Initializes logging based on the configuration.
Args:
config (Config): The configuration dictionary.
Returns:
logging.Logger: The application logger instance.
"""
# Configure logging based on configuration
coloredlogs.install(level=config.logLevel.general)
app_logger = logging.getLogger("application")
app_logger.setLevel(config.logLevel.application) # Application level specifically
# Disable propagation of logging to other loggers
app_logger.propagate = False
# Handler for the application logger (if you want to also log to file or elsewhere)
stream_handler = logging.StreamHandler()
formatter = coloredlogs.ColoredFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stream_handler.setFormatter(formatter)
app_logger.addHandler(stream_handler)
return app_logger
# Load configuration and initialize logger
config: Config = load_config()
app_logger = init_loggers(config)