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