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