|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import click |
| 8 | + |
| 9 | +from ai.backend.common.dependencies.stacks.visualizing import VisualizingDependencyStack |
| 10 | + |
| 11 | +from ..dependencies.composer import StorageDependencyComposer |
| 12 | +from .context import CLIContext |
| 13 | + |
| 14 | + |
| 15 | +@click.group() |
| 16 | +def cli() -> None: |
| 17 | + """Dependency verification commands for storage proxy setup.""" |
| 18 | + pass |
| 19 | + |
| 20 | + |
| 21 | +@cli.command(name="verify") |
| 22 | +@click.option( |
| 23 | + "--no-timestamps", |
| 24 | + is_flag=True, |
| 25 | + help="Hide timestamps in output", |
| 26 | +) |
| 27 | +@click.option( |
| 28 | + "--log-level", |
| 29 | + type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], case_sensitive=False), |
| 30 | + default="WARNING", |
| 31 | + help="Set the logging level (default: WARNING)", |
| 32 | +) |
| 33 | +@click.pass_obj |
| 34 | +def verify( |
| 35 | + cli_ctx: CLIContext, |
| 36 | + no_timestamps: bool, |
| 37 | + log_level: str, |
| 38 | +) -> None: |
| 39 | + """ |
| 40 | + Verify all storage proxy dependencies can be initialized successfully. |
| 41 | +
|
| 42 | + This command attempts to initialize all storage proxy dependencies (infrastructure, |
| 43 | + components) and reports any failures during the setup process. |
| 44 | +
|
| 45 | + The output shows dependency initialization with status indicators: |
| 46 | + - ▶ Starting (for composers) |
| 47 | + - ✓ Completed (for dependencies) |
| 48 | + - ✗ Failed (for dependencies) |
| 49 | +
|
| 50 | + Examples: |
| 51 | +
|
| 52 | + # Verify all dependencies with default WARNING log level |
| 53 | + $ backend.ai storage dependencies verify |
| 54 | +
|
| 55 | + # Hide timestamps in output |
| 56 | + $ backend.ai storage dependencies verify --no-timestamps |
| 57 | +
|
| 58 | + # Set log level to DEBUG to see detailed initialization logs |
| 59 | + $ backend.ai storage dependencies verify --log-level DEBUG |
| 60 | + """ |
| 61 | + import logging as std_logging |
| 62 | + |
| 63 | + # Set logging level BEFORE any dependency initialization |
| 64 | + std_logging.basicConfig( |
| 65 | + level=getattr(std_logging, log_level.upper()), |
| 66 | + format="%(levelname)s:%(name)s:%(message)s", |
| 67 | + force=True, |
| 68 | + ) |
| 69 | + |
| 70 | + config_path = cli_ctx.config_path |
| 71 | + |
| 72 | + async def _verify_dependencies() -> bool: |
| 73 | + """Verify all storage proxy dependencies can be initialized.""" |
| 74 | + print("\n" + "=" * 60) |
| 75 | + print("Storage Proxy Dependency Verification") |
| 76 | + print("=" * 60) |
| 77 | + print(f"Config: {config_path or 'default locations'}") |
| 78 | + print(f"Log Level: {log_level.upper()}") |
| 79 | + print("=" * 60 + "\n") |
| 80 | + |
| 81 | + # Create visualizing stack |
| 82 | + stack = VisualizingDependencyStack( |
| 83 | + output=sys.stdout, |
| 84 | + show_timestamps=not no_timestamps, |
| 85 | + ) |
| 86 | + |
| 87 | + try: |
| 88 | + async with stack: |
| 89 | + from ..dependencies.composer import DependencyInput |
| 90 | + |
| 91 | + dependency_input = DependencyInput( |
| 92 | + config_path=config_path or Path("storage-proxy.toml"), |
| 93 | + ) |
| 94 | + composer = StorageDependencyComposer() |
| 95 | + |
| 96 | + # Initialize all dependencies through the composer |
| 97 | + await stack.enter_composer(composer, dependency_input) |
| 98 | + |
| 99 | + except Exception as e: |
| 100 | + print(f"\n❌ Dependency initialization failed: {e}\n") |
| 101 | + |
| 102 | + # Print summary |
| 103 | + print() |
| 104 | + stack.print_summary() |
| 105 | + |
| 106 | + return not stack.has_failures() |
| 107 | + |
| 108 | + # Run the verification |
| 109 | + success = asyncio.run(_verify_dependencies()) |
| 110 | + |
| 111 | + if success: |
| 112 | + print("All dependencies verified successfully!\n") |
| 113 | + sys.exit(0) |
| 114 | + else: |
| 115 | + print("Some dependencies failed. Check the output above for details.\n") |
| 116 | + sys.exit(1) |
0 commit comments