ob-metaflow-extensions 1.1.175rc1__py2.py3-none-any.whl → 1.1.175rc3__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ob-metaflow-extensions might be problematic. Click here for more details.

Files changed (28) hide show
  1. metaflow_extensions/outerbounds/plugins/__init__.py +1 -1
  2. metaflow_extensions/outerbounds/plugins/apps/app_deploy_decorator.py +112 -0
  3. metaflow_extensions/outerbounds/plugins/apps/core/__init__.py +9 -0
  4. metaflow_extensions/outerbounds/plugins/apps/core/app_cli.py +42 -374
  5. metaflow_extensions/outerbounds/plugins/apps/core/app_config.py +45 -225
  6. metaflow_extensions/outerbounds/plugins/apps/core/capsule.py +22 -6
  7. metaflow_extensions/outerbounds/plugins/apps/core/code_package/code_packager.py +16 -15
  8. metaflow_extensions/outerbounds/plugins/apps/core/config/__init__.py +12 -0
  9. metaflow_extensions/outerbounds/plugins/apps/core/config/cli_generator.py +161 -0
  10. metaflow_extensions/outerbounds/plugins/apps/core/config/config_utils.py +828 -0
  11. metaflow_extensions/outerbounds/plugins/apps/core/config/schema_export.py +285 -0
  12. metaflow_extensions/outerbounds/plugins/apps/core/config/typed_configs.py +104 -0
  13. metaflow_extensions/outerbounds/plugins/apps/core/config/typed_init_generator.py +317 -0
  14. metaflow_extensions/outerbounds/plugins/apps/core/config/unified_config.py +994 -0
  15. metaflow_extensions/outerbounds/plugins/apps/core/config_schema.yaml +217 -211
  16. metaflow_extensions/outerbounds/plugins/apps/core/dependencies.py +3 -3
  17. metaflow_extensions/outerbounds/plugins/apps/core/deployer.py +132 -0
  18. metaflow_extensions/outerbounds/plugins/apps/core/experimental/__init__.py +4 -36
  19. metaflow_extensions/outerbounds/plugins/apps/core/perimeters.py +44 -2
  20. metaflow_extensions/outerbounds/plugins/apps/core/validations.py +4 -9
  21. metaflow_extensions/outerbounds/toplevel/global_aliases_for_metaflow_package.py +1 -0
  22. metaflow_extensions/outerbounds/toplevel/ob_internal.py +1 -0
  23. {ob_metaflow_extensions-1.1.175rc1.dist-info → ob_metaflow_extensions-1.1.175rc3.dist-info}/METADATA +1 -1
  24. {ob_metaflow_extensions-1.1.175rc1.dist-info → ob_metaflow_extensions-1.1.175rc3.dist-info}/RECORD +26 -20
  25. metaflow_extensions/outerbounds/plugins/apps/core/cli_to_config.py +0 -99
  26. metaflow_extensions/outerbounds/plugins/apps/core/config_schema_autogen.json +0 -336
  27. {ob_metaflow_extensions-1.1.175rc1.dist-info → ob_metaflow_extensions-1.1.175rc3.dist-info}/WHEEL +0 -0
  28. {ob_metaflow_extensions-1.1.175rc1.dist-info → ob_metaflow_extensions-1.1.175rc3.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,994 @@
1
+ """
2
+ Unified Configuration System for Outerbounds Apps
3
+
4
+ This module provides a type-safe, declarative configuration system that serves as the
5
+ single source of truth for app configuration. It automatically generates CLI options,
6
+ handles config file parsing, and manages field merging behavior.
7
+
8
+ No external dependencies required - uses only Python standard library.
9
+ """
10
+
11
+
12
+ import os
13
+ import json
14
+ from typing import Any, Dict, List, Optional, Union, Type
15
+ import re
16
+
17
+ from .config_utils import (
18
+ ConfigField,
19
+ ConfigMeta,
20
+ JsonFriendlyKeyValuePairType,
21
+ PureStringKVPairType,
22
+ CommaSeparatedListType,
23
+ FieldBehavior,
24
+ CLIOption,
25
+ config_meta_to_dict,
26
+ merge_field_values,
27
+ apply_defaults,
28
+ populate_config_recursive,
29
+ validate_config_meta,
30
+ validate_required_fields,
31
+ ConfigValidationFailedException,
32
+ )
33
+
34
+
35
+ class AuthType:
36
+ BROWSER = "Browser"
37
+ API = "API"
38
+
39
+ @classmethod
40
+ def choices(cls):
41
+ return [cls.BROWSER, cls.API]
42
+
43
+
44
+ class UnitParser:
45
+ UNIT_FREE_REGEX = r"^\d+$"
46
+
47
+ metrics = {
48
+ "memory": {
49
+ "default_unit": "Mi",
50
+ "requires_unit": True, # if a Unit free value is provided then we will add the default unit to it.
51
+ # Regex to match values with units (e.g., "512Mi", "4Gi", "1024Ki")
52
+ "unit_regex": r"^\d+(\.\d+)?(Ki|Mi|Gi|Ti|Pi|Ei)$",
53
+ },
54
+ "cpu": {
55
+ "default_unit": None,
56
+ "requires_unit": False, # if a Unit free value is provided then we will not add the default unit to it.
57
+ # Accepts values like 400m, 4, 0.4, 1000n, etc.
58
+ # Regex to match values with units (e.g., "400m", "1000n", "2", "0.5")
59
+ "unit_regex": r"^(\d+(\.\d+)?(m|n)?|\d+(\.\d+)?)$",
60
+ },
61
+ "disk": {
62
+ "default_unit": "Mi",
63
+ "requires_unit": True, # if a Unit free value is provided then we will add the default unit to it.
64
+ # Regex to match values with units (e.g., "100Mi", "1Gi", "500Ki")
65
+ "unit_regex": r"^\d+(\.\d+)?(Ki|Mi|Gi|Ti|Pi|Ei)$",
66
+ },
67
+ "gpu": {
68
+ "default_unit": None,
69
+ "requires_unit": False,
70
+ # Regex to match values with units (usually just integer count, e.g., "1", "2")
71
+ "unit_regex": r"^\d+$",
72
+ },
73
+ }
74
+
75
+ def __init__(self, metric_name: str):
76
+ self.metric_name = metric_name
77
+
78
+ def validate(self, value: str):
79
+ if self.metrics[self.metric_name]["requires_unit"]:
80
+ if not re.match(self.metrics[self.metric_name]["unit_regex"], value):
81
+ return False
82
+ return True
83
+
84
+ def process(self, value: str):
85
+ value = str(value)
86
+ if self.metrics[self.metric_name]["requires_unit"]:
87
+ if re.match(self.UNIT_FREE_REGEX, value):
88
+ # This means the value is unit free and we need to add the default unit to it.
89
+ value = "%s%s" % (
90
+ value.strip(),
91
+ self.metrics[self.metric_name]["default_unit"],
92
+ )
93
+ return value
94
+
95
+ if re.match(self.metrics[self.metric_name]["unit_regex"], value):
96
+ return value
97
+
98
+ return value
99
+
100
+ def parse(self, value: str):
101
+ if value is None:
102
+ return None
103
+ return self.process(value)
104
+
105
+ @staticmethod
106
+ def validation_wrapper_fn(
107
+ metric_name: str,
108
+ ):
109
+ def validation_fn(value: str):
110
+ if value is None:
111
+ return True
112
+ field_info = ResourceConfig._get_field(ResourceConfig, metric_name) # type: ignore
113
+ parser = UnitParser(metric_name)
114
+ validation = parser.validate(value)
115
+ if not validation:
116
+ raise ConfigValidationFailedException(
117
+ field_name=metric_name,
118
+ field_info=field_info,
119
+ current_value=value,
120
+ message=f"Invalid value for `{metric_name}`. Must be of the format {parser.metrics[metric_name]['unit_regex']}.",
121
+ )
122
+ return validation
123
+
124
+ return validation_fn
125
+
126
+
127
+ class BasicValidations:
128
+ def __init__(self, config_meta_class, field_name):
129
+ self.config_meta_class = config_meta_class
130
+ self.field_name = field_name
131
+
132
+ def _get_field(self):
133
+ return self.config_meta_class._get_field(self.config_meta_class, self.field_name) # type: ignore
134
+
135
+ def enum_validation(self, enums: List[str], current_value):
136
+ if current_value not in enums:
137
+ raise ConfigValidationFailedException(
138
+ field_name=self.field_name,
139
+ field_info=self._get_field(),
140
+ current_value=current_value,
141
+ message=f"Configuration field {self.field_name} has invalid value {current_value}. Value must be one of: {' '.join(enums)}",
142
+ )
143
+ return True
144
+
145
+ def range_validation(self, min_value, max_value, current_value):
146
+ if current_value < min_value or current_value > max_value:
147
+ raise ConfigValidationFailedException(
148
+ field_name=self.field_name,
149
+ field_info=self._get_field(),
150
+ current_value=current_value,
151
+ message=f"Configuration field {self.field_name} has invalid value {current_value}. Value must be between {min_value} and {max_value}",
152
+ )
153
+ return True
154
+
155
+ def length_validation(self, max_length, current_value):
156
+ if len(current_value) > max_length:
157
+ raise ConfigValidationFailedException(
158
+ field_name=self.field_name,
159
+ field_info=self._get_field(),
160
+ current_value=current_value,
161
+ message=f"Configuration field {self.field_name} has invalid value {current_value}. Value must be less than {max_length}",
162
+ )
163
+ return True
164
+
165
+ def regex_validation(self, regex, current_value):
166
+ if not re.match(regex, current_value):
167
+ raise ConfigValidationFailedException(
168
+ field_name=self.field_name,
169
+ field_info=self._get_field(),
170
+ current_value=current_value,
171
+ message=f"Configuration field {self.field_name} has invalid value {current_value}. Value must match regex {regex}",
172
+ )
173
+ return True
174
+
175
+
176
+ class ResourceConfig(metaclass=ConfigMeta):
177
+ """Resource configuration for the app."""
178
+
179
+ # TODO: Add Unit Validation/Parsing Support for the Fields.
180
+ cpu = ConfigField(
181
+ default="1",
182
+ cli_meta=CLIOption(
183
+ name="cpu",
184
+ cli_option_str="--cpu",
185
+ help="CPU resource request and limit.",
186
+ ),
187
+ field_type=str,
188
+ example="500m",
189
+ validation_fn=UnitParser.validation_wrapper_fn("cpu"),
190
+ parsing_fn=UnitParser("cpu").parse,
191
+ )
192
+ memory = ConfigField(
193
+ default="4Gi",
194
+ cli_meta=CLIOption(
195
+ name="memory",
196
+ cli_option_str="--memory",
197
+ help="Memory resource request and limit.",
198
+ ),
199
+ field_type=str,
200
+ example="512Mi",
201
+ validation_fn=UnitParser.validation_wrapper_fn("memory"),
202
+ parsing_fn=UnitParser("memory").parse,
203
+ )
204
+ gpu = ConfigField(
205
+ cli_meta=CLIOption(
206
+ name="gpu",
207
+ cli_option_str="--gpu",
208
+ help="GPU resource request and limit.",
209
+ ),
210
+ field_type=str,
211
+ example="1",
212
+ validation_fn=UnitParser.validation_wrapper_fn("gpu"),
213
+ parsing_fn=UnitParser("gpu").parse,
214
+ )
215
+ disk = ConfigField(
216
+ default="20Gi",
217
+ cli_meta=CLIOption(
218
+ name="disk",
219
+ cli_option_str="--disk",
220
+ help="Storage resource request and limit.",
221
+ ),
222
+ field_type=str,
223
+ example="1Gi",
224
+ validation_fn=UnitParser.validation_wrapper_fn("disk"),
225
+ parsing_fn=UnitParser("disk").parse,
226
+ )
227
+
228
+
229
+ class HealthCheckConfig(metaclass=ConfigMeta):
230
+ """Health check configuration."""
231
+
232
+ enabled = ConfigField(
233
+ default=False,
234
+ cli_meta=CLIOption(
235
+ name="health_check_enabled",
236
+ cli_option_str="--health-check-enabled",
237
+ help="Whether to enable health checks.",
238
+ is_flag=True,
239
+ ),
240
+ field_type=bool,
241
+ example=True,
242
+ )
243
+ path = ConfigField(
244
+ cli_meta=CLIOption(
245
+ name="health_check_path",
246
+ cli_option_str="--health-check-path",
247
+ help="The path for health checks.",
248
+ ),
249
+ field_type=str,
250
+ example="/health",
251
+ )
252
+ initial_delay_seconds = ConfigField(
253
+ cli_meta=CLIOption(
254
+ name="health_check_initial_delay",
255
+ cli_option_str="--health-check-initial-delay",
256
+ help="Number of seconds to wait before performing the first health check.",
257
+ ),
258
+ field_type=int,
259
+ example=10,
260
+ )
261
+ period_seconds = ConfigField(
262
+ cli_meta=CLIOption(
263
+ name="health_check_period",
264
+ cli_option_str="--health-check-period",
265
+ help="How often to perform the health check.",
266
+ ),
267
+ field_type=int,
268
+ example=30,
269
+ )
270
+
271
+
272
+ class AuthConfig(metaclass=ConfigMeta):
273
+ """Authentication configuration."""
274
+
275
+ type = ConfigField(
276
+ default=AuthType.BROWSER,
277
+ cli_meta=CLIOption(
278
+ name="auth_type",
279
+ cli_option_str="--auth-type",
280
+ help="The type of authentication to use for the app.",
281
+ choices=AuthType.choices(),
282
+ ),
283
+ field_type=str,
284
+ example="Browser",
285
+ )
286
+ public = ConfigField(
287
+ default=True,
288
+ cli_meta=CLIOption(
289
+ name="auth_public",
290
+ cli_option_str="--public-access/--private-access",
291
+ help="Whether the app is public or not.",
292
+ is_flag=True,
293
+ ),
294
+ field_type=bool,
295
+ example=True,
296
+ )
297
+
298
+ @staticmethod
299
+ def validate(auth_config: "AuthConfig"):
300
+ if auth_config.type is None:
301
+ return True
302
+ return BasicValidations(AuthConfig, "type").enum_validation(
303
+ AuthType.choices(), auth_config.type
304
+ )
305
+
306
+
307
+ class ReplicaConfig(metaclass=ConfigMeta):
308
+ """Replica configuration."""
309
+
310
+ fixed = ConfigField(
311
+ cli_meta=CLIOption(
312
+ name="fixed_replicas",
313
+ cli_option_str="--fixed-replicas",
314
+ help="The fixed number of replicas to deploy the app with. If min and max are set, this will raise an error.",
315
+ ),
316
+ field_type=int,
317
+ example=1,
318
+ )
319
+
320
+ min = ConfigField(
321
+ cli_meta=CLIOption(
322
+ name="min_replicas",
323
+ cli_option_str="--min-replicas",
324
+ help="The minimum number of replicas to deploy the app with.",
325
+ ),
326
+ field_type=int,
327
+ example=1,
328
+ )
329
+ max = ConfigField(
330
+ cli_meta=CLIOption(
331
+ name="max_replicas",
332
+ cli_option_str="--max-replicas",
333
+ help="The maximum number of replicas to deploy the app with.",
334
+ ),
335
+ field_type=int,
336
+ example=10,
337
+ )
338
+
339
+ @staticmethod
340
+ def defaults(replica_config: "ReplicaConfig"):
341
+ if all(
342
+ [
343
+ replica_config.min is None,
344
+ replica_config.max is None,
345
+ replica_config.fixed is None,
346
+ ]
347
+ ):
348
+ replica_config.fixed = 1
349
+ return
350
+
351
+ @staticmethod
352
+ def validate(replica_config: "ReplicaConfig"):
353
+ # TODO: Have a better validation story.
354
+ both_min_max_set = (
355
+ replica_config.min is not None and replica_config.max is not None
356
+ )
357
+ fixed_set = replica_config.fixed is not None
358
+ max_is_set = replica_config.max is not None
359
+ min_is_set = replica_config.min is not None
360
+ any_min_max_set = (
361
+ replica_config.min is not None or replica_config.max is not None
362
+ )
363
+
364
+ def _greater_than_equals_zero(x):
365
+ return x is not None and x >= 0
366
+
367
+ if both_min_max_set and replica_config.min > replica_config.max: # type: ignore
368
+ raise ConfigValidationFailedException(
369
+ field_name="min",
370
+ field_info=replica_config._get_field("min"), # type: ignore
371
+ current_value=replica_config.min,
372
+ message="Min replicas cannot be greater than max replicas",
373
+ )
374
+ if fixed_set and any_min_max_set:
375
+ raise ConfigValidationFailedException(
376
+ field_name="fixed",
377
+ field_info=replica_config._get_field("fixed"), # type: ignore
378
+ current_value=replica_config.fixed,
379
+ message="Fixed replicas cannot be set when min or max replicas are set",
380
+ )
381
+
382
+ if max_is_set and not min_is_set:
383
+ raise ConfigValidationFailedException(
384
+ field_name="min",
385
+ field_info=replica_config._get_field("min"), # type: ignore
386
+ current_value=replica_config.min,
387
+ message="If max replicas is set then min replicas must be set too.",
388
+ )
389
+
390
+ if fixed_set and replica_config.fixed < 0: # type: ignore
391
+ raise ConfigValidationFailedException(
392
+ field_name="fixed",
393
+ field_info=replica_config._get_field("fixed"), # type: ignore
394
+ current_value=replica_config.fixed,
395
+ message="Fixed replicas cannot be less than 0",
396
+ )
397
+
398
+ if min_is_set and not _greater_than_equals_zero(replica_config.min):
399
+ raise ConfigValidationFailedException(
400
+ field_name="min",
401
+ field_info=replica_config._get_field("min"), # type: ignore
402
+ current_value=replica_config.min,
403
+ message="Min replicas cannot be less than 0",
404
+ )
405
+
406
+ if max_is_set and not _greater_than_equals_zero(replica_config.max):
407
+ raise ConfigValidationFailedException(
408
+ field_name="max",
409
+ field_info=replica_config._get_field("max"), # type: ignore
410
+ current_value=replica_config.max,
411
+ message="Max replicas cannot be less than 0",
412
+ )
413
+ return True
414
+
415
+
416
+ def more_than_n_not_none(n, *args):
417
+ return sum(1 for arg in args if arg is not None) > n
418
+
419
+
420
+ class DependencyConfig(metaclass=ConfigMeta):
421
+ """Dependency configuration."""
422
+
423
+ from_requirements_file = ConfigField(
424
+ cli_meta=CLIOption(
425
+ name="dep_from_requirements",
426
+ cli_option_str="--dep-from-requirements",
427
+ help="The path to the requirements.txt file to attach to the app.",
428
+ ),
429
+ field_type=str,
430
+ behavior=FieldBehavior.NOT_ALLOWED,
431
+ example="requirements.txt",
432
+ )
433
+ from_pyproject_toml = ConfigField(
434
+ cli_meta=CLIOption(
435
+ name="dep_from_pyproject",
436
+ cli_option_str="--dep-from-pyproject",
437
+ help="The path to the pyproject.toml file to attach to the app.",
438
+ ),
439
+ field_type=str,
440
+ behavior=FieldBehavior.NOT_ALLOWED,
441
+ example="pyproject.toml",
442
+ )
443
+ python = ConfigField(
444
+ cli_meta=CLIOption(
445
+ name="python",
446
+ cli_option_str="--python",
447
+ help="The Python version to use for the app.",
448
+ ),
449
+ field_type=str,
450
+ behavior=FieldBehavior.NOT_ALLOWED,
451
+ example="3.10",
452
+ )
453
+ pypi = ConfigField(
454
+ cli_meta=CLIOption(
455
+ name="pypi", # TODO: Can set CLI meta to None
456
+ cli_option_str="--pypi",
457
+ help="A dictionary of pypi dependencies to attach to the app. The key is the package name and the value is the version.",
458
+ hidden=True, # Complex structure, better handled in config file
459
+ ),
460
+ field_type=dict,
461
+ behavior=FieldBehavior.NOT_ALLOWED,
462
+ example={"numpy": "1.23.0", "pandas": ""},
463
+ )
464
+ conda = ConfigField(
465
+ cli_meta=CLIOption( # TODO: Can set CLI meta to None
466
+ name="conda",
467
+ cli_option_str="--conda",
468
+ help="A dictionary of conda dependencies to attach to the app. The key is the package name and the value is the version.",
469
+ hidden=True, # Complex structure, better handled in config file
470
+ ),
471
+ field_type=dict,
472
+ behavior=FieldBehavior.NOT_ALLOWED,
473
+ example={"numpy": "1.23.0", "pandas": ""},
474
+ )
475
+
476
+ @staticmethod
477
+ def validate(dependency_config: "DependencyConfig"):
478
+ # You can either have from_requirements_file or from_pyproject_toml or python with pypi or conda
479
+ # but not more than one of them.
480
+ if more_than_n_not_none(
481
+ 1,
482
+ dependency_config.from_requirements_file,
483
+ dependency_config.from_pyproject_toml,
484
+ ):
485
+ raise ConfigValidationFailedException(
486
+ field_name="from_requirements_file",
487
+ field_info=dependency_config._get_field("from_requirements_file"), # type: ignore
488
+ current_value=dependency_config.from_requirements_file,
489
+ message="Cannot set from_requirements_file and from_pyproject_toml at the same time",
490
+ )
491
+ if any([dependency_config.pypi, dependency_config.conda]) and any(
492
+ [
493
+ dependency_config.from_requirements_file,
494
+ dependency_config.from_pyproject_toml,
495
+ ]
496
+ ):
497
+ raise ConfigValidationFailedException(
498
+ field_name="pypi" if dependency_config.pypi else "conda",
499
+ field_info=dependency_config._get_field( # type: ignore
500
+ "pypi" if dependency_config.pypi else "conda"
501
+ ),
502
+ current_value=dependency_config.pypi or dependency_config.conda,
503
+ message="Cannot set pypi or conda when from_requirements_file or from_pyproject_toml is set",
504
+ )
505
+ return True
506
+
507
+
508
+ class PackageConfig(metaclass=ConfigMeta):
509
+ """Package configuration."""
510
+
511
+ src_path = ConfigField(
512
+ cli_meta=CLIOption(
513
+ name="package_src_path",
514
+ cli_option_str="--package-src-path",
515
+ help="The path to the source code to deploy with the App.",
516
+ ),
517
+ field_type=str,
518
+ example="./",
519
+ )
520
+ suffixes = ConfigField(
521
+ cli_meta=CLIOption(
522
+ name="package_suffixes",
523
+ cli_option_str="--package-suffixes",
524
+ help="A list of suffixes to add to the source code to deploy with the App.",
525
+ ),
526
+ field_type=list,
527
+ example=[".py", ".ipynb"],
528
+ )
529
+
530
+
531
+ class BasicAppValidations:
532
+ @staticmethod
533
+ def name(name):
534
+ if name is None:
535
+ return True
536
+ regex = r"^[a-z0-9-]+$" # Only allow lowercase letters, numbers, and hyphens
537
+ validator = BasicValidations(CoreConfig, "name")
538
+ return validator.length_validation(20, name) and validator.regex_validation(
539
+ regex, name
540
+ )
541
+
542
+ @staticmethod
543
+ def port(port):
544
+ if port is None:
545
+ return True
546
+ return BasicValidations(CoreConfig, "port").range_validation(1, 65535, port)
547
+
548
+ @staticmethod
549
+ def tags(tags):
550
+ if tags is None:
551
+ return True
552
+ if not all(isinstance(tag, dict) and len(tag) == 1 for tag in tags):
553
+ raise ConfigValidationFailedException(
554
+ field_name="tags",
555
+ field_info=CoreConfig._get_field(CoreConfig, "tags"), # type: ignore
556
+ current_value=tags,
557
+ message="Tags must be a list of dictionaries with one key. Currently they are set to %s "
558
+ % (str(tags)),
559
+ )
560
+ return True
561
+
562
+ @staticmethod
563
+ def secrets(secrets):
564
+ if secrets is None: # If nothing is set we dont care.
565
+ return True
566
+
567
+ if not isinstance(secrets, list):
568
+ raise ConfigValidationFailedException(
569
+ field_name="secrets",
570
+ field_info=CoreConfig._get_field(CoreConfig, "secrets"), # type: ignore
571
+ current_value=secrets,
572
+ message="Secrets must be a list of strings. Currently they are set to %s "
573
+ % (str(secrets)),
574
+ )
575
+ from ..validations import secrets_validator
576
+
577
+ try:
578
+ secrets_validator(secrets)
579
+ except Exception as e:
580
+ raise ConfigValidationFailedException(
581
+ field_name="secrets",
582
+ field_info=CoreConfig._get_field(CoreConfig, "secrets"), # type: ignore
583
+ current_value=secrets,
584
+ message=f"Secrets validation failed, {e}",
585
+ )
586
+ return True
587
+
588
+ @staticmethod
589
+ def persistence(persistence):
590
+ if persistence is None:
591
+ return True
592
+ return BasicValidations(CoreConfig, "persistence").enum_validation(
593
+ ["none", "postgres"], persistence
594
+ )
595
+
596
+
597
+ class CoreConfig(metaclass=ConfigMeta):
598
+ """Unified App Configuration - The single source of truth for application configuration.
599
+
600
+ CoreConfig is the central configuration class that defines all application settings using the
601
+ ConfigMeta metaclass and ConfigField descriptors. It provides a declarative, type-safe way
602
+ to manage configuration from multiple sources (CLI, config files, environment) with automatic
603
+ validation, merging, and CLI generation.
604
+
605
+ Core Features:
606
+ - **Declarative Configuration**: All fields are defined using ConfigField descriptors
607
+ - **Multi-Source Configuration**: Supports CLI options, config files (JSON/YAML), and programmatic setting
608
+ - **Automatic CLI Generation**: CLI options are automatically generated from field metadata
609
+ - **Type Safety**: Built-in type checking and validation for all fields
610
+ - **Hierarchical Structure**: Supports nested configuration objects (resources, auth, dependencies)
611
+ - **Intelligent Merging**: Configurable merging behavior for different field types
612
+ - **Validation Framework**: Comprehensive validation with custom validation functions
613
+
614
+ Configuration Lifecycle:
615
+ 1. **Definition**: Fields are defined declaratively using ConfigField descriptors
616
+ 2. **Instantiation**: Objects are created with all fields initialized to None or nested objects
617
+ 3. **Population**: Values are populated from CLI options, config files, or direct assignment
618
+ 4. **Merging**: Multiple config sources are merged according to field behavior settings
619
+ 5. **Validation**: Field validation functions and required field checks are performed
620
+ 6. **Default Application**: Default values are applied to any remaining None fields
621
+ 7. **Commit**: Final validation and preparation for use
622
+
623
+
624
+ Usage Examples:
625
+ Create from CLI options:
626
+ ```python
627
+ config = CoreConfig.from_cli({
628
+ 'name': 'myapp',
629
+ 'port': 8080,
630
+ 'commands': ['python app.py']
631
+ })
632
+ ```
633
+
634
+ Create from config file:
635
+ ```python
636
+ config = CoreConfig.from_file('config.yaml')
637
+ ```
638
+
639
+ Create from dictionary:
640
+ ```python
641
+ config = CoreConfig.from_dict({
642
+ 'name': 'myapp',
643
+ 'port': 8080,
644
+ 'resources': {
645
+ 'cpu': '500m',
646
+ 'memory': '1Gi'
647
+ }
648
+ })
649
+ ```
650
+
651
+ Merge configurations:
652
+ ```python
653
+ file_config = CoreConfig.from_file('config.yaml')
654
+ cli_config = CoreConfig.from_cli(cli_options)
655
+ final_config = CoreConfig.merge_configs(file_config, cli_config)
656
+ final_config.commit() # Validate and apply defaults
657
+ ```
658
+ """
659
+
660
+ # TODO: We can add Force Upgrade / No Deps flags here too if we need to.
661
+ # Since those can be exposed on the CLI side and the APP state will anyways
662
+ # be expored before being worked upon.
663
+
664
+ SCHEMA_DOC = """Schema for defining Outerbounds Apps configuration. This schema is what we will end up using on the CLI/programmatic interface.
665
+ How to read this schema:
666
+ 1. If the a property has `mutation_behavior` set to `union` then it will allow overrides of values at runtime from the CLI.
667
+ 2. If the property has `mutation_behavior`set to `not_allowed` then either the CLI or the config file value will be used (which ever is not None). If the user supplies something in both then an error will be raised.
668
+ 3. If a property has `experimental` set to true then a lot its validations may-be skipped and parsing handled somewhere else.
669
+ """
670
+
671
+ # Required fields
672
+ name = ConfigField(
673
+ cli_meta=CLIOption(
674
+ name="name",
675
+ cli_option_str="--name",
676
+ ),
677
+ validation_fn=BasicAppValidations.name,
678
+ field_type=str,
679
+ required=True,
680
+ help="The name of the app to deploy.",
681
+ example="myapp",
682
+ )
683
+ port = ConfigField(
684
+ cli_meta=CLIOption(
685
+ name="port",
686
+ cli_option_str="--port",
687
+ ),
688
+ validation_fn=BasicAppValidations.port,
689
+ field_type=int,
690
+ required=True,
691
+ help="Port where the app is hosted. When deployed this will be port on which we will deploy the app.",
692
+ example=8000,
693
+ )
694
+
695
+ # Optional basic fields
696
+ description = ConfigField(
697
+ cli_meta=CLIOption(
698
+ name="description",
699
+ cli_option_str="--description",
700
+ help="The description of the app to deploy.",
701
+ ),
702
+ field_type=str,
703
+ example="This is a description of my app.",
704
+ )
705
+ app_type = ConfigField(
706
+ cli_meta=CLIOption(
707
+ name="app_type",
708
+ cli_option_str="--app-type",
709
+ help="The User defined type of app to deploy. Its only used for bookkeeping purposes.",
710
+ ),
711
+ field_type=str,
712
+ example="MyCustomAgent",
713
+ )
714
+ image = ConfigField(
715
+ cli_meta=CLIOption(
716
+ name="image",
717
+ cli_option_str="--image",
718
+ help="The Docker image to deploy with the App.",
719
+ ),
720
+ field_type=str,
721
+ example="python:3.10-slim",
722
+ )
723
+
724
+ # List fields
725
+ tags = ConfigField(
726
+ cli_meta=CLIOption(
727
+ name="tags",
728
+ cli_option_str="--tag",
729
+ multiple=True,
730
+ click_type=PureStringKVPairType,
731
+ ),
732
+ field_type=list,
733
+ validation_fn=BasicAppValidations.tags,
734
+ help="The tags of the app to deploy.",
735
+ example=[{"foo": "bar"}, {"x": "y"}],
736
+ )
737
+ secrets = ConfigField(
738
+ cli_meta=CLIOption(
739
+ name="secrets", cli_option_str="--secret", multiple=True, click_type=str
740
+ ),
741
+ field_type=list,
742
+ help="Outerbounds integrations to attach to the app. You can use the value you set in the `@secrets` decorator in your code.",
743
+ example=["hf-token"],
744
+ validation_fn=BasicAppValidations.secrets,
745
+ )
746
+ compute_pools = ConfigField(
747
+ cli_meta=CLIOption(
748
+ name="compute_pools",
749
+ cli_option_str="--compute-pools",
750
+ help="A list of compute pools to deploy the app to.",
751
+ multiple=True,
752
+ click_type=str,
753
+ ),
754
+ field_type=list,
755
+ example=["default", "large"],
756
+ )
757
+ environment = ConfigField(
758
+ cli_meta=CLIOption(
759
+ name="environment",
760
+ cli_option_str="--env",
761
+ multiple=True,
762
+ click_type=JsonFriendlyKeyValuePairType, # TODO: Fix me.
763
+ ),
764
+ field_type=dict,
765
+ help="Environment variables to deploy with the App.",
766
+ example={
767
+ "DEBUG": True,
768
+ "DATABASE_CONFIG": {"host": "localhost", "port": 5432},
769
+ "ALLOWED_ORIGINS": ["http://localhost:3000", "https://myapp.com"],
770
+ },
771
+ )
772
+ commands = ConfigField(
773
+ cli_meta=None, # We dont expose commands as an options. We rather expose it like `--` with click.
774
+ field_type=list,
775
+ required=True, # Either from CLI or from config file.
776
+ help="A list of commands to run the app with.", # TODO: Fix me: make me configurable via the -- stuff in click.
777
+ example=["python app.py", "python app.py --foo bar"],
778
+ behavior=FieldBehavior.NOT_ALLOWED,
779
+ )
780
+
781
+ # Complex nested fields
782
+ resources = ConfigField(
783
+ cli_meta=None, # No top-level CLI option, only nested fields have CLI options
784
+ field_type=ResourceConfig,
785
+ # TODO : see if we can add a validation func for resources.
786
+ help="Resource configuration for the app.",
787
+ )
788
+ auth = ConfigField(
789
+ cli_meta=None, # No top-level CLI option, only nested fields have CLI options
790
+ field_type=AuthConfig,
791
+ help="Auth related configurations.",
792
+ validation_fn=AuthConfig.validate,
793
+ )
794
+ replicas = ConfigField(
795
+ cli_meta=None, # No top-level CLI option, only nested fields have CLI options
796
+ validation_fn=ReplicaConfig.validate,
797
+ field_type=ReplicaConfig,
798
+ default=ReplicaConfig.defaults,
799
+ help="The number of replicas to deploy the app with.",
800
+ )
801
+ dependencies = ConfigField(
802
+ cli_meta=None, # No top-level CLI option, only nested fields have CLI options
803
+ validation_fn=DependencyConfig.validate,
804
+ field_type=DependencyConfig,
805
+ help="The dependencies to attach to the app. ",
806
+ )
807
+ package = ConfigField(
808
+ cli_meta=None, # No top-level CLI option, only nested fields have CLI options
809
+ field_type=PackageConfig,
810
+ help="Configurations associated with packaging the app.",
811
+ )
812
+
813
+ no_deps = ConfigField(
814
+ cli_meta=CLIOption(
815
+ name="no_deps",
816
+ cli_option_str="--no-deps",
817
+ help="Do not any dependencies. Directly used the image provided",
818
+ is_flag=True,
819
+ ),
820
+ field_type=bool,
821
+ default=False,
822
+ help="Do not bake any dependencies. Directly used the image provided",
823
+ )
824
+
825
+ force_upgrade = ConfigField(
826
+ cli_meta=CLIOption(
827
+ name="force_upgrade",
828
+ cli_option_str="--force-upgrade",
829
+ help="Force upgrade the app even if it is currently being upgraded.",
830
+ is_flag=True,
831
+ ),
832
+ field_type=bool,
833
+ default=False,
834
+ help="Force upgrade the app even if it is currently being upgraded.",
835
+ )
836
+
837
+ # ------- Experimental -------------
838
+ # These options get treated in the `..experimental` module.
839
+ # If we move any option as a first class citizen then we need to move
840
+ # its capsule parsing from the `..experimental` module to the `..capsule.CapsuleInput` module.
841
+
842
+ persistence = ConfigField(
843
+ cli_meta=CLIOption(
844
+ name="persistence",
845
+ cli_option_str="--persistence",
846
+ help="The persistence mode to deploy the app with.",
847
+ choices=["none", "postgres"],
848
+ ),
849
+ validation_fn=BasicAppValidations.persistence,
850
+ field_type=str,
851
+ default="none",
852
+ example="postgres",
853
+ is_experimental=True,
854
+ )
855
+
856
+ project = ConfigField(
857
+ cli_meta=CLIOption(
858
+ name="project",
859
+ cli_option_str="--project",
860
+ help="The project name to deploy the app to.",
861
+ ),
862
+ field_type=str,
863
+ is_experimental=True,
864
+ example="my-project",
865
+ )
866
+ branch = ConfigField(
867
+ cli_meta=CLIOption(
868
+ name="branch",
869
+ cli_option_str="--branch",
870
+ help="The branch name to deploy the app to.",
871
+ ),
872
+ field_type=str,
873
+ is_experimental=True,
874
+ example="main",
875
+ )
876
+ models = ConfigField(
877
+ cli_meta=None,
878
+ field_type=list,
879
+ is_experimental=True,
880
+ example=[{"asset_id": "model-123", "asset_instance_id": "instance-456"}],
881
+ )
882
+ data = ConfigField(
883
+ cli_meta=None,
884
+ field_type=list,
885
+ is_experimental=True,
886
+ example=[{"asset_id": "data-789", "asset_instance_id": "instance-101"}],
887
+ )
888
+ # ------- /Experimental -------------
889
+
890
+ def to_dict(self):
891
+ return config_meta_to_dict(self)
892
+
893
+ @staticmethod
894
+ def merge_configs(
895
+ base_config: "CoreConfig", override_config: "CoreConfig"
896
+ ) -> "CoreConfig":
897
+ """
898
+ Merge two configurations with override taking precedence.
899
+
900
+ Handles FieldBehavior for proper merging:
901
+ - UNION: Merge values (for lists, dicts)
902
+ - NOT_ALLOWED: Base config value takes precedence (override is ignored)
903
+
904
+ Args:
905
+ base_config: Base configuration (lower precedence)
906
+ override_config: Override configuration (higher precedence)
907
+
908
+ Returns:
909
+ Merged CoreConfig instance
910
+ """
911
+ merged_config = CoreConfig()
912
+
913
+ # Process each field according to its behavior
914
+ for field_name, field_info in CoreConfig._fields.items(): # type: ignore
915
+ base_value = getattr(base_config, field_name, None)
916
+ override_value = getattr(override_config, field_name, None)
917
+
918
+ # Get the behavior for this field
919
+ behavior = getattr(field_info, "behavior", FieldBehavior.UNION)
920
+
921
+ merged_value = merge_field_values(
922
+ base_value, override_value, field_info, behavior
923
+ )
924
+
925
+ setattr(merged_config, field_name, merged_value)
926
+
927
+ return merged_config
928
+
929
+ def set_defaults(self):
930
+ apply_defaults(self)
931
+
932
+ def validate(self):
933
+ validate_config_meta(self)
934
+
935
+ def commit(self):
936
+ self.validate()
937
+ validate_required_fields(self)
938
+ self.set_defaults()
939
+
940
+ @classmethod
941
+ def from_dict(cls, config_data: Dict[str, Any]) -> "CoreConfig":
942
+ config = cls()
943
+ # Define functions for dict source
944
+ def get_dict_key(field_name, field_info):
945
+ return field_name
946
+
947
+ def get_dict_value(source_data, key):
948
+ return source_data.get(key)
949
+
950
+ populate_config_recursive(
951
+ config, cls, config_data, get_dict_key, get_dict_value
952
+ )
953
+ return config
954
+
955
+ @classmethod
956
+ def from_cli(cls, cli_options: Dict[str, Any]) -> "CoreConfig":
957
+ config = cls()
958
+ # Define functions for CLI source
959
+ def get_cli_key(field_name, field_info):
960
+ # Need to have a special Exception for commands since the Commands
961
+ # are passed down via unprocessed args after `--` in click
962
+ if field_name == cls.commands.name:
963
+ return field_name
964
+ # Return the CLI parameter name if CLI metadata exists
965
+ if field_info.cli_meta and not field_info.cli_meta.hidden:
966
+ return field_info.cli_meta.name
967
+ return None
968
+
969
+ def get_cli_value(source_data, key):
970
+ value = source_data.get(key)
971
+ # Only return non-None values since None means not set in CLI
972
+ if value is None:
973
+ return None
974
+ if key == cls.environment.name:
975
+ _env_dict = {}
976
+ for v in value:
977
+ _env_dict.update(v)
978
+ return _env_dict
979
+ if type(value) == tuple or type(value) == list:
980
+ obj = list(x for x in source_data[key])
981
+ if len(obj) == 0:
982
+ return None # Dont return Empty Lists so that we can set Nones
983
+ return obj
984
+ return value
985
+
986
+ # Use common recursive population function with nested value checking
987
+ populate_config_recursive(
988
+ config,
989
+ cls,
990
+ cli_options,
991
+ get_cli_key,
992
+ get_cli_value,
993
+ )
994
+ return config