|
4 | 4 | from rich.console import Console |
5 | 5 | from rich.table import Table |
6 | 6 | from rich.panel import Panel |
| 7 | +from rich.progress import Progress, SpinnerColumn, TextColumn |
7 | 8 | import questionary |
| 9 | +import asyncio |
| 10 | +from typing import Optional |
8 | 11 |
|
| 12 | +from ..utils.app import discover_flash_project |
9 | 13 | from ..utils.deployment import ( |
10 | 14 | get_deployment_environments, |
11 | 15 | create_deployment_environment, |
|
15 | 19 | get_environment_info, |
16 | 20 | ) |
17 | 21 |
|
| 22 | +from tetra_rp.core.resources import FlashApp |
| 23 | + |
18 | 24 | console = Console() |
19 | 25 |
|
| 26 | +deploy_app = typer.Typer(short_help="Deploy flash application code to managed environments on Runpod") |
| 27 | + |
| 28 | +@deploy_app.callback(invoke_without_command=True) |
| 29 | +def deploy_callback( |
| 30 | + ctx: typer.Context, |
| 31 | + app_name: str = typer.Option(None, "--app-name", "-a", help="Flash app name to deploy a build for"), |
| 32 | + env_name: str = typer.Option("dev", "--env-name", "-e", help="Flash env name to deploy a build to. If not provided, default to 'dev'"), |
| 33 | + build_id: str = typer.Option(None, "--build-id", "-b", help="Flash build id to deploy to env. If not provided, default to most recently uploaded build"), |
| 34 | +): |
| 35 | + if ctx.invoked_subcommand is None: |
| 36 | + deploy_build_sync(app_name, env_name, build_id) |
| 37 | + |
| 38 | +def deploy_build_sync(app_name: str, env_name: str, build_id: str): |
| 39 | + """ |
| 40 | + Convenience wrapper to run async methods from cli |
| 41 | + """ |
| 42 | + asyncio.run(deploy_build(app_name, env_name, build_id)) |
| 43 | + |
| 44 | +async def deploy_build(app_name: Optional[str] = "", env_name: Optional[str] = "dev", build_id: Optional[str] = None): |
| 45 | + target_env = env_name or "dev" |
| 46 | + progress_columns = [SpinnerColumn(), TextColumn("[progress.description]{task.description}")] |
| 47 | + |
| 48 | + with Progress(*progress_columns, console=console) as progress: |
| 49 | + task = progress.add_task("Preparing deployment", start=True) |
| 50 | + |
| 51 | + if not app_name: |
| 52 | + progress.update(task, description="Discovering Flash project...") |
| 53 | + _, app_name = discover_flash_project() |
| 54 | + progress.update(task, description=f"Detected Flash app '{app_name}'") |
| 55 | + |
| 56 | + progress.update(task, description=f"Fetching Flash app '{app_name}'...") |
| 57 | + app = await FlashApp.from_name(app_name) |
| 58 | + |
| 59 | + if not build_id: |
| 60 | + progress.update(task, description="Fetching available builds...") |
| 61 | + builds = await app.list_builds() |
| 62 | + if not builds: |
| 63 | + progress.update(task, description="No builds found for app") |
| 64 | + progress.stop_task(task) |
| 65 | + raise ValueError("No builds found for app. Run 'flash build' first.") |
| 66 | + build_id = builds[0]["id"] |
| 67 | + progress.update(task, description=f"Using latest build {build_id}") |
| 68 | + |
| 69 | + progress.update( |
| 70 | + task, |
| 71 | + description=f"Promoting build {build_id} to environment '{target_env}'...", |
| 72 | + ) |
| 73 | + result = await app.deploy_build_to_environment(build_id, environment_name=target_env) |
| 74 | + progress.update( |
| 75 | + task, |
| 76 | + description=f"[green]✓ Promoted build {build_id} to '{target_env}'", |
| 77 | + ) |
| 78 | + progress.stop_task(task) |
| 79 | + |
| 80 | + panel_content = ( |
| 81 | + f"Promoted build [bold]{build_id}[/bold] to environment [bold]{target_env}[/bold]\n" |
| 82 | + f"Flash app: [bold]{app_name}[/bold]" |
| 83 | + ) |
| 84 | + console.print(Panel(panel_content, title="Deployment Promotion", expand=False)) |
| 85 | + |
| 86 | + return result |
20 | 87 |
|
21 | 88 | def list_command(): |
22 | 89 | """Show available deployment environments.""" |
|
0 commit comments