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