outerbounds 0.3.180rc5__py3-none-any.whl → 0.3.181__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.
@@ -1,1312 +0,0 @@
1
- import json
2
- import os
3
- import sys
4
- import random
5
- from functools import wraps, partial
6
- from typing import Dict, List, Any, Optional, Union
7
-
8
- # IF this CLI is supposed to be reusable across Metaflow Too
9
- # Then we need to ensure that the click IMPORT happens from metaflow
10
- # so that any object class comparisons end up working as expected.
11
- # since Metaflow lazy loads Click Modules, we need to ensure that the
12
- # module's click Groups correlate to the same module otherwise Metaflow
13
- # will not accept the cli as valid.
14
- # But the BIGGEST Problem of adding a `metaflow._vendor` import is that
15
- # It will run the remote-config check and that can raise a really dirty exception
16
- # That will break the CLI.
17
- # So we need to find a way to import click without having it try to check for remote-config
18
- # or load the config from the environment.
19
- # If we import click from metaflow over here then it might
20
- # end up creating issues with click in general. So we need to figure a
21
- # way to figure the right import of click dynamically. a neat way to handle that would be
22
- # to have a function that can import the correct click based on the context in which stuff is being loaded.
23
- from metaflow._vendor import click
24
- from outerbounds._vendor import yaml
25
- from outerbounds.utils import metaflowconfig
26
- from .app_config import (
27
- AppConfig,
28
- AppConfigError,
29
- CODE_PACKAGE_PREFIX,
30
- CAPSULE_DEBUG,
31
- AuthType,
32
- )
33
- from .perimeters import PerimeterExtractor
34
- from .cli_to_config import build_config_from_options
35
- from .utils import (
36
- CommaSeparatedListType,
37
- KVPairType,
38
- KVDictType,
39
- MultiStepSpinner,
40
- )
41
- from . import experimental
42
- from .validations import deploy_validations
43
- from .code_package import CodePackager
44
- from .capsule import (
45
- CapsuleDeployer,
46
- list_and_filter_capsules,
47
- CapsuleApi,
48
- DEPLOYMENT_READY_CONDITIONS,
49
- CapsuleApiException,
50
- CapsuleDeploymentException,
51
- )
52
- from .dependencies import bake_deployment_image
53
- import shlex
54
- import time
55
- import uuid
56
- from datetime import datetime
57
-
58
-
59
- class ColorTheme:
60
- TIMESTAMP = "magenta"
61
- LOADING_COLOR = "cyan"
62
- BAD_COLOR = "red"
63
- INFO_COLOR = "green"
64
-
65
- TL_HEADER_COLOR = "magenta"
66
- ROW_COLOR = "bright_white"
67
-
68
- INFO_KEY_COLOR = "green"
69
- INFO_VALUE_COLOR = "bright_white"
70
-
71
-
72
- NativeList = list
73
-
74
-
75
- def _logger(
76
- body="", system_msg=False, head="", bad=False, timestamp=True, nl=True, color=None
77
- ):
78
- if timestamp:
79
- if timestamp is True:
80
- dt = datetime.now()
81
- else:
82
- dt = timestamp
83
- tstamp = dt.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
84
- click.secho(tstamp + " ", fg=ColorTheme.TIMESTAMP, nl=False)
85
- if head:
86
- click.secho(head, fg=ColorTheme.INFO_COLOR, nl=False)
87
- click.secho(
88
- body,
89
- bold=system_msg,
90
- fg=ColorTheme.BAD_COLOR if bad else color if color is not None else None,
91
- nl=nl,
92
- )
93
-
94
-
95
- def _logger_styled(
96
- body="", system_msg=False, head="", bad=False, timestamp=True, nl=True, color=None
97
- ):
98
- message_parts = []
99
-
100
- if timestamp:
101
- if timestamp is True:
102
- dt = datetime.now()
103
- else:
104
- dt = timestamp
105
- tstamp = dt.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
106
- message_parts.append(click.style(tstamp + " ", fg=ColorTheme.TIMESTAMP))
107
-
108
- if head:
109
- message_parts.append(click.style(head, fg=ColorTheme.INFO_COLOR))
110
-
111
- message_parts.append(
112
- click.style(
113
- body,
114
- bold=system_msg,
115
- fg=ColorTheme.BAD_COLOR if bad else color if color is not None else None,
116
- )
117
- )
118
-
119
- return "".join(message_parts)
120
-
121
-
122
- def _spinner_logger(spinner, *msg):
123
- spinner.log(*[_logger_styled(x, timestamp=True) for x in msg])
124
-
125
-
126
- class CliState(object):
127
- pass
128
-
129
-
130
- def _pre_create_debug(
131
- app_config: AppConfig,
132
- capsule: CapsuleDeployer,
133
- state_dir: str,
134
- options: Dict[str, Any],
135
- cli_parsed_config: Dict[str, Any],
136
- ):
137
- if CAPSULE_DEBUG:
138
- os.makedirs(state_dir, exist_ok=True)
139
- debug_path = os.path.join(state_dir, f"debug_{time.time()}.yaml")
140
- with open(
141
- debug_path,
142
- "w",
143
- ) as f:
144
- f.write(
145
- json.dumps(
146
- {
147
- "app_state": app_config.dump_state(), # This is the state of the app config after parsing the CLI options and right before the capsule deploy API is called
148
- "capsule_input": capsule.create_input(), # This is the input that is passed to the capsule deploy API
149
- "deploy_response": capsule._capsule_deploy_response, # This is the response from the capsule deploy API
150
- "cli_options": options, # These are the actual options passing down to the CLI
151
- "cli_parsed_config": cli_parsed_config, # This is the config object that is created after parsing options from the CLI
152
- },
153
- indent=2,
154
- default=str,
155
- )
156
- )
157
-
158
-
159
- def _post_create_debug(capsule: CapsuleDeployer, state_dir: str):
160
- if CAPSULE_DEBUG:
161
- debug_path = os.path.join(
162
- state_dir, f"debug_deploy_response_{time.time()}.yaml"
163
- )
164
- with open(debug_path, "w") as f:
165
- f.write(
166
- yaml.dump(
167
- capsule._capsule_deploy_response, default_flow_style=False, indent=2
168
- )
169
- )
170
-
171
-
172
- def _bake_image(app_config: AppConfig, cache_dir: str, logger):
173
- baking_status = bake_deployment_image(
174
- app_config=app_config,
175
- cache_file_path=os.path.join(cache_dir, "image_cache"),
176
- logger=logger,
177
- )
178
- app_config.set_state(
179
- "image",
180
- baking_status.resolved_image,
181
- )
182
- app_config.set_state("python_path", baking_status.python_path)
183
- logger("🐳 Using The Docker Image : %s" % app_config.get_state("image"))
184
-
185
-
186
- def print_table(data, headers):
187
- """Print data in a formatted table."""
188
-
189
- if not data:
190
- return
191
-
192
- # Calculate column widths
193
- col_widths = [len(h) for h in headers]
194
-
195
- # Calculate actual widths based on data
196
- for row in data:
197
- for i, cell in enumerate(row):
198
- col_widths[i] = max(col_widths[i], len(str(cell)))
199
-
200
- # Print header
201
- header_row = " | ".join(
202
- [headers[i].ljust(col_widths[i]) for i in range(len(headers))]
203
- )
204
- click.secho("-" * len(header_row), fg=ColorTheme.TL_HEADER_COLOR)
205
- click.secho(header_row, fg=ColorTheme.TL_HEADER_COLOR, bold=True)
206
- click.secho("-" * len(header_row), fg=ColorTheme.TL_HEADER_COLOR)
207
-
208
- # Print data rows
209
- for row in data:
210
- formatted_row = " | ".join(
211
- [str(row[i]).ljust(col_widths[i]) for i in range(len(row))]
212
- )
213
- click.secho(formatted_row, fg=ColorTheme.ROW_COLOR, bold=True)
214
- click.secho("-" * len(header_row), fg=ColorTheme.TL_HEADER_COLOR)
215
-
216
-
217
- @click.group()
218
- def cli():
219
- """Outerbounds CLI tool."""
220
- pass
221
-
222
-
223
- @cli.group(
224
- help="Commands related to Deploying/Running/Managing Apps on Outerbounds Platform."
225
- )
226
- @click.pass_context
227
- def app(ctx):
228
- """App-related commands."""
229
- metaflow_set_context = getattr(ctx, "obj", None)
230
- ctx.obj = CliState()
231
- ctx.obj.trace_id = str(uuid.uuid4())
232
- ctx.obj.app_state_dir = os.path.join(os.curdir, ".ob_apps")
233
- profile = os.environ.get("METAFLOW_PROFILE", "")
234
- config_dir = os.path.expanduser(
235
- os.environ.get("METAFLOW_HOME", "~/.metaflowconfig")
236
- )
237
- perimeter, api_server = PerimeterExtractor.for_ob_cli(
238
- config_dir=config_dir, profile=profile
239
- )
240
- if perimeter is None or api_server is None:
241
- raise AppConfigError(
242
- "Perimeter not found in the environment, Found perimeter: %s, api_server: %s"
243
- % (perimeter, api_server)
244
- )
245
- ctx.obj.perimeter = perimeter
246
- ctx.obj.api_url = api_server
247
- os.makedirs(ctx.obj.app_state_dir, exist_ok=True)
248
-
249
-
250
- def parse_commands(app_config: AppConfig, cli_command_input):
251
- # There can be two modes:
252
- # 1. User passes command via `--` in the CLI
253
- # 2. User passes the `commands` key in the config.
254
- base_commands = []
255
- if len(cli_command_input) > 0:
256
- # TODO: we can be a little more fancy here by allowing the user to just call
257
- # `outerbounds app deploy -- foo.py` and figure out if we need to stuff python
258
- # in front of the command or not. But for sake of dumb simplicity, we can just
259
- # assume what ever the user called on local needs to be called remotely, we can
260
- # just ask them to add the outerbounds command in front of it.
261
- # So the dev ex would be :
262
- # `python foo.py` -> `outerbounds app deploy -- python foo.py`
263
- if type(cli_command_input) == str:
264
- base_commands.append(cli_command_input)
265
- else:
266
- base_commands.append(shlex.join(cli_command_input))
267
- elif app_config.get("commands", None) is not None:
268
- base_commands.extend(app_config.get("commands"))
269
- return base_commands
270
-
271
-
272
- def deployment_instance_options(func):
273
- # These parameters influence how the CLI behaves for each instance of a launched deployment.
274
- @click.option(
275
- "--readiness-condition",
276
- type=click.Choice(DEPLOYMENT_READY_CONDITIONS.enums()),
277
- help=DEPLOYMENT_READY_CONDITIONS.__doc__,
278
- default=DEPLOYMENT_READY_CONDITIONS.ATLEAST_ONE_RUNNING,
279
- )
280
- @click.option(
281
- "--readiness-wait-time",
282
- type=int,
283
- help="The time (in seconds) to monitor the deployment for readiness after the readiness condition is met.",
284
- default=4,
285
- )
286
- @click.option(
287
- "--max-wait-time",
288
- type=int,
289
- help="The maximum time (in seconds) to wait for the deployment to be ready.",
290
- default=600,
291
- )
292
- @click.option(
293
- "--no-loader",
294
- is_flag=True,
295
- help="Do not use the loading spinner for the deployment.",
296
- default=False,
297
- )
298
- @wraps(func)
299
- def wrapper(*args, **kwargs):
300
- return func(*args, **kwargs)
301
-
302
- return wrapper
303
-
304
-
305
- def common_deploy_options(func):
306
- @click.option(
307
- "--name",
308
- type=str,
309
- help="The name of the app to deploy.",
310
- )
311
- @click.option("--port", type=int, help="Port where the app is hosted.")
312
- @click.option(
313
- "--tag",
314
- "tags",
315
- multiple=True,
316
- type=KVPairType,
317
- help="The tags of the app to deploy. Format KEY=VALUE. Example --tag foo=bar --tag x=y",
318
- default=None,
319
- )
320
- @click.option(
321
- "--image",
322
- type=str,
323
- help="The Docker image to deploy with the App",
324
- default=None,
325
- )
326
- @click.option(
327
- "--cpu",
328
- type=str,
329
- help="CPU resource request and limit",
330
- default=None,
331
- )
332
- @click.option(
333
- "--memory",
334
- type=str,
335
- help="Memory resource request and limit",
336
- default=None,
337
- )
338
- @click.option(
339
- "--gpu",
340
- type=str,
341
- help="GPU resource request and limit",
342
- default=None,
343
- )
344
- @click.option(
345
- "--disk",
346
- type=str,
347
- help="Storage resource request and limit",
348
- default=None,
349
- )
350
- @click.option(
351
- "--health-check-enabled",
352
- type=bool,
353
- help="Enable health checks",
354
- default=None,
355
- )
356
- @click.option(
357
- "--health-check-path",
358
- type=str,
359
- help="Health check path",
360
- default=None,
361
- )
362
- @click.option(
363
- "--health-check-initial-delay",
364
- type=int,
365
- help="Initial delay seconds for health check",
366
- default=None,
367
- )
368
- @click.option(
369
- "--health-check-period",
370
- type=int,
371
- help="Period seconds for health check",
372
- default=None,
373
- )
374
- @click.option(
375
- "--compute-pools",
376
- type=CommaSeparatedListType,
377
- help="The compute pools to deploy the app to. Example: --compute-pools default,large",
378
- default=None,
379
- )
380
- @click.option(
381
- "--auth-type",
382
- type=click.Choice(AuthType.enums()),
383
- help="The type of authentication to use for the app.",
384
- default=None,
385
- )
386
- @click.option(
387
- "--public-access/--private-access",
388
- "auth_public",
389
- type=bool,
390
- help="Whether the app is public or not.",
391
- default=None,
392
- )
393
- @click.option(
394
- "--no-deps",
395
- is_flag=True,
396
- help="Do not any dependencies. Directly used the image provided",
397
- default=False,
398
- )
399
- @click.option(
400
- "--min-replicas",
401
- type=int,
402
- help="Minimum number of replicas to deploy",
403
- default=None,
404
- )
405
- @click.option(
406
- "--max-replicas",
407
- type=int,
408
- help="Maximum number of replicas to deploy",
409
- default=None,
410
- )
411
- @click.option(
412
- "--description",
413
- type=str,
414
- help="The description of the app to deploy.",
415
- default=None,
416
- )
417
- @click.option(
418
- "--app-type",
419
- type=str,
420
- help="The type of app to deploy.",
421
- default=None,
422
- )
423
- @click.option(
424
- "--force-upgrade",
425
- is_flag=True,
426
- help="Force upgrade the app even if it is currently being upgraded.",
427
- default=False,
428
- )
429
- @wraps(func)
430
- def wrapper(*args, **kwargs):
431
- return func(*args, **kwargs)
432
-
433
- return wrapper
434
-
435
-
436
- def common_run_options(func):
437
- """Common options for running and deploying apps."""
438
-
439
- @click.option(
440
- "--config-file",
441
- type=str,
442
- help="The config file to use for the App (YAML or JSON)",
443
- default=None,
444
- )
445
- @click.option(
446
- "--secret",
447
- "secrets",
448
- multiple=True,
449
- type=str,
450
- help="Secrets to deploy with the App",
451
- default=None,
452
- )
453
- @click.option(
454
- "--env",
455
- "envs",
456
- multiple=True,
457
- type=KVPairType,
458
- help="Environment variables to deploy with the App. Use format KEY=VALUE",
459
- default=None,
460
- )
461
- @click.option(
462
- "--package-src-path",
463
- type=str,
464
- help="The path to the source code to deploy with the App.",
465
- default=None,
466
- )
467
- @click.option(
468
- "--package-suffixes",
469
- type=CommaSeparatedListType,
470
- help="The suffixes of the source code to deploy with the App.",
471
- default=None,
472
- )
473
- @click.option(
474
- "--dep-from-requirements",
475
- type=str,
476
- help="Path to requirements.txt file for dependencies",
477
- default=None,
478
- )
479
- @click.option(
480
- "--dep-from-pyproject",
481
- type=str,
482
- help="Path to pyproject.toml file for dependencies",
483
- default=None,
484
- )
485
- # TODO: [FIX ME]: Get better CLI abstraction for pypi/conda dependencies
486
- @wraps(func)
487
- def wrapper(*args, **kwargs):
488
- return func(*args, **kwargs)
489
-
490
- return wrapper
491
-
492
-
493
- def _package_necessary_things(app_config: AppConfig, logger):
494
- # Packaging has a few things to be thought through:
495
- # 1. if `entrypoint_path` exists then should we package the directory
496
- # where the entrypoint lives. For example : if the user calls
497
- # `outerbounds app deploy foo/bar.py` should we package `foo` dir
498
- # or should we package the cwd from which foo/bar.py is being called.
499
- # 2. if the src path is used with the config file then how should we view
500
- # that path ?
501
- # 3. It becomes interesting when users call the deployment with config files
502
- # where there is a `src_path` and then is the src_path relative to the config file
503
- # or is it relative to where the caller command is sitting. Ideally it should work
504
- # like Kustomizations where its relative to where the yaml file sits for simplicity
505
- # of understanding relationships between config files. Ideally users can pass the src_path
506
- # from the command line and that will aliviate any need to package any other directories for
507
- #
508
-
509
- package_dir = app_config.get_state("packaging_directory")
510
- if package_dir is None:
511
- app_config.set_state("code_package_url", None)
512
- app_config.set_state("code_package_key", None)
513
- return
514
- from metaflow.metaflow_config import DEFAULT_DATASTORE
515
-
516
- package = app_config.get_state("package") or {}
517
- suffixes = package.get("suffixes", None)
518
-
519
- packager = CodePackager(
520
- datastore_type=DEFAULT_DATASTORE, code_package_prefix=CODE_PACKAGE_PREFIX
521
- )
522
- package_url, package_key = packager.store(
523
- paths_to_include=[package_dir], file_suffixes=suffixes
524
- )
525
- app_config.set_state("code_package_url", package_url)
526
- app_config.set_state("code_package_key", package_key)
527
- logger("💾 Code Package Saved to : %s" % app_config.get_state("code_package_url"))
528
-
529
-
530
- @app.command(help="Deploy an app to the Outerbounds Platform.")
531
- @common_deploy_options
532
- @common_run_options
533
- @deployment_instance_options
534
- @experimental.wrapping_cli_options
535
- @click.pass_context
536
- @click.argument("command", nargs=-1, type=click.UNPROCESSED, required=False)
537
- def deploy(
538
- ctx,
539
- command,
540
- readiness_condition=None,
541
- max_wait_time=None,
542
- readiness_wait_time=None,
543
- no_loader=False,
544
- **options,
545
- ):
546
- """Deploy an app to the Outerbounds Platform."""
547
- from functools import partial
548
-
549
- if not ctx.obj.perimeter:
550
- raise AppConfigError("OB_CURRENT_PERIMETER is not set")
551
-
552
- logger = partial(_logger, timestamp=True)
553
- try:
554
- _cli_parsed_config = build_config_from_options(options)
555
- # Create configuration
556
- if options["config_file"]:
557
- # Load from file
558
- app_config = AppConfig.from_file(options["config_file"])
559
-
560
- # Update with any CLI options using the unified method
561
- app_config.update_from_cli_options(options)
562
- else:
563
- # Create from CLI options
564
- app_config = AppConfig(_cli_parsed_config)
565
-
566
- # Validate the configuration
567
- app_config.validate()
568
- logger(
569
- f"🚀 Deploying {app_config.get('name')} to the Outerbounds platform...",
570
- color=ColorTheme.INFO_COLOR,
571
- system_msg=True,
572
- )
573
-
574
- packaging_directory = None
575
- package_src_path = app_config.get("package", {}).get("src_path", None)
576
- if package_src_path:
577
- if os.path.isfile(package_src_path):
578
- raise AppConfigError("src_path must be a directory, not a file")
579
- elif os.path.isdir(package_src_path):
580
- packaging_directory = os.path.abspath(package_src_path)
581
- else:
582
- raise AppConfigError(f"src_path '{package_src_path}' does not exist")
583
- else:
584
- # If src_path is None then we assume then we can assume for the moment
585
- # that we can package the current working directory.
586
- packaging_directory = os.getcwd()
587
-
588
- app_config.set_state("packaging_directory", packaging_directory)
589
- logger(
590
- "📦 Packaging Directory : %s" % app_config.get_state("packaging_directory"),
591
- )
592
- # TODO: Construct the command needed to run the app
593
- # If we are constructing the directory with the src_path
594
- # then we need to add the command from the option otherwise
595
- # we use the command from the entrypoint path and whatever follows `--`
596
- # is the command to run.
597
-
598
- # Set some defaults for the deploy command
599
- app_config.set_deploy_defaults(packaging_directory)
600
-
601
- if options.get("no_deps") == True:
602
- # Setting this in the state will make it skip the fast-bakery step
603
- # of building an image.
604
- app_config.set_state("skip_dependencies", True)
605
- else:
606
- # Check if the user has set the dependencies in the app config
607
- dependencies = app_config.get("dependencies", {})
608
- if len(dependencies) == 0:
609
- # The user has not set any dependencies, so we can sniff the packaging directory
610
- # for a dependencies file.
611
- requirements_file = os.path.join(
612
- packaging_directory, "requirements.txt"
613
- )
614
- pyproject_toml = os.path.join(packaging_directory, "pyproject.toml")
615
- if os.path.exists(pyproject_toml):
616
- app_config.set_state(
617
- "dependencies", {"from_pyproject_toml": pyproject_toml}
618
- )
619
- logger(
620
- "📦 Using dependencies from pyproject.toml: %s" % pyproject_toml
621
- )
622
- elif os.path.exists(requirements_file):
623
- app_config.set_state(
624
- "dependencies", {"from_requirements_file": requirements_file}
625
- )
626
- logger(
627
- "📦 Using dependencies from requirements.txt: %s"
628
- % requirements_file
629
- )
630
-
631
- # Print the configuration
632
- # 1. validate that the secrets for the app exist
633
- # 2. TODO: validate that the compute pool specified in the app exists.
634
- # 3. Building Docker image if necessary (based on parameters)
635
- # - We will bake images with fastbakery and pass it to the deploy command
636
- # TODO: validation logic can be wrapped in try catch so that we can provide
637
- # better error messages.
638
- cache_dir = os.path.join(
639
- ctx.obj.app_state_dir, app_config.get("name", "default")
640
- )
641
-
642
- def _non_spinner_logger(*msg):
643
- for m in msg:
644
- logger(m)
645
-
646
- deploy_validations(
647
- app_config,
648
- cache_dir=cache_dir,
649
- logger=logger,
650
- )
651
- image_spinner = None
652
- img_logger = _non_spinner_logger
653
- if not no_loader:
654
- image_spinner = MultiStepSpinner(
655
- text=lambda: _logger_styled(
656
- "🍞 Baking Docker Image",
657
- timestamp=True,
658
- ),
659
- color=ColorTheme.LOADING_COLOR,
660
- )
661
- img_logger = partial(_spinner_logger, image_spinner)
662
- image_spinner.start()
663
-
664
- _bake_image(app_config, cache_dir, img_logger)
665
- if image_spinner:
666
- image_spinner.stop()
667
-
668
- base_commands = parse_commands(app_config, command)
669
-
670
- app_config.set_state("commands", base_commands)
671
-
672
- # TODO: Handle the case where packaging_directory is None
673
- # This would involve:
674
- # 1. Packaging the code:
675
- # - We need to package the code and throw the tarball to some object store
676
- _package_necessary_things(app_config, logger)
677
-
678
- app_config.set_state("perimeter", ctx.obj.perimeter)
679
-
680
- # 2. Convert to the IR that the backend accepts
681
- capsule = CapsuleDeployer(
682
- app_config,
683
- ctx.obj.api_url,
684
- debug_dir=cache_dir,
685
- success_terminal_state_condition=readiness_condition,
686
- create_timeout=max_wait_time,
687
- readiness_wait_time=readiness_wait_time,
688
- )
689
- currently_present_capsules = list_and_filter_capsules(
690
- capsule.capsule_api,
691
- None,
692
- None,
693
- capsule.name,
694
- None,
695
- None,
696
- None,
697
- )
698
-
699
- force_upgrade = app_config.get_state("force_upgrade", False)
700
-
701
- _pre_create_debug(app_config, capsule, cache_dir, options, _cli_parsed_config)
702
-
703
- if len(currently_present_capsules) > 0:
704
- # Only update the capsule if there is no upgrade in progress
705
- # Only update a "already updating" capsule if the `--force-upgrade` flag is provided.
706
- _curr_cap = currently_present_capsules[0]
707
- this_capsule_is_being_updated = _curr_cap.get("status", {}).get(
708
- "updateInProgress", False
709
- )
710
-
711
- if this_capsule_is_being_updated and not force_upgrade:
712
- _upgrader = _curr_cap.get("metadata", {}).get("lastModifiedBy", None)
713
- message = f"{capsule.capsule_type} is currently being upgraded"
714
- if _upgrader:
715
- message = (
716
- f"{capsule.capsule_type} is currently being upgraded. Upgrade was launched by {_upgrader}. "
717
- "If you wish to force upgrade, you can do so by providing the `--force-upgrade` flag."
718
- )
719
- raise AppConfigError(message)
720
- logger(
721
- f"🚀 {'' if not force_upgrade else 'Force'} Upgrading {capsule.capsule_type.lower()} `{capsule.name}`....",
722
- color=ColorTheme.INFO_COLOR,
723
- system_msg=True,
724
- )
725
- else:
726
- logger(
727
- f"🚀 Deploying {capsule.capsule_type.lower()} to the platform....",
728
- color=ColorTheme.INFO_COLOR,
729
- system_msg=True,
730
- )
731
- # 3. Throw the job into the platform and report deployment status
732
- capsule.create()
733
- _post_create_debug(capsule, cache_dir)
734
-
735
- capsule_spinner = None
736
- capsule_logger = _non_spinner_logger
737
- if not no_loader:
738
- capsule_spinner = MultiStepSpinner(
739
- text=lambda: _logger_styled(
740
- "💊 Waiting for %s %s to be ready to serve traffic"
741
- % (capsule.capsule_type.lower(), capsule.identifier),
742
- timestamp=True,
743
- ),
744
- color=ColorTheme.LOADING_COLOR,
745
- )
746
- capsule_logger = partial(_spinner_logger, capsule_spinner)
747
- capsule_spinner.start()
748
-
749
- capsule.wait_for_terminal_state(logger=capsule_logger)
750
- if capsule_spinner:
751
- capsule_spinner.stop()
752
-
753
- logger(
754
- f"💊 {capsule.capsule_type} {app_config.config['name']} ({capsule.identifier}) deployed successfully! You can access it at {capsule.status.out_of_cluster_url}",
755
- color=ColorTheme.INFO_COLOR,
756
- system_msg=True,
757
- )
758
-
759
- except Exception as e:
760
- logger(
761
- f"Deployment failed: [{e.__class__.__name__}]: {e}",
762
- bad=True,
763
- system_msg=True,
764
- )
765
- exit(1)
766
-
767
-
768
- def _parse_capsule_table(filtered_capsules):
769
- headers = ["Name", "ID", "Ready", "App Type", "Port", "Tags", "URL"]
770
- table_data = []
771
-
772
- for capsule in filtered_capsules:
773
- spec = capsule.get("spec", {})
774
- status = capsule.get("status", {}) or {}
775
- cap_id = capsule.get("id")
776
- display_name = spec.get("displayName", "")
777
- ready = str(status.get("readyToServeTraffic", False))
778
- auth_type = spec.get("authConfig", {}).get("authType", "")
779
- port = str(spec.get("port", ""))
780
- tags_str = ", ".join(
781
- [f"{tag['key']}={tag['value']}" for tag in spec.get("tags", [])]
782
- )
783
- access_info = status.get("accessInfo", {}) or {}
784
- url = access_info.get("outOfClusterURL", None)
785
-
786
- table_data.append(
787
- [
788
- display_name,
789
- cap_id,
790
- ready,
791
- auth_type,
792
- port,
793
- tags_str,
794
- f"https://{url}" if url else "URL not available",
795
- ]
796
- )
797
- return headers, table_data
798
-
799
-
800
- @app.command(help="List apps in the Outerbounds Platform.")
801
- @click.option("--project", type=str, help="Filter apps by project")
802
- @click.option("--branch", type=str, help="Filter apps by branch")
803
- @click.option("--name", type=str, help="Filter apps by name")
804
- @click.option(
805
- "--tag",
806
- "tags",
807
- type=KVDictType,
808
- help="Filter apps by tag. Format KEY=VALUE. Example --tag foo=bar --tag x=y. If multiple tags are provided, the app must match all of them.",
809
- multiple=True,
810
- )
811
- @click.option(
812
- "--format",
813
- type=click.Choice(["json", "text"]),
814
- help="Format the output",
815
- default="text",
816
- )
817
- @click.option(
818
- "--auth-type", type=click.Choice(AuthType.enums()), help="Filter apps by Auth type"
819
- )
820
- @click.pass_context
821
- def list(ctx, project, branch, name, tags, format, auth_type):
822
- """List apps in the Outerbounds Platform."""
823
- capsule_api = CapsuleApi(
824
- ctx.obj.api_url,
825
- ctx.obj.perimeter,
826
- )
827
- filtered_capsules = list_and_filter_capsules(
828
- capsule_api, project, branch, name, tags, auth_type, None
829
- )
830
- if format == "json":
831
- click.echo(json.dumps(filtered_capsules, indent=4))
832
- else:
833
- headers, table_data = _parse_capsule_table(filtered_capsules)
834
- print_table(table_data, headers)
835
-
836
-
837
- @app.command(help="Delete an app/apps from the Outerbounds Platform.")
838
- @click.option("--name", type=str, help="Filter app to delete by name")
839
- @click.option("--id", "cap_id", type=str, help="Filter app to delete by id")
840
- @click.option("--project", type=str, help="Filter apps to delete by project")
841
- @click.option("--branch", type=str, help="Filter apps to delete by branch")
842
- @click.option(
843
- "--tag",
844
- "tags",
845
- multiple=True,
846
- type=KVDictType,
847
- help="Filter apps to delete by tag. Format KEY=VALUE. Example --tag foo=bar --tag x=y. If multiple tags are provided, the app must match all of them.",
848
- )
849
- @click.option("--auto-approve", is_flag=True, help="Do not prompt for confirmation")
850
- @click.pass_context
851
- def delete(ctx, name, cap_id, project, branch, tags, auto_approve):
852
-
853
- """Delete an app/apps from the Outerbounds Platform."""
854
- # Atleast one of the args need to be provided
855
- if not any(
856
- [
857
- name is not None,
858
- cap_id is not None,
859
- project is not None,
860
- branch is not None,
861
- len(tags) != 0,
862
- ]
863
- ):
864
- raise AppConfigError(
865
- "Atleast one of the options need to be provided. You can use --name, --id, --project, --branch, --tag"
866
- )
867
-
868
- capsule_api = CapsuleApi(ctx.obj.api_url, ctx.obj.perimeter)
869
- filtered_capsules = list_and_filter_capsules(
870
- capsule_api, project, branch, name, tags, None, cap_id
871
- )
872
-
873
- headers, table_data = _parse_capsule_table(filtered_capsules)
874
- click.secho("The following apps will be deleted:", fg="red", bold=True)
875
- print_table(table_data, headers)
876
-
877
- # Confirm the deletion
878
- if not auto_approve:
879
- confirm = click.prompt(
880
- click.style(
881
- "💊 Are you sure you want to delete these apps?", fg="red", bold=True
882
- ),
883
- default="no",
884
- type=click.Choice(["yes", "no"]),
885
- )
886
- if confirm == "no":
887
- exit(1)
888
-
889
- def item_show_func(x):
890
- if not x:
891
- return None
892
- name = x.get("spec", {}).get("displayName", "")
893
- id = x.get("id", "")
894
- return click.style(
895
- "💊 deleting %s [%s]" % (name, id),
896
- fg=ColorTheme.BAD_COLOR,
897
- bold=True,
898
- )
899
-
900
- with click.progressbar(
901
- filtered_capsules,
902
- label=click.style("💊 Deleting apps...", fg=ColorTheme.BAD_COLOR, bold=True),
903
- fill_char=click.style("█", fg=ColorTheme.BAD_COLOR, bold=True),
904
- empty_char=click.style("░", fg=ColorTheme.BAD_COLOR, bold=True),
905
- item_show_func=item_show_func,
906
- ) as bar:
907
- for capsule in bar:
908
- capsule_api.delete(capsule.get("id"))
909
- time.sleep(0.5 + random.random() * 2) # delay to avoid rate limiting
910
-
911
-
912
- @app.command(help="Run an app locally (for testing).")
913
- @common_run_options
914
- @click.pass_context
915
- def run(ctx, **options):
916
- """Run an app locally for testing."""
917
- try:
918
- # Create configuration
919
- if options["config_file"]:
920
- # Load from file
921
- app_config = AppConfig.from_file(options["config_file"])
922
-
923
- # Update with any CLI options using the unified method
924
- app_config.update_from_cli_options(options)
925
- else:
926
- # Create from CLI options
927
- config_dict = build_config_from_options(options)
928
- app_config = AppConfig(config_dict)
929
-
930
- # Validate the configuration
931
- app_config.validate()
932
-
933
- # Print the configuration
934
- click.echo("Running App with configuration:")
935
- click.echo(app_config.to_yaml())
936
-
937
- # TODO: Implement local run logic
938
- # This would involve:
939
- # 1. Setting up the environment
940
- # 2. Running the app locally
941
- # 3. Reporting status
942
-
943
- click.echo(f"App '{app_config.config['name']}' running locally!")
944
-
945
- except AppConfigError as e:
946
- click.echo(f"Error in app configuration: {e}", err=True)
947
- ctx.exit(1)
948
- except Exception as e:
949
- click.echo(f"Error running app: {e}", err=True)
950
- ctx.exit(1)
951
-
952
-
953
- @app.command(
954
- help="Get detailed information about an app from the Outerbounds Platform."
955
- )
956
- @click.option("--name", type=str, help="Get info for app by name")
957
- @click.option("--id", "cap_id", type=str, help="Get info for app by id")
958
- @click.option(
959
- "--format",
960
- type=click.Choice(["json", "text"]),
961
- help="Format the output",
962
- default="text",
963
- )
964
- @click.pass_context
965
- def info(ctx, name, cap_id, format):
966
- """Get detailed information about an app from the Outerbounds Platform."""
967
- # Require either name or id
968
- if not any([name is not None, cap_id is not None]):
969
- raise AppConfigError(
970
- "Either --name or --id must be provided to get app information."
971
- )
972
-
973
- # Ensure only one is provided
974
- if name is not None and cap_id is not None:
975
- raise AppConfigError("Please provide either --name or --id, not both.")
976
-
977
- capsule_api = CapsuleApi(
978
- ctx.obj.api_url,
979
- ctx.obj.perimeter,
980
- )
981
-
982
- # First, find the capsule using list_and_filter_capsules
983
- filtered_capsules = list_and_filter_capsules(
984
- capsule_api, None, None, name, None, None, cap_id
985
- )
986
-
987
- if len(filtered_capsules) == 0:
988
- identifier = name if name else cap_id
989
- identifier_type = "name" if name else "id"
990
- raise AppConfigError(f"No app found with {identifier_type}: {identifier}")
991
-
992
- if len(filtered_capsules) > 1:
993
- raise AppConfigError(
994
- f"Multiple apps found with name: {name}. Please use --id to specify exactly which app you want info for."
995
- )
996
-
997
- # Get the capsule info
998
- capsule = filtered_capsules[0]
999
- capsule_id = capsule.get("id")
1000
-
1001
- # Get detailed capsule info and workers
1002
- try:
1003
- detailed_capsule_info = capsule_api.get(capsule_id)
1004
- workers_info = capsule_api.get_workers(capsule_id)
1005
-
1006
- if format == "json":
1007
- # Output in JSON format for piping to jq
1008
- info_data = {"capsule": detailed_capsule_info, "workers": workers_info}
1009
- click.echo(json.dumps(info_data, indent=4))
1010
- else:
1011
- # Output in text format
1012
- _display_capsule_info_text(detailed_capsule_info, workers_info)
1013
-
1014
- except Exception as e:
1015
- raise AppConfigError(f"Error retrieving information for app {capsule_id}: {e}")
1016
-
1017
-
1018
- def _display_capsule_info_text(capsule_info, workers_info):
1019
- """Display capsule information in a human-readable text format."""
1020
- spec = capsule_info.get("spec", {})
1021
- status = capsule_info.get("status", {}) or {}
1022
- metadata = capsule_info.get("metadata", {}) or {}
1023
-
1024
- info_color = ColorTheme.INFO_COLOR
1025
- tl_color = ColorTheme.TL_HEADER_COLOR
1026
-
1027
- def _key_style(key: str, value: str):
1028
- return "%s: %s" % (
1029
- click.style(
1030
- key,
1031
- fg=ColorTheme.INFO_KEY_COLOR,
1032
- ),
1033
- click.style(str(value), fg=ColorTheme.INFO_VALUE_COLOR, bold=True),
1034
- )
1035
-
1036
- # Basic Info
1037
- click.secho("=== App Information ===", fg=tl_color, bold=True)
1038
- click.secho(_key_style("Name", spec.get("displayName", "N/A")), fg=info_color)
1039
- click.secho(_key_style("ID", capsule_info.get("id", "N/A")), fg=info_color)
1040
- click.secho(
1041
- _key_style("Version", capsule_info.get("version", "N/A")), fg=info_color
1042
- )
1043
- click.secho(
1044
- _key_style(
1045
- "Ready to Serve Traffic", str(status.get("readyToServeTraffic", False))
1046
- ),
1047
- fg=info_color,
1048
- )
1049
- click.secho(
1050
- _key_style("Update In Progress", str(status.get("updateInProgress", False))),
1051
- fg=info_color,
1052
- )
1053
- click.secho(
1054
- _key_style(
1055
- "Currently Served Version", str(status.get("currentlyServedVersion", "N/A"))
1056
- ),
1057
- fg=info_color,
1058
- )
1059
-
1060
- # URLs
1061
- access_info = status.get("accessInfo", {}) or {}
1062
- out_cluster_url = access_info.get("outOfClusterURL")
1063
- in_cluster_url = access_info.get("inClusterURL")
1064
-
1065
- if out_cluster_url:
1066
- click.secho(
1067
- _key_style("External URL", f"https://{out_cluster_url}"), fg=info_color
1068
- )
1069
- if in_cluster_url:
1070
- click.secho(
1071
- _key_style("Internal URL", f"https://{in_cluster_url}"), fg=info_color
1072
- )
1073
-
1074
- # Resource Configuration
1075
- click.secho("\n=== Resource Configuration ===", fg=tl_color, bold=True)
1076
- resource_config = spec.get("resourceConfig", {})
1077
- click.secho(_key_style("CPU", resource_config.get("cpu", "N/A")), fg=info_color)
1078
- click.secho(
1079
- _key_style("Memory", resource_config.get("memory", "N/A")), fg=info_color
1080
- )
1081
- click.secho(
1082
- _key_style("Ephemeral Storage", resource_config.get("ephemeralStorage", "N/A")),
1083
- fg=info_color,
1084
- )
1085
- if resource_config.get("gpu"):
1086
- click.secho(_key_style("GPU", resource_config.get("gpu")), fg=info_color)
1087
-
1088
- # Autoscaling
1089
- click.secho("\n=== Autoscaling Configuration ===", fg=tl_color, bold=True)
1090
- autoscaling_config = spec.get("autoscalingConfig", {})
1091
- click.secho(
1092
- _key_style("Min Replicas", str(autoscaling_config.get("minReplicas", "N/A"))),
1093
- fg=info_color,
1094
- )
1095
- click.secho(
1096
- _key_style("Max Replicas", str(autoscaling_config.get("maxReplicas", "N/A"))),
1097
- fg=info_color,
1098
- )
1099
- click.secho(
1100
- _key_style("Available Replicas", str(status.get("availableReplicas", "N/A"))),
1101
- fg=info_color,
1102
- )
1103
-
1104
- # Auth Configuration
1105
- click.secho("\n=== Authentication Configuration ===", fg=tl_color, bold=True)
1106
- auth_config = spec.get("authConfig", {})
1107
- click.secho(
1108
- _key_style("Auth Type", auth_config.get("authType", "N/A")), fg=info_color
1109
- )
1110
- click.secho(
1111
- _key_style("Public Access", str(auth_config.get("publicToDeployment", "N/A"))),
1112
- fg=info_color,
1113
- )
1114
-
1115
- # Tags
1116
- tags = spec.get("tags", [])
1117
- if tags:
1118
- click.secho("\n=== Tags ===", fg=tl_color, bold=True)
1119
- for tag in tags:
1120
- click.secho(
1121
- _key_style(str(tag.get("key", "N/A")), str(tag.get("value", "N/A"))),
1122
- fg=info_color,
1123
- )
1124
-
1125
- # Metadata
1126
- click.secho("\n=== Metadata ===", fg=tl_color, bold=True)
1127
- click.secho(
1128
- _key_style("Created At", metadata.get("createdAt", "N/A")), fg=info_color
1129
- )
1130
- click.secho(
1131
- _key_style("Last Modified At", metadata.get("lastModifiedAt", "N/A")),
1132
- fg=info_color,
1133
- )
1134
- click.secho(
1135
- _key_style("Last Modified By", metadata.get("lastModifiedBy", "N/A")),
1136
- fg=info_color,
1137
- )
1138
-
1139
- # Workers Information
1140
- click.secho("\n=== Workers Information ===", fg=tl_color, bold=True)
1141
- if not workers_info:
1142
- click.secho("No workers found", fg=info_color)
1143
- else:
1144
- click.secho(_key_style("Total Workers", str(len(workers_info))), fg=tl_color)
1145
-
1146
- # Create a table for workers
1147
- workers_headers = [
1148
- "Worker ID",
1149
- "Phase",
1150
- "Version",
1151
- "Activity",
1152
- "Activity Data Available",
1153
- ]
1154
- workers_table_data = []
1155
-
1156
- for worker in workers_info:
1157
- worker_id = worker.get("workerId", "N/A")
1158
- phase = worker.get("phase", "N/A")
1159
- version = worker.get("version", "N/A")
1160
- activity = str(worker.get("activity", "N/A"))
1161
- activity_data_available = str(worker.get("activityDataAvailable", False))
1162
-
1163
- workers_table_data.append(
1164
- [
1165
- worker_id[:20] + "..." if len(worker_id) > 23 else worker_id,
1166
- phase,
1167
- version[:10] + "..." if len(version) > 13 else version,
1168
- activity,
1169
- activity_data_available,
1170
- ]
1171
- )
1172
-
1173
- print_table(workers_table_data, workers_headers)
1174
-
1175
-
1176
- @app.command(help="Get logs for an app worker from the Outerbounds Platform.")
1177
- @click.option("--name", type=str, help="Get logs for app by name")
1178
- @click.option("--id", "cap_id", type=str, help="Get logs for app by id")
1179
- @click.option("--worker-id", type=str, help="Get logs for specific worker")
1180
- @click.option("--file", type=str, help="Save logs to file")
1181
- @click.option(
1182
- "--previous",
1183
- is_flag=True,
1184
- help="Get logs from previous container instance",
1185
- default=False,
1186
- )
1187
- @click.pass_context
1188
- def logs(ctx, name, cap_id, worker_id, file, previous):
1189
- """Get logs for an app worker from the Outerbounds Platform."""
1190
- # Require either name or id
1191
- if not any([name is not None, cap_id is not None]):
1192
- raise AppConfigError("Either --name or --id must be provided to get app logs.")
1193
-
1194
- # Ensure only one is provided
1195
- if name is not None and cap_id is not None:
1196
- raise AppConfigError("Please provide either --name or --id, not both.")
1197
-
1198
- capsule_api = CapsuleApi(
1199
- ctx.obj.api_url,
1200
- ctx.obj.perimeter,
1201
- )
1202
-
1203
- # First, find the capsule using list_and_filter_capsules
1204
- filtered_capsules = list_and_filter_capsules(
1205
- capsule_api, None, None, name, None, None, cap_id
1206
- )
1207
-
1208
- if len(filtered_capsules) == 0:
1209
- identifier = name if name else cap_id
1210
- identifier_type = "name" if name else "id"
1211
- raise AppConfigError(f"No app found with {identifier_type}: {identifier}")
1212
-
1213
- if len(filtered_capsules) > 1:
1214
- raise AppConfigError(
1215
- f"Multiple apps found with name: {name}. Please use --id to specify exactly which app you want logs for."
1216
- )
1217
-
1218
- capsule = filtered_capsules[0]
1219
- capsule_id = capsule.get("id")
1220
-
1221
- # Get workers
1222
- try:
1223
- workers_info = capsule_api.get_workers(capsule_id)
1224
- except Exception as e:
1225
- raise AppConfigError(f"Error retrieving workers for app {capsule_id}: {e}")
1226
-
1227
- if not workers_info:
1228
- raise AppConfigError(f"No workers found for app {capsule_id}")
1229
-
1230
- # If worker_id not provided, show interactive selection
1231
- if not worker_id:
1232
- if len(workers_info) == 1:
1233
- # Only one worker, use it automatically
1234
- selected_worker = workers_info[0]
1235
- worker_id = selected_worker.get("workerId")
1236
- worker_phase = selected_worker.get("phase", "N/A")
1237
- worker_version = selected_worker.get("version", "N/A")[:10]
1238
- click.echo(
1239
- f"📋 Using the only available worker: {worker_id[:20]}... (phase: {worker_phase}, version: {worker_version}...)"
1240
- )
1241
- else:
1242
- # Multiple workers, show selection
1243
- click.secho(
1244
- "📋 Multiple workers found. Please select one:",
1245
- fg=ColorTheme.INFO_COLOR,
1246
- bold=True,
1247
- )
1248
-
1249
- # Display workers in a table format for better readability
1250
- headers = ["#", "Worker ID", "Phase", "Version", "Activity"]
1251
- table_data = []
1252
-
1253
- for i, worker in enumerate(workers_info, 1):
1254
- w_id = worker.get("workerId", "N/A")
1255
- phase = worker.get("phase", "N/A")
1256
- version = worker.get("version", "N/A")
1257
- activity = str(worker.get("activity", "N/A"))
1258
-
1259
- table_data.append(
1260
- [
1261
- str(i),
1262
- w_id[:30] + "..." if len(w_id) > 33 else w_id,
1263
- phase,
1264
- version[:15] + "..." if len(version) > 18 else version,
1265
- activity,
1266
- ]
1267
- )
1268
-
1269
- print_table(table_data, headers)
1270
-
1271
- # Create choices for the prompt
1272
- worker_choices = []
1273
- for i, worker in enumerate(workers_info, 1):
1274
- worker_choices.append(str(i))
1275
-
1276
- selected_index = click.prompt(
1277
- click.style(
1278
- "Select worker number", fg=ColorTheme.INFO_COLOR, bold=True
1279
- ),
1280
- type=click.Choice(worker_choices),
1281
- )
1282
-
1283
- # Get the selected worker
1284
- selected_worker = workers_info[int(selected_index) - 1]
1285
- worker_id = selected_worker.get("workerId")
1286
-
1287
- # Get logs for the selected worker
1288
- try:
1289
- logs_response = capsule_api.logs(capsule_id, worker_id, previous=previous)
1290
- except Exception as e:
1291
- raise AppConfigError(f"Error retrieving logs for worker {worker_id}: {e}")
1292
-
1293
- # Format logs content
1294
- logs_content = "\n".join([log.get("message", "") for log in logs_response])
1295
-
1296
- # Display or save logs
1297
- if file:
1298
- try:
1299
- with open(file, "w") as f:
1300
- f.write(logs_content)
1301
- click.echo(f"📁 Logs saved to {file}")
1302
- except Exception as e:
1303
- raise AppConfigError(f"Error saving logs to file {file}: {e}")
1304
- else:
1305
- if logs_content.strip():
1306
- click.echo(logs_content)
1307
- else:
1308
- click.echo("📝 No logs available for this worker.")
1309
-
1310
-
1311
- # if __name__ == "__main__":
1312
- # cli()