|
7 | 7 | from rich.box import ROUNDED
|
8 | 8 | from rich.panel import Panel
|
9 | 9 |
|
| 10 | +from codegen.configs.models.secrets import SecretsConfig |
| 11 | +from codegen.git.schemas.repo_config import RepoConfig |
| 12 | + |
10 | 13 |
|
11 | 14 | @click.command(name="start")
|
12 | 15 | @click.option("--platform", "-t", type=click.Choice(["linux/amd64", "linux/arm64", "linux/amd64,linux/arm64"]), default="linux/amd64,linux/arm64", help="Target platform(s) for the Docker image")
|
|
15 | 18 | def start_command(port: int, platform: str, detached: bool):
|
16 | 19 | """Starts a local codegen server"""
|
17 | 20 | codegen_version = version("codegen")
|
18 |
| - rich.print(codegen_version) |
19 |
| - repo_root = Path(__file__).parent.parent.parent.parent.parent.parent |
20 |
| - dockerfile_path = repo_root / "Dockerfile-runner" |
| 21 | + rich.print(f"[bold green]Codegen version:[/bold green] {codegen_version}") |
| 22 | + codegen_root = Path(__file__).parent.parent.parent.parent.parent.parent |
| 23 | + |
| 24 | + try: |
| 25 | + rich.print("[bold blue]Building Docker image...[/bold blue]") |
| 26 | + _build_docker_image(codegen_root, platform) |
| 27 | + rich.print("[bold blue]Starting Docker container...[/bold blue]") |
| 28 | + _run_docker_container(port, platform, detached) |
| 29 | + rich.print(Panel(f"[green]Server started successfully![/green]\nAccess the server at: [bold]http://0.0.0.0:{port}[/bold]", box=ROUNDED, title="Codegen Server")) |
| 30 | + except subprocess.CalledProcessError as e: |
| 31 | + rich.print(f"[bold red]Error:[/bold red] Failed to {e.cmd[0]} Docker container") |
| 32 | + raise click.Abort() |
| 33 | + except Exception as e: |
| 34 | + rich.print(f"[bold red]Error:[/bold red] {e!s}") |
| 35 | + raise click.Abort() |
21 | 36 |
|
22 |
| - # Build the Docker image |
23 |
| - rich.print("[bold blue]Building Docker image...[/bold blue]") |
| 37 | + |
| 38 | +def _build_docker_image(codegen_root: Path, platform: str): |
24 | 39 | build_cmd = [
|
25 | 40 | "docker",
|
26 | 41 | "buildx",
|
27 | 42 | "build",
|
28 | 43 | "--platform",
|
29 | 44 | platform,
|
30 | 45 | "-f",
|
31 |
| - str(dockerfile_path), |
| 46 | + str(codegen_root / "Dockerfile-runner"), |
32 | 47 | "-t",
|
33 | 48 | "codegen-runner",
|
34 | 49 | "--load",
|
35 |
| - str(repo_root), |
| 50 | + str(codegen_root), |
36 | 51 | ]
|
37 | 52 | rich.print(f"build_cmd: {str.join(' ', build_cmd)}")
|
| 53 | + subprocess.run(build_cmd, check=True) |
38 | 54 |
|
39 |
| - try: |
40 |
| - subprocess.run(build_cmd, check=True) |
41 |
| - |
42 |
| - # Run the Docker container |
43 |
| - rich.print("[bold blue]Starting Docker container...[/bold blue]") |
44 |
| - run_mode = "-d" if detached else "-it" |
45 |
| - entry_point = f'"uv run --frozen uvicorn codegen.runner.sandbox.server:app --host 0.0.0.0 --port {port}"' |
46 |
| - run_cmd = ["docker", "run", run_mode, "-p", f"8000:{port}", "codegen-runner", entry_point] |
47 | 55 |
|
48 |
| - rich.print(f"run_cmd: {str.join(' ', run_cmd)}") |
49 |
| - subprocess.run(run_cmd, check=True) |
| 56 | +def _run_docker_container(port: int, detached: bool): |
| 57 | + repo_path = Path.cwd().resolve() |
| 58 | + repo_config = RepoConfig.from_repo_path(repo_path) |
| 59 | + container_repo_path = f"/app/git/{repo_config.name}" |
| 60 | + envvars = { |
| 61 | + "REPOSITORY_LANGUAGE": repo_config.language.value, |
| 62 | + "REPOSITORY_OWNER": repo_config.organization_name, |
| 63 | + "REPOSITORY_PATH": container_repo_path, |
| 64 | + "GITHUB_TOKEN": SecretsConfig().github_token, |
| 65 | + } |
| 66 | + envvars_args = [arg for k, v in envvars.items() for arg in ("--env", f"{k}={v}")] |
50 | 67 |
|
51 |
| - rich.print(Panel(f"[green]Server started successfully![/green]\nAccess the server at: [bold]http://0.0.0.0:{port}[/bold]", box=ROUNDED, title="Codegen Server")) |
| 68 | + mount_args = ["-v", f"{repo_path}:{container_repo_path}"] |
| 69 | + run_mode = "-d" if detached else "-it" |
| 70 | + entry_point = f"uv run --frozen uvicorn codegen.runner.sandbox.server:app --host 0.0.0.0 --port {port}" |
| 71 | + run_cmd = ["docker", "run", run_mode, "-p", f"8000:{port}", *mount_args, *envvars_args, "codegen-runner", entry_point] |
52 | 72 |
|
53 |
| - except subprocess.CalledProcessError as e: |
54 |
| - rich.print(f"[bold red]Error:[/bold red] Failed to {e.cmd[0]} Docker container") |
55 |
| - raise click.Abort() |
56 |
| - except Exception as e: |
57 |
| - rich.print(f"[bold red]Error:[/bold red] {e!s}") |
58 |
| - raise click.Abort() |
| 73 | + rich.print(f"run_cmd: {str.join(' ', run_cmd)}") |
| 74 | + subprocess.run(run_cmd, check=True) |
0 commit comments